λ¬Έμ
νμ΄ κ³Όμ
κ° μ¬λλ€μ ν€ h
μ μμ λ³΄λ€ μμ μμλ μ¬λλ€ μ€ μμ λ³΄λ€ ν€κ° ν° μ¬λλ€μ μ n
μ΄ μ£Όμ΄μ§ λ
쑰건μ λ§λλ‘ μ¬λλ€μ λμ΄νλ λ°©λ²μ μ°Ύλ λ¬Έμ μ
λλ€.
λ°λΌμ μ°μ n
μ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ©° n
μ΄ κ°μ κ²½μ°μλ h
κ° μ¦κ°νλ μμΌλ‘ μ λ ¬ν©λλ€.
κ·Έλ¦¬κ³ μ λ ¬λ μ¬λλ€μ νλͺ
μ© μμ μ μμΉλ₯Ό μ°Ύμ μ½μ
ν΄μ£Όλ λ°©μμΌλ‘ μ§νν©λλ€.
μ΄λ n
μ λ§μ‘±μν€λ©΄μ μ΅λν λ€μ μμΉνλλ‘ μ€μ ν΄μ£Όλλ‘ νλ νμμ μ ν
μ νμ©ν©λλ€.
μ½λ
/**
* @param {number[][]} people
* @return {number[][]}
*/
var reconstructQueue = function (people) {
const answer = [];
people.sort((a, b) => a[1] - b[1] || a[0] - b[0]);
people.forEach((p) => {
let count = 0,
i = 0;
for (; i < answer.length; i++) {
if (answer[i][0] >= p[0]) count++;
if (count > p[1]) break;
}
answer.splice(i, 0, p);
});
return answer;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 130 - Surrounded Regions (Medium) (0) | 2021.03.03 |
---|---|
LeetCode 226 - Invert Binary Tree (Easy) (0) | 2021.03.03 |
LeetCode 54 - Spiral Matrix (Medium) (0) | 2021.03.03 |
LeetCode 279 - Perfect Squares (Medium) (0) | 2021.03.03 |
LeetCode 94 - Binary Tree Inorder Traversal (Medium) (0) | 2021.03.03 |
π¬ λκΈ