Skip to content

Commit 59f2785

Browse files
committed
[Function add]
1. Add leetcode solutions with tag binary search.
1 parent a6c7b26 commit 59f2785

5 files changed

+197
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@
319319

320320
[173. Binary Search Tree Iterator](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/173.%20Binary%20Search%20Tree%20Iterator.md)
321321

322-
[174. Dungeon Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/174.%20Dungeon20Game.md)
322+
[174. Dungeon Game](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/174.%20Dungeon%20Game.md)
323323

324324
[175. Combine Two Tables](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/175.%20Combine%20Two%20Tables.md)
325325

leetcode/4. Median of Two Sorted Arrays.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,31 @@ class Solution {
107107
(double)merge[merge.length / 2] : (double)(merge[merge.length / 2] + merge[merge.length / 2 - 1]) / 2;
108108
}
109109
}
110-
```
110+
```
111+
112+
### Third Time
113+
* Method 1: binary search
114+
```Java
115+
class Solution {
116+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
117+
int m = nums1.length, n = nums2.length;
118+
if(m > n) return findMedianSortedArrays(nums2, nums1);
119+
int k = (m + n + 1) / 2;
120+
int left = 0, right = m;
121+
while(left < right){
122+
int mid1 = left + (right - left) / 2;
123+
int mid2 = k - mid1;
124+
if(nums1[mid1] < nums2[mid2 - 1]) left = mid1 + 1;
125+
else right = mid1;
126+
}
127+
int mid1 = left;
128+
int mid2 = k - left;
129+
int c1 = Math.max(mid1 <= 0 ? Integer.MIN_VALUE: nums1[mid1 - 1],
130+
mid2 <= 0 ? Integer.MIN_VALUE: nums2[mid2 - 1]);
131+
if((m + n) % 2 != 0) return (double)c1;
132+
int c2 = Math.min(mid1 >= m ? Integer.MAX_VALUE: nums1[mid1],
133+
mid2 >= n ? Integer.MAX_VALUE: nums2[mid2]);
134+
return (c1 + c2) * 0.5;
135+
}
136+
}
137+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## 719. Find K-th Smallest Pair Distance
2+
3+
### Question:
4+
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
5+
6+
```
7+
Example 1:
8+
9+
Input:
10+
nums = [1,3,1]
11+
k = 1
12+
Output: 0
13+
Explanation:
14+
Here are all the pairs:
15+
(1,3) -> 2
16+
(1,1) -> 0
17+
(3,1) -> 2
18+
Then the 1st smallest distance pair is (1,1), and its distance is 0.
19+
```
20+
21+
Note:
22+
1. 2 <= len(nums) <= 10000.
23+
2. 0 <= nums[i] < 1000000.
24+
3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.
25+
26+
### Solution:
27+
* Method 1: Bucket sort
28+
```Java
29+
class Solution {
30+
public int smallestDistancePair(int[] nums, int k) {
31+
int len = nums.length;
32+
int size = len * (len - 1) / 2;
33+
int[] arr = new int[1000000];
34+
for(int i = 0; i < len; i++){
35+
for(int j = i + 1; j < len; j++){
36+
arr[Math.abs(nums[i] - nums[j])]++;
37+
}
38+
}
39+
int cur = 0;
40+
while(k > 0){
41+
k -= arr[cur++];
42+
}
43+
return cur - 1;
44+
}
45+
}
46+
```
47+
48+
* Method 2: Binary Search + dp
49+
```Java
50+
class Solution {
51+
public int smallestDistancePair(int[] nums, int k) {
52+
Arrays.sort(nums);
53+
int len = nums.length;
54+
int left = 0, right = nums[nums.length - 1];
55+
while(left <= right){
56+
int mid = left + (right - left) / 2;
57+
int j = 0;
58+
int count = 0;
59+
for(int i = 0; i < len; i++){
60+
while(j < len && nums[j] - nums[i] <= mid) ++j;
61+
count += j - i - 1;
62+
}
63+
if(count >= k) right = mid - 1;
64+
else left = mid + 1;
65+
}
66+
return left;
67+
}
68+
}
69+
```
70+

leetcode/778. Swim in Rising Water.md

+40
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,43 @@ Note:
7272
}
7373
}
7474
```
75+
76+
* Method 2: Binary Search
77+
```Java
78+
class Solution {
79+
private int[][] g;
80+
private int N;
81+
private static final int[][] dir = new int[][]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
82+
private boolean hasPath(int t){
83+
if(g[0][0] > t) return false;
84+
Queue<int[]> q = new LinkedList<>();
85+
q.offer(new int[]{0, 0});
86+
Set<Integer> seen = new HashSet<>();
87+
seen.add(0);
88+
while(!q.isEmpty()){
89+
int[] cur = q.poll();
90+
if(cur[0] == N - 1 && cur[1] == N - 1) return true;
91+
int tx = 0, ty = 0;
92+
for(int d = 0; d < 4; d++){
93+
tx = cur[0] + dir[d][0];
94+
ty = cur[1] + dir[d][1];
95+
if(tx < 0 || tx >= N || ty < 0 || ty >= N || g[tx][ty] > t || seen.contains(tx * N + ty)) continue;
96+
seen.add(tx * N + ty);
97+
q.offer(new int[]{tx, ty});
98+
}
99+
}
100+
return false;
101+
}
102+
public int swimInWater(int[][] grid) {
103+
this.g = grid;
104+
this.N = grid.length;
105+
int left = 0, right = N * N;
106+
while(left <= right){
107+
int mid = left + (right - left) / 2;
108+
if(hasPath(mid)) right = mid - 1;
109+
else left = mid + 1;
110+
}
111+
return left;
112+
}
113+
}
114+
```

leetcode/875. Koko Eating Bananas.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 875. Koko Eating Bananas
2+
3+
### Question:
4+
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
5+
6+
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
7+
8+
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
9+
10+
Return the minimum integer K such that she can eat all the bananas within H hours.
11+
12+
```
13+
Example 1:
14+
15+
Input: piles = [3,6,7,11], H = 8
16+
Output: 4
17+
18+
Example 2:
19+
20+
Input: piles = [30,11,23,4,20], H = 5
21+
Output: 30
22+
23+
Example 3:
24+
25+
Input: piles = [30,11,23,4,20], H = 6
26+
Output: 23
27+
```
28+
29+
Note:
30+
1. 1 <= piles.length <= 10^4
31+
2. piles.length <= H <= 10^9
32+
3. 1 <= piles[i] <= 10^9
33+
34+
### Solution:
35+
* Method 1: Binary Search
36+
```Java
37+
class Solution {
38+
private int[] p;
39+
private boolean canEat(int H, int K){
40+
int res = 0;
41+
for(int pp : p){
42+
res += ((pp / K) + (((pp % K) == 0) ? 0: 1));
43+
}
44+
return res <= H;
45+
}
46+
public int minEatingSpeed(int[] piles, int H) {
47+
this.p = piles;
48+
int slow = 1, fast = 1000000000;
49+
while(slow <= fast){
50+
int mid = slow + (fast - slow) / 2;
51+
if(canEat(H, mid)) fast = mid - 1;
52+
else slow = mid + 1;
53+
}
54+
return slow;
55+
}
56+
}
57+
```
58+

0 commit comments

Comments
 (0)