일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- Regular Expression
- DWG
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- 315. Count of Smaller Numbers After Self
- Python Implementation
- t1
- Substring with Concatenation of All Words
- kaggle
- 밴픽
- 43. Multiply Strings
- iterator
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- concurrency
- Python
- 시바견
- Python Code
- Generator
- 30. Substring with Concatenation of All Words
- Class
- 컴퓨터의 구조
- LeetCode
- 파이썬
- Decorator
- attribute
- 운영체제
- shiba
- 715. Range Module
- data science
Archives
- Today
- Total
Scribbling
LeetCode: 74. Search a 2D Matrix 본문
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if target < matrix[0][0] or target > matrix[-1][-1]:
return False
m, n = len(matrix), len(matrix[0])
row = 0
# row search
l, r = 0, m - 1
while l <= r:
mid = (l + r) // 2
if matrix[mid][0] > target:
r = mid - 1
else:
row = max(row, mid)
l = mid + 1
# column search
l, r = 0, n - 1
while l <= r:
mid = (l + r) // 2
if matrix[row][mid] == target:
return True
if matrix[row][mid] < target:
l = mid + 1
else:
r = mid - 1
return False
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 77. Combinations (0) | 2021.09.28 |
---|---|
LeetCode: 75. Sort Colors (0) | 2021.09.28 |
LeetCode: 73. Set Matrix Zeroes (0) | 2021.09.28 |
LeetCode: 72. Edit Distance (0) | 2021.09.26 |
LeetCode: 71. Simplify Path (0) | 2021.09.26 |