Skip to content

Commit

Permalink
Added tasks 152-200
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Jan 11, 2024
1 parent e9e41b1 commit 64b18ec
Show file tree
Hide file tree
Showing 25 changed files with 792 additions and 0 deletions.
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}));
}
}
}
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 LeetCodeNet.Tests/G0101_0200/S0155_min_stack/MinStackTest.cs
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());
}
}
}
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));
}
}
}
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 LeetCodeNet.Tests/G0101_0200/S0189_rotate_array/SolutionTest.cs
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 LeetCodeNet.Tests/G0101_0200/S0198_house_robber/SolutionTest.cs
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}));
}
}
}
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 LeetCodeNet/G0101_0200/S0152_maximum_product_subarray/Solution.cs
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 LeetCodeNet/G0101_0200/S0152_maximum_product_subarray/readme.md
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.
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);
}
}
}
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.
59 changes: 59 additions & 0 deletions LeetCodeNet/G0101_0200/S0155_min_stack/MinStack.cs
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();
*/
Loading

0 comments on commit 64b18ec

Please sign in to comment.