Skip to content

Commit 1473e07

Browse files
committed
add 585 600
1 parent 8e58c5c commit 1473e07

7 files changed

+312
-48
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4444
| [] | [blind75.md](./list/blind75.md) | 77/77 | - |
4545
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4646
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
47-
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 7/128 | 3 vips |
47+
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 9/128 | 3 vips |
4848
| [🔲] | [9c-top.md](./list/9c-top.md) | 5/48 | - |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/56 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |
5151

52-
**Solved**: 456 problems
52+
**Solved**: 457 problems
5353

5454
## 类型/Category
5555

@@ -305,7 +305,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
305305

306306
## Binary Search
307307

308-
| Link | Problem(40) | Solution | Tag | Time | Space | Ref |
308+
| Link | Problem(41) | Solution | Tag | Time | Space | Ref |
309309
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
310310
| [Leetcode-704](https://leetcode.com/problems/binary-search/) | Binary Search | [c++](./leetcode/704.binary-search.cpp), [python3](./leetcode/704.binary-search.py) | Binary Search | O\(logN\) | O\(1\) | - |
311311
| [Leetcode-3048](https://leetcode.com/problems/earliest-second-to-mark-indices-i/) | Earliest Second To Mark Indices I | [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp), [python3](./leetcode/3048.earliest-second-to-mark-indices-i.py) | Binary Search | O\(NlogN\) | O\(N\) | - |
@@ -345,7 +345,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
345345
| [Lintcode-437](https://www.lintcode.com/problem/copy-books/) | Copy Books | [c++](./lintcode/437.copy-books.cpp), [python3](./lintcode/437.copy-books.py) | Binary Search | O\(N\*logP\) | O\(1\) | - |
346346
| [Lintcode-14](https://www.lintcode.com/problem/first-position-of-target/) | First Position Of Target | [c++](./lintcode/14.first-position-of-target.cpp), [python3](./lintcode/14.first-position-of-target.py) | Binary Search | O\(logN\) | O\(1\) | - |
347347
| [Lintcode-617](https://www.lintcode.com/problem/maximum-average-subarray-ii/) | Maximum Average Subarray II | [c++](./lintcode/617.maximum-average-subarray-ii.cpp), [python3](./lintcode/617.maximum-average-subarray-ii.py) | Binary Search | O\(Nlog\(A/ε\)\) | O\(N\) | Leetcode-644 |
348-
| [Lintcode-600](https://www.lintcode.com/problem/smallest-rectangle-enclosing-black-pixels/) | Smallest Rectangle Enclosing Black Pixels | [c++](./lintcode/600.smallest-rectangle-enclosing-black-pixels.cpp), [python3](./lintcode/600.smallest-rectangle-enclosing-black-pixels.py) | Binary Search | O\(N \* logM \+ M \* logN\) | O\(1\) | - |
348+
| [Lintcode-585](https://www.lintcode.com/problem/maximum-number-in-mountain-sequence/) | Maximum Number In Mountain Sequence | [c++](./lintcode/585.maximum-number-in-mountain-sequence.cpp), [python3](./lintcode/585.maximum-number-in-mountain-sequence.py) | Binary Search | O\(logN\) | O\(1\) | - |
349+
| [Lintcode-600](https://www.lintcode.com/problem/smallest-rectangle-enclosing-black-pixels/) | Smallest Rectangle Enclosing Black Pixels | [c++](./lintcode/600.smallest-rectangle-enclosing-black-pixels.cpp), [python3](./lintcode/600.smallest-rectangle-enclosing-black-pixels.py) | Binary Search | O\(N \* logM \+ M \* logN\) | O\(1\) | Leetcode-302 |
349350
| [Lintcode-183](https://www.lintcode.com/problem/wood-cut/) | Wood Cut | [c++](./lintcode/183.wood-cut.cpp), [python3](./lintcode/183.wood-cut.py) | Binary Search | O\(NlogA\) | O\(1\) | - |
350351

351352
## Linked List
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Tag: Binary Search
2+
// Time: O(logN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a mountain sequence of `n` integers which increase firstly and then decrease, find the mountain top(Maximum).
8+
//
9+
// ---
10+
//
11+
//
12+
// Example 1:
13+
// ```
14+
// Input: nums = [1, 2, 4, 8, 6, 3]
15+
// Output: 8
16+
// ```
17+
// Example 2:
18+
// ```
19+
// Input: nums = [10, 9, 8, 7],
20+
// Output: 10
21+
// ```
22+
//
23+
// Arrays are strictly incremented, strictly decreasing
24+
25+
class Solution {
26+
public:
27+
/**
28+
* @param nums: a mountain sequence which increase firstly and then decrease
29+
* @return: then mountain top
30+
*/
31+
int mountainSequence(vector<int> &nums) {
32+
// write your code here
33+
int n = nums.size();
34+
int left = 0;
35+
int right = n - 1;
36+
while (left < right) {
37+
int mid = left + (right - left) / 2;
38+
if (nums[mid] < nums[mid + 1]) {
39+
left = mid + 1;
40+
} else {
41+
right = mid;
42+
}
43+
}
44+
45+
return nums[left];
46+
}
47+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Tag: Binary Search
2+
# Time: O(logN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a mountain sequence of `n` integers which increase firstly and then decrease, find the mountain top(Maximum).
8+
#
9+
# ---
10+
#
11+
#
12+
# Example 1:
13+
# ```
14+
# Input: nums = [1, 2, 4, 8, 6, 3]
15+
# Output: 8
16+
# ```
17+
# Example 2:
18+
# ```
19+
# Input: nums = [10, 9, 8, 7],
20+
# Output: 10
21+
# ```
22+
#
23+
# Arrays are strictly incremented, strictly decreasing
24+
25+
from typing import (
26+
List,
27+
)
28+
29+
class Solution:
30+
"""
31+
@param nums: a mountain sequence which increase firstly and then decrease
32+
@return: then mountain top
33+
"""
34+
def mountain_sequence(self, nums: List[int]) -> int:
35+
# write your code here
36+
n = len(nums)
37+
left = 0
38+
right = n - 1
39+
while left < right:
40+
mid = left + (right - left) // 2
41+
if (nums[mid] < nums[mid + 1]):
42+
left = mid + 1
43+
else:
44+
right = mid
45+
46+
return nums[left]

lintcode/600.smallest-rectangle-enclosing-black-pixels.cpp

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Tag: Binary Search on Answer, Binary Search
22
// Time: O(N * logM + M * logN)
33
// Space: O(1)
4-
// Ref: -
5-
// Note: Graph | Leetcode-302
4+
// Ref: Leetcode-302
5+
// Note: Graph
66

77
// An image is represented by a binary matrix with `0` as a white pixel and `1` as a black pixel.
88
// The black pixels are connected, i.e., there is only one black region.
@@ -39,10 +39,13 @@ class Solution {
3939
*/
4040
int minArea(vector<vector<char>> &image, int x, int y) {
4141
// write your code here
42+
int n = image.size();
43+
int m = image[0].size();
44+
4245
int left = search(image, 0, y, checkCol, false);
43-
int right = search(image, y, image[0].size(), checkCol, true);
46+
int right = search(image, y, m, checkCol, true);
4447
int top = search(image, 0, x, checkRow, false);
45-
int bottom = search(image, x, image.size(), checkRow, true);
48+
int bottom = search(image, x, n, checkRow, true);
4649
return (right - left) * (bottom - top);
4750
}
4851

@@ -79,4 +82,97 @@ class Solution {
7982
}
8083
return start;
8184
}
85+
};
86+
87+
class Solution {
88+
public:
89+
/**
90+
* @param image: a binary matrix with '0' and '1'
91+
* @param x: the location of one of the black pixels
92+
* @param y: the location of one of the black pixels
93+
* @return: an integer
94+
*/
95+
int minArea(vector<vector<char>> &image, int x, int y) {
96+
// write your code here
97+
int n = image.size();
98+
int m = image[0].size();
99+
100+
int left = 0, right = y;
101+
while (left < right) {
102+
int mid = (left + right) / 2;
103+
bool hasBlackPixel = false;
104+
for (int i = 0; i < n; i++) {
105+
if (image[i][mid] == '1') {
106+
hasBlackPixel = true;
107+
break;
108+
}
109+
}
110+
if (hasBlackPixel) {
111+
right = mid;
112+
} else {
113+
left = mid + 1;
114+
}
115+
}
116+
int left_most = left;
117+
118+
left = y;
119+
right = m - 1;
120+
while (left < right) {
121+
int mid = (left + right + 1) / 2;
122+
bool hasBlackPixel = false;
123+
for (int i = 0; i < n; i++) {
124+
if (image[i][mid] == '1') {
125+
hasBlackPixel = true;
126+
break;
127+
}
128+
}
129+
if (hasBlackPixel) {
130+
left = mid;
131+
} else {
132+
right = mid - 1;
133+
}
134+
}
135+
int right_most = left;
136+
137+
int top = 0, bottom = x;
138+
while (top < bottom) {
139+
int mid = (top + bottom) / 2;
140+
bool hasBlackPixel = false;
141+
for (int j = left_most; j <= right_most; j++) {
142+
if (image[mid][j] == '1') {
143+
hasBlackPixel = true;
144+
break;
145+
}
146+
}
147+
if (hasBlackPixel) {
148+
bottom = mid;
149+
} else {
150+
top = mid + 1;
151+
}
152+
}
153+
int top_most = top;
154+
155+
top = x;
156+
bottom = n - 1;
157+
while (top < bottom) {
158+
int mid = (top + bottom + 1) / 2;
159+
bool hasBlackPixel = false;
160+
for (int j = left_most; j <= right_most; j++) {
161+
if (image[mid][j] == '1') {
162+
hasBlackPixel = true;
163+
break;
164+
}
165+
}
166+
if (hasBlackPixel) {
167+
top = mid;
168+
} else {
169+
bottom = mid - 1;
170+
}
171+
}
172+
int bottom_most = top;
173+
174+
int width = right_most - left_most + 1;
175+
int height = bottom_most - top_most + 1;
176+
return width * height;
177+
}
82178
};

lintcode/600.smallest-rectangle-enclosing-black-pixels.py

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Tag: Binary Search on Answer, Binary Search
22
# Time: O(N * logM + M * logN)
33
# Space: O(1)
4-
# Ref: -
5-
# Note: Graph | Leetcode-302
4+
# Ref: Leetcode-302
5+
# Note: Graph
66

77
# An image is represented by a binary matrix with `0` as a white pixel and `1` as a black pixel.
88
# The black pixels are connected, i.e., there is only one black region.
@@ -37,34 +37,33 @@
3737
class Solution:
3838
"""
3939
@param image: a binary matrix with '0' and '1'
40-
@param x: the location of one of the black pixels
41-
@param y: the location of one of the black pixels
40+
@param x: the location of one of the white pixels
41+
@param y: the location of one of the white pixels
4242
@return: an integer
4343
"""
4444
def min_area(self, image: List[List[str]], x: int, y: int) -> int:
4545
# write your code here
4646

47+
n = len(image)
48+
m = len(image[0])
49+
4750
left = self.search(image, 0, y, self.check_col, False)
48-
right = self.search(image, y, len(image[0]), self.check_col, True)
51+
right = self.search(image, y, m, self.check_col, True)
4952
top = self.search(image, 0, x, self.check_row, False)
50-
bottom = self.search(image, x, len(image), self.check_row, True)
53+
bottom = self.search(image, x, n, self.check_row, True)
5154
return (bottom - top) * (right - left)
5255

53-
def check_row(self, image: List[List[str]], row: int, opposite: bool) -> bool:
54-
island = False
55-
for col in image[row]:
56-
if col == '1':
57-
island = True
58-
break
59-
return not island if opposite else island
60-
61-
def check_col(self, image: List[List[str]], col: int, opposite: bool) -> bool:
62-
island = False
63-
for row in image:
64-
if row[col] == '1':
65-
island = True
66-
break
67-
return not island if opposite else island
56+
def check_row(self, image: List[List[str]], row: int, white: bool) -> bool:
57+
if white:
58+
return all(image[row][j] == '0' for j in range(len(image[row])))
59+
else:
60+
return any(image[row][j] == '1' for j in range(len(image[row])))
61+
62+
def check_col(self, image: List[List[str]], col: int, white: bool) -> bool:
63+
if white:
64+
return all(image[i][col] == '0' for i in range(len(image)))
65+
else:
66+
return any(image[i][col] == '1' for i in range(len(image)))
6867

6968
def search(self, image: List[List[str]], start: int, end: int, check: Callable, oppsite: True) -> int:
7069
while start < end:
@@ -73,4 +72,65 @@ def search(self, image: List[List[str]], start: int, end: int, check: Callable,
7372
end = mid
7473
else:
7574
start = mid + 1
76-
return start
75+
return start
76+
77+
78+
class Solution:
79+
"""
80+
@param image: a binary matrix with '0' and '1'
81+
@param x: the location of one of the black pixels
82+
@param y: the location of one of the black pixels
83+
@return: an integer
84+
"""
85+
def min_area(self, image: List[List[str]], x: int, y: int) -> int:
86+
# write your code here
87+
n = len(image)
88+
m = len(image[0])
89+
90+
left = 0
91+
right = y
92+
while left < right:
93+
mid = (left + right) // 2
94+
if any(image[i][mid] == '1' for i in range(n)):
95+
right = mid
96+
else:
97+
left = mid + 1
98+
99+
left_most = left
100+
101+
left = y
102+
right = m - 1
103+
while left < right:
104+
mid = (left + right) // 2 + 1
105+
if any(image[i][mid] == '1' for i in range(n)):
106+
left = mid
107+
else:
108+
right = mid - 1
109+
110+
right_most = left
111+
112+
113+
top = 0
114+
bottom = x
115+
while top < bottom:
116+
mid = (top + bottom) // 2
117+
if any(image[mid][j] == '1' for j in range(left_most, right_most + 1)):
118+
bottom = mid
119+
else:
120+
top = mid + 1
121+
122+
top_most = top
123+
124+
top = x
125+
bottom = n - 1
126+
while top < bottom:
127+
mid = (top + bottom) // 2 + 1
128+
if any(image[mid][j] == '1' for j in range(left_most, right_most + 1)):
129+
top = mid
130+
else:
131+
bottom = mid - 1
132+
bottom_most = top
133+
134+
width = right_most - left_most + 1
135+
height = bottom_most - top_most + 1
136+
return width * height

0 commit comments

Comments
 (0)