KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • js
  • programmers
  • Level 0
  • 제코베 JS 100제
  • 그래프
  • C
  • 다이나믹 프로그래밍
  • 백준
  • tree
  • LeetCode
  • recursion
  • codeup
  • 정렬
  • Level 2
  • Level 1
  • 문자열
  • string
  • 수학
  • Level1
  • C++

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS

2023. 4. 28. 17:41

< 문제 바로가기 >

 

Search a 2D Matrix - LeetCode

Can you solve this real interview question? Search a 2D Matrix - You are given an m x n integer matrix matrix with the following two properties: * Each row is sorted in non-decreasing order. * The first integer of each row is greater than the last integer

leetcode.com

 

< 문제 간단설명 >

주어진 2차원 배열에서 target 값이 존재하는지 찾는 문제이다.

그냥 간단히 할 수도 있겠지만... topic이 Binary Search인 만큼 이진탐색으로 찾아보았다.

 

/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function(matrix, target) {
    let [row, col] = [0, matrix[0].length - 1];

    let cur = 0;
    while(row < matrix.length && col >= 0) {
        cur = matrix[row][col];

        if(cur === target) {
            return true;
        }
        if(cur < target) {
            row += 1;
        }
        if(cur > target) {
            col -= 1;
        }
    }

    return false;
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS  (0) 2023.04.29
LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS  (0) 2023.04.28
LeetCode / Tree / 437번 / Path Sum III / JS  (0) 2023.04.28
LeetCode / Tree / 543번 / Diameter of Binary Tree / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search Tree / 108번 / Convert Sorted Array to Binary Search Tree / JS
    • LeetCode / Binary Search / 33번 / Search in Rotated Sorted Array / JS
    • LeetCode / Tree / 437번 / Path Sum III / JS
    • LeetCode / Tree / 543번 / Diameter of Binary Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바