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.

0 commit comments

Comments
 (0)