Skip to content

Commit 739f521

Browse files
just4oncegitbook-bot
authored andcommitted
GitBook: [master] 6 pages modified
1 parent e68376d commit 739f521

6 files changed

+307
-0
lines changed

SUMMARY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@
261261
* [774-minimize-max-distance-to-gas-station](leetcode/binary-search/774-minimize-max-distance-to-gas-station.md)
262262
* [778-swim-in-rising-water](leetcode/binary-search/778-swim-in-rising-water.md)
263263
* [786-k-th-smallest-prime-fraction](leetcode/binary-search/untitled.md)
264+
* [793-preimage-size-of-factorial-zeroes-function](leetcode/binary-search/793-preimage-size-of-factorial-zeroes-function.md)
265+
* [852-peak-index-in-a-mountain-array](leetcode/binary-search/852-peak-index-in-a-mountain-array.md)
266+
* [862-shortest-subarray-with-sum-at-least-k](leetcode/binary-search/862-shortest-subarray-with-sum-at-least-k.md)
267+
* [875-koko-eating-bananas](leetcode/binary-search/875-koko-eating-bananas.md)
264268
* [dynamic-programing](leetcode/dynamic-programing/README.md)
265269
* [174-dungeon-game](leetcode/dynamic-programing/174-dungeon-game.md)
266270
* [bit-manipulation](leetcode/bit-manipulation-1/README.md)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 793-preimage-size-of-factorial-zeroes-function
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/description/](https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/description/)
6+
7+
Let f\(x\) be the number of zeroes at the end of x!. \(Recall that x! = 1 _2_ 3 _..._ x, and by convention, 0! = 1.\)
8+
9+
For example, f\(3\) = 0 because 3! = 6 has no zeroes at the end, while f\(11\) = 2 because 11! = 39916800 has 2 zeroes at the end. Given K, find how many non-negative integers x have the property that f\(x\) = K.
10+
11+
**Example:**
12+
13+
```text
14+
Example 1:
15+
Input: K = 0
16+
Output: 5
17+
Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes.
18+
19+
Example 2:
20+
Input: K = 5
21+
Output: 0
22+
Explanation: There is no x such that x! ends in K = 5 zeroes.
23+
```
24+
25+
**Note:**
26+
27+
K will be an integer in the range \[0, 10^9\].
28+
29+
## Thought Process {#thought-process}
30+
31+
1. Binary Search
32+
1. The number of trailing zeroes increase every time we have multiplication result of 10, meaning factor of 5 and 2
33+
2. So, the number of zeroes = min\(num of 2's, num of 5's\), and because of factor 2 appear at least every other number, we can simply count the number of factor 5
34+
3. For each x, K <= x < 5K + 1
35+
4. Performing binary search on this borders until the borders meet
36+
5. If we have found a value where we have exactly K zeroes, we can return 5
37+
6. Else there is no possible way to have K zeroes we return 0
38+
7. Time complexity O\(logK\)
39+
8. Space complexity O\(1\)
40+
41+
## Solution
42+
43+
```java
44+
class Solution {
45+
public int preimageSizeFZF(int K) {
46+
// x / 5 <= f(x) <= x
47+
// so x >= K && x <= 5K
48+
// Therefore x will in the range of [K, ]
49+
long lo = K, hi = 5L * K + 1;
50+
while (lo < hi) {
51+
long mi = lo + (hi - lo) / 2;
52+
int k = f(mi);
53+
if (k == K) return 5;
54+
else if (k < K) lo = mi + 1;
55+
else hi = mi - 1;
56+
}
57+
return 0;
58+
}
59+
60+
private int f(long x) {
61+
int count = 0;
62+
while (x > 0) {
63+
x /= 5;
64+
count += x;
65+
}
66+
return count;
67+
}
68+
}
69+
```
70+
71+
## Additional {#additional}
72+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 852-peak-index-in-a-mountain-array
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/peak-index-in-a-mountain-array/description/](https://leetcode.com/problems/peak-index-in-a-mountain-array/description/)
6+
7+
Let's call an array A a mountain if the following properties hold:
8+
9+
A.length &gt;= 3 There exists some 0 &lt; i &lt; A.length - 1 such that A\[0\] &lt; A\[1\] &lt; ... A\[i-1\] &lt; A\[i\] &gt; A\[i+1\] &gt; ... &gt; A\[A.length - 1\] Given an array that is definitely a mountain, return any i such that A\[0\] &lt; A\[1\] &lt; ... A\[i-1\] &lt; A\[i\] &gt; A\[i+1\] &gt; ... &gt; A\[A.length - 1\].
10+
11+
**Example:**
12+
13+
```text
14+
Input: [0,1,0]
15+
Output: 1
16+
17+
Input: [0,2,1,0]
18+
Output: 1
19+
```
20+
21+
**Note:**
22+
23+
3 &lt;= A.length &lt;= 10000 0 &lt;= A\[i\] &lt;= 10^6 A is a mountain, as defined above.
24+
25+
## Thought Process {#thought-process}
26+
27+
1. Binary Search
28+
1. Since there is only one peak, we can use binary search to successively cut the search range by half
29+
2. The boundaries are lo = 1, hi = A.length - 2
30+
3. We compare A\[mi\] to A\[mi + 1\]
31+
4. If A\[mi\] &lt; A\[mi + 1\], the peak lies on the right, so lo = mi + 1
32+
5. Else hi = mi
33+
6. Time complexity O\(logn\)
34+
7. Space complexity O\(1\)
35+
36+
## Solution
37+
38+
```java
39+
class Solution {
40+
public int peakIndexInMountainArray(int[] A) {
41+
int lo = 1, hi = A.length - 2;
42+
while (lo < hi) {
43+
int mi = lo + (hi - lo) / 2;
44+
if (A[mi] < A[mi + 1]) lo = mi + 1;
45+
else hi = mi;
46+
}
47+
return lo;
48+
}
49+
}
50+
```
51+
52+
## Additional {#additional}
53+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 862-shortest-subarray-with-sum-at-least-k
2+
3+
## Question {#question}
4+
5+
[https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/description/)
6+
7+
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
8+
9+
If there is no non-empty subarray with sum at least K, return -1.
10+
11+
**Example 1:**
12+
13+
```text
14+
Input: A = [1], K = 1
15+
Output: 1
16+
```
17+
18+
**Example 2:**
19+
20+
```text
21+
Input: A = [1,2], K = 4
22+
Output: -1
23+
```
24+
25+
**Example 3:**
26+
27+
```text
28+
Input: A = [2,-1,2], K = 3
29+
Output: 3
30+
```
31+
32+
**Note:**
33+
34+
* 1 &lt;= A.length &lt;= 50000
35+
* -10 ^ 5 &lt;= A\[i\] &lt;= 10 ^ 5
36+
* 1 &lt;= K &lt;= 10 ^ 9
37+
38+
## Thought Process {#thought-process}
39+
40+
1. Deque
41+
1. This problem can be rephrased as prefix sums of A, let's call it P
42+
2. For every index j, we are trying to find largest i such that P\[j\] - P\[i\] &gt;= K
43+
3. If we check all the prefix sums before j, the run time will become n^2
44+
4. For every j we visited, we add it to queue as potential answer for next j, let's define Q\[0\] to be the 1st item and Q\[1\] to be 2nd item and the rest follows the same pattern
45+
5. For current j, we compare P\[j\] to P\[Q\[0\]\] and check if their difference &gt;= K
46+
6. If the difference is &gt;= K, we can pop the first item because the in the next iteration, the length of using the first item will not be any better than current length
47+
7. Another observation is that if P\[j\] is &lt;= P\[Q\[last\]\], it's a better candidate than the last deque element, therefore we use while loop to removeLast
48+
8. Essentially, we are maintaining a increasing P\[i\] in the deque
49+
9. Time complexity O\(n\)
50+
10. Space complexity O\(n\)
51+
52+
## Solution
53+
54+
```java
55+
class Solution {
56+
public int shortestSubarray(int[] A, int K) {
57+
int n = A.length;
58+
int[] P = new int[n + 1];
59+
for(int i = 1; i < P.length; i++) P[i] = P[i - 1] + A[i - 1];
60+
Deque<Integer> deque = new LinkedList<>();
61+
int res = n + 1;
62+
for (int j = 0; j < P.length; j++) {
63+
while (!deque.isEmpty() && P[j] - P[deque.getFirst()] >= K) res = Math.min(res, j - deque.removeFirst());
64+
while (!deque.isEmpty() && P[j] <= P[deque.getLast()]) deque.pollLast();
65+
deque.offer(j);
66+
}
67+
return res == n + 1 ? - 1 : res;
68+
}
69+
}
70+
```
71+
72+
## Additional {#additional}
73+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# 875-koko-eating-bananas
2+
3+
## Question {#question}
4+
5+
\*\*\*\*[**https://leetcode.com/problems/koko-eating-bananas/description/**](https://leetcode.com/problems/koko-eating-bananas/description/)\*\*\*\*
6+
7+
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.
8+
9+
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.
10+
11+
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
12+
13+
Return the minimum integer `K` such that she can eat all the bananas within `H` hours.
14+
15+
*
16+
**Example 1:**
17+
18+
```text
19+
Input: piles = [3,6,7,11], H = 8
20+
Output: 4
21+
```
22+
23+
**Example 2:**
24+
25+
```text
26+
Input: piles = [30,11,23,4,20], H = 5
27+
Output: 30
28+
```
29+
30+
**Example 3:**
31+
32+
```text
33+
Input: piles = [30,11,23,4,20], H = 6
34+
Output: 23
35+
```
36+
37+
**Note:**
38+
39+
* `1 <= piles.length <= 10^4`
40+
* `piles.length <= H <= 10^9`
41+
* `1 <= piles[i] <= 10^9`
42+
43+
## Thought Process {#thought-process}
44+
45+
1. Binary Search
46+
1. Since the time is bounded by 1 and 10e9, we can successively half our search, lo and hi respectively
47+
2. When the function canFinish\(mi\), we set our hi = mi, else we can increase our speed, so lo = mi + 1
48+
3. Time complexity O\(nlog\(w\)\), where w is the range and n is number of elements
49+
4. Space complexity O\(1\)
50+
2. Search Starts from Average Speed
51+
1. Instead of making guess, we can smartly guess our initial value to be sum / H
52+
2. If the speed is not enough, we can increase speed by 1 until Koko can finish it
53+
3. Time complexity O\(n\)
54+
4. Space complexity O\(1\)
55+
56+
## Solution
57+
58+
```java
59+
class Solution {
60+
public int minEatingSpeed(int[] piles, int H) {
61+
int lo = 1, hi = 1000000000;
62+
while (lo < hi) {
63+
int mi = lo + (hi - lo) / 2;
64+
if (canFinish(piles, mi, H)) hi = mi;
65+
else lo = mi + 1;
66+
}
67+
return lo;
68+
}
69+
70+
private boolean canFinish(int[] piles, int K, int H) {
71+
int hour = 0;
72+
for (int pile : piles) {
73+
hour += (pile - 1) / K + 1;
74+
}
75+
return hour <= H;
76+
}
77+
}
78+
```
79+
80+
```java
81+
class Solution {
82+
public int minEatingSpeed(int[] piles, int H) {
83+
long sum = 0;
84+
for (int pile : piles) sum += pile;
85+
long speed = (sum - 1) / H + 1L;
86+
while (true) {
87+
if (canFinish(piles, speed, H)) return (int) speed;
88+
else speed++;
89+
}
90+
}
91+
92+
private boolean canFinish(int[] piles, long K, int H) {
93+
int hour = 0;
94+
for (int pile : piles) {
95+
hour += (pile - 1) / K + 1;
96+
}
97+
return hour <= H;
98+
}
99+
}
100+
```
101+
102+
## Additional {#additional}
103+

leetcode/binary-search/untitled.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Question {#question}
44

5+
[https://leetcode.com/problems/k-th-smallest-prime-fraction/description/](https://leetcode.com/problems/k-th-smallest-prime-fraction/description/)
6+
57
A sorted list A contains 1, plus some number of primes. Then, for every p &lt; q in the list, we consider the fraction p/q.
68

79
What is the K-th smallest fraction considered? Return your answer as an array of ints, where answer\[0\] = p and answer\[1\] = q.

0 commit comments

Comments
 (0)