Skip to content

Commit 64b18ec

Browse files
authored
Added tasks 152-200
1 parent e9e41b1 commit 64b18ec

File tree

25 files changed

+792
-0
lines changed

25 files changed

+792
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0152_maximum_product_subarray {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxProduct() {
8+
Assert.Equal(6, new Solution().MaxProduct(new int[] {2, 3, -2, 4}));
9+
}
10+
11+
[Fact]
12+
public void MaxProduct2() {
13+
Assert.Equal(0, new Solution().MaxProduct(new int[] {-2, 0, -1}));
14+
}
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0153_find_minimum_in_rotated_sorted_array {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void FindMin() {
8+
Assert.Equal(1, new Solution().FindMin(new int[] {3, 4, 5, 1, 2}));
9+
}
10+
11+
[Fact]
12+
public void FindMin2() {
13+
Assert.Equal(0, new Solution().FindMin(new int[] {4, 5, 6, 7, 0, 1, 2}));
14+
}
15+
16+
[Fact]
17+
public void FindMin3() {
18+
Assert.Equal(11, new Solution().FindMin(new int[] {11, 13, 15, 17}));
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0155_min_stack {
2+
3+
using Xunit;
4+
5+
public class MinStackTest {
6+
[Fact]
7+
public void MinStack() {
8+
MinStack minStack = new MinStack();
9+
minStack.Push(-2);
10+
minStack.Push(0);
11+
minStack.Push(-3);
12+
// return -3
13+
Assert.Equal(-3, minStack.GetMin());
14+
minStack.Pop();
15+
// return 0
16+
Assert.Equal(0, minStack.Top());
17+
// return -2
18+
Assert.Equal(-2, minStack.GetMin());
19+
}
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace LeetCodeNet.G0101_0200.S0160_intersection_of_two_linked_lists {
2+
3+
using Xunit;
4+
using LeetCodeNet.Com_github_leetcode;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void GetIntersectionNode() {
9+
ListNode intersectionListNode = new ListNode(8, new ListNode(4, new ListNode(5)));
10+
ListNode nodeA = new ListNode(4, new ListNode(1, intersectionListNode));
11+
ListNode nodeB = new ListNode(5, new ListNode(6, new ListNode(1, intersectionListNode)));
12+
Assert.Equal(8, new Solution().GetIntersectionNode(nodeA, nodeB).val);
13+
}
14+
15+
[Fact]
16+
public void GetIntersectionNode2() {
17+
ListNode nodeA = new ListNode(4, new ListNode(1, new ListNode(2)));
18+
ListNode nodeB = new ListNode(5, new ListNode(6, new ListNode(1, new ListNode(2))));
19+
Assert.Equal(null, new Solution().GetIntersectionNode(nodeA, nodeB));
20+
}
21+
}
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0169_majority_element {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MajorityElement() {
8+
Assert.Equal(3, new Solution().MajorityElement(new int[] {3, 2, 3}));
9+
}
10+
11+
[Fact]
12+
public void MajorityElement2() {
13+
Assert.Equal(2, new Solution().MajorityElement(new int[] {2, 2, 1, 1, 1, 2, 2}));
14+
}
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LeetCodeNet.G0101_0200.S0189_rotate_array {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Rotate() {
8+
int[] array = new int[] {1, 2, 3, 4, 5, 6, 7};
9+
new Solution().Rotate(array, 3);
10+
Assert.Equal(new int[] {5, 6, 7, 1, 2, 3, 4}, array);
11+
}
12+
13+
[Fact]
14+
public void Rotate2() {
15+
int[] array = new int[] {-1, -100, 3, 99};
16+
new Solution().Rotate(array, 2);
17+
Assert.Equal(new int[] {3, 99, -1, -100}, array);
18+
}
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace LeetCodeNet.G0101_0200.S0198_house_robber {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void Rob() {
8+
Assert.Equal(4, new Solution().Rob(new int[] {1, 2, 3, 1}));
9+
}
10+
11+
[Fact]
12+
public void Rob2() {
13+
Assert.Equal(12, new Solution().Rob(new int[] {2, 7, 9, 3, 1}));
14+
}
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace LeetCodeNet.G0101_0200.S0200_number_of_islands {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void NumIslands() {
8+
char[][] grid = new char[][] {
9+
new char[] {'1', '1', '1', '1', '0'},
10+
new char[] {'1', '1', '0', '1', '0'},
11+
new char[] {'1', '1', '0', '0', '0'},
12+
new char[] {'0', '0', '0', '0', '0'}
13+
};
14+
Assert.Equal(1, new Solution().NumIslands(grid));
15+
}
16+
17+
[Fact]
18+
public void NumIslands2() {
19+
char[][] grid = new char[][] {
20+
new char[] {'1', '1', '0', '0', '0'},
21+
new char[] {'1', '1', '0', '0', '0'},
22+
new char[] {'0', '0', '1', '0', '0'},
23+
new char[] {'0', '0', '0', '1', '1'}
24+
};
25+
Assert.Equal(3, new Solution().NumIslands(grid));
26+
}
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LeetCodeNet.G0101_0200.S0152_maximum_product_subarray {
2+
3+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
4+
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
5+
// #Big_O_Time_O(N)_Space_O(1) #2024_01_11_Time_71_ms_(90.35%)_Space_42.7_MB_(13.88%)
6+
7+
public class Solution {
8+
public int MaxProduct(int[] nums) {
9+
int ans = int.MinValue;
10+
int cprod = 1;
11+
foreach (int j in nums) {
12+
cprod = cprod * j;
13+
ans = Math.Max(ans, cprod);
14+
if (cprod == 0) {
15+
cprod = 1;
16+
}
17+
}
18+
cprod = 1;
19+
for (int i = nums.Length - 1; i >= 0; i--) {
20+
cprod = cprod * nums[i];
21+
ans = Math.Max(ans, cprod);
22+
if (cprod == 0) {
23+
cprod = 1;
24+
}
25+
}
26+
return ans;
27+
}
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
152\. Maximum Product Subarray
2+
3+
Medium
4+
5+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
6+
7+
It is **guaranteed** that the answer will fit in a **32-bit** integer.
8+
9+
A **subarray** is a contiguous subsequence of the array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,3,-2,4]
14+
15+
**Output:** 6
16+
17+
**Explanation:** [2,3] has the largest product 6.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [-2,0,-1]
22+
23+
**Output:** 0
24+
25+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
30+
* `-10 <= nums[i] <= 10`
31+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace LeetCodeNet.G0101_0200.S0153_find_minimum_in_rotated_sorted_array {
2+
3+
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search
4+
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N)
5+
// #2024_01_11_Time_64_ms_(88.59%)_Space_40.9_MB_(17.68%)
6+
7+
public class Solution {
8+
private int FindMinUtil(int[] nums, int l, int r) {
9+
if (l == r) {
10+
return nums[l];
11+
}
12+
int mid = (l + r) / 2;
13+
if (mid == l && nums[mid] < nums[r]) {
14+
return nums[l];
15+
}
16+
if (mid - 1 >= 0 && nums[mid - 1] > nums[mid]) {
17+
return nums[mid];
18+
}
19+
if (nums[mid] < nums[l]) {
20+
return FindMinUtil(nums, l, mid - 1);
21+
} else if (nums[mid] > nums[r]) {
22+
return FindMinUtil(nums, mid + 1, r);
23+
}
24+
return FindMinUtil(nums, l, mid - 1);
25+
}
26+
27+
public int FindMin(int[] nums) {
28+
int l = 0;
29+
int r = nums.Length - 1;
30+
return FindMinUtil(nums, l, r);
31+
}
32+
}
33+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
153\. Find Minimum in Rotated Sorted Array
2+
3+
Medium
4+
5+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
6+
7+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
8+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
9+
10+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
11+
12+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
13+
14+
You must write an algorithm that runs in `O(log n) time.`
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [3,4,5,1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [4,5,6,7,0,1,2]
27+
28+
**Output:** 0
29+
30+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [11,13,15,17]
35+
36+
**Output:** 11
37+
38+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
39+
40+
**Constraints:**
41+
42+
* `n == nums.length`
43+
* `1 <= n <= 5000`
44+
* `-5000 <= nums[i] <= 5000`
45+
* All the integers of `nums` are **unique**.
46+
* `nums` is sorted and rotated between `1` and `n` times.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace LeetCodeNet.G0101_0200.S0155_min_stack {
2+
3+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design
4+
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design
5+
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N)
6+
// #2024_01_11_Time_105_ms_(95.77%)_Space_55.6_MB_(13.78%)
7+
8+
public class MinStack {
9+
private class Node {
10+
public int min;
11+
public int data;
12+
public Node nextNode;
13+
public Node previousNode;
14+
15+
public Node(int min, int data, Node previousNode, Node nextNode) {
16+
this.min = min;
17+
this.data = data;
18+
this.previousNode = previousNode;
19+
this.nextNode = nextNode;
20+
}
21+
}
22+
23+
private Node currentNode;
24+
25+
// initialize your data structure here.
26+
public MinStack() {
27+
// no initialization needed.
28+
}
29+
30+
public void Push(int val) {
31+
if (currentNode == null) {
32+
currentNode = new Node(val, val, null, null);
33+
} else {
34+
currentNode.nextNode = new Node(Math.Min(currentNode.min, val), val, currentNode, null);
35+
currentNode = currentNode.nextNode;
36+
}
37+
}
38+
39+
public void Pop() {
40+
currentNode = currentNode.previousNode;
41+
}
42+
43+
public int Top() {
44+
return currentNode.data;
45+
}
46+
47+
public int GetMin() {
48+
return currentNode.min;
49+
}
50+
}
51+
}
52+
/**
53+
* Your MinStack object will be instantiated and called as such:
54+
* MinStack obj = new MinStack();
55+
* obj.Push(val);
56+
* obj.Pop();
57+
* int param_3 = obj.Top();
58+
* int param_4 = obj.GetMin();
59+
*/

0 commit comments

Comments
 (0)