Skip to content

Added tasks-283-383 #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions LeetCodeNet.Tests/G0201_0300/S0289_game_of_life/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace LeetCodeNet.G0201_0300.S0289_game_of_life {

using Xunit;

public class SolutionTest {
[Fact]
public void GameOfLife1() {
int[][] board = new int[][] {
new int[] {0, 1, 0},
new int[] {0, 0, 1},
new int[] {1, 1, 1},
new int[] {0, 0, 0}
};
new Solution().GameOfLife(board);
int[][] expected = new int[][] {
new int[] {0, 0, 0},
new int[] {1, 0, 1},
new int[] {0, 1, 1},
new int[] {0, 1, 0}
};
Assert.Equal(expected, board);
}

[Fact]
public void GameOfLife2() {
int[][] board = new int[][] {
new int[] {1, 1},
new int[] {1, 0}
};
new Solution().GameOfLife(board);
int[][] expected = new int[][] {
new int[] {1, 1},
new int[] {1, 1}
};
Assert.Equal(expected, board);
}
}
}
23 changes: 23 additions & 0 deletions LeetCodeNet.Tests/G0201_0300/S0290_word_pattern/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace LeetCodeNet.G0201_0300.S0290_word_pattern {

using Xunit;

public class SolutionTest {
[Fact]
public void WordPattern1() {
Assert.True(new Solution().WordPattern("abba", "dog cat cat dog"));
}
[Fact]
public void WordPattern2() {
Assert.False(new Solution().WordPattern("abba", "dog cat cat fish"));
}
[Fact]
public void WordPattern3() {
Assert.False(new Solution().WordPattern("aaaa", "dog cat cat dog"));
}
[Fact]
public void WordPattern4() {
Assert.False(new Solution().WordPattern("abba", "dog dog dog dog"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace LeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums {

using System;
using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void KSmallestPairs1() {
var result = new Solution().KSmallestPairs(new int[] {1, 7, 11}, new int[] {2, 4, 6}, 3);
var expected = new List<IList<int>> {
new List<int> {1, 2},
new List<int> {1, 4},
new List<int> {1, 6}
};
Assert.Equal(expected, result);
}
[Fact]
public void KSmallestPairs2() {
var result = new Solution().KSmallestPairs(new int[] {1, 1, 2}, new int[] {1, 2, 3}, 2);
var expected = new List<IList<int>> {
new List<int> {1, 1},
new List<int> {1, 1}
};
Assert.Equal(expected, result);
}
[Fact]
public void KSmallestPairs3() {
var result = new Solution().KSmallestPairs(new int[] {1, 2}, new int[] {3}, 3);
var expected = new List<IList<int>> {
new List<int> {1, 3},
new List<int> {2, 3}
};
Assert.Equal(expected, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace LeetCodeNet.G0301_0400.S0380_insert_delete_getrandom_o1 {

using System;
using System.Collections.Generic;
using Xunit;

public class RandomizedSetTest {
[Fact]
public void RandomizedSetBasic() {
var result = new List<string>();
RandomizedSet randomizedSet = null;
result.Add("null");
randomizedSet = new RandomizedSet();
result.Add(randomizedSet.Insert(1).ToString().ToLower());
result.Add(randomizedSet.Remove(2).ToString().ToLower());
result.Add(randomizedSet.Insert(2).ToString().ToLower());
int random = randomizedSet.GetRandom();
result.Add(random.ToString());
result.Add(randomizedSet.Remove(1).ToString().ToLower());
result.Add(randomizedSet.Insert(2).ToString().ToLower());
result.Add(randomizedSet.GetRandom().ToString());
var expected1 = new List<string> {"null", "true", "false", "true", "1", "true", "false", "2"};
var expected2 = new List<string> {"null", "true", "false", "true", "2", "true", "false", "2"};
if (random == 1) {
Assert.Equal(expected1, result);
} else {
Assert.Equal(expected2, result);
}
}
}
}
20 changes: 20 additions & 0 deletions LeetCodeNet.Tests/G0301_0400/S0383_ransom_note/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace LeetCodeNet.G0301_0400.S0383_ransom_note {

using System;
using Xunit;

public class SolutionTest {
[Fact]
public void CanConstruct1() {
Assert.False(new Solution().CanConstruct("a", "b"));
}
[Fact]
public void CanConstruct2() {
Assert.False(new Solution().CanConstruct("aa", "ab"));
}
[Fact]
public void CanConstruct3() {
Assert.True(new Solution().CanConstruct("aa", "aab"));
}
}
}
32 changes: 32 additions & 0 deletions LeetCodeNet/G0201_0300/S0289_game_of_life/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace LeetCodeNet.G0201_0300.S0289_game_of_life {

// #Medium #Array #Matrix #Simulation #Top_Interview_150_Matrix
// #2025_07_16_Time_0_ms_(100.00%)_Space_46.31_MB_(66.67%)

public class Solution {
public void GameOfLife(int[][] board) {
int m = board.Length, n = board[0].Length;
int[] dx = {0, 0, 1, 1, 1, -1, -1, -1};
int[] dy = {1, -1, 0, 1, -1, 0, 1, -1};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int live = 0;
for (int d = 0; d < 8; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (ni >= 0 && ni < m && nj >= 0 && nj < n && (board[ni][nj] & 1) == 1) live++;
}
if ((board[i][j] & 1) == 1) {
if (live == 2 || live == 3) board[i][j] |= 2;
} else {
if (live == 3) board[i][j] |= 2;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] >>= 1;
}
}
}
}
}
42 changes: 42 additions & 0 deletions LeetCodeNet/G0201_0300/S0289_game_of_life/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
289\. Game of Life

Medium

According to [Wikipedia's article](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life): "The **Game of Life**, also known simply as **Life**, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

The board is made up of an `m x n` grid of cells, where each cell has an initial state: **live** (represented by a `1`) or **dead** (represented by a `0`). Each cell interacts with its [eight neighbors](https://en.wikipedia.org/wiki/Moore_neighborhood) (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by over-population.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the `m x n` grid `board`, return _the next state_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg)

**Input:** board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]

**Output:** [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg)

**Input:** board = [[1,1],[1,0]]

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

**Constraints:**

* `m == board.length`
* `n == board[i].length`
* `1 <= m, n <= 25`
* `board[i][j]` is `0` or `1`.

**Follow up:**

* Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
* In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
24 changes: 24 additions & 0 deletions LeetCodeNet/G0201_0300/S0290_word_pattern/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LeetCodeNet.G0201_0300.S0290_word_pattern {

// #Easy #String #Hash_Table #Data_Structure_II_Day_7_String #Top_Interview_150_Hashmap
// #2025_07_16_Time_0_ms_(100.00%)_Space_40.84_MB_(71.08%)

public class Solution {
public bool WordPattern(string pattern, string s) {
Dictionary<char, string> dict = new();
string[] str = s.Split(" ");
if (pattern.Length != str.Length) {
return false;
}
for (int i = 0; i < pattern.Length; i++) {
if (!dict.ContainsKey(pattern[i]) && !dict.ContainsValue(str[i])) {
dict.Add(pattern[i], str[i]);
}
if (!dict.ContainsKey(pattern[i]) || !str[i].Equals(dict[pattern[i]])) {
return false;
}
}
return true;
}
}
}
40 changes: 40 additions & 0 deletions LeetCodeNet/G0201_0300/S0290_word_pattern/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
290\. Word Pattern

Easy

Given a `pattern` and a string `s`, find if `s` follows the same pattern.

Here **follow** means a full match, such that there is a bijection between a letter in `pattern` and a **non-empty** word in `s`.

**Example 1:**

**Input:** pattern = "abba", s = "dog cat cat dog"

**Output:** true

**Example 2:**

**Input:** pattern = "abba", s = "dog cat cat fish"

**Output:** false

**Example 3:**

**Input:** pattern = "aaaa", s = "dog cat cat dog"

**Output:** false

**Example 4:**

**Input:** pattern = "abba", s = "dog dog dog dog"

**Output:** false

**Constraints:**

* `1 <= pattern.length <= 300`
* `pattern` contains only lower-case English letters.
* `1 <= s.length <= 3000`
* `s` contains only lower-case English letters and spaces `' '`.
* `s` **does not contain** any leading or trailing spaces.
* All the words in `s` are separated by a **single space**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums {

// #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
// #2025_07_16_Time_52_ms_(73.33%)_Space_92.81_MB_(30.00%)

using System.Collections.Generic;

public class Solution {
public IList<IList<int>> KSmallestPairs(int[] nums1, int[] nums2, int k) {
var result = new List<IList<int>>();
if (nums1.Length == 0 || nums2.Length == 0 || k == 0) {
return result;
}
var pq = new PriorityQueue<(int i, int j), int>();
// Initialize with pairs from nums1[0] with each element in nums2 up to `k`
for (int j = 0; j < nums2.Length && j < k; j++) {
pq.Enqueue((0, j), nums1[0] + nums2[j]);
}
while (k-- > 0 && pq.Count > 0) {
var (i, j) = pq.Dequeue();
result.Add(new List<int> { nums1[i], nums2[j] });
// If there's another pair with the next element in nums1, add it
if (i + 1 < nums1.Length) {
pq.Enqueue((i + 1, j), nums1[i + 1] + nums2[j]);
}
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
373\. Find K Pairs with Smallest Sums

Medium

You are given two integer arrays `nums1` and `nums2` sorted in **ascending order** and an integer `k`.

Define a pair `(u, v)` which consists of one element from the first array and one element from the second array.

Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v<sub>2</sub>), ..., (u<sub>k</sub>, v<sub>k</sub>)</code> _with the smallest sums_.

**Example 1:**

**Input:** nums1 = [1,7,11], nums2 = [2,4,6], k = 3

**Output:** [[1,2],[1,4],[1,6]]

**Explanation:** The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]

**Example 2:**

**Input:** nums1 = [1,1,2], nums2 = [1,2,3], k = 2

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

**Explanation:** The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]

**Example 3:**

**Input:** nums1 = [1,2], nums2 = [3], k = 3

**Output:** [[1,3],[2,3]]

**Explanation:** All possible pairs are returned from the sequence: [1,3],[2,3]

**Constraints:**

* <code>1 <= nums1.length, nums2.length <= 10<sup>5</sup></code>
* <code>-10<sup>9</sup> <= nums1[i], nums2[i] <= 10<sup>9</sup></code>
* `nums1` and `nums2` both are sorted in **ascending order**.
* `1 <= k <= 1000`
Loading
Loading