Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1af4127

Browse files
updating all READMEs
1 parent 2893a41 commit 1af4127

File tree

8 files changed

+343
-34
lines changed

8 files changed

+343
-34
lines changed

‎.dev/logs.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@
196196
"sols": {}
197197
},
198198
"41": {
199-
"len": 0,
200-
"sols": {}
199+
"len": 3,
200+
"sols": {
201+
"first_missing_positive.go": "e02693e7029a6b1220431f50b11a9402a81f106b3e5737e6d6a25e3050ad30b5"
202+
}
201203
},
202204
"42": {
203-
"len": 0,
204-
"sols": {}
205+
"len": 2,
206+
"sols": {
207+
"trapping_rain_water.rs": "da65b05df7c2398f7cf8f4233d74779530a6c74830e59dbebac95eec3c170929"
208+
}
205209
},
206210
"43": {
207211
"len": 0,
@@ -1293,8 +1297,10 @@
12931297
}
12941298
},
12951299
"279": {
1296-
"len": 0,
1297-
"sols": {}
1300+
"len": 2,
1301+
"sols": {
1302+
"perfect_square.rs": "59c6210278bc443619fccd1cee0a38311fe7633d51172bf38c851dc5887f6d1e"
1303+
}
12981304
},
12991305
"280": {
13001306
"len": 0,
@@ -6049,7 +6055,8 @@
60496055
"1431": {
60506056
"len": 4,
60516057
"sols": {
6052-
"kids_with_the_greatest_number_of_candies.go": "806440bf922c44de9e5d554fc2fdd0db4c70106596b548345ecc2650a6cd7bea"
6058+
"kids_with_the_greatest_number_of_candies.go": "806440bf922c44de9e5d554fc2fdd0db4c70106596b548345ecc2650a6cd7bea",
6059+
"KidsWithTheGreatestNumberOfCandies.java": "ae0f6e1d16485039792f8ec4aab6160331eac4f3dd87d5c25afa232d0d1477c8"
60536060
}
60546061
},
60556062
"1432": {

‎README.md

Lines changed: 1 addition & 1 deletion
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: _185_**
98+
### **Total Problems Solved: _188_**
9999

100100
---
101101

‎TOPICWISE.md

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
| **0027** | [Remove Element][27] | Array, Two Pointers | ![][easy] | |
2525
| **0035** | [Search Insert Position][35] | Array, BS | ![][easy] | |
2626
| **0037** | [Sudoku Solver][37] | Array, Backtracking, Matrix | ![][hard] | |
27+
| **0041** | [First Missing Positive][41] | Array, Hash Table | ![][hard] | |
28+
| **0042** | [Trapping Rain Water][42] | Array, 2P, DP, ST, Monotonic Stack | ![][hard] | |
2729
| **0051** | [N-Queens][51] | Array, Backtracking | ![][hard] | |
2830
| **0053** | [Maximum Subarray][53] | Array, DNC, DP | ![][medium] | |
2931
| **0055** | [Jump Game][55] | Array, DP, Greedy | ![][medium] | |
@@ -143,6 +145,7 @@
143145
| :------: | :---------------------------------------------------------------: | :-----------------------------------------------: | :---------: | :----: |
144146
| **0001** | [Two Sum][1] | Array, Hash Table | ![][easy] | |
145147
| **0013** | [Roman to Integer][13] | String, Math, Hash Table | ![][easy] | |
148+
| **0041** | [First Missing Positive][41] | Array, Hash Table | ![][hard] | |
146149
| **0106** | [Construct Binary Tree from Inorder and Postorder Traversal][106] | Array, Hash Table, DNC, Tree, Binary Tree | ![][medium] | |
147150
| **0141** | [Linked List Cycle][141] | Hash Table, LL, 2P | ![][easy] | |
148151
| **0142** | [Linked List Cycle II][142] | Hash Table, LL, 2P | ![][medium] | |
@@ -175,28 +178,30 @@
175178

176179
## Dynamic Programming
177180

178-
| # | Solution | Tags | Difficulty | Remark |
179-
| :------: | :---------------------------------------: | :------------------------------: | :---------: | :----: |
180-
| **0022** | [Generate Parentheses][22] | String, DP, Backtracking | ![][medium] | |
181-
| **0053** | [Maximum Subarray][53] | Array, DNC, DP | ![][medium] | |
182-
| **0055** | [Jump Game][55] | Array, DP, Greedy | ![][medium] | |
183-
| **0070** | [Climbing Stairs][70] | Math, DP, Memoization | ![][easy] | |
184-
| **0072** | [Edit Distance][72] | String, DP | ![][hard] | |
185-
| **0118** | [Pascal's Triangle][118] | Array, DP | ![][easy] | |
186-
| **0119** | [Pascal's Triangle II][119] | Array, DP | ![][easy] | |
187-
| **0121** | [Best Time to Buy and Sell Stock][121] | Array, DP | ![][easy] | |
188-
| **0122** | [Best Time to Buy and Sell Stock II][122] | Array, DP, Greedy | ![][medium] | |
189-
| **0300** | [Longest Increasing Subsequence][300] | Array, Binary Search, DP | ![][medium] | |
190-
| **0322** | [Coin Change][322] | Array, DP, BFS | ![][medium] | |
191-
| **0337** | [House Robber III][337] | DP, Tree, DFS, Binary Tree | ![][medium] | |
192-
| **0338** | [Counting Bits][338] | DP, Bit Manip | ![][easy] | |
193-
| **0392** | [Is Subsequence][392] | 2P, String, DP | ![][easy] | |
194-
| **0509** | [Fibonacci Number][509] | Math, Recursion, DP, Memoization | ![][easy] | |
195-
| **0516** | [Longest Palindromic Subsequence][516] | String, DP | ![][medium] | |
196-
| **0518** | [Coin Change 2][518] | Array, DP | ![][medium] | |
197-
| **1092** | [Shortest Common Supersequence][1092] | String, DP | ![][hard] | |
198-
| **1143** | [Longest Common Subsequence][1143] | String, DP | ![][medium] | |
199-
| **1537** | [Get the Maximum Score][1537] | Array, Two Pointer, DP, Greedy | ![][hard] | |
181+
| # | Solution | Tags | Difficulty | Remark |
182+
| :------: | :---------------------------------------: | :--------------------------------: | :---------: | :----: |
183+
| **0022** | [Generate Parentheses][22] | String, DP, Backtracking | ![][medium] | |
184+
| **0042** | [Trapping Rain Water][42] | Array, 2P, DP, ST, Monotonic Stack | ![][hard] | |
185+
| **0053** | [Maximum Subarray][53] | Array, DNC, DP | ![][medium] | |
186+
| **0055** | [Jump Game][55] | Array, DP, Greedy | ![][medium] | |
187+
| **0070** | [Climbing Stairs][70] | Math, DP, Memoization | ![][easy] | |
188+
| **0072** | [Edit Distance][72] | String, DP | ![][hard] | |
189+
| **0118** | [Pascal's Triangle][118] | Array, DP | ![][easy] | |
190+
| **0119** | [Pascal's Triangle II][119] | Array, DP | ![][easy] | |
191+
| **0121** | [Best Time to Buy and Sell Stock][121] | Array, DP | ![][easy] | |
192+
| **0122** | [Best Time to Buy and Sell Stock II][122] | Array, DP, Greedy | ![][medium] | |
193+
| **0279** | [Perfect Squares][279] | Math, DP, Breadth-First Search | ![][medium] | |
194+
| **0300** | [Longest Increasing Subsequence][300] | Array, Binary Search, DP | ![][medium] | |
195+
| **0322** | [Coin Change][322] | Array, DP, BFS | ![][medium] | |
196+
| **0337** | [House Robber III][337] | DP, Tree, DFS, Binary Tree | ![][medium] | |
197+
| **0338** | [Counting Bits][338] | DP, Bit Manip | ![][easy] | |
198+
| **0392** | [Is Subsequence][392] | 2P, String, DP | ![][easy] | |
199+
| **0509** | [Fibonacci Number][509] | Math, Recursion, DP, Memoization | ![][easy] | |
200+
| **0516** | [Longest Palindromic Subsequence][516] | String, DP | ![][medium] | |
201+
| **0518** | [Coin Change 2][518] | Array, DP | ![][medium] | |
202+
| **1092** | [Shortest Common Supersequence][1092] | String, DP | ![][hard] | |
203+
| **1143** | [Longest Common Subsequence][1143] | String, DP | ![][medium] | |
204+
| **1537** | [Get the Maximum Score][1537] | Array, Two Pointer, DP, Greedy | ![][hard] | |
200205

201206
<!---------------------------------{ Solution Table }-------------------------->
202207

@@ -218,6 +223,7 @@
218223
| **0258** | [Add Digits][258] | Math, Simulation, Number Theory | ![][easy] | |
219224
| **0263** | [Ugly Number][263] | Math | ![][easy] | |
220225
| **0268** | [Missing Number][268] | Array, Hash Table, Math, BS, Bit Manip, Sorting | ![][easy] | |
226+
| **0279** | [Perfect Squares][279] | Math, DP, Breadth-First Search | ![][medium] | |
221227
| **0292** | [Nim Game][292] | Math, Brainteaser, Game Theory | ![][easy] | |
222228
| **0326** | [Power of Three][326] | Math, Recursion | ![][easy] | |
223229
| **0342** | [Power of Four][342] | Math, Bit Manip, Recursion | ![][easy] | |
@@ -444,6 +450,7 @@
444450
| **0004** | [Median of Two Sorted Arrays][4] | Array, Binary Search, Divide & Conquer | ![][hard] | |
445451
| **0027** | [Remove Element][27] | Array, Two Pointers | ![][easy] | |
446452
| **0028** | [Find the Index of the First Occurrence in a String][28] | 2P, String, String Matching | ![][medium] | |
453+
| **0042** | [Trapping Rain Water][42] | Array, 2P, DP, ST, Monotonic Stack | ![][hard] | |
447454
| **0061** | [Rotate List][61] | Linked List, Two Pointers | ![][medium] | |
448455
| **0075** | [Sort Colors][75] | Array, Two Pointers, Sorting | ![][medium] | |
449456
| **0088** | [Merge Sorted Array][88] | Array, 2P, Sorting | ![][easy] | |
@@ -495,6 +502,7 @@
495502
| # | Solution | Tags | Difficulty | Remark |
496503
| :------: | :----------------------------------------------: | :----------------------------------------------------: | :---------: | :----: |
497504
| **0020** | [Valid Parentheses][20] | String, ST | ![][easy] | |
505+
| **0042** | [Trapping Rain Water][42] | Array, 2P, DP, ST, Monotonic Stack | ![][hard] | |
498506
| **0094** | [Binary Tree Inorder Traversal][94] | Tree, Stack, DFS, Binary Tree | ![][easy] | |
499507
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
500508
| **0145** | [Binary Tree Postorder Traversal][145] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
@@ -632,9 +640,10 @@
632640

633641
## Monotonic Stack
634642

635-
| # | Solution | Tags | Difficulty | Remark |
636-
| :------: | :-----------------------: | :---------------------------: | :---------: | :----: |
637-
| **0739** | [Daily Temperatures][739] | Array, Stack, Monotonic Stack | ![][medium] | |
643+
| # | Solution | Tags | Difficulty | Remark |
644+
| :------: | :-----------------------: | :--------------------------------: | :---------: | :----: |
645+
| **0042** | [Trapping Rain Water][42] | Array, 2P, DP, ST, Monotonic Stack | ![][hard] | |
646+
| **0739** | [Daily Temperatures][739] | Array, Stack, Monotonic Stack | ![][medium] | |
638647

639648
<!---------------------------------{ Solution Table }-------------------------->
640649

@@ -1005,6 +1014,8 @@
10051014
[28]: ./src/0001-0100/028%20-%20Find%20the%20Index%20of%20the%20First%20Occurrence%20in%20a%20String/
10061015
[35]: ./src/0001-0100/035%20-%20Search%20Insert%20Position/
10071016
[37]: ./src/0001-0100/037%20-%20Sudoku%20Solver/
1017+
[41]: ./src/0001-0100/041%20-%20First%20Missing%20Positive/
1018+
[42]: ./src/0001-0100/042%20-%20Trapping%20Rain%20Water/
10081019
[51]: ./src/0001-0100/051%20-%20N-Queens/
10091020
[53]: ./src/0001-0100/053%20-%20Maximum%20Subarray/
10101021
[55]: ./src/0001-0100/055%20-%20Jump%20Game/
@@ -1077,6 +1088,7 @@
10771088
[263]: ./src/0201-0300/263%20-%20Ugly%20Number/
10781089
[268]: ./src/0201-0300/268%20-%20Missing%20Number/
10791090
[278]: ./src/0201-0300/278%20-%20First%20Bad%20Version/
1091+
[279]: ./src/0201-0300/279%20-%20Perfect%20Squares/
10801092
[283]: ./src/0201-0300/283%20-%20Move%20Zeroes/
10811093
[290]: ./src/0201-0300/290%20-%20Word%20Pattern/
10821094
[292]: ./src/0201-0300/292%20-%20Nim%20Game/

‎docs/index.md

Lines changed: 1 addition & 1 deletion
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: _185_**
106+
### **Total Problems Solved: _188_**
107107

108108
## Contributors
109109

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 41. First Missing Positive [![share]](https://leetcode.com/problems/first-missing-positive/)
2+
3+
![][hard]
4+
5+
## Problem Statement
6+
7+
<p>Given an unsorted integer array <code>nums</code>, return the smallest missing positive integer.</p>
8+
<p>You must implement an algorithm that runs in <code>O(n)</code> time and uses <code>O(1)</code> auxiliary space.</p>
9+
<p> </p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
```
13+
Input: nums = [1,2,0]
14+
Output: 3
15+
Explanation: The numbers in the range [1,2] are all in the array.
16+
```
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
```
21+
Input: nums = [3,4,-1,1]
22+
Output: 2
23+
Explanation: 1 is in the array but 2 is missing.
24+
```
25+
26+
<p><strong class="example">Example 3:</strong></p>
27+
28+
```
29+
Input: nums = [7,8,9,11,12]
30+
Output: 1
31+
Explanation: The smallest positive integer 1 is missing.
32+
```
33+
34+
<p> </p>
35+
<p><strong>Constraints:</strong></p>
36+
<ul>
37+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
38+
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
39+
</ul>
40+
41+
<details>
42+
<summary>
43+
44+
#### _Click to open Hints_
45+
46+
</summary>
47+
48+
- Think about how you would solve the problem in non-constant space. Can you apply that logic to the existing space?
49+
- We don't care about duplicates or non-positive integers
50+
- Remember that O(2n) = O(n)
51+
52+
</details>
53+
54+
## Solutions
55+
56+
### [_Go_](first_missing_positive.go)
57+
58+
```go [Go]
59+
package main
60+
61+
func firstMissingPositive(nums []int) int {
62+
i := 0
63+
64+
for i < len(nums) {
65+
if nums[i] > 0 && nums[i] <= len(nums) && nums[nums[i]-1] != nums[i] {
66+
nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1]
67+
} else {
68+
i++
69+
}
70+
}
71+
72+
for i, num := range nums {
73+
if num != i+1 {
74+
return i + 1
75+
}
76+
}
77+
78+
return len(nums) + 1
79+
}
80+
81+
```
82+
83+
### [_Rust_](first_missing_positive.rs)
84+
85+
```rs [Rust]
86+
impl Solution {
87+
pub fn first_missing_positive(nums: Vec<i32>) -> i32 {
88+
let (mut nums, mut i) = (nums, 0);
89+
90+
while i < nums.len() {
91+
let num = nums[i];
92+
93+
// if the number is in the range [1, nums.len()] and not in the right position
94+
// swap it with the number at the right position
95+
if num > 0 && num <= nums.len() as i32 && num != nums[num as usize - 1] {
96+
nums.swap(i, num as usize - 1);
97+
} else {
98+
i += 1;
99+
}
100+
}
101+
102+
// find the first missing positive number
103+
for (i, num) in nums.iter().enumerate() {
104+
if num != &(i as i32 + 1) {
105+
return i as i32 + 1;
106+
}
107+
}
108+
109+
nums.len() as i32 + 1
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)

AltStyle によって変換されたページ (->オリジナル) /