Skip to content

Commit 530ff83

Browse files
committed
add 14
1 parent e71d69b commit 530ff83

6 files changed

+205
-30
lines changed

README.md

Lines changed: 4 additions & 3 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) | 1/128 | - |
47+
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 2/128 | 1 vip |
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(39) | Solution | Tag | Time | Space | Ref |
308+
| Link | Problem(40) | 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\) | - |
@@ -343,6 +343,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
343343
| [Leetcode-367](https://leetcode.com/problems/valid-perfect-square/) | Valid Perfect Square | [c++](./leetcode/367.valid-perfect-square.cpp), [python3](./leetcode/367.valid-perfect-square.py) | Binary Search | O\(logN\) | O\(1\) | - |
344344
| [Lintcode-457](https://www.lintcode.com/problem/classical-binary-search/) | Classical Binary Search | [c++](./lintcode/457.classical-binary-search.cpp), [python3](./lintcode/457.classical-binary-search.py) | Binary Search | O\(logN\) | O\(1\) | - |
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\) | - |
346+
| [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\) | - |
346347
| [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 |
347348
| [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\) | - |
348349
| [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\) | - |
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Tag: Binary Search
2+
// Time: O(logN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a sorted array (ascending order) and a `target` number, find the first index of this number in $O(log n)$ time complexity.
8+
//
9+
// If the `target` number does not exist in the array, return `-1`.
10+
//
11+
// **Example 1:**
12+
//
13+
// Input:
14+
// ```
15+
// tuple = [1,4,4,5,7,7,8,9,9,10]
16+
// target = 1
17+
// ```
18+
// Output:
19+
// ```
20+
// 0
21+
// ```
22+
// Explanation:
23+
//
24+
// The first index of 1 is 0.
25+
//
26+
// **Example 2:**
27+
//
28+
// Input:
29+
// ```
30+
// tuple = [1, 2, 3, 3, 4, 5, 10]
31+
// target = 3
32+
// ```
33+
// Output:
34+
// ```
35+
// 2
36+
// ```
37+
// Explanation:
38+
//
39+
// The first index of 3 is 2.
40+
//
41+
// **Example 3:**
42+
//
43+
// Input:
44+
// ```
45+
// tuple = [1, 2, 3, 3, 4, 5, 10]
46+
// target = 6
47+
// ```
48+
// Output:
49+
// ```
50+
// -1
51+
// ```
52+
// Explanation:
53+
//
54+
// There is no 6 in the array,return -1.
55+
//
56+
//
57+
58+
class Solution {
59+
public:
60+
/**
61+
* @param nums: The integer array.
62+
* @param target: Target to find.
63+
* @return: The first position of target. Position starts from 0.
64+
*/
65+
int binarySearch(vector<int> &nums, int target) {
66+
// write your code here
67+
int n = nums.size();
68+
if (n == 0) {
69+
return -1;
70+
}
71+
72+
73+
int left = 0;
74+
int right = n - 1;
75+
while (left < right) {
76+
int mid = left + (right - left) / 2;
77+
if (nums[mid] < target) {
78+
left = mid + 1;
79+
} else {
80+
right = mid;
81+
}
82+
}
83+
84+
return nums[left] == target ? left : -1;
85+
86+
}
87+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Tag: Binary Search
2+
# Time: O(logN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a sorted array (ascending order) and a `target` number, find the first index of this number in $O(log n)$ time complexity.
8+
#
9+
# If the `target` number does not exist in the array, return `-1`.
10+
#
11+
# **Example 1:**
12+
#
13+
# Input:
14+
# ```
15+
# tuple = [1,4,4,5,7,7,8,9,9,10]
16+
# target = 1
17+
# ```
18+
# Output:
19+
# ```
20+
# 0
21+
# ```
22+
# Explanation:
23+
#
24+
# The first index of 1 is 0.
25+
#
26+
# **Example 2:**
27+
#
28+
# Input:
29+
# ```
30+
# tuple = [1, 2, 3, 3, 4, 5, 10]
31+
# target = 3
32+
# ```
33+
# Output:
34+
# ```
35+
# 2
36+
# ```
37+
# Explanation:
38+
#
39+
# The first index of 3 is 2.
40+
#
41+
# **Example 3:**
42+
#
43+
# Input:
44+
# ```
45+
# tuple = [1, 2, 3, 3, 4, 5, 10]
46+
# target = 6
47+
# ```
48+
# Output:
49+
# ```
50+
# -1
51+
# ```
52+
# Explanation:
53+
#
54+
# There is no 6 in the array,return -1.
55+
#
56+
#
57+
58+
from typing import (
59+
List,
60+
)
61+
62+
class Solution:
63+
"""
64+
@param nums: The integer array.
65+
@param target: Target to find.
66+
@return: The first position of target. Position starts from 0.
67+
"""
68+
def binary_search(self, nums: List[int], target: int) -> int:
69+
# write your code here
70+
n = len(nums)
71+
if n == 0:
72+
return -1
73+
74+
left = 0
75+
right = n - 1
76+
77+
while left < right:
78+
mid = left + (right - left) // 2
79+
if nums[mid] < target:
80+
left = mid + 1
81+
else:
82+
right = mid
83+
84+
return left if nums[left] == target else -1

lintcode/458.last-position-of-target.vip

Whitespace-only changes.

list/9c-basic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
### 二分法
99

1010
3. https://www.lintcode.com/problem/classical-binary-search/
11-
- https://www.lintcode.com/problem/first-position-of-target/
12-
- https://www.lintcode.com/problem/last-position-of-target/
11+
4. https://www.lintcode.com/problem/first-position-of-target/
12+
5. https://www.lintcode.com/problem/last-position-of-target/
1313
- https://www.lintcode.com/problem/first-bad-version/
1414
- https://www.lintcode.com/problem/search-in-a-big-sorted-array/
1515
- https://www.lintcode.com/problem/find-minimum-in-rotated-sorted-array/

test/python.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,40 @@
88

99
import heapq
1010

11+
from typing import (
12+
List,
13+
)
14+
1115
class Solution:
12-
def minTimeToReach(self, moveTime: List[List[int]]) -> int:
13-
n = len(moveTime)
14-
m = len(moveTime[0])
15-
16-
heap = [(0, 0, 0, 0)]
17-
directions = [-1, 0, 1, 0, -1]
18-
visited = set()
19-
20-
res = float('inf')
21-
while len(heap) > 0:
22-
cost, i, j, delta = heapq.heappop(heap)
23-
if (i, j) in visited:
24-
continue
25-
26-
visited.add((i, j))
27-
if i == n - 1 and j == m - 1:
28-
return cost
29-
30-
for d in range(4):
31-
x = i + directions[d]
32-
y = j + directions[d + 1]
33-
if 0 <= x < n and 0 <= y < m and (x, y) not in visited:
34-
next_cost = max(moveTime[x][y], cost) + delta + 1
35-
heapq.heappush(heap, (next_cost, x, y, 1 - delta))
16+
"""
17+
@param nums: The integer array.
18+
@param target: Target to find.
19+
@return: The first position of target. Position starts from 0.
20+
"""
21+
def binary_search(self, nums: List[int], target: int) -> int:
22+
# write your code here
23+
n = len(nums)
24+
if n == 0:
25+
return -1
26+
27+
print(n)
28+
left = 0
29+
right = n - 1
30+
31+
while left < right:
32+
mid = left + (right - left) // 2
33+
if nums[mid] < target:
34+
left = mid + 1
35+
else:
36+
right = mid
37+
38+
return left if nums[left] == target else -1
3639

3740

3841
s = Solution()
3942
ts = datetime.now()
4043

41-
res = s.minTimeToReach([[0,1],[1,2]])
44+
res = s.binary_search([], 6)
4245

4346
print(datetime.now() - ts)
4447
print(res)

0 commit comments

Comments
 (0)