Skip to content

Commit 05d91f6

Browse files
committed
Updated readme
1 parent b6dc617 commit 05d91f6

File tree

8 files changed

+256
-246
lines changed

8 files changed

+256
-246
lines changed

README.md

Lines changed: 162 additions & 162 deletions
Large diffs are not rendered by default.

src/main/go/g0001_0100/s0041_first_missing_positive/readme.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,33 @@
55

66
Hard
77

8-
Given an unsorted integer array `nums`, return the smallest missing positive integer.
8+
Given an unsorted integer array `nums`. Return the _smallest positive integer_ that is _not present_ in `nums`.
99

10-
You must implement an algorithm that runs in `O(n)` time and uses constant extra space.
10+
You must implement an algorithm that runs in `O(n)` time and uses `O(1)` auxiliary space.
1111

1212
**Example 1:**
1313

1414
**Input:** nums = [1,2,0]
1515

1616
**Output:** 3
1717

18-
**Explanation:** The numbers in the range [1,2] are all in the array.
18+
**Explanation:** The numbers in the range [1,2] are all in the array.
1919

2020
**Example 2:**
2121

2222
**Input:** nums = [3,4,-1,1]
2323

2424
**Output:** 2
2525

26-
**Explanation:** 1 is in the array but 2 is missing.
26+
**Explanation:** 1 is in the array but 2 is missing.
2727

2828
**Example 3:**
2929

3030
**Input:** nums = [7,8,9,11,12]
3131

3232
**Output:** 1
3333

34-
**Explanation:** The smallest positive integer 1 is missing.
34+
**Explanation:** The smallest positive integer 1 is missing.
3535

3636
**Constraints:**
3737

@@ -47,35 +47,30 @@ func firstMissingPositive(nums []int) int {
4747
nums[i] = 0
4848
}
4949
}
50-
5150
for i := 0; i < len(nums); i++ {
5251
val := abs(nums[i])
5352
if val > 0 && val <= len(nums) {
5453
if nums[val-1] > 0 {
5554
nums[val-1] = nums[val-1] * -1
5655
continue
5756
}
58-
5957
if nums[val-1] == 0 {
6058
nums[val-1] = -1 * (len(nums) + 1)
6159
}
6260
}
6361
}
64-
6562
for i := 1; i <= len(nums); i++ {
6663
if nums[i-1] >= 0 {
6764
return i
6865
}
6966
}
70-
7167
return len(nums) + 1
7268
}
7369

7470
func abs(n int) int {
7571
if n >= 0 {
7672
return n
7773
}
78-
7974
return n * -1
8075
}
8176
```

src/main/go/g0001_0100/s0045_jump_game_ii/readme.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@
55

66
Medium
77

8-
Given an array of non-negative integers `nums`, you are initially positioned at the first index of the array.
8+
You are given a **0-indexed** array of integers `nums` of length `n`. You are initially positioned at `nums[0]`.
99

10-
Each element in the array represents your maximum jump length at that position.
10+
Each element `nums[i]` represents the maximum length of a forward jump from index `i`. In other words, if you are at `nums[i]`, you can jump to any `nums[i + j]` where:
1111

12-
Your goal is to reach the last index in the minimum number of jumps.
12+
* `0 <= j <= nums[i]` and
13+
* `i + j < n`
1314

14-
You can assume that you can always reach the last index.
15+
Return _the minimum number of jumps to reach_ `nums[n - 1]`. The test cases are generated such that you can reach `nums[n - 1]`.
1516

1617
**Example 1:**
1718

1819
**Input:** nums = [2,3,1,1,4]
1920

2021
**Output:** 2
2122

22-
**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
23+
**Explanation:** The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.
2324

2425
**Example 2:**
2526

2627
**Input:** nums = [2,3,0,1,4]
2728

28-
**Output:** 2
29+
**Output:** 2
2930

3031
**Constraints:**
3132

3233
* <code>1 <= nums.length <= 10<sup>4</sup></code>
3334
* `0 <= nums[i] <= 1000`
35+
* It's guaranteed that you can reach `nums[n - 1]`.
3436

3537
## Solution
3638

src/main/go/g0001_0100/s0046_permutations/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
Medium
77

8-
Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
8+
Given an array `nums` of distinct integers, return all the possible permutations. You can return the answer in **any order**.
99

1010
**Example 1:**
1111

1212
**Input:** nums = [1,2,3]
1313

14-
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
14+
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
1515

1616
**Example 2:**
1717

1818
**Input:** nums = [0,1]
1919

20-
**Output:** [[0,1],[1,0]]
20+
**Output:** [[0,1],[1,0]]
2121

2222
**Example 3:**
2323

2424
**Input:** nums = [1]
2525

26-
**Output:** [[1]]
26+
**Output:** [[1]]
2727

2828
**Constraints:**
2929

src/main/go/g0101_0200/s0142_linked_list_cycle_ii/readme.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,14 @@ type ListNode struct {
6565
* }
6666
*/
6767
func detectCycle(head *ListNode) *ListNode {
68-
if head == nil || head.Next == nil {
69-
return nil
70-
}
71-
slow := head
72-
fast := head
73-
for fast != nil && fast.Next != nil {
74-
fast = fast.Next.Next
75-
slow = slow.Next
76-
// intersected inside the loop.
77-
if slow == fast {
78-
break
68+
m := map[*ListNode]bool{}
69+
for head != nil {
70+
if m[head] == true {
71+
return head
7972
}
73+
m[head] = true
74+
head = head.Next
8075
}
81-
if fast == nil || fast.Next == nil {
82-
return nil
83-
}
84-
slow = head
85-
for slow != fast {
86-
slow = slow.Next
87-
fast = fast.Next
88-
}
89-
return slow
76+
return nil
9077
}
9178
```

src/main/go/g0101_0200/s0148_sort_list/readme.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ Given the `head` of a linked list, return _the list after sorting it in **ascend
3939
## Solution
4040

4141
```golang
42-
import "slices"
43-
4442
type ListNode struct {
4543
Val int
4644
Next *ListNode
@@ -54,22 +52,46 @@ type ListNode struct {
5452
* }
5553
*/
5654
func sortList(head *ListNode) *ListNode {
57-
curr := head
58-
a := []*ListNode{}
59-
for curr != nil {
60-
a = append(a, curr)
61-
curr = curr.Next
55+
if head == nil || head.Next == nil {
56+
return head
57+
}
58+
mid := findMid(head)
59+
left := sortList(head)
60+
right := sortList(mid)
61+
return merge(left, right)
62+
}
63+
64+
func findMid(head *ListNode) *ListNode {
65+
var prev *ListNode
66+
slow, fast := head, head
67+
for fast != nil && fast.Next != nil {
68+
prev = slow
69+
slow = slow.Next
70+
fast = fast.Next.Next
71+
}
72+
prev.Next = nil
73+
return slow
74+
}
75+
76+
func merge(left, right *ListNode) *ListNode {
77+
pointer := new(ListNode)
78+
res := pointer
79+
for left != nil && right != nil {
80+
if left.Val < right.Val {
81+
pointer.Next = left
82+
left = left.Next
83+
} else {
84+
pointer.Next = right
85+
right = right.Next
86+
}
87+
pointer = pointer.Next
88+
}
89+
if left != nil {
90+
pointer.Next = left
6291
}
63-
slices.SortFunc(a, func(l1, l2 *ListNode) int {
64-
return l1.Val - l2.Val
65-
})
66-
newNode := &ListNode{}
67-
curr = newNode
68-
for _, node := range a {
69-
curr.Next = node
70-
curr = node
92+
if right != nil {
93+
pointer.Next = right
7194
}
72-
curr.Next = nil
73-
return newNode.Next
95+
return res.Next
7496
}
7597
```

src/main/go/g0201_0300/s0207_course_schedule/readme.md

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,44 @@ Return `true` if you can finish all courses. Otherwise, return `false`.
3838
## Solution
3939

4040
```golang
41+
type State int
42+
43+
const (
44+
Unvisited State = iota
45+
Visiting
46+
Visited
47+
)
48+
4149
func canFinish(numCourses int, prerequisites [][]int) bool {
42-
graph := map[int][]int{}
43-
for _, edge := range prerequisites {
44-
graph[edge[1]] = append(graph[edge[1]], edge[0])
50+
visited := make([]State, numCourses)
51+
graph := make([][]int, numCourses)
52+
for _, dep := range prerequisites {
53+
graph[dep[1]] = append(graph[dep[1]], dep[0])
4554
}
46-
indegree := make([]int, numCourses)
47-
queue := []int{}
4855
for i := 0; i < numCourses; i++ {
49-
for _, v := range graph[i] {
50-
indegree[v]++
56+
if visited[i] == Unvisited {
57+
if !dfs(i, visited, graph) {
58+
return false
59+
}
5160
}
5261
}
53-
for i := 0; i < numCourses; i++ {
54-
if indegree[i] == 0 {
55-
queue = append(queue, i)
56-
}
62+
return true
63+
}
64+
65+
func dfs(start int, visited []State, graph [][]int) bool {
66+
if visited[start] == Visiting {
67+
return false
5768
}
58-
visited := 0
59-
for len(queue) > 0 {
60-
node := queue[0]
61-
queue = queue[1:]
62-
visited++
63-
for _, v := range graph[node] {
64-
indegree[v]--
65-
if indegree[v] == 0 {
66-
queue = append(queue, v)
67-
}
69+
if visited[start] == Visited {
70+
return true
71+
}
72+
visited[start] = Visiting
73+
for _, next := range graph[start] {
74+
if !dfs(next, visited, graph) {
75+
return false
6876
}
6977
}
70-
return visited == numCourses
78+
visited[start] = Visited
79+
return true
7180
}
7281
```

src/main/go/g0401_0500/s0494_target_sum/readme.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,21 @@ func findTargetSumWays(nums []int, s int) int {
5555
for _, num := range nums {
5656
sum += num
5757
}
58-
5958
if s > sum || (sum+s)%2 != 0 {
6059
return 0
6160
}
62-
6361
dp := make([][]int, (sum+s)/2+1)
6462
for i := range dp {
6563
dp[i] = make([]int, len(nums)+1)
6664
}
6765
dp[0][0] = 1
68-
6966
for i := 0; i < len(nums); i++ {
7067
if nums[i] == 0 {
7168
dp[0][i+1] = dp[0][i] * 2
7269
} else {
7370
dp[0][i+1] = dp[0][i]
7471
}
7572
}
76-
7773
for i := 1; i < len(dp); i++ {
7874
for j := 0; j < len(nums); j++ {
7975
dp[i][j+1] += dp[i][j]
@@ -82,7 +78,6 @@ func findTargetSumWays(nums []int, s int) int {
8278
}
8379
}
8480
}
85-
8681
return dp[(sum+s)/2][len(nums)]
8782
}
8883
```

0 commit comments

Comments
 (0)