Skip to content

Commit 8887b82

Browse files
authored
Added tasks 283-383
1 parent a2a6d20 commit 8887b82

File tree

15 files changed

+487
-0
lines changed

15 files changed

+487
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace LeetCodeNet.G0201_0300.S0289_game_of_life {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void GameOfLife1() {
8+
int[][] board = new int[][] {
9+
new int[] {0, 1, 0},
10+
new int[] {0, 0, 1},
11+
new int[] {1, 1, 1},
12+
new int[] {0, 0, 0}
13+
};
14+
new Solution().GameOfLife(board);
15+
int[][] expected = new int[][] {
16+
new int[] {0, 0, 0},
17+
new int[] {1, 0, 1},
18+
new int[] {0, 1, 1},
19+
new int[] {0, 1, 0}
20+
};
21+
Assert.Equal(expected, board);
22+
}
23+
24+
[Fact]
25+
public void GameOfLife2() {
26+
int[][] board = new int[][] {
27+
new int[] {1, 1},
28+
new int[] {1, 0}
29+
};
30+
new Solution().GameOfLife(board);
31+
int[][] expected = new int[][] {
32+
new int[] {1, 1},
33+
new int[] {1, 1}
34+
};
35+
Assert.Equal(expected, board);
36+
}
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LeetCodeNet.G0201_0300.S0290_word_pattern {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void WordPattern1() {
8+
Assert.True(new Solution().WordPattern("abba", "dog cat cat dog"));
9+
}
10+
[Fact]
11+
public void WordPattern2() {
12+
Assert.False(new Solution().WordPattern("abba", "dog cat cat fish"));
13+
}
14+
[Fact]
15+
public void WordPattern3() {
16+
Assert.False(new Solution().WordPattern("aaaa", "dog cat cat dog"));
17+
}
18+
[Fact]
19+
public void WordPattern4() {
20+
Assert.False(new Solution().WordPattern("abba", "dog dog dog dog"));
21+
}
22+
}
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace LeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums {
2+
3+
using System;
4+
using Xunit;
5+
using System.Collections.Generic;
6+
7+
public class SolutionTest {
8+
[Fact]
9+
public void KSmallestPairs1() {
10+
var result = new Solution().KSmallestPairs(new int[] {1, 7, 11}, new int[] {2, 4, 6}, 3);
11+
var expected = new List<IList<int>> {
12+
new List<int> {1, 2},
13+
new List<int> {1, 4},
14+
new List<int> {1, 6}
15+
};
16+
Assert.Equal(expected, result);
17+
}
18+
[Fact]
19+
public void KSmallestPairs2() {
20+
var result = new Solution().KSmallestPairs(new int[] {1, 1, 2}, new int[] {1, 2, 3}, 2);
21+
var expected = new List<IList<int>> {
22+
new List<int> {1, 1},
23+
new List<int> {1, 1}
24+
};
25+
Assert.Equal(expected, result);
26+
}
27+
[Fact]
28+
public void KSmallestPairs3() {
29+
var result = new Solution().KSmallestPairs(new int[] {1, 2}, new int[] {3}, 3);
30+
var expected = new List<IList<int>> {
31+
new List<int> {1, 3},
32+
new List<int> {2, 3}
33+
};
34+
Assert.Equal(expected, result);
35+
}
36+
}
37+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace LeetCodeNet.G0301_0400.S0380_insert_delete_getrandom_o1 {
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using Xunit;
6+
7+
public class RandomizedSetTest {
8+
[Fact]
9+
public void RandomizedSetBasic() {
10+
var result = new List<string>();
11+
RandomizedSet randomizedSet = null;
12+
result.Add("null");
13+
randomizedSet = new RandomizedSet();
14+
result.Add(randomizedSet.Insert(1).ToString().ToLower());
15+
result.Add(randomizedSet.Remove(2).ToString().ToLower());
16+
result.Add(randomizedSet.Insert(2).ToString().ToLower());
17+
int random = randomizedSet.GetRandom();
18+
result.Add(random.ToString());
19+
result.Add(randomizedSet.Remove(1).ToString().ToLower());
20+
result.Add(randomizedSet.Insert(2).ToString().ToLower());
21+
result.Add(randomizedSet.GetRandom().ToString());
22+
var expected1 = new List<string> {"null", "true", "false", "true", "1", "true", "false", "2"};
23+
var expected2 = new List<string> {"null", "true", "false", "true", "2", "true", "false", "2"};
24+
if (random == 1) {
25+
Assert.Equal(expected1, result);
26+
} else {
27+
Assert.Equal(expected2, result);
28+
}
29+
}
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0301_0400.S0383_ransom_note {
2+
3+
using System;
4+
using Xunit;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void CanConstruct1() {
9+
Assert.False(new Solution().CanConstruct("a", "b"));
10+
}
11+
[Fact]
12+
public void CanConstruct2() {
13+
Assert.False(new Solution().CanConstruct("aa", "ab"));
14+
}
15+
[Fact]
16+
public void CanConstruct3() {
17+
Assert.True(new Solution().CanConstruct("aa", "aab"));
18+
}
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace LeetCodeNet.G0201_0300.S0289_game_of_life {
2+
3+
// #Medium #Array #Matrix #Simulation #Top_Interview_150_Matrix
4+
// #2025_07_16_Time_0_ms_(100.00%)_Space_46.31_MB_(66.67%)
5+
6+
public class Solution {
7+
public void GameOfLife(int[][] board) {
8+
int m = board.Length, n = board[0].Length;
9+
int[] dx = {0, 0, 1, 1, 1, -1, -1, -1};
10+
int[] dy = {1, -1, 0, 1, -1, 0, 1, -1};
11+
for (int i = 0; i < m; i++) {
12+
for (int j = 0; j < n; j++) {
13+
int live = 0;
14+
for (int d = 0; d < 8; d++) {
15+
int ni = i + dx[d], nj = j + dy[d];
16+
if (ni >= 0 && ni < m && nj >= 0 && nj < n && (board[ni][nj] & 1) == 1) live++;
17+
}
18+
if ((board[i][j] & 1) == 1) {
19+
if (live == 2 || live == 3) board[i][j] |= 2;
20+
} else {
21+
if (live == 3) board[i][j] |= 2;
22+
}
23+
}
24+
}
25+
for (int i = 0; i < m; i++) {
26+
for (int j = 0; j < n; j++) {
27+
board[i][j] >>= 1;
28+
}
29+
}
30+
}
31+
}
32+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
289\. Game of Life
2+
3+
Medium
4+
5+
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."
6+
7+
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):
8+
9+
1. Any live cell with fewer than two live neighbors dies as if caused by under-population.
10+
2. Any live cell with two or three live neighbors lives on to the next generation.
11+
3. Any live cell with more than three live neighbors dies, as if by over-population.
12+
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
13+
14+
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_.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/12/26/grid1.jpg)
19+
20+
**Input:** board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
21+
22+
**Output:** [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2020/12/26/grid2.jpg)
27+
28+
**Input:** board = [[1,1],[1,0]]
29+
30+
**Output:** [[1,1],[1,1]]
31+
32+
**Constraints:**
33+
34+
* `m == board.length`
35+
* `n == board[i].length`
36+
* `1 <= m, n <= 25`
37+
* `board[i][j]` is `0` or `1`.
38+
39+
**Follow up:**
40+
41+
* 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.
42+
* 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?
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LeetCodeNet.G0201_0300.S0290_word_pattern {
2+
3+
// #Easy #String #Hash_Table #Data_Structure_II_Day_7_String #Top_Interview_150_Hashmap
4+
// #2025_07_16_Time_0_ms_(100.00%)_Space_40.84_MB_(71.08%)
5+
6+
public class Solution {
7+
public bool WordPattern(string pattern, string s) {
8+
Dictionary<char, string> dict = new();
9+
string[] str = s.Split(" ");
10+
if (pattern.Length != str.Length) {
11+
return false;
12+
}
13+
for (int i = 0; i < pattern.Length; i++) {
14+
if (!dict.ContainsKey(pattern[i]) && !dict.ContainsValue(str[i])) {
15+
dict.Add(pattern[i], str[i]);
16+
}
17+
if (!dict.ContainsKey(pattern[i]) || !str[i].Equals(dict[pattern[i]])) {
18+
return false;
19+
}
20+
}
21+
return true;
22+
}
23+
}
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
290\. Word Pattern
2+
3+
Easy
4+
5+
Given a `pattern` and a string `s`, find if `s` follows the same pattern.
6+
7+
Here **follow** means a full match, such that there is a bijection between a letter in `pattern` and a **non-empty** word in `s`.
8+
9+
**Example 1:**
10+
11+
**Input:** pattern = "abba", s = "dog cat cat dog"
12+
13+
**Output:** true
14+
15+
**Example 2:**
16+
17+
**Input:** pattern = "abba", s = "dog cat cat fish"
18+
19+
**Output:** false
20+
21+
**Example 3:**
22+
23+
**Input:** pattern = "aaaa", s = "dog cat cat dog"
24+
25+
**Output:** false
26+
27+
**Example 4:**
28+
29+
**Input:** pattern = "abba", s = "dog dog dog dog"
30+
31+
**Output:** false
32+
33+
**Constraints:**
34+
35+
* `1 <= pattern.length <= 300`
36+
* `pattern` contains only lower-case English letters.
37+
* `1 <= s.length <= 3000`
38+
* `s` contains only lower-case English letters and spaces `' '`.
39+
* `s` **does not contain** any leading or trailing spaces.
40+
* All the words in `s` are separated by a **single space**.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LeetCodeNet.G0301_0400.S0373_find_k_pairs_with_smallest_sums {
2+
3+
// #Medium #Array #Heap_Priority_Queue #Top_Interview_150_Heap
4+
// #2025_07_16_Time_52_ms_(73.33%)_Space_92.81_MB_(30.00%)
5+
6+
using System.Collections.Generic;
7+
8+
public class Solution {
9+
public IList<IList<int>> KSmallestPairs(int[] nums1, int[] nums2, int k) {
10+
var result = new List<IList<int>>();
11+
if (nums1.Length == 0 || nums2.Length == 0 || k == 0) {
12+
return result;
13+
}
14+
var pq = new PriorityQueue<(int i, int j), int>();
15+
// Initialize with pairs from nums1[0] with each element in nums2 up to `k`
16+
for (int j = 0; j < nums2.Length && j < k; j++) {
17+
pq.Enqueue((0, j), nums1[0] + nums2[j]);
18+
}
19+
while (k-- > 0 && pq.Count > 0) {
20+
var (i, j) = pq.Dequeue();
21+
result.Add(new List<int> { nums1[i], nums2[j] });
22+
// If there's another pair with the next element in nums1, add it
23+
if (i + 1 < nums1.Length) {
24+
pq.Enqueue((i + 1, j), nums1[i + 1] + nums2[j]);
25+
}
26+
}
27+
return result;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)