Skip to content

Commit

Permalink
Added tasks 11-25
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 26, 2023
1 parent c35f8f3 commit be9f83e
Show file tree
Hide file tree
Showing 33 changed files with 1,034 additions and 5 deletions.
17 changes: 17 additions & 0 deletions LeetCodeNet.Tests/Com_github_leetcode/ArrayUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace LeetCodeNet.Com_github_leetcode {
using System.Collections.Generic;

public static class ArrayUtils {
public static List<List<int>> GetLists(int[][] expected) {
List<List<int>> expectedList = new List<List<int>>();
foreach (int[] value in expected) {
List<int> expectedItem = new List<int>();
expectedList.Add(expectedItem);
foreach (int item in value) {
expectedItem.Add(item);
}
}
return expectedList;
}
}
}
2 changes: 1 addition & 1 deletion LeetCodeNet.Tests/Com_github_leetcode/LinkedListUtils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace LeetCodeNet.Com_github_leetcode {

public class LinkedListUtils {
public static ListNode ContructLinkedList(int[] nums) {
public static ListNode ConstructLinkedList(int[] nums) {
if (nums == null || nums.Length == 0) {
throw new ArgumentException(
"Please pass in a valid listValues to create a linked list.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace LeetCodeNet.G0001_0100.S0002_add_two_numbers {
public class SolutionTest {
[Fact]
public void AddTwoNumbers() {
ListNode listNode1 = LinkedListUtils.ContructLinkedList(new int[] {2, 4, 3});
ListNode listNode2 = LinkedListUtils.ContructLinkedList(new int[] {5, 6, 4});
ListNode listNode1 = LinkedListUtils.ConstructLinkedList(new int[] {2, 4, 3});
ListNode listNode2 = LinkedListUtils.ConstructLinkedList(new int[] {5, 6, 4});
Assert.Equal("7, 0, 8", new Solution().AddTwoNumbers(listNode1, listNode2).ToString());
}

Expand All @@ -18,8 +18,8 @@ public void AddTwoNumbers2() {

[Fact]
public void AddTwoNumbers3() {
ListNode listNode1 = LinkedListUtils.ContructLinkedList(new int[] {9, 9, 9, 9, 9, 9, 9});
ListNode listNode2 = LinkedListUtils.ContructLinkedList(new int[] {9, 9, 9, 9});
ListNode listNode1 = LinkedListUtils.ConstructLinkedList(new int[] {9, 9, 9, 9, 9, 9, 9});
ListNode listNode2 = LinkedListUtils.ConstructLinkedList(new int[] {9, 9, 9, 9});
Assert.Equal("8, 9, 9, 9, 0, 0, 0, 1", new Solution().AddTwoNumbers(listNode1, listNode2).ToString());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace LeetCodeNet.G0001_0100.S0011_container_with_most_water {

using Xunit;

public class SolutionTest {
[Fact]
public void MaxArea() {
Assert.Equal(49, new Solution().MaxArea(new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7 }));
}

[Fact]
public void MaxArea2() {
Assert.Equal(1, new Solution().MaxArea(new int[] { 1, 1 }));
}
}
}
31 changes: 31 additions & 0 deletions LeetCodeNet.Tests/G0001_0100/S0015_3sum/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace LeetCodeNet.G0001_0100.S0015_3sum {

using Xunit;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void ThreeSum() {
Assert.Equal(
ArrayUtils.GetLists(new int[][] { new int[] { -1, -1, 2 }, new int[] { -1, 0, 1 } }),
new Solution().ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 })
);
}

[Fact]
public void ThreeSum2() {
Assert.Equal(
ArrayUtils.GetLists(new int[][] { }),
new Solution().ThreeSum(new int[] { })
);
}

[Fact]
public void ThreeSum3() {
Assert.Equal(
ArrayUtils.GetLists(new int[][] { }),
new Solution().ThreeSum(new int[] { 0 })
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
namespace LeetCodeNet.G0001_0100.S0017_letter_combinations_of_a_phone_number {

using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void LetterCombinations() {
Assert.Equal(
new List<string> { "ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf" },
new Solution().LetterCombinations("23")
);
}

[Fact]
public void LetterCombinations2() {
Assert.Empty(new Solution().LetterCombinations(""));
}

[Fact]
public void LetterCombinations3() {
Assert.Equal(new List<string> { "a", "b", "c" }, new Solution().LetterCombinations("2"));
}

[Fact]
public void LetterCombinations4() {
Assert.Equal(new List<string> { "g", "h", "i" }, new Solution().LetterCombinations("4"));
}

[Fact]
public void LetterCombinations5() {
Assert.Equal(new List<string> { "j", "k", "l" }, new Solution().LetterCombinations("5"));
}

[Fact]
public void LetterCombinations6() {
Assert.Equal(new List<string> { "m", "n", "o" }, new Solution().LetterCombinations("6"));
}

[Fact]
public void LetterCombinations7() {
Assert.Equal(new List<string> { "p", "q", "r", "s" }, new Solution().LetterCombinations("7"));
}

[Fact]
public void LetterCombinations8() {
Assert.Equal(new List<string> { "t", "u", "v" }, new Solution().LetterCombinations("8"));
}

[Fact]
public void LetterCombinations9() {
Assert.Equal(new List<string> { "w", "x", "y", "z" }, new Solution().LetterCombinations("9"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LeetCodeNet.G0001_0100.S0019_remove_nth_node_from_end_of_list {

using Xunit;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void RemoveNthFromEnd() {
ListNode node1 = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 });
Assert.Equal("1, 2, 3, 5", new Solution().RemoveNthFromEnd(node1, 2).ToString());
}

[Fact]
public void RemoveNthFromEnd2() {
ListNode node1 = new ListNode(1);
Assert.Null(new Solution().RemoveNthFromEnd(node1, 1));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace LeetCodeNet.G0001_0100.S0020_valid_parentheses {

using Xunit;

public class SolutionTest {
[Fact]
public void IsValid() {
Assert.True(new Solution().IsValid("()"));
}

[Fact]
public void IsValid2() {
Assert.True(new Solution().IsValid("()[]{}"));
}

[Fact]
public void IsValid3() {
Assert.False(new Solution().IsValid("(]"));
}

[Fact]
public void IsValid4() {
Assert.False(new Solution().IsValid("([)]"));
}

[Fact]
public void IsValid5() {
Assert.True(new Solution().IsValid("{[]}"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LeetCodeNet.G0001_0100.S0021_merge_two_sorted_lists {

using Xunit;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void MergeTwoLists() {
ListNode l1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 2, 4 });
ListNode l2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 4 });
Assert.Equal("1, 1, 2, 3, 4, 4", new Solution().MergeTwoLists(l1, l2).ToString());
}

[Fact]
public void MergeTwoLists2() {
Assert.Equal("0, 0", new Solution().MergeTwoLists(new ListNode(), new ListNode()).ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace LeetCodeNet.G0001_0100.S0022_generate_parentheses {

using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void GenerateParenthesis() {
Assert.Equal(
new List<string> { "((()))", "(()())", "(())()", "()(())", "()()()" },
new Solution().GenerateParenthesis(3)
);
}

[Fact]
public void GenerateParenthesis2() {
Assert.Equal(
new List<string> { "()" },
new Solution().GenerateParenthesis(1)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LeetCodeNet.G0001_0100.S0023_merge_k_sorted_lists {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void MergeKLists() {
ListNode head1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 4, 5 });
ListNode head2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 4 });
ListNode head3 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 2, 6 });
Assert.Equal(
"1, 1, 2, 3, 4, 4, 5, 6",
new Solution().MergeKLists(new ListNode[] { head1, head2, head3 }).ToString()
);
}

[Fact]
public void MergeKLists2() {
ListNode head1 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 3, 5, 7, 11 });
ListNode head2 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 2, 8, 12 });
ListNode head3 = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 4, 6, 9, 10 });
Assert.Equal(
"1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12",
new Solution().MergeKLists(new ListNode[] { head1, head2, head3 }).ToString()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LeetCodeNet.G0001_0100.S0024_swap_nodes_in_pairs {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void SwapPairs() {
ListNode head = LinkedListUtils.CreateSinglyLinkedList(new List<int> { 1, 2, 3, 4 });
Assert.Equal("2, 1, 4, 3", new Solution().SwapPairs(head).ToString());
}

[Fact]
public void SwapPairs2() {
Assert.Equal("1", new Solution().SwapPairs(new ListNode(1)).ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace LeetCodeNet.G0001_0100.S0025_reverse_nodes_in_k_group {

using Xunit;
using System.Collections.Generic;
using LeetCodeNet.Com_github_leetcode;

public class SolutionTest {
[Fact]
public void ReverseKGroup() {
ListNode head = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 });
Assert.Equal("2, 1, 4, 3, 5", new Solution().ReverseKGroup(head, 2).ToString());
}

[Fact]
public void ReverseKGroup2() {
ListNode head = LinkedListUtils.ConstructLinkedList(new int[] { 1, 2, 3, 4, 5 });
Assert.Equal("3, 2, 1, 4, 5", new Solution().ReverseKGroup(head, 3).ToString());
}
}
}
26 changes: 26 additions & 0 deletions LeetCodeNet/G0001_0100/S0011_container_with_most_water/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LeetCodeNet.G0001_0100.S0011_container_with_most_water {

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2023_12_26_Time_248_ms_(11.15%)_Space_62.1_MB_(5.59%)

public class Solution {
public int MaxArea(int[] height) {
int maxArea = -1;
int left = 0;
int right = height.Length - 1;

while (left < right) {
if (height[left] < height[right]) {
maxArea = Math.Max(maxArea, height[left] * (right - left));
left++;
} else {
maxArea = Math.Max(maxArea, height[right] * (right - left));
right--;
}
}

return maxArea;
}
}
}
41 changes: 41 additions & 0 deletions LeetCodeNet/G0001_0100/S0011_container_with_most_water/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
11\. Container With Most Water

Medium

Given `n` non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> , where each represents a point at coordinate <code>(i, a<sub>i</sub>)</code>. `n` vertical lines are drawn such that the two endpoints of the line `i` is at <code>(i, a<sub>i</sub>)</code> and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

**Notice** that you may not slant the container.

**Example 1:**

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)

**Input:** height = [1,8,6,2,5,4,8,3,7]

**Output:** 49

**Explanation:** The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

**Example 2:**

**Input:** height = [1,1]

**Output:** 1

**Example 3:**

**Input:** height = [4,3,2,1,4]

**Output:** 16

**Example 4:**

**Input:** height = [1,2,1]

**Output:** 2

**Constraints:**

* `n == height.length`
* <code>2 <= n <= 10<sup>5</sup></code>
* <code>0 <= height[i] <= 10<sup>4</sup></code>
Loading

0 comments on commit be9f83e

Please sign in to comment.