Skip to content

Commit 35a5e33

Browse files
authored
Added tasks 209-219
1 parent 86a56ae commit 35a5e33

File tree

15 files changed

+587
-0
lines changed

15 files changed

+587
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0209_minimum_size_subarray_sum {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MinSubArrayLen() {
8+
Assert.Equal(2, new Solution().MinSubArrayLen(7, new int[] {2, 3, 1, 2, 4, 3}));
9+
}
10+
11+
[Fact]
12+
public void MinSubArrayLen2() {
13+
Assert.Equal(1, new Solution().MinSubArrayLen(4, new int[] {1, 4, 4}));
14+
}
15+
16+
[Fact]
17+
public void MinSubArrayLen3() {
18+
Assert.Equal(0, new Solution().MinSubArrayLen(11, new int[] {1, 1, 1, 1, 1, 1, 1, 1}));
19+
}
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LeetCodeNet.G0201_0300.S0210_course_schedule_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void CourseScheduleII() {
8+
int[][] prerequisites = new int[][] { new int[] {1, 0} };
9+
int numCourses = 2;
10+
Assert.Equal(new int[] {0, 1}, new Solution().FindOrder(numCourses, prerequisites));
11+
}
12+
13+
[Fact]
14+
public void CourseScheduleII2() {
15+
int[][] prerequisites = new int[][] { new int[] {1, 0}, new int[] {2, 0}, new int[] {3, 1}, new int[] {3, 2} };
16+
int numCourses = 4;
17+
Assert.Equal(new int[] {0, 1, 2, 3}, new Solution().FindOrder(numCourses, prerequisites));
18+
}
19+
20+
[Fact]
21+
public void CourseScheduleII3() {
22+
int[][] prerequisites = new int[][] { };
23+
int numCourses = 1;
24+
Assert.Equal(new int[] {0}, new Solution().FindOrder(numCourses, prerequisites));
25+
}
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0201_0300.S0211_design_add_and_search_words_data_structure {
2+
3+
using Xunit;
4+
5+
public class WordDictionaryTest {
6+
[Fact]
7+
public void WorldDataStructure() {
8+
string[] input = {"bad", "dad", "mad"};
9+
var dictionary = new WordDictionary();
10+
foreach (var s in input) {
11+
dictionary.AddWord(s);
12+
}
13+
Assert.False(dictionary.Search("pad"));
14+
Assert.True(dictionary.Search("bad"));
15+
Assert.True(dictionary.Search(".ad"));
16+
Assert.True(dictionary.Search("b.."));
17+
}
18+
}
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0201_0300.S0212_word_search_ii {
2+
3+
using System.Collections.Generic;
4+
using Xunit;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void FindWords() {
9+
char[][] board = {
10+
new[] { 'o', 'a', 'a', 'n' },
11+
new[] { 'e', 't', 'a', 'e' },
12+
new[] { 'i', 'h', 'k', 'r' },
13+
new[] { 'i', 'f', 'l', 'v' }
14+
};
15+
string[] words = { "oath", "pea", "eat", "rain" };
16+
var expected = new List<string> { "oath", "eat" };
17+
var result = new Solution().FindWords(board, words);
18+
Assert.Equal(expected, result);
19+
}
20+
21+
[Fact]
22+
public void FindWords2() {
23+
char[][] board = {
24+
new[] { 'a', 'b' },
25+
new[] { 'c', 'd' }
26+
};
27+
string[] words = { "abcb" };
28+
var result = new Solution().FindWords(board, words);
29+
Assert.Empty(result);
30+
}
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0219_contains_duplicate_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void ContainsNearbyDuplicate() {
8+
Assert.True(new Solution().ContainsNearbyDuplicate(new[] { 1, 2, 3, 1 }, 3));
9+
}
10+
11+
[Fact]
12+
public void ContainsNearbyDuplicate2() {
13+
Assert.True(new Solution().ContainsNearbyDuplicate(new[] { 1, 0, 1, 1 }, 1));
14+
}
15+
16+
[Fact]
17+
public void ContainsNearbyDuplicate3() {
18+
Assert.False(new Solution().ContainsNearbyDuplicate(new[] { 1, 2, 3, 1, 2, 3 }, 2));
19+
}
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0201_0300.S0209_minimum_size_subarray_sum {
2+
3+
// #Medium #Array #Binary_Search #Prefix_Sum #Sliding_Window #Algorithm_II_Day_5_Sliding_Window
4+
// #Binary_Search_II_Day_1 #Top_Interview_150_Sliding_Window
5+
// #2025_07_14_Time_0_ms_(100.00%)_Space_52.33_MB_(91.45%)
6+
7+
public class Solution {
8+
public int MinSubArrayLen(int target, int[] nums) {
9+
int i = 0, j = 0, sum = 0, min = int.MaxValue;
10+
while (j < nums.Length) {
11+
sum += nums[j];
12+
if (sum >= target) {
13+
while (i <= j) {
14+
if (sum - nums[i] >= target) {
15+
sum -= nums[i];
16+
i++;
17+
} else {
18+
break;
19+
}
20+
}
21+
if (j - i + 1 < min) {
22+
min = j - i + 1;
23+
}
24+
}
25+
j++;
26+
}
27+
return min == int.MaxValue ? 0 : min;
28+
}
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
209\. Minimum Size Subarray Sum
2+
3+
Medium
4+
5+
Given an array of positive integers `nums` and a positive integer `target`, return the minimal length of a **contiguous subarray** <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> of which the sum is greater than or equal to `target`. If there is no such subarray, return `0` instead.
6+
7+
**Example 1:**
8+
9+
**Input:** target = 7, nums = [2,3,1,2,4,3]
10+
11+
**Output:** 2
12+
13+
**Explanation:** The subarray [4,3] has the minimal length under the problem constraint.
14+
15+
**Example 2:**
16+
17+
**Input:** target = 4, nums = [1,4,4]
18+
19+
**Output:** 1
20+
21+
**Example 3:**
22+
23+
**Input:** target = 11, nums = [1,1,1,1,1,1,1,1]
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* <code>1 <= target <= 10<sup>9</sup></code>
30+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
31+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
32+
33+
**Follow up:** If you have figured out the `O(n)` solution, try coding another solution of which the time complexity is `O(n log(n))`.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace LeetCodeNet.G0201_0300.S0210_course_schedule_ii {
2+
3+
// #Medium #Top_Interview_Questions #Depth_First_Search #Breadth_First_Search #Graph
4+
// #Topological_Sort #Level_2_Day_11_Graph/BFS/DFS #Top_Interview_150_Graph_General
5+
// #2025_07_14_Time_4_ms_(91.35%)_Space_53.47_MB_(15.48%)
6+
7+
using System.Collections.Generic;
8+
9+
public class Solution {
10+
public int[] FindOrder(int numCourses, int[][] prerequisites) {
11+
var graph = new Dictionary<int, List<int>>();
12+
for (int i = 0; i < numCourses; i++) {
13+
graph[i] = new List<int>();
14+
}
15+
foreach (var classes in prerequisites) {
16+
graph[classes[0]].Add(classes[1]);
17+
}
18+
var output = new List<int>();
19+
var visited = new Dictionary<int, bool>();
20+
foreach (var c in graph.Keys) {
21+
if (Dfs(c, graph, visited, output)) {
22+
return new int[0];
23+
}
24+
}
25+
return output.ToArray();
26+
}
27+
28+
private bool Dfs(int course, Dictionary<int, List<int>> graph, Dictionary<int, bool> visited, List<int> output) {
29+
if (visited.ContainsKey(course)) {
30+
return visited[course];
31+
}
32+
visited[course] = true;
33+
foreach (var c in graph[course]) {
34+
if (Dfs(c, graph, visited, output)) {
35+
return true;
36+
}
37+
}
38+
visited[course] = false;
39+
output.Add(course);
40+
return false;
41+
}
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
210\. Course Schedule II
2+
3+
Medium
4+
5+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you **must** take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.
6+
7+
* For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
8+
9+
Return _the ordering of courses you should take to finish all courses_. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**.
10+
11+
**Example 1:**
12+
13+
**Input:** numCourses = 2, prerequisites = [[1,0]]
14+
15+
**Output:** [0,1]
16+
17+
**Explanation:**
18+
19+
There are a total of 2 courses to take. To take course 1 you should have finished course 0.
20+
So the correct course order is [0,1].
21+
22+
**Example 2:**
23+
24+
**Input:** numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
25+
26+
**Output:** [0,2,1,3]
27+
28+
**Explanation:**
29+
30+
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
31+
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
32+
33+
**Example 3:**
34+
35+
**Input:** numCourses = 1, prerequisites = []
36+
37+
**Output:** [0]
38+
39+
**Constraints:**
40+
41+
* `1 <= numCourses <= 2000`
42+
* `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
43+
* `prerequisites[i].length == 2`
44+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code>
45+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
46+
* All the pairs <code>[a<sub>i</sub>, b<sub>i</sub>]</code> are **distinct**.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace LeetCodeNet.G0201_0300.S0211_design_add_and_search_words_data_structure {
2+
3+
// #Medium #String #Depth_First_Search #Design #Trie #Top_Interview_150_Trie
4+
// #2025_07_14_Time_380_ms_(93.39%)_Space_147.60_MB_(63.66%)
5+
6+
public class WordDictionary {
7+
private class Node {
8+
public Node[] kids = new Node[26];
9+
public bool isTerminal;
10+
}
11+
12+
private readonly Node[] root = new Node[26];
13+
14+
public WordDictionary() {
15+
// empty constructor
16+
}
17+
18+
public void AddWord(string word) {
19+
int n = word.Length;
20+
if (root[n] == null) {
21+
root[n] = new Node();
22+
}
23+
Node node = root[n];
24+
for (int i = 0; i < n; i++) {
25+
int c = word[i] - 'a';
26+
Node kid = node.kids[c];
27+
if (kid == null) {
28+
kid = new Node();
29+
node.kids[c] = kid;
30+
}
31+
node = kid;
32+
}
33+
node.isTerminal = true;
34+
}
35+
36+
public bool Search(string word) {
37+
Node node = root[word.Length];
38+
return node != null && Dfs(0, node, word);
39+
}
40+
41+
private bool Dfs(int i, Node node, string word) {
42+
int len = word.Length;
43+
if (i == len) {
44+
return false;
45+
}
46+
char c = word[i];
47+
if (c == '.') {
48+
foreach (Node kid in node.kids) {
49+
if (kid == null) {
50+
continue;
51+
}
52+
if ((i == len - 1 && kid.isTerminal) || Dfs(i + 1, kid, word)) {
53+
return true;
54+
}
55+
}
56+
return false;
57+
}
58+
Node next = node.kids[c - 'a'];
59+
if (next == null) {
60+
return false;
61+
}
62+
if (i == len - 1) {
63+
return next.isTerminal;
64+
}
65+
return Dfs(i + 1, next, word);
66+
}
67+
}
68+
}
69+
70+
/**
71+
* Your WordDictionary object will be instantiated and called as such:
72+
* WordDictionary obj = new WordDictionary();
73+
* obj.AddWord(word);
74+
* bool param_2 = obj.Search(word);
75+
*/

0 commit comments

Comments
 (0)