λ¬Έμ
νμ΄ κ³Όμ
μ΄μ°¨μ λ°°μ΄μμ λͺ©νκ°μ΄ μ‘΄μ¬νλμ§ νλ¨νλ λ¬Έμ μ
λλ€.
κ·Έλ₯ λΈλ£¨νΈ ν¬μ€
λ‘ μ κ·Όνλ€λ©΄ O(MN)
μ μκ° λ³΅μ‘λλ‘ ν΄κ²°ν μλ μμ§λ§
μ’ λ ν¨μ¨μ μΈ νμμ μν΄ μ΄λΆ νμ
μ μ μ©νμ΅λλ€.
κ° νμ΄ μ€λ¦μ°¨μμΌλ‘ μ λ ¬λμ΄ μλ€λ μ μ μ΄μ©ν΄μ κ° νλ§λ€ μ΄λΆ νμ
μ μνν΄ μνλ κ°μ΄ μ‘΄μ¬νλμ§ νλ¨ν©λλ€.
μ½λ
/**
* @param {number[][]} matrix
* @param {number} target
* @return {boolean}
*/
var searchMatrix = function (matrix, target) {
for (let row of matrix) {
if (bisect(row, target)) return true;
}
return false;
};
function bisect(arr, target) {
let lo = 0;
let hi = arr.length - 1;
while (lo <= hi) {
let mid = Math.floor((lo + hi) / 2);
if (arr[mid] === target) return true;
else if (arr[mid] > target) hi = mid - 1;
else lo = mid + 1;
}
return false;
}
λ°μν
'π algorithm > leetcode' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LeetCode 46 - Permutations (Medium) (0) | 2021.03.03 |
---|---|
LeetCode 21 - Merge Two Sorted Lists (Easy) (0) | 2021.03.02 |
LeetCode 155 - Min Stack (Easy) (0) | 2021.03.02 |
LeetCode 39 - Combination Sum (Medium) (0) | 2021.03.02 |
LeetCode 20 - Valid Parentheses (Easy) (0) | 2021.03.02 |
π¬ λκΈ