π‘ λ¬Έμ
π― νμ΄ κ³Όμ
λ°°μ΄μ λ΄κΈ΄ μ«μλ€ μ€μμ μΌμ κΈ°μ€κ° μ΄μμ μ«μλ€μ μ°Ύμμ λ°ννλ λ¬Έμ μ λλ€.
λ°°μ΄μ λ΄κΈΈ μ μλ μ«μμ λ²μκ° ν¬κΈ° λλ¬Έμ ν΄μ±μ μ΄μ©ν΄μ μ«μλ€μ κ°μλ₯Ό μΈμ΄μ€λλ€.
μ¬κΈ°μλ κ°μ²΄ λμ μ μ½κ³ μ°λ μ±λ₯μ΄ λ λ°μ΄λ Map
μ μ¬μ©νμ΅λλ€.
π¨π» μ½λ
var majorityElement = function (nums) {
const numCount = new Map();
const pivot = nums.length / 3;
for (const num of nums) {
if (numCount.has(num)) {
const oldVal = numCount.get(num);
numCount.set(num, oldVal + 1);
} else {
numCount.set(num, 1);
}
}
const answer = [];
for (const [key, value] of numCount) {
if (value > pivot) {
answer.push(key);
}
}
return answer;
};
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 169 - Majority Element (Easy) (2) | 2023.02.05 |
---|---|
LeetCode 733 - Flood Fill (Easy) (0) | 2022.03.07 |
LeetCode 300 - Longest Increasing Subsequence (Medium) (0) | 2021.06.14 |
LeetCode 36 - Valid Sudoku (Medium) (0) | 2021.04.13 |
LeetCode 120 - Triangle (Medium) (0) | 2021.04.13 |
π¬ λκΈ