Skip to content

Commit d3467bd

Browse files
updating docs
1 parent 438022d commit d3467bd

File tree

6 files changed

+238
-15
lines changed

6 files changed

+238
-15
lines changed

docs/.vitepress/Sidebar.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ export default Sidebar = [
334334
text: "290 - Word Pattern",
335335
link: "/solution/0201-0300/290 - Word Pattern.md",
336336
},
337+
{
338+
text: "292 - Nim Game",
339+
link: "/solution/0201-0300/292 - Nim Game.md",
340+
},
337341
{
338342
text: "300 - Longest Increasing Subsequence",
339343
link: "/solution/0201-0300/300 - Longest Increasing Subsequence.md",
@@ -498,6 +502,10 @@ export default Sidebar = [
498502
collapsible: true,
499503
collapsed: true,
500504
items: [
505+
{
506+
text: "1011 - Capacity To Ship Packages Within D Days",
507+
link: "/solution/1001-1100/1011 - Capacity To Ship Packages Within D Days.md",
508+
},
501509
{
502510
text: "1047 - Remove All Adjacent Duplicates In String",
503511
link: "/solution/1001-1100/1047 - Remove All Adjacent Duplicates In String.md",

docs/SERIALWISE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
| **0278** | [First Bad Version][278] | Binary Search, Interactive | ![][easy] | |
9494
| **0283** | [Move Zeroes][283] | Array, Two Pointers | ![][easy] | |
9595
| **0290** | [Word Pattern][290] | Hash Table, String | ![][easy] | |
96+
| **0292** | [Nim Game][292] | Math, Brainteaser, Game Theory | ![][easy] | |
9697
| **0300** | [Longest Increasing Subsequence][300] | Array, Binary Search, DP | ![][medium] | |
9798
| **0322** | [Coin Change][322] | Array, Dynamic Programming, BFS | ![][medium] | |
9899
| **0337** | [House Robber III][337] | Dynamic Programming, Tree, Depth First Search, Binary Tree | ![][medium] | |
@@ -121,6 +122,7 @@
121122
| **0944** | [Delete Columns to Make Sorted][944] | Array, String | ![][easy] | |
122123
| **0997** | [Find the Town Judge][997] | Array, Hash Table, Graph | ![][easy] | |
123124
| **1047** | [Remove All Adjacent Duplicates In String][1047] | String, Stack | ![][easy] | |
125+
| **1056** | [Capacity To Ship Packages Within D Days][1056] | Array, BS | ![][medium] | |
124126
| **1092** | [Shortest Common Supersequence][1092] | String, DP | ![][hard] | |
125127
| **1143** | [Longest Common Subsequence][1143] | String, DP | ![][medium] | |
126128
| **1232** | [Check If It Is a Straight Line][1232] | Array, Math, Geometry | ![][easy] | |
@@ -223,6 +225,7 @@
223225
[278]: ./solution/0201-0300/278%20-%20First%20Bad%20Version.md
224226
[283]: ./solution/0201-0300/283%20-%20Move%20Zeroes.md
225227
[290]: ./solution/0201-0300/290%20-%20Word%20Pattern.md
228+
[292]: ./solution/0201-0300/292%20-%20Nim%20Game.md
226229
[300]: ./solution/0201-0300/300%20-%20Longest%20Increasing%20Subsequence.md
227230
[322]: ./solution/0301-0400/322%20-%20Coin%20Change.md
228231
[337]: ./solution/0301-0400/337%20-%20House%20Robber%20III.md
@@ -254,6 +257,7 @@
254257
[997]: ./solution/0901-1000/997%20-%20Find%20the%20Town%20Judge.md
255258
[997]: ./solution/0901-1000/997%20-%20Find%20the%20Town%20Judge.md
256259
[1047]: ./solution/1001-1100/1047%20-%20Remove%20All%20Adjacent%20Duplicates%20In%20String.md
260+
[1056]: ./solution/1001-1100/1011%20-%20Capacity%20To%20Ship%20Packages%20Within%20D%20Days.md
257261
[1092]: ./solution/1001-1100/1092%20-%20Shortest%20Common%20Supersequence.md
258262
[1143]: ./solution/1101-1200/1143%20-%20Longest%20Common%20Subsequence.md
259263
[1232]: ./solution/1201-1300/1232%20-%20Check%20If%20It%20Is%20a%20Straight%20Line.md

docs/TOPICWISE.md

Lines changed: 11 additions & 4 deletions
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, Binary Search, Bit Manipulation, 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, Dynamic Programming, 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, Two Pointers, Binary Search, 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]: ./solution/0201-0300/278%20-%20First%20Bad%20Version.md
915920
[283]: ./solution/0201-0300/283%20-%20Move%20Zeroes.md
916921
[290]: ./solution/0201-0300/290%20-%20Word%20Pattern.md
922+
[292]: ./solution/0201-0300/292%20-%20Nim%20Game.md
917923
[300]: ./solution/0201-0300/300%20-%20Longest%20Increasing%20Subsequence.md
918924
[322]: ./solution/0301-0400/322%20-%20Coin%20Change.md
919925
[337]: ./solution/0301-0400/337%20-%20House%20Robber%20III.md
@@ -945,6 +951,7 @@
945951
[997]: ./solution/0901-1000/997%20-%20Find%20the%20Town%20Judge.md
946952
[997]: ./solution/0901-1000/997%20-%20Find%20the%20Town%20Judge.md
947953
[1047]: ./solution/1001-1100/1047%20-%20Remove%20All%20Adjacent%20Duplicates%20In%20String.md
954+
[1056]: ./solution/1001-1100/1011%20-%20Capacity%20To%20Ship%20Packages%20Within%20D%20Days.md
948955
[1092]: ./solution/1001-1100/1092%20-%20Shortest%20Common%20Supersequence.md
949956
[1143]: ./solution/1101-1200/1143%20-%20Longest%20Common%20Subsequence.md
950957
[1232]: ./solution/1201-1300/1232%20-%20Check%20If%20It%20Is%20a%20Straight%20Line.md

docs/solution/0001-0100/035 - Search Insert Position.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,13 @@ class Solution:
6969
```rs [Rust]
7070
impl Solution {
7171
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
72-
let (mut start, mut end) = (0, nums.len() - 1);
73-
74-
while start <= end {
75-
let mid = start + (end - start) / 2;
76-
if nums[mid] == target {
77-
return mid as i32;
78-
} else if nums[mid] < target {
79-
start = mid + 1;
80-
} else {
81-
end = mid - 1;
72+
for (idx, val) in nums.iter().enumerate() {
73+
if target <= *val {
74+
return idx as i32;
8275
}
8376
}
8477

85-
start as i32
78+
nums.len() as i32
8679
}
8780
}
8881

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 _Click to open Hints_
49+
50+
- 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?
51+
52+
:::
53+
54+
## Solution:
55+
56+
::: code-group
57+
58+
```rs [Rust]
59+
impl Solution {
60+
pub fn can_win_nim(n: i32) -> bool {
61+
// if n is divisible by 4, then the first player will always lose
62+
if n % 4 == 0 {
63+
return false;
64+
}
65+
66+
true
67+
}
68+
}
69+
70+
```
71+
72+
:::
73+
74+
### [_..._](#)
75+
76+
```
77+
78+
```
79+
80+
<!----------------------------------{ link }--------------------------------->
81+
82+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
83+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
84+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
85+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 _Click to open Hints_
60+
61+
- 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.
62+
63+
:::
64+
65+
## Solution:
66+
67+
::: code-group
68+
69+
```rs [Rust]
70+
impl Solution {
71+
pub fn ship_within_days(weights: Vec<i32>, days: i32) -> i32 {
72+
let mut min_cap = 0; // it is the max of all weights
73+
let mut max_cap = 0; // sum of all weights
74+
75+
for w in weights.iter() {
76+
// assign the max of all weights to min_cap
77+
min_cap = min_cap.max(*w);
78+
// add all the weights to right
79+
max_cap += w;
80+
}
81+
82+
// binary search
83+
while min_cap < max_cap {
84+
let mid = (min_cap + max_cap) / 2;
85+
let mut days_used = 1;
86+
let mut cur_cap = 0;
87+
88+
// for each weight, if the current capacity + weight is greater than mid,
89+
// then we need to use another day
90+
// otherwise, we can use the current day
91+
for w in weights.iter() {
92+
if cur_cap + w > mid {
93+
days_used += 1;
94+
cur_cap = 0;
95+
}
96+
cur_cap += w;
97+
}
98+
99+
// if days_used is greater than days, then we need to increase the capacity
100+
if days_used > days {
101+
min_cap = mid + 1;
102+
} else {
103+
max_cap = mid;
104+
}
105+
}
106+
107+
min_cap
108+
}
109+
}
110+
111+
```
112+
113+
:::
114+
115+
### [_..._](#)
116+
117+
```
118+
119+
```
120+
121+
<!----------------------------------{ link }--------------------------------->
122+
123+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
124+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
125+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
126+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

0 commit comments

Comments
 (0)