forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
792 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
LeetCodeNet.Tests/G0101_0200/S0152_maximum_product_subarray/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace LeetCodeNet.G0101_0200.S0152_maximum_product_subarray { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MaxProduct() { | ||
Assert.Equal(6, new Solution().MaxProduct(new int[] {2, 3, -2, 4})); | ||
} | ||
|
||
[Fact] | ||
public void MaxProduct2() { | ||
Assert.Equal(0, new Solution().MaxProduct(new int[] {-2, 0, -1})); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
LeetCodeNet.Tests/G0101_0200/S0153_find_minimum_in_rotated_sorted_array/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace LeetCodeNet.G0101_0200.S0153_find_minimum_in_rotated_sorted_array { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void FindMin() { | ||
Assert.Equal(1, new Solution().FindMin(new int[] {3, 4, 5, 1, 2})); | ||
} | ||
|
||
[Fact] | ||
public void FindMin2() { | ||
Assert.Equal(0, new Solution().FindMin(new int[] {4, 5, 6, 7, 0, 1, 2})); | ||
} | ||
|
||
[Fact] | ||
public void FindMin3() { | ||
Assert.Equal(11, new Solution().FindMin(new int[] {11, 13, 15, 17})); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
LeetCodeNet.Tests/G0101_0200/S0155_min_stack/MinStackTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace LeetCodeNet.G0101_0200.S0155_min_stack { | ||
|
||
using Xunit; | ||
|
||
public class MinStackTest { | ||
[Fact] | ||
public void MinStack() { | ||
MinStack minStack = new MinStack(); | ||
minStack.Push(-2); | ||
minStack.Push(0); | ||
minStack.Push(-3); | ||
// return -3 | ||
Assert.Equal(-3, minStack.GetMin()); | ||
minStack.Pop(); | ||
// return 0 | ||
Assert.Equal(0, minStack.Top()); | ||
// return -2 | ||
Assert.Equal(-2, minStack.GetMin()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
LeetCodeNet.Tests/G0101_0200/S0160_intersection_of_two_linked_lists/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace LeetCodeNet.G0101_0200.S0160_intersection_of_two_linked_lists { | ||
|
||
using Xunit; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void GetIntersectionNode() { | ||
ListNode intersectionListNode = new ListNode(8, new ListNode(4, new ListNode(5))); | ||
ListNode nodeA = new ListNode(4, new ListNode(1, intersectionListNode)); | ||
ListNode nodeB = new ListNode(5, new ListNode(6, new ListNode(1, intersectionListNode))); | ||
Assert.Equal(8, new Solution().GetIntersectionNode(nodeA, nodeB).val); | ||
} | ||
|
||
[Fact] | ||
public void GetIntersectionNode2() { | ||
ListNode nodeA = new ListNode(4, new ListNode(1, new ListNode(2))); | ||
ListNode nodeB = new ListNode(5, new ListNode(6, new ListNode(1, new ListNode(2)))); | ||
Assert.Equal(null, new Solution().GetIntersectionNode(nodeA, nodeB)); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
LeetCodeNet.Tests/G0101_0200/S0169_majority_element/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace LeetCodeNet.G0101_0200.S0169_majority_element { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MajorityElement() { | ||
Assert.Equal(3, new Solution().MajorityElement(new int[] {3, 2, 3})); | ||
} | ||
|
||
[Fact] | ||
public void MajorityElement2() { | ||
Assert.Equal(2, new Solution().MajorityElement(new int[] {2, 2, 1, 1, 1, 2, 2})); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
LeetCodeNet.Tests/G0101_0200/S0189_rotate_array/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace LeetCodeNet.G0101_0200.S0189_rotate_array { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void Rotate() { | ||
int[] array = new int[] {1, 2, 3, 4, 5, 6, 7}; | ||
new Solution().Rotate(array, 3); | ||
Assert.Equal(new int[] {5, 6, 7, 1, 2, 3, 4}, array); | ||
} | ||
|
||
[Fact] | ||
public void Rotate2() { | ||
int[] array = new int[] {-1, -100, 3, 99}; | ||
new Solution().Rotate(array, 2); | ||
Assert.Equal(new int[] {3, 99, -1, -100}, array); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
LeetCodeNet.Tests/G0101_0200/S0198_house_robber/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace LeetCodeNet.G0101_0200.S0198_house_robber { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void Rob() { | ||
Assert.Equal(4, new Solution().Rob(new int[] {1, 2, 3, 1})); | ||
} | ||
|
||
[Fact] | ||
public void Rob2() { | ||
Assert.Equal(12, new Solution().Rob(new int[] {2, 7, 9, 3, 1})); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
LeetCodeNet.Tests/G0101_0200/S0200_number_of_islands/SolutionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace LeetCodeNet.G0101_0200.S0200_number_of_islands { | ||
|
||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void NumIslands() { | ||
char[][] grid = new char[][] { | ||
new char[] {'1', '1', '1', '1', '0'}, | ||
new char[] {'1', '1', '0', '1', '0'}, | ||
new char[] {'1', '1', '0', '0', '0'}, | ||
new char[] {'0', '0', '0', '0', '0'} | ||
}; | ||
Assert.Equal(1, new Solution().NumIslands(grid)); | ||
} | ||
|
||
[Fact] | ||
public void NumIslands2() { | ||
char[][] grid = new char[][] { | ||
new char[] {'1', '1', '0', '0', '0'}, | ||
new char[] {'1', '1', '0', '0', '0'}, | ||
new char[] {'0', '0', '1', '0', '0'}, | ||
new char[] {'0', '0', '0', '1', '1'} | ||
}; | ||
Assert.Equal(3, new Solution().NumIslands(grid)); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
LeetCodeNet/G0101_0200/S0152_maximum_product_subarray/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace LeetCodeNet.G0101_0200.S0152_maximum_product_subarray { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming | ||
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming | ||
// #Big_O_Time_O(N)_Space_O(1) #2024_01_11_Time_71_ms_(90.35%)_Space_42.7_MB_(13.88%) | ||
|
||
public class Solution { | ||
public int MaxProduct(int[] nums) { | ||
int ans = int.MinValue; | ||
int cprod = 1; | ||
foreach (int j in nums) { | ||
cprod = cprod * j; | ||
ans = Math.Max(ans, cprod); | ||
if (cprod == 0) { | ||
cprod = 1; | ||
} | ||
} | ||
cprod = 1; | ||
for (int i = nums.Length - 1; i >= 0; i--) { | ||
cprod = cprod * nums[i]; | ||
ans = Math.Max(ans, cprod); | ||
if (cprod == 0) { | ||
cprod = 1; | ||
} | ||
} | ||
return ans; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
LeetCodeNet/G0101_0200/S0152_maximum_product_subarray/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
152\. Maximum Product Subarray | ||
|
||
Medium | ||
|
||
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_. | ||
|
||
It is **guaranteed** that the answer will fit in a **32-bit** integer. | ||
|
||
A **subarray** is a contiguous subsequence of the array. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [2,3,-2,4] | ||
|
||
**Output:** 6 | ||
|
||
**Explanation:** [2,3] has the largest product 6. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [-2,0,-1] | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code> | ||
* `-10 <= nums[i] <= 10` | ||
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer. |
33 changes: 33 additions & 0 deletions
33
LeetCodeNet/G0101_0200/S0153_find_minimum_in_rotated_sorted_array/Solution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace LeetCodeNet.G0101_0200.S0153_find_minimum_in_rotated_sorted_array { | ||
|
||
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search | ||
// #Binary_Search_I_Day_12 #Udemy_Binary_Search #Big_O_Time_O(log_N)_Space_O(log_N) | ||
// #2024_01_11_Time_64_ms_(88.59%)_Space_40.9_MB_(17.68%) | ||
|
||
public class Solution { | ||
private int FindMinUtil(int[] nums, int l, int r) { | ||
if (l == r) { | ||
return nums[l]; | ||
} | ||
int mid = (l + r) / 2; | ||
if (mid == l && nums[mid] < nums[r]) { | ||
return nums[l]; | ||
} | ||
if (mid - 1 >= 0 && nums[mid - 1] > nums[mid]) { | ||
return nums[mid]; | ||
} | ||
if (nums[mid] < nums[l]) { | ||
return FindMinUtil(nums, l, mid - 1); | ||
} else if (nums[mid] > nums[r]) { | ||
return FindMinUtil(nums, mid + 1, r); | ||
} | ||
return FindMinUtil(nums, l, mid - 1); | ||
} | ||
|
||
public int FindMin(int[] nums) { | ||
int l = 0; | ||
int r = nums.Length - 1; | ||
return FindMinUtil(nums, l, r); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
LeetCodeNet/G0101_0200/S0153_find_minimum_in_rotated_sorted_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
153\. Find Minimum in Rotated Sorted Array | ||
|
||
Medium | ||
|
||
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: | ||
|
||
* `[4,5,6,7,0,1,2]` if it was rotated `4` times. | ||
* `[0,1,2,4,5,6,7]` if it was rotated `7` times. | ||
|
||
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]]`. | ||
|
||
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_. | ||
|
||
You must write an algorithm that runs in `O(log n) time.` | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [3,4,5,1,2] | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [4,5,6,7,0,1,2] | ||
|
||
**Output:** 0 | ||
|
||
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [11,13,15,17] | ||
|
||
**Output:** 11 | ||
|
||
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times. | ||
|
||
**Constraints:** | ||
|
||
* `n == nums.length` | ||
* `1 <= n <= 5000` | ||
* `-5000 <= nums[i] <= 5000` | ||
* All the integers of `nums` are **unique**. | ||
* `nums` is sorted and rotated between `1` and `n` times. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace LeetCodeNet.G0101_0200.S0155_min_stack { | ||
|
||
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Stack #Design | ||
// #Data_Structure_II_Day_14_Stack_Queue #Programming_Skills_II_Day_18 #Level_2_Day_16_Design | ||
// #Udemy_Design #Big_O_Time_O(1)_Space_O(N) | ||
// #2024_01_11_Time_105_ms_(95.77%)_Space_55.6_MB_(13.78%) | ||
|
||
public class MinStack { | ||
private class Node { | ||
public int min; | ||
public int data; | ||
public Node nextNode; | ||
public Node previousNode; | ||
|
||
public Node(int min, int data, Node previousNode, Node nextNode) { | ||
this.min = min; | ||
this.data = data; | ||
this.previousNode = previousNode; | ||
this.nextNode = nextNode; | ||
} | ||
} | ||
|
||
private Node currentNode; | ||
|
||
// initialize your data structure here. | ||
public MinStack() { | ||
// no initialization needed. | ||
} | ||
|
||
public void Push(int val) { | ||
if (currentNode == null) { | ||
currentNode = new Node(val, val, null, null); | ||
} else { | ||
currentNode.nextNode = new Node(Math.Min(currentNode.min, val), val, currentNode, null); | ||
currentNode = currentNode.nextNode; | ||
} | ||
} | ||
|
||
public void Pop() { | ||
currentNode = currentNode.previousNode; | ||
} | ||
|
||
public int Top() { | ||
return currentNode.data; | ||
} | ||
|
||
public int GetMin() { | ||
return currentNode.min; | ||
} | ||
} | ||
} | ||
/** | ||
* Your MinStack object will be instantiated and called as such: | ||
* MinStack obj = new MinStack(); | ||
* obj.Push(val); | ||
* obj.Pop(); | ||
* int param_3 = obj.Top(); | ||
* int param_4 = obj.GetMin(); | ||
*/ |
Oops, something went wrong.