Skip to content

Commit 1dbb9d2

Browse files
authored
Added tasks 61-68
1 parent ccde28b commit 1dbb9d2

File tree

15 files changed

+547
-0
lines changed

15 files changed

+547
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace LeetCodeNet.G0001_0100.S0061_rotate_list {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
private ListNode BuildList(int[] vals) {
8+
ListNode dummy = new ListNode(0);
9+
ListNode curr = dummy;
10+
foreach (var v in vals) {
11+
curr.next = new ListNode(v);
12+
curr = curr.next;
13+
}
14+
return dummy.next;
15+
}
16+
17+
private int[] ToArray(ListNode head) {
18+
var list = new System.Collections.Generic.List<int>();
19+
while (head != null) {
20+
list.Add(head.val);
21+
head = head.next;
22+
}
23+
return list.ToArray();
24+
}
25+
26+
[Fact]
27+
public void RotateRight_Example1() {
28+
var solution = new Solution();
29+
var head = BuildList(new int[] {1,2,3,4,5});
30+
var result = solution.RotateRight(head, 2);
31+
Assert.Equal(new int[] {4,5,1,2,3}, ToArray(result));
32+
}
33+
34+
[Fact]
35+
public void RotateRight_Example2() {
36+
var solution = new Solution();
37+
var head = BuildList(new int[] {0,1,2});
38+
var result = solution.RotateRight(head, 4);
39+
Assert.Equal(new int[] {2,0,1}, ToArray(result));
40+
}
41+
42+
[Fact]
43+
public void RotateRight_Empty() {
44+
var solution = new Solution();
45+
var result = solution.RotateRight(null, 1);
46+
Assert.Null(result);
47+
}
48+
49+
[Fact]
50+
public void RotateRight_SingleNode() {
51+
var solution = new Solution();
52+
var head = BuildList(new int[] {1});
53+
var result = solution.RotateRight(head, 99);
54+
Assert.Equal(new int[] {1}, ToArray(result));
55+
}
56+
}
57+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace LeetCodeNet.G0001_0100.S0063_unique_paths_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void UniquePathsWithObstacles_Example1() {
8+
var solution = new Solution();
9+
int[][] grid = new int[][] {
10+
new int[] {0,0,0},
11+
new int[] {0,1,0},
12+
new int[] {0,0,0}
13+
};
14+
Assert.Equal(2, solution.UniquePathsWithObstacles(grid));
15+
}
16+
17+
[Fact]
18+
public void UniquePathsWithObstacles_Example2() {
19+
var solution = new Solution();
20+
int[][] grid = new int[][] {
21+
new int[] {0,1},
22+
new int[] {0,0}
23+
};
24+
Assert.Equal(1, solution.UniquePathsWithObstacles(grid));
25+
}
26+
27+
[Fact]
28+
public void UniquePathsWithObstacles_AllBlocked() {
29+
var solution = new Solution();
30+
int[][] grid = new int[][] {
31+
new int[] {1,0},
32+
new int[] {0,0}
33+
};
34+
Assert.Equal(0, solution.UniquePathsWithObstacles(grid));
35+
}
36+
}
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LeetCodeNet.G0001_0100.S0066_plus_one {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void PlusOne_Example1() {
8+
var solution = new Solution();
9+
Assert.Equal(new int[] {1,2,4}, solution.PlusOne(new int[] {1,2,3}));
10+
}
11+
12+
[Fact]
13+
public void PlusOne_Example2() {
14+
var solution = new Solution();
15+
Assert.Equal(new int[] {4,3,2,2}, solution.PlusOne(new int[] {4,3,2,1}));
16+
}
17+
18+
[Fact]
19+
public void PlusOne_Example3() {
20+
var solution = new Solution();
21+
Assert.Equal(new int[] {1}, solution.PlusOne(new int[] {0}));
22+
}
23+
24+
[Fact]
25+
public void PlusOne_Example4() {
26+
var solution = new Solution();
27+
Assert.Equal(new int[] {1,0}, solution.PlusOne(new int[] {9}));
28+
}
29+
30+
[Fact]
31+
public void PlusOne_AllNines() {
32+
var solution = new Solution();
33+
Assert.Equal(new int[] {1,0,0,0}, solution.PlusOne(new int[] {9,9,9}));
34+
}
35+
}
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0001_0100.S0067_add_binary {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void AddBinary_Example1() {
8+
var solution = new Solution();
9+
Assert.Equal("100", solution.AddBinary("11", "1"));
10+
}
11+
12+
[Fact]
13+
public void AddBinary_Example2() {
14+
var solution = new Solution();
15+
Assert.Equal("10101", solution.AddBinary("1010", "1011"));
16+
}
17+
18+
[Fact]
19+
public void AddBinary_Zero() {
20+
var solution = new Solution();
21+
Assert.Equal("0", solution.AddBinary("0", "0"));
22+
}
23+
24+
[Fact]
25+
public void AddBinary_DifferentLengths() {
26+
var solution = new Solution();
27+
Assert.Equal("1000", solution.AddBinary("1", "111"));
28+
}
29+
}
30+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace LeetCodeNet.G0001_0100.S0068_text_justification {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void FullJustify_Example1() {
9+
var solution = new Solution();
10+
var words = new string[] {"This", "is", "an", "example", "of", "text", "justification."};
11+
int maxWidth = 16;
12+
var expected = new List<string> {
13+
"This is an",
14+
"example of text",
15+
"justification. "
16+
};
17+
Assert.Equal(expected, solution.FullJustify(words, maxWidth));
18+
}
19+
20+
[Fact]
21+
public void FullJustify_Example2() {
22+
var solution = new Solution();
23+
var words = new string[] {"What","must","be","acknowledgment","shall","be"};
24+
int maxWidth = 16;
25+
var expected = new List<string> {
26+
"What must be",
27+
"acknowledgment ",
28+
"shall be "
29+
};
30+
Assert.Equal(expected, solution.FullJustify(words, maxWidth));
31+
}
32+
33+
[Fact]
34+
public void FullJustify_Example3() {
35+
var solution = new Solution();
36+
var words = new string[] {"Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"};
37+
int maxWidth = 20;
38+
var expected = new List<string> {
39+
"Science is what we",
40+
"understand well",
41+
"enough to explain to",
42+
"a computer. Art is",
43+
"everything else we",
44+
"do "
45+
};
46+
Assert.Equal(expected, solution.FullJustify(words, maxWidth));
47+
}
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace LeetCodeNet.G0001_0100.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_07_02_Time_0_ms_(100.00%)_Space_43.04_MB_(61.65%)
5+
6+
using LeetCodeNet.Com_github_leetcode;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* public int val;
12+
* public ListNode next;
13+
* public ListNode(int val=0, ListNode next=null) {
14+
* this.val = val;
15+
* this.next = next;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public ListNode RotateRight(ListNode head, int k) {
21+
if (head == null || head.next == null || k == 0) {
22+
return head;
23+
}
24+
// Compute the length
25+
int len = 1;
26+
ListNode tail = head;
27+
while (tail.next != null) {
28+
tail = tail.next;
29+
len++;
30+
}
31+
k = k % len;
32+
if (k == 0) {
33+
return head;
34+
}
35+
// Make it a circle
36+
tail.next = head;
37+
// Find new tail: (len - k - 1)th node
38+
ListNode newTail = head;
39+
for (int i = 0; i < len - k - 1; i++) {
40+
newTail = newTail.next;
41+
}
42+
ListNode newHead = newTail.next;
43+
newTail.next = null;
44+
return newHead;
45+
}
46+
}
47+
}
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace LeetCodeNet.G0001_0100.S0063_unique_paths_ii {
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
4+
// #Top_Interview_150_Multidimensional_DP #2025_07_02_Time_0_ms_(100.00%)_Space_42.12_MB_(14.91%)
5+
6+
public class Solution {
7+
public int UniquePathsWithObstacles(int[][] obstacleGrid) {
8+
int m = obstacleGrid.Length;
9+
int n = obstacleGrid[0].Length;
10+
int[,] dp = new int[m, n];
11+
for (int i = 0; i < m; i++) {
12+
for (int j = 0; j < n; j++) {
13+
if (obstacleGrid[i][j] == 1) {
14+
dp[i, j] = 0;
15+
} else if (i == 0 && j == 0) {
16+
dp[i, j] = 1;
17+
} else {
18+
dp[i, j] = (i > 0 ? dp[i - 1, j] : 0) + (j > 0 ? dp[i, j - 1] : 0);
19+
}
20+
}
21+
}
22+
return dp[m - 1, n - 1];
23+
}
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
63\. Unique Paths II
2+
3+
Medium
4+
5+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
6+
7+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
8+
9+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
10+
11+
An obstacle and space is marked as `1` and `0` respectively in the grid.
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. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
26+
27+
**Input:** obstacleGrid = [[0,1],[0,0]]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `m == obstacleGrid.length`
34+
* `n == obstacleGrid[i].length`
35+
* `1 <= m, n <= 100`
36+
* `obstacleGrid[i][j]` is `0` or `1`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0001_0100.S0066_plus_one {
2+
3+
// #Easy #Top_Interview_Questions #Array #Math #Programming_Skills_II_Day_3 #Udemy_Arrays
4+
// #Top_Interview_150_Math #2025_07_02_Time_0_ms_(100.00%)_Space_46.64_MB_(51.35%)
5+
6+
public class Solution {
7+
public int[] PlusOne(int[] digits) {
8+
for (int i = digits.Length - 1; i >= 0; i--) {
9+
if (digits[i] < 9) {
10+
digits[i]++;
11+
return digits;
12+
}
13+
digits[i] = 0;
14+
}
15+
int[] res = new int[digits.Length + 1];
16+
res[0] = 1;
17+
return res;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)