Skip to content

Commit 438022d

Browse files
updating all READMEs
1 parent 9ede0e8 commit 438022d

File tree

8 files changed

+249
-23
lines changed

8 files changed

+249
-23
lines changed

.dev/logs.json

+11-6
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168
"len": 3,
169169
"sols": {
170170
"solution.py": "6ed311fbdf421b9d9d3c4c0cd7131a362306f58b6563ecf21fde6281de398e71",
171-
"search_insert_position.rs": "f8ea51542be3eb8bdf94c0d1e93862cdd93b947106bc6f4e91a35159c19e2ec0"
171+
"search_insert_position.rs": "fd805d477dd36261f23c7441f2d83bf39c5ffdb986be3957aafc7c3f1d292cf5"
172172
}
173173
},
174174
"36": {
@@ -1331,8 +1331,10 @@
13311331
"sols": {}
13321332
},
13331333
"292": {
1334-
"len": 0,
1335-
"sols": {}
1334+
"len": 2,
1335+
"sols": {
1336+
"nim_game.rs": "163b742857fcca7b2d30e3e09f22d661a2ec0cb0e91129e6329f21de96ff0f68"
1337+
}
13361338
},
13371339
"293": {
13381340
"len": 0,
@@ -2355,7 +2357,8 @@
23552357
"540": {
23562358
"len": 3,
23572359
"sols": {
2358-
"single_element_in_a_sorted_array.go": "dd09479928d273f34941a56d90b195cf9faed2f73e00df125084e785886bcc2e"
2360+
"single_element_in_a_sorted_array.go": "dd09479928d273f34941a56d90b195cf9faed2f73e00df125084e785886bcc2e",
2361+
"single_element_in_a_sorted_array.rs": "39e6dce0547f86488a2b6ca94ea3a81d95d2f8484d0c4bf1e16123ac2d09e174"
23592362
}
23602363
},
23612364
"541": {
@@ -4261,8 +4264,10 @@
42614264
"sols": {}
42624265
},
42634266
"1011": {
4264-
"len": 0,
4265-
"sols": {}
4267+
"len": 2,
4268+
"sols": {
4269+
"capacity_to_ship_packages_within_d_days.rs": "212be1fc5b4c283a11bdf9b810865fb72c496335353dc280cb557ad4d524e9e8"
4270+
}
42664271
},
42674272
"1012": {
42684273
"len": 0,

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
[**_Rejection Sampling_**][rejection sampling]   
9696
[**_Biconnected Component_**][biconnected component]
9797

98-
### **Total Problems Solved: _119_**
98+
### **Total Problems Solved: _121_**
9999

100100
---
101101

TOPICWISE.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
| **0888** | [Fair Candy Swap][888] | Array, Hash Table, Binary Search, Sorting | ![][easy] | |
5656
| **0944** | [Delete Columns to Make Sorted][944] | Array, String | ![][easy] | |
5757
| **0997** | [Find the Town Judge][997] | Array, Hash Table, Graph | ![][easy] | |
58+
| **1056** | [Capacity To Ship Packages Within D Days][1056] | Array, BS | ![][medium] | |
5859
| **1232** | [Check If It Is a Straight Line][1232] | Array, Math, Geometry | ![][easy] | |
5960
| **1480** | [Running Sum of 1d Array][1480] | Array, Prefix Sum | ![][easy] | |
6061
| **1491** | [Average Salary Excluding the Minimum and Maximum][1491] | Array, Sorting | ![][easy] | |
@@ -164,6 +165,7 @@
164165
| **0258** | [Add Digits][258] | Math, Simulation, Number Theory | ![][easy] | |
165166
| **0263** | [Ugly Number][263] | Math | ![][easy] | |
166167
| **0268** | [Missing Number][268] | Array, Hash Table, Math, BS, Bit Manip, Sorting | ![][easy] | |
168+
| **0292** | [Nim Game][292] | Math, Brainteaser, Game Theory | ![][easy] | |
167169
| **0367** | [Valid Perfect Square][367] | Math, Binary Search | ![][easy] | |
168170
| **0445** | [Add Two Numbers II][445] | Linked List, Math, Stack | ![][medium] | |
169171
| **0509** | [Fibonacci Number][509] | Math, Recursion, DP, Memoization | ![][easy] | |
@@ -288,6 +290,7 @@
288290
| **0367** | [Valid Perfect Square][367] | Math, Binary Search | ![][easy] | |
289291
| **0540** | [Single Element in a Sorted Array][540] | Array, BS | ![][medium] | |
290292
| **0704** | [Binary Search][704] | Array, Binary Search | ![][easy] | |
293+
| **1056** | [Capacity To Ship Packages Within D Days][1056] | Array, BS | ![][medium] | |
291294
| **1498** | [Number of Subsequences That Satisfy the Given Sum Condition][1498] | Array, 2P, BS, Sorting | ![][medium] | |
292295
| **0888** | [Fair Candy Swap][888] | Array, Hash Table, Binary Search, Sorting | ![][easy] | |
293296

@@ -605,8 +608,9 @@
605608

606609
## Game Theory
607610

608-
| # | Solution | Tags | Difficulty | Remark |
609-
| :-: | :------: | :--: | :--------: | :----: |
611+
| # | Solution | Tags | Difficulty | Remark |
612+
| :------: | :-------------: | :----------------------------: | :--------: | :----: |
613+
| **0292** | [Nim Game][292] | Math, Brainteaser, Game Theory | ![][easy] | |
610614

611615
<!---------------------------------{ Solution Table }-------------------------->
612616

@@ -721,8 +725,9 @@
721725

722726
## Brainteaser
723727

724-
| # | Solution | Tags | Difficulty | Remark |
725-
| :-: | :------: | :--: | :--------: | :----: |
728+
| # | Solution | Tags | Difficulty | Remark |
729+
| :------: | :-------------: | :----------------------------: | :--------: | :----: |
730+
| **0292** | [Nim Game][292] | Math, Brainteaser, Game Theory | ![][easy] | |
726731

727732
<!---------------------------------{ Solution Table }-------------------------->
728733

@@ -914,6 +919,7 @@
914919
[278]: ./src/0201-0300/278%20-%20First%20Bad%20Version/
915920
[283]: ./src/0201-0300/283%20-%20Move%20Zeroes/
916921
[290]: ./src/0201-0300/290%20-%20Word%20Pattern/
922+
[292]: ./src/0201-0300/292%20-%20Nim%20Game/
917923
[300]: ./src/0201-0300/300%20-%20Longest%20Increasing%20Subsequence/
918924
[322]: ./src/0301-0400/322%20-%20Coin%20Change/
919925
[337]: ./src/0301-0400/337%20-%20House%20Robber%20III/
@@ -945,6 +951,7 @@
945951
[997]: ./src/0901-1000/997%20-%20Find%20the%20Town%20Judge/
946952
[997]: ./src/0901-1000/997%20-%20Find%20the%20Town%20Judge/
947953
[1047]: ./src/1001-1100/1047%20-%20Remove%20All%20Adjacent%20Duplicates%20In%20String/
954+
[1056]: ./src/1001-1100/1011%20-%20Capacity%20To%20Ship%20Packages%20Within%20D%20Days/
948955
[1092]: ./src/1001-1100/1092%20-%20Shortest%20Common%20Supersequence/
949956
[1143]: ./src/1101-1200/1143%20-%20Longest%20Common%20Subsequence/
950957
[1232]: ./src/1201-1300/1232%20-%20Check%20If%20It%20Is%20a%20Straight%20Line/

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ It might take some time to reflect changes from the repository.
103103
[**_Rejection Sampling_**][rejection sampling] &nbsp;&nbsp;
104104
[**_Biconnected Component_**][biconnected component]
105105

106-
### **Total Problems Solved: _119_**
106+
### **Total Problems Solved: _121_**
107107

108108
## Contributors
109109

src/0001-0100/035 - Search Insert Position/README.md

+4-11
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,13 @@ class Solution:
7171
```rs [Rust]
7272
impl Solution {
7373
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
74-
let (mut start, mut end) = (0, nums.len() - 1);
75-
76-
while start <= end {
77-
let mid = start + (end - start) / 2;
78-
if nums[mid] == target {
79-
return mid as i32;
80-
} else if nums[mid] < target {
81-
start = mid + 1;
82-
} else {
83-
end = mid - 1;
74+
for (idx, val) in nums.iter().enumerate() {
75+
if target <= *val {
76+
return idx as i32;
8477
}
8578
}
8679

87-
start as i32
80+
nums.len() as i32
8881
}
8982
}
9083

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 292. Nim Game [![share]](https://leetcode.com/problems/nim-game/)
2+
3+
![][easy]
4+
5+
## Problem Statement
6+
7+
<p>You are playing the following Nim Game with your friend:</p>
8+
<ul>
9+
<li>Initially, there is a heap of stones on the table.</li>
10+
<li>You and your friend will alternate taking turns, and <strong>you go first</strong>.</li>
11+
<li>On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.</li>
12+
<li>The one who removes the last stone is the winner.</li>
13+
</ul>
14+
<p>Given <code>n</code>, the number of stones in the heap, return <code>true</code><em> if you can win the game assuming both you and your friend play optimally, otherwise return </em><code>false</code>.</p>
15+
<p> </p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
```
19+
Input: n = 4
20+
Output: false
21+
Explanation: These are the possible outcomes:
22+
1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins.
23+
2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins.
24+
3. You remove 3 stones. Your friend removes the last stone. Your friend wins.
25+
In all outcomes, your friend wins.
26+
```
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
```
31+
Input: n = 1
32+
Output: true
33+
```
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
```
38+
Input: n = 2
39+
Output: true
40+
```
41+
42+
<p> </p>
43+
<p><strong>Constraints:</strong></p>
44+
<ul>
45+
<li><code>1 &lt;= n &lt;= 2<sup>31</sup> - 1</code></li>
46+
</ul>
47+
48+
<details>
49+
<summary>
50+
51+
#### _Click to open Hints_
52+
53+
</summary>
54+
55+
- If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
56+
57+
</details>
58+
59+
## Solutions
60+
61+
### [_Rust_](nim_game.rs)
62+
63+
```rs [Rust]
64+
impl Solution {
65+
pub fn can_win_nim(n: i32) -> bool {
66+
// if n is divisible by 4, then the first player will always lose
67+
if n % 4 == 0 {
68+
return false;
69+
}
70+
71+
true
72+
}
73+
}
74+
75+
```
76+
77+
### [_..._]()
78+
79+
```
80+
81+
```
82+
83+
<!----------------------------------{ link }--------------------------------->
84+
85+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
86+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
87+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
88+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# 1011. Capacity To Ship Packages Within D Days [![share]](https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/)
2+
3+
![][medium]
4+
5+
## Problem Statement
6+
7+
<p>A conveyor belt has packages that must be shipped from one port to another within <code>days</code> days.</p>
8+
<p>The <code>i<sup>th</sup></code> package on the conveyor belt has a weight of <code>weights[i]</code>. Each day, we load the ship with packages on the conveyor belt (in the order given by <code>weights</code>). We may not load more weight than the maximum weight capacity of the ship.</p>
9+
<p>Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within <code>days</code> days.</p>
10+
<p> </p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
```
14+
15+
Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
16+
Output: 15
17+
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
18+
1st day: 1, 2, 3, 4, 5
19+
2nd day: 6, 7
20+
3rd day: 8
21+
4th day: 9
22+
5th day: 10
23+
24+
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
25+
```
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
```
30+
31+
Input: weights = [3,2,2,4,1,4], days = 3
32+
Output: 6
33+
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
34+
1st day: 3, 2
35+
2nd day: 2, 4
36+
3rd day: 1, 4
37+
```
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
```
42+
43+
Input: weights = [1,2,3,1,1], days = 4
44+
Output: 3
45+
Explanation:
46+
1st day: 1
47+
2nd day: 2
48+
3rd day: 3
49+
4th day: 1, 1
50+
```
51+
52+
<p> </p>
53+
<p><strong>Constraints:</strong></p>
54+
<ul>
55+
<li><code>1 &lt;= days &lt;= weights.length &lt;= 5 * 10<sup>4</sup></code></li>
56+
<li><code>1 &lt;= weights[i] &lt;= 500</code></li>
57+
</ul>
58+
59+
<details>
60+
<summary>
61+
62+
#### _Click to open Hints_
63+
64+
</summary>
65+
66+
- Binary search on the answer. We need a function possible(capacity) which returns true if and only if we can do the task in D days.
67+
68+
</details>
69+
70+
## Solutions
71+
72+
### [_Rust_](capacity_to_ship_packages_within_d_days.rs)
73+
74+
```rs [Rust]
75+
impl Solution {
76+
pub fn ship_within_days(weights: Vec<i32>, days: i32) -> i32 {
77+
let mut min_cap = 0; // it is the max of all weights
78+
let mut max_cap = 0; // sum of all weights
79+
80+
for w in weights.iter() {
81+
// assign the max of all weights to min_cap
82+
min_cap = min_cap.max(*w);
83+
// add all the weights to right
84+
max_cap += w;
85+
}
86+
87+
// binary search
88+
while min_cap < max_cap {
89+
let mid = (min_cap + max_cap) / 2;
90+
let mut days_used = 1;
91+
let mut cur_cap = 0;
92+
93+
// for each weight, if the current capacity + weight is greater than mid,
94+
// then we need to use another day
95+
// otherwise, we can use the current day
96+
for w in weights.iter() {
97+
if cur_cap + w > mid {
98+
days_used += 1;
99+
cur_cap = 0;
100+
}
101+
cur_cap += w;
102+
}
103+
104+
// if days_used is greater than days, then we need to increase the capacity
105+
if days_used > days {
106+
min_cap = mid + 1;
107+
} else {
108+
max_cap = mid;
109+
}
110+
}
111+
112+
min_cap
113+
}
114+
}
115+
116+
```
117+
118+
### [_..._]()
119+
120+
```
121+
122+
```
123+
124+
<!----------------------------------{ link }--------------------------------->
125+
126+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
127+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
128+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
129+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

0 commit comments

Comments
 (0)