Skip to content

Commit 65a62d1

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 35 and 74
1 parent e70684b commit 65a62d1

File tree

5 files changed

+94
-1
lines changed

5 files changed

+94
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<img src="https://img.shields.io/badge/tests-passed-brightgreen">
1616
</a>
1717
<a>
18-
<img src="https://img.shields.io/badge/coverage-97%25-brightgreen">
18+
<img src="https://img.shields.io/badge/coverage-98%25-brightgreen">
1919
</a>
2020
</h1>
2121
</div>
@@ -45,6 +45,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
4545
- [27 Remove Element](https://leetcode.com/problems/remove-element/description/)
4646
- [28 Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/)
4747
- [30 Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/)
48+
- [35 Search Insert Position](https://leetcode.com/problems/search-insert-position/description/)
4849
- [36 Valid Sudoku](https://leetcode.com/problems/valid-sudoku/description/)
4950
- [39 Combination Sum](https://leetcode.com/problems/combination-sum/description/)
5051
- [42 Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)
@@ -67,6 +68,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
6768
- [69 Sqrt(x)](https://leetcode.com/problems/sqrtx/description/)
6869
- [71 Simplify Path](https://leetcode.com/problems/simplify-path/description/)
6970
- [73 Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/)
71+
- [74 Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
7072
- [76 Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/)
7173
- [77 Combinations](https://leetcode.com/problems/combinations/description/)
7274
- [79 Word Search](https://leetcode.com/problems/word-search/description/)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def searchInsert(self, nums: List[int], target: int) -> int:
8+
"""
9+
Given a sorted array of distinct integers and a target value, return the index
10+
if the target is found. If not, return the index where it would be if it were
11+
inserted in order.
12+
13+
You must write an algorithm with O(log n) runtime complexity.
14+
"""
15+
left, right = 0, len(nums) - 1
16+
while left <= right:
17+
mid = (right + left) // 2
18+
if nums[mid] < target:
19+
left = mid + 1
20+
elif nums[mid] > target:
21+
right = mid - 1
22+
else:
23+
return mid
24+
return left
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
8+
"""
9+
You are given an m x n integer matrix matrix with the following two properties:
10+
- Each row is sorted in non-decreasing order.
11+
- The first integer of each row is greater than the last integer of the previous
12+
row.
13+
14+
Given an integer target, return true if target is in matrix or false otherwise.
15+
16+
You must write a solution in O(log(m * n)) time complexity.
17+
"""
18+
rows, cols = len(matrix), len(matrix[0])
19+
left, right = 0, (rows * cols) - 1
20+
while left <= right:
21+
mid = (right + left) // 2
22+
row = mid // cols
23+
col = mid % cols
24+
if matrix[row][col] < target:
25+
left = mid + 1
26+
elif matrix[row][col] > target:
27+
right = mid - 1
28+
else:
29+
return True
30+
return False
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._35_search_insert_position import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "target", "expected"],
10+
argvalues=[
11+
([1, 3, 5, 6], 5, 2),
12+
([1, 3, 5, 6], 2, 1),
13+
([1, 3, 5, 6], 7, 4),
14+
],
15+
)
16+
def test_func(nums: List[int], target: int, expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
idx = Solution().searchInsert(nums, target)
19+
assert idx == expected

tests/test_74_search_a_2d_matrix.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._74_search_a_2d_matrix import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["matrix", "target", "expected"],
10+
argvalues=[
11+
([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 3, True),
12+
([[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 60]], 13, False),
13+
],
14+
)
15+
def test_func(matrix: List[List[int]], target: int, expected: bool):
16+
"""Tests the solution of a LeetCode problem."""
17+
actual = Solution().searchMatrix(matrix, target)
18+
assert actual is expected

0 commit comments

Comments
 (0)