Skip to content

Commit f3fad08

Browse files
authored
Added tasks 239-394
1 parent c178a7b commit f3fad08

File tree

31 files changed

+937
-0
lines changed

31 files changed

+937
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LeetCodeNet.G0201_0300.S0239_sliding_window_maximum {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxSlidingWindow() {
8+
Assert.Equal(new int[] {3, 3, 5, 5, 6, 7},
9+
new Solution().MaxSlidingWindow(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 3));
10+
}
11+
12+
[Fact]
13+
public void MaxSlidingWindow2() {
14+
Assert.Equal(new int[] {1}, new Solution().MaxSlidingWindow(new int[] {1}, 1));
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void SearchMatrix() {
8+
int[][] matrix = {
9+
new int[] {1, 4, 7, 11, 15},
10+
new int[] {2, 5, 8, 12, 19},
11+
new int[] {3, 6, 9, 16, 22},
12+
new int[] {10, 13, 14, 17, 24},
13+
new int[] {18, 21, 23, 26, 30}
14+
};
15+
Assert.Equal(true, new Solution().SearchMatrix(matrix, 5));
16+
}
17+
18+
[Fact]
19+
public void SearchMatrix2() {
20+
int[][] matrix = {
21+
new int[] {1, 4, 7, 11, 15},
22+
new int[] {2, 5, 8, 12, 19},
23+
new int[] {3, 6, 9, 16, 22},
24+
new int[] {10, 13, 14, 17, 24},
25+
new int[] {18, 21, 23, 26, 30}
26+
};
27+
Assert.Equal(false, new Solution().SearchMatrix(matrix, 20));
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0201_0300.S0283_move_zeroes {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MoveZeroes() {
8+
int[] array = {0, 1, 0, 3, 12};
9+
new Solution().MoveZeroes(array);
10+
Assert.Equal(new int[] {1, 3, 12, 0, 0}, array);
11+
}
12+
13+
[Fact]
14+
public void MoveZeroes2() {
15+
int[] array = {0};
16+
new Solution().MoveZeroes(array);
17+
Assert.Equal(new int[] {0}, array);
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace LeetCodeNet.G0201_0300.S0287_find_the_duplicate_number {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void FindDuplicate() {
8+
Assert.Equal(2, new Solution().FindDuplicate(new int[] {1, 3, 4, 2, 2}));
9+
}
10+
11+
[Fact]
12+
public void FindDuplicate2() {
13+
Assert.Equal(3, new Solution().FindDuplicate(new int[] {3, 1, 3, 4, 2}));
14+
}
15+
16+
[Fact]
17+
public void FindDuplicate3() {
18+
Assert.Equal(1, new Solution().FindDuplicate(new int[] {1, 1}));
19+
}
20+
21+
[Fact]
22+
public void FindDuplicate4() {
23+
Assert.Equal(1, new Solution().FindDuplicate(new int[] {1, 1, 2}));
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0201_0300.S0295_find_median_from_data_stream {
2+
3+
using Xunit;
4+
5+
public class MedianFinderTest {
6+
[Fact]
7+
public void MedianFinder() {
8+
MedianFinder medianFinder = new MedianFinder();
9+
// arr = [1]
10+
medianFinder.AddNum(1);
11+
// arr = [1, 2]
12+
medianFinder.AddNum(2);
13+
// return 1.5 (i.e., (1 + 2) / 2)
14+
Assert.Equal(1.5, medianFinder.FindMedian());
15+
// arr[1, 2, 3]
16+
medianFinder.AddNum(3);
17+
// return 2.0
18+
Assert.Equal(2.0, medianFinder.FindMedian());
19+
}
20+
21+
[Fact]
22+
public void MedianFinder2() {
23+
MedianFinder medianFinder = new MedianFinder();
24+
medianFinder.AddNum(1);
25+
medianFinder.AddNum(3);
26+
medianFinder.AddNum(-1);
27+
Assert.Equal(1.0, medianFinder.FindMedian());
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0201_0300.S0300_longest_increasing_subsequence {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void LengthOfLIS() {
8+
Assert.Equal(4, new Solution().LengthOfLIS(new int[] {10, 9, 2, 5, 3, 7, 101, 18}));
9+
}
10+
11+
[Fact]
12+
public void LengthOfLIS2() {
13+
Assert.Equal(4, new Solution().LengthOfLIS(new int[] {0, 1, 0, 3, 2, 3}));
14+
}
15+
16+
[Fact]
17+
public void LengthOfLIS3() {
18+
Assert.Equal(1, new Solution().LengthOfLIS(new int[] {7, 7, 7, 7, 7, 7, 7}));
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0301_0400.S0322_coin_change {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void CoinChange() {
8+
Assert.Equal(3, new Solution().CoinChange(new int[] {1, 2, 5}, 11));
9+
}
10+
11+
[Fact]
12+
public void CoinChange2() {
13+
Assert.Equal(-1, new Solution().CoinChange(new int[] {2}, 3));
14+
}
15+
16+
[Fact]
17+
public void CoinChange3() {
18+
Assert.Equal(0, new Solution().CoinChange(new int[] {1}, 0));
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0301_0400.S0338_counting_bits {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void CountBits() {
8+
Assert.Equal(new int[] {0, 1, 1}, new Solution().CountBits(2));
9+
}
10+
11+
[Fact]
12+
public void CountBits2() {
13+
Assert.Equal(new int[] {0, 1, 1, 2, 1, 2}, new Solution().CountBits(5));
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LeetCodeNet.G0301_0400.S0347_top_k_frequent_elements {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void TopKFrequent() {
8+
Assert.Equal(new int[] {1, 2},
9+
new Solution().TopKFrequent(new int[] {1, 1, 1, 2, 2, 3}, 2));
10+
}
11+
12+
[Fact]
13+
public void TopKFrequent2() {
14+
Assert.Equal(new int[] {1}, new Solution().TopKFrequent(new int[] {1}, 1));
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace LeetCodeNet.G0301_0400.S0394_decode_string {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void DecodeString() {
8+
Assert.Equal("aaabcbc", new Solution().DecodeString("3[a]2[bc]"));
9+
}
10+
11+
[Fact]
12+
public void DecodeString2() {
13+
Assert.Equal("accaccacc", new Solution().DecodeString("3[a2[c]]"));
14+
}
15+
16+
[Fact]
17+
public void DecodeString3() {
18+
Assert.Equal("abcabccdcdcdef", new Solution().DecodeString("2[abc]3[cd]ef"));
19+
}
20+
21+
[Fact]
22+
public void DecodeString4() {
23+
Assert.Equal("abccdcdcdxyz", new Solution().DecodeString("abc3[cd]xyz"));
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LeetCodeNet.G0201_0300.S0239_sliding_window_maximum {
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
4+
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
5+
// #2024_01_07_Time_493_ms_(46.05%)_Space_133.5_MB_(14.15%)
6+
7+
using System;
8+
using System.Collections.Generic;
9+
10+
public class Solution {
11+
public int[] MaxSlidingWindow(int[] nums, int k) {
12+
int n = nums.Length;
13+
int[] res = new int[n - k + 1];
14+
int x = 0;
15+
LinkedList<int> dq = new LinkedList<int>();
16+
int i = 0;
17+
int j = 0;
18+
while (j < nums.Length) {
19+
while (dq.Count != 0 && dq.Last.Value < nums[j]) {
20+
dq.RemoveLast();
21+
}
22+
dq.AddLast(nums[j]);
23+
if (j - i + 1 == k) {
24+
res[x] = dq.First.Value;
25+
++x;
26+
if (dq.First.Value == nums[i]) {
27+
dq.RemoveFirst();
28+
}
29+
++i;
30+
}
31+
++j;
32+
}
33+
return res;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
239\. Sliding Window Maximum
2+
3+
Hard
4+
5+
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
6+
7+
Return _the max sliding window_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
12+
13+
**Output:** [3,3,5,5,6,7]
14+
15+
**Explanation:**
16+
17+
Window position Max
18+
--------------- -----
19+
[1 3 -1] -3 5 3 6 7 3
20+
1 [3 -1 -3] 5 3 6 7 3
21+
1 3 [-1 -3 5] 3 6 7 5
22+
1 3 -1 [-3 5 3] 6 7 5
23+
1 3 -1 -3 [5 3 6] 7 6
24+
1 3 -1 -3 5 [3 6 7] 7
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1], k = 1
29+
30+
**Output:** [1]
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [1,-1], k = 1
35+
36+
**Output:** [1,-1]
37+
38+
**Example 4:**
39+
40+
**Input:** nums = [9,11], k = 2
41+
42+
**Output:** [11]
43+
44+
**Example 5:**
45+
46+
**Input:** nums = [4,-2], k = 2
47+
48+
**Output:** [4]
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
54+
* `1 <= k <= nums.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii {
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix
4+
// #Divide_and_Conquer #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8
5+
// #Big_O_Time_O(n+m)_Space_O(1) #2024_01_07_Time_142_ms_(60.76%)_Space_54_MB_(79.75%)
6+
7+
public class Solution {
8+
public bool SearchMatrix(int[][] matrix, int target) {
9+
int r = 0;
10+
int c = matrix[0].Length - 1;
11+
while (r < matrix.Length && c >= 0) {
12+
if (matrix[r][c] == target) {
13+
return true;
14+
} else if (matrix[r][c] > target) {
15+
c--;
16+
} else {
17+
r++;
18+
}
19+
}
20+
return false;
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
240\. Search a 2D Matrix II
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties:
6+
7+
* Integers in each row are sorted in ascending from left to right.
8+
* Integers in each column are sorted in ascending from top to bottom.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)
13+
14+
**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)
21+
22+
**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= n, m <= 300`
31+
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
32+
* All the integers in each row are **sorted** in ascending order.
33+
* All the integers in each column are **sorted** in ascending order.
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)