Skip to content

Commit

Permalink
Added tasks 239-394
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jan 7, 2024
1 parent c178a7b commit f3fad08
Show file tree
Hide file tree
Showing 31 changed files with 937 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace LeetCodeNet.G0201_0300.S0239_sliding_window_maximum {

using Xunit;

public class SolutionTest {
[Fact]
public void MaxSlidingWindow() {
Assert.Equal(new int[] {3, 3, 5, 5, 6, 7},
new Solution().MaxSlidingWindow(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 3));
}

[Fact]
public void MaxSlidingWindow2() {
Assert.Equal(new int[] {1}, new Solution().MaxSlidingWindow(new int[] {1}, 1));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii {

using Xunit;

public class SolutionTest {
[Fact]
public void SearchMatrix() {
int[][] matrix = {
new int[] {1, 4, 7, 11, 15},
new int[] {2, 5, 8, 12, 19},
new int[] {3, 6, 9, 16, 22},
new int[] {10, 13, 14, 17, 24},
new int[] {18, 21, 23, 26, 30}
};
Assert.Equal(true, new Solution().SearchMatrix(matrix, 5));
}

[Fact]
public void SearchMatrix2() {
int[][] matrix = {
new int[] {1, 4, 7, 11, 15},
new int[] {2, 5, 8, 12, 19},
new int[] {3, 6, 9, 16, 22},
new int[] {10, 13, 14, 17, 24},
new int[] {18, 21, 23, 26, 30}
};
Assert.Equal(false, new Solution().SearchMatrix(matrix, 20));
}
}
}
20 changes: 20 additions & 0 deletions LeetCodeNet.Tests/G0201_0300/S0283_move_zeroes/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace LeetCodeNet.G0201_0300.S0283_move_zeroes {

using Xunit;

public class SolutionTest {
[Fact]
public void MoveZeroes() {
int[] array = {0, 1, 0, 3, 12};
new Solution().MoveZeroes(array);
Assert.Equal(new int[] {1, 3, 12, 0, 0}, array);
}

[Fact]
public void MoveZeroes2() {
int[] array = {0};
new Solution().MoveZeroes(array);
Assert.Equal(new int[] {0}, array);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LeetCodeNet.G0201_0300.S0287_find_the_duplicate_number {

using Xunit;

public class SolutionTest {
[Fact]
public void FindDuplicate() {
Assert.Equal(2, new Solution().FindDuplicate(new int[] {1, 3, 4, 2, 2}));
}

[Fact]
public void FindDuplicate2() {
Assert.Equal(3, new Solution().FindDuplicate(new int[] {3, 1, 3, 4, 2}));
}

[Fact]
public void FindDuplicate3() {
Assert.Equal(1, new Solution().FindDuplicate(new int[] {1, 1}));
}

[Fact]
public void FindDuplicate4() {
Assert.Equal(1, new Solution().FindDuplicate(new int[] {1, 1, 2}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LeetCodeNet.G0201_0300.S0295_find_median_from_data_stream {

using Xunit;

public class MedianFinderTest {
[Fact]
public void MedianFinder() {
MedianFinder medianFinder = new MedianFinder();
// arr = [1]
medianFinder.AddNum(1);
// arr = [1, 2]
medianFinder.AddNum(2);
// return 1.5 (i.e., (1 + 2) / 2)
Assert.Equal(1.5, medianFinder.FindMedian());
// arr[1, 2, 3]
medianFinder.AddNum(3);
// return 2.0
Assert.Equal(2.0, medianFinder.FindMedian());
}

[Fact]
public void MedianFinder2() {
MedianFinder medianFinder = new MedianFinder();
medianFinder.AddNum(1);
medianFinder.AddNum(3);
medianFinder.AddNum(-1);
Assert.Equal(1.0, medianFinder.FindMedian());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0201_0300.S0300_longest_increasing_subsequence {

using Xunit;

public class SolutionTest {
[Fact]
public void LengthOfLIS() {
Assert.Equal(4, new Solution().LengthOfLIS(new int[] {10, 9, 2, 5, 3, 7, 101, 18}));
}

[Fact]
public void LengthOfLIS2() {
Assert.Equal(4, new Solution().LengthOfLIS(new int[] {0, 1, 0, 3, 2, 3}));
}

[Fact]
public void LengthOfLIS3() {
Assert.Equal(1, new Solution().LengthOfLIS(new int[] {7, 7, 7, 7, 7, 7, 7}));
}
}
}
21 changes: 21 additions & 0 deletions LeetCodeNet.Tests/G0301_0400/S0322_coin_change/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0301_0400.S0322_coin_change {

using Xunit;

public class SolutionTest {
[Fact]
public void CoinChange() {
Assert.Equal(3, new Solution().CoinChange(new int[] {1, 2, 5}, 11));
}

[Fact]
public void CoinChange2() {
Assert.Equal(-1, new Solution().CoinChange(new int[] {2}, 3));
}

[Fact]
public void CoinChange3() {
Assert.Equal(0, new Solution().CoinChange(new int[] {1}, 0));
}
}
}
16 changes: 16 additions & 0 deletions LeetCodeNet.Tests/G0301_0400/S0338_counting_bits/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0301_0400.S0338_counting_bits {

using Xunit;

public class SolutionTest {
[Fact]
public void CountBits() {
Assert.Equal(new int[] {0, 1, 1}, new Solution().CountBits(2));
}

[Fact]
public void CountBits2() {
Assert.Equal(new int[] {0, 1, 1, 2, 1, 2}, new Solution().CountBits(5));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace LeetCodeNet.G0301_0400.S0347_top_k_frequent_elements {

using Xunit;

public class SolutionTest {
[Fact]
public void TopKFrequent() {
Assert.Equal(new int[] {1, 2},
new Solution().TopKFrequent(new int[] {1, 1, 1, 2, 2, 3}, 2));
}

[Fact]
public void TopKFrequent2() {
Assert.Equal(new int[] {1}, new Solution().TopKFrequent(new int[] {1}, 1));
}
}
}
26 changes: 26 additions & 0 deletions LeetCodeNet.Tests/G0301_0400/S0394_decode_string/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LeetCodeNet.G0301_0400.S0394_decode_string {

using Xunit;

public class SolutionTest {
[Fact]
public void DecodeString() {
Assert.Equal("aaabcbc", new Solution().DecodeString("3[a]2[bc]"));
}

[Fact]
public void DecodeString2() {
Assert.Equal("accaccacc", new Solution().DecodeString("3[a2[c]]"));
}

[Fact]
public void DecodeString3() {
Assert.Equal("abcabccdcdcdef", new Solution().DecodeString("2[abc]3[cd]ef"));
}

[Fact]
public void DecodeString4() {
Assert.Equal("abccdcdcdxyz", new Solution().DecodeString("abc3[cd]xyz"));
}
}
}
36 changes: 36 additions & 0 deletions LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace LeetCodeNet.G0201_0300.S0239_sliding_window_maximum {

// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Heap_Priority_Queue
// #Sliding_Window #Queue #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
// #2024_01_07_Time_493_ms_(46.05%)_Space_133.5_MB_(14.15%)

using System;
using System.Collections.Generic;

public class Solution {
public int[] MaxSlidingWindow(int[] nums, int k) {
int n = nums.Length;
int[] res = new int[n - k + 1];
int x = 0;
LinkedList<int> dq = new LinkedList<int>();
int i = 0;
int j = 0;
while (j < nums.Length) {
while (dq.Count != 0 && dq.Last.Value < nums[j]) {

Check warning on line 19 in LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Dereference of a possibly null reference.

Check warning on line 19 in LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Dereference of a possibly null reference.
dq.RemoveLast();
}
dq.AddLast(nums[j]);
if (j - i + 1 == k) {
res[x] = dq.First.Value;

Check warning on line 24 in LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Dereference of a possibly null reference.

Check warning on line 24 in LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/Solution.cs

View workflow job for this annotation

GitHub Actions / build-windows

Dereference of a possibly null reference.
++x;
if (dq.First.Value == nums[i]) {
dq.RemoveFirst();
}
++i;
}
++j;
}
return res;
}
}
}
54 changes: 54 additions & 0 deletions LeetCodeNet/G0201_0300/S0239_sliding_window_maximum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
239\. Sliding Window Maximum

Hard

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.

Return _the max sliding window_.

**Example 1:**

**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3

**Output:** [3,3,5,5,6,7]

**Explanation:**

Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7

**Example 2:**

**Input:** nums = [1], k = 1

**Output:** [1]

**Example 3:**

**Input:** nums = [1,-1], k = 1

**Output:** [1,-1]

**Example 4:**

**Input:** nums = [9,11], k = 2

**Output:** [11]

**Example 5:**

**Input:** nums = [4,-2], k = 2

**Output:** [4]

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
* `1 <= k <= nums.length`
23 changes: 23 additions & 0 deletions LeetCodeNet/G0201_0300/S0240_search_a_2d_matrix_ii/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace LeetCodeNet.G0201_0300.S0240_search_a_2d_matrix_ii {

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Matrix
// #Divide_and_Conquer #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8
// #Big_O_Time_O(n+m)_Space_O(1) #2024_01_07_Time_142_ms_(60.76%)_Space_54_MB_(79.75%)

public class Solution {
public bool SearchMatrix(int[][] matrix, int target) {
int r = 0;
int c = matrix[0].Length - 1;
while (r < matrix.Length && c >= 0) {
if (matrix[r][c] == target) {
return true;
} else if (matrix[r][c] > target) {
c--;
} else {
r++;
}
}
return false;
}
}
}
34 changes: 34 additions & 0 deletions LeetCodeNet/G0201_0300/S0240_search_a_2d_matrix_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
240\. Search a 2D Matrix II

Medium

Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties:

* Integers in each row are sorted in ascending from left to right.
* Integers in each column are sorted in ascending from top to bottom.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)

**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

**Output:** true

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)

**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

**Output:** false

**Constraints:**

* `m == matrix.length`
* `n == matrix[i].length`
* `1 <= n, m <= 300`
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
* All the integers in each row are **sorted** in ascending order.
* All the integers in each column are **sorted** in ascending order.
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Loading

0 comments on commit f3fad08

Please sign in to comment.