Skip to content

Commit 1c43a61

Browse files
authored
Added tasks 61-82
1 parent 91e204b commit 1c43a61

File tree

29 files changed

+918
-0
lines changed

29 files changed

+918
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package list_node
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
61\. Rotate List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, rotate the list to the right by `k` places.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], k = 2
12+
13+
**Output:** [4,5,1,2,3]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
18+
19+
**Input:** head = [0,1,2], k = 4
20+
21+
**Output:** [2,0,1]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 500]`.
26+
* `-100 <= Node.val <= 100`
27+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package s0061_rotate_list
2+
3+
// #Medium #Two_Pointers #Linked_List #Programming_Skills_II_Day_16 #Udemy_Linked_List
4+
// #Top_Interview_150_Linked_List #2025_03_04_Time_0_ms_(100.00%)_Space_42.42_MB_(78.37%)
5+
6+
import (
7+
"github.com/LeetCode-in-Go/LeetCode-in-Go/src/main/go/com_github_leetcode/list_node"
8+
)
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
func rotateRight(head *list_node.ListNode, k int) *list_node.ListNode {
18+
if head == nil || k == 0 {
19+
return head
20+
}
21+
tail := head
22+
count := 1
23+
for tail != nil && tail.Next != nil {
24+
count++
25+
tail = tail.Next
26+
}
27+
times := k % count
28+
if times == 0 {
29+
return head
30+
}
31+
temp := head
32+
for i := 1; i <= count-times-1 && temp != nil; i++ {
33+
temp = temp.Next
34+
}
35+
var newHead *list_node.ListNode
36+
if temp != nil && tail != nil {
37+
newHead = temp.Next
38+
temp.Next = nil
39+
tail.Next = head
40+
}
41+
return newHead
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package s0061_rotate_list
2+
3+
import (
4+
"testing"
5+
6+
"github.com/LeetCode-in-Go/LeetCode-in-Go/src/main/go/com_github_leetcode/list_node"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestRotateRight(t *testing.T) {
11+
head := &list_node.ListNode{Val: 1, Next: &list_node.ListNode{Val: 2, Next: &list_node.ListNode{Val: 3, Next: &list_node.ListNode{Val: 4, Next: &list_node.ListNode{Val: 5}}}}}
12+
result := rotateRight(head, 2)
13+
assert.Equal(t, 4, result.Val)
14+
assert.Equal(t, 5, result.Next.Val)
15+
assert.Equal(t, 1, result.Next.Next.Val)
16+
assert.Equal(t, 2, result.Next.Next.Next.Val)
17+
assert.Equal(t, 3, result.Next.Next.Next.Next.Val)
18+
}
19+
20+
func TestRotateRight2(t *testing.T) {
21+
head := &list_node.ListNode{Val: 0, Next: &list_node.ListNode{Val: 1, Next: &list_node.ListNode{Val: 2}}}
22+
result := rotateRight(head, 4)
23+
assert.Equal(t, 2, result.Val)
24+
assert.Equal(t, 0, result.Next.Val)
25+
assert.Equal(t, 1, result.Next.Next.Val)
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
63\. Unique Paths II
2+
3+
Medium
4+
5+
You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m-1][n-1]`). The robot can only move either down or right at any point in time.
6+
7+
An obstacle and space are marked as `1` or `0` respectively in `grid`. A path that the robot takes cannot include **any** square that is an obstacle.
8+
9+
Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
10+
11+
The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
16+
17+
**Input:** obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above.
22+
23+
There are two ways to reach the bottom-right corner:
24+
25+
1. Right -> Right -> Down -> Down
26+
27+
2. Down -> Down -> Right -> Right
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
32+
33+
**Input:** obstacleGrid = [[0,1],[0,0]]
34+
35+
**Output:** 1
36+
37+
**Constraints:**
38+
39+
* `m == obstacleGrid.length`
40+
* `n == obstacleGrid[i].length`
41+
* `1 <= m, n <= 100`
42+
* `obstacleGrid[i][j]` is `0` or `1`.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package s0063_unique_paths_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
4+
// #Top_Interview_150_Multidimensional_DP #2025_05_14_Time_0_ms_(100.00%)_Space_4.14_MB_(96.06%)
5+
6+
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
7+
if obstacleGrid[0][0] == 1 {
8+
return 0
9+
}
10+
obstacleGrid[0][0] = 1
11+
m := len(obstacleGrid)
12+
n := len(obstacleGrid[0])
13+
for i := 1; i < m; i++ {
14+
if obstacleGrid[i][0] == 1 {
15+
obstacleGrid[i][0] = 0
16+
} else {
17+
obstacleGrid[i][0] = obstacleGrid[i-1][0]
18+
}
19+
}
20+
for j := 1; j < n; j++ {
21+
if obstacleGrid[0][j] == 1 {
22+
obstacleGrid[0][j] = 0
23+
} else {
24+
obstacleGrid[0][j] = obstacleGrid[0][j-1]
25+
}
26+
}
27+
for i := 1; i < m; i++ {
28+
for j := 1; j < n; j++ {
29+
if obstacleGrid[i][j] == 1 {
30+
obstacleGrid[i][j] = 0
31+
} else {
32+
obstacleGrid[i][j] = obstacleGrid[i-1][j] + obstacleGrid[i][j-1]
33+
}
34+
}
35+
}
36+
return obstacleGrid[m-1][n-1]
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package s0063_unique_paths_ii
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestUniquePathsWithObstacles(t *testing.T) {
10+
obstacleGrid := [][]int{
11+
{0, 0, 0},
12+
{0, 1, 0},
13+
{0, 0, 0},
14+
}
15+
assert.Equal(t, 2, uniquePathsWithObstacles(obstacleGrid))
16+
}
17+
18+
func TestUniquePathsWithObstacles2(t *testing.T) {
19+
obstacleGrid := [][]int{
20+
{0, 1},
21+
{0, 0},
22+
}
23+
assert.Equal(t, 1, uniquePathsWithObstacles(obstacleGrid))
24+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
66\. Plus One
2+
3+
Easy
4+
5+
You are given a **large integer** represented as an integer array `digits`, where each `digits[i]` is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading `0`'s.
6+
7+
Increment the large integer by one and return _the resulting array of digits_.
8+
9+
**Example 1:**
10+
11+
**Input:** digits = [1,2,3]
12+
13+
**Output:** [1,2,4]
14+
15+
**Explanation:**
16+
17+
The array represents the integer 123.
18+
19+
Incrementing by one gives 123 + 1 = 124.
20+
21+
Thus, the result should be [1,2,4].
22+
23+
**Example 2:**
24+
25+
**Input:** digits = [4,3,2,1]
26+
27+
**Output:** [4,3,2,2]
28+
29+
**Explanation:**
30+
31+
The array represents the integer 4321.
32+
33+
Incrementing by one gives 4321 + 1 = 4322.
34+
35+
Thus, the result should be [4,3,2,2].
36+
37+
**Example 3:**
38+
39+
**Input:** digits = [9]
40+
41+
**Output:** [1,0]
42+
43+
**Explanation:**
44+
45+
The array represents the integer 9.
46+
47+
Incrementing by one gives 9 + 1 = 10.
48+
49+
Thus, the result should be [1,0].
50+
51+
**Constraints:**
52+
53+
* `1 <= digits.length <= 100`
54+
* `0 <= digits[i] <= 9`
55+
* `digits` does not contain any leading `0`'s.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package s0066_plus_one
2+
3+
// #Easy #Top_Interview_Questions #Array #Math #Programming_Skills_II_Day_3 #Udemy_Arrays
4+
// #Top_Interview_150_Math #2025_05_14_Time_0_ms_(100.00%)_Space_3.99_MB_(76.14%)
5+
6+
func plusOne(digits []int) []int {
7+
num := 1
8+
carry := 0
9+
var sum int
10+
for i := len(digits) - 1; i >= 0; i-- {
11+
if i == len(digits)-1 {
12+
sum = digits[i] + carry + num
13+
} else {
14+
sum = digits[i] + carry
15+
}
16+
carry = sum / 10
17+
digits[i] = sum % 10
18+
}
19+
if carry != 0 {
20+
ans := make([]int, len(digits)+1)
21+
ans[0] = carry
22+
copy(ans[1:], digits)
23+
return ans
24+
}
25+
return digits
26+
}

0 commit comments

Comments
 (0)