Skip to content

Commit

Permalink
Add a solution to the following problems: Two Sum and Two Sum II.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Jul 17, 2024
1 parent 3c70d39 commit e4492fa
Show file tree
Hide file tree
Showing 30 changed files with 187 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.AddTwoNumbers;

public class Solution
public class BruteForceSolution
{
public ListNode? AddTwoNumbers(ListNode? firstNode, ListNode? secondNode)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.Assessments.Largest_Time_for_Given_Digits;

public class Solution
public class BruteForceSolution
{
public string LargestTimeFromDigits(int[] arr)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.ClimbingStairs;

public class Solution
public class BruteForceSolution
{
public int ClimbStairs(int n)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.ClimbingStairs_MyVersion;

public class Solution
public class BruteForceSolution
{
public int ClimbStairs(int n)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.GenerateParentheses;

public class Solution
public class BruteForceSolution
{
private readonly List<string> result = new List<string>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.IsSubsequence;

public class Solution
public class BruteForceSolution
{
public bool IsSubsequence(string substring, string originalString)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.LengthOfLastWord;

public class Solution
public class BruteForceSolution
{
// The complexity is O(n).
public static int GetLengthOfLastWordV1(string s)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.LongestPalindromicSubstring;

public class Solution
public class BruteForceSolution
{
public static string GetTheLongestPalindrome(string initialString)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.LongestSubstringWithoutRepeating;

public class Solution
public class BruteForceSolution
{
public int GetLongestSubstringLength(string input)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.MedianOfTwoSortedArrays;

public class Solution
public class BruteForceSolution
{
public double FindMedianSortedArrays(int[] nums1, int[] nums2)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.MergeSortedArray;

public class Solution
public class BruteForceSolution
{
public void Merge(int[] nums1, int m, int[] nums2, int n)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.ReverseInteger;

public class Solution
public class BruteForceSolution
{
public int Reverse(int x)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace LeetCode.Challenges.TwoSum;

public class Solution
public static class BruteForceSolution
{
public int[] GetResult(int[] numbers, int target)
public static int[] GetResult(int[] numbers, int target)
{
var result = new int[2];
for (var i = 0; i < numbers.Length; i++)
{
for (var j = i + 1; j < numbers.Length; j++)
Expand All @@ -14,9 +13,7 @@ public int[] GetResult(int[] numbers, int target)
continue;
}

result[0] = i;
result[1] = j;
return result;
return [i, j];
}
}

Expand Down
4 changes: 4 additions & 0 deletions LeetCode/src/LeetCode.Challenges/TwoSum/Description.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ Output: [0,1]
## Follow-up:

**Can you come up with an algorithm that is less than `O(n2)` time complexity?**

## References:

- [Original Problem](https://leetcode.com/problems/two-sum/description/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace LeetCode.Challenges.TwoSum;

public static class TwoPointerWithSortingSolution
{
public static int[] GetResult(int[] numbers, int target)
{
List<(int number, int originalIndex)> sortedNumbersWithIndex = numbers.Select((n, i) => (n, i)).ToList();
sortedNumbersWithIndex.Sort((a, b) => a.number.CompareTo(b.number));

int leftPointer = 0;
int rightPointer = sortedNumbersWithIndex.Count - 1;

while (leftPointer < rightPointer)
{
int sum = sortedNumbersWithIndex[leftPointer].number + sortedNumbersWithIndex[rightPointer].number;
if (sum == target)
{
return [sortedNumbersWithIndex[leftPointer].originalIndex, sortedNumbersWithIndex[rightPointer].originalIndex];
}

if (sum < target)
{
leftPointer++;
}
else
{
rightPointer--;
}
}

throw new InvalidOperationException("Solution Not Found");
}
}
43 changes: 43 additions & 0 deletions LeetCode/src/LeetCode.Challenges/TwoSum2/Description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Two Sum II - Input Array Is Sorted

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order,
find two numbers such that they add up to a specific target number.
Let these two numbers be `numbers[index1]` and `numbers[index2]` where `1 <= index1 < index2 <= numbers.length`.

Return the indices of the two numbers, `index1` and `index2`, added by one as an integer array `[index1, index2]` of length 2.

The tests are generated such that there is exactly one solution. You may not use the same element twice.

Your solution must use only constant extra space.

## Examples:

### Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].

### Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].

### Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].

## Constraints:

- `2 <= numbers.length <= 3 * 104`
- `-1000 <= numbers[i] <= 1000`
- numbers is sorted in non-decreasing order.
- `-1000 <= target <= 1000`
- The tests are generated such that there is exactly one solution.

## References:

- [Original Problem](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)
30 changes: 30 additions & 0 deletions LeetCode/src/LeetCode.Challenges/TwoSum2/TwoPointerSolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace LeetCode.Challenges.TwoSum2;

public static class TwoPointerSolution
{
public static int[] GetResult(int[] numbers, int target)
{
int leftPointer = 0;
int rightPointer = numbers.Length - 1;

while (leftPointer < rightPointer)
{
int sum = numbers[leftPointer] + numbers[rightPointer];
if (sum == target)
{
return [leftPointer + 1, rightPointer + 1];
}

if (sum < target)
{
leftPointer++;
}
else
{
rightPointer--;
}
}

throw new InvalidOperationException("Solution Not Found");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace LeetCode.Challenges.ValidParentheses;

public class Solution
public class BruteForceSolution
{
public bool IsValid(string s)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void GivenNumbers_WhenAddTwoNumbers_ThenResultAsExpected(int[] first, int
var b = GetListNodeFromArray(second);
var expectedResult = GetListNodeFromArray(total);

var actualResult = new Solution().AddTwoNumbers(a, b);
var actualResult = new BruteForceSolution().AddTwoNumbers(a, b);
actualResult.ShouldBeEquivalentTo(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ClimbingStairsTests
[MemberData(nameof(TestData))]
public void GivenNumbers_WhenAddTwoNumbers_ThenResultAsExpected(int n, int expectedResult)
{
var actualResult = new Solution().ClimbStairs(n);
var actualResult = new BruteForceSolution().ClimbStairs(n);
actualResult.ShouldBeEquivalentTo(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ClimbingStairsTests
[MemberData(nameof(TestData))]
public void GivenNumbers_WhenAddTwoNumbers_ThenResultAsExpected(int n, int expectedResult)
{
var actualResult = new Solution().ClimbStairs(n);
var actualResult = new BruteForceSolution().ClimbStairs(n);
actualResult.ShouldBeEquivalentTo(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace LeetCode.Challenges.UnitTests.GenerateParentheses;

public class SolutionTests
public class BruteForceSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenPairsNumber_WhenAddTwoNumbers_ThenResultAsExpected(int pairsNumber, ICollection<string> expectedResult)
{
var actualResult = new Solution().GenerateParenthesis(pairsNumber);
var actualResult = new BruteForceSolution().GenerateParenthesis(pairsNumber);
actualResult.Count.ShouldBe(expectedResult.Count);
actualResult.ShouldBe(expectedResult);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SolutionTests
public void GivenStrings_WhenCallIsSubsequence_ThenResultAsExpected(
string originalString, string substring, bool expectedResult)
{
var actualResult = new Solution().IsSubsequence(substring, originalString);
var actualResult = new BruteForceSolution().IsSubsequence(substring, originalString);
actualResult.ShouldBe(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace LeetCode.Challenges.UnitTests.LongestPalindromicSubstring;

public class SolutionTests
public class BruteForceSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenString_WhenGetTheLongestPalindrome_ThenResultAsExpected(string initialString, string expectedResult)
{
var actualResult = Solution.GetTheLongestPalindrome(initialString);
var actualResult = BruteForceSolution.GetTheLongestPalindrome(initialString);
actualResult.ShouldBeEquivalentTo(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace LeetCode.Challenges.UnitTests.LongestSubstringWithoutRepeating;

public class SolutionTests
public class BruteForceSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenString_WhenGetLengthOfLongestSubstring_ThenResultAsExpected(string input, int expectedResult)
{
var actualResult = new Solution().GetLongestSubstringLength(input);
var actualResult = new BruteForceSolution().GetLongestSubstringLength(input);
actualResult.ShouldBe(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class SolutionTests
[MemberData(nameof(TestData))]
public void GivenArrays_WhenMerge_ThenResultAsExpected(int[] nums1, int[] nums2, double expectedResult)
{
var actualResult = new Solution().FindMedianSortedArrays(nums1, nums2);
var actualResult = new BruteForceSolution().FindMedianSortedArrays(nums1, nums2);
actualResult.ShouldBe(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace LeetCode.Challenges.UnitTests.MergeSortedArray;

public class SolutionTests
public class BruteForceSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenArrays_WhenMerge_ThenResultAsExpected(int[] nums1, int[] nums2, int m, int n, int[] expectedResult)
{
new Solution().Merge(nums1, m, nums2, n);
new BruteForceSolution().Merge(nums1, m, nums2, n);
nums1.ShouldBe(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace LeetCode.Challenges.UnitTests.TwoSum;

public class SolutionTests
public class BruteForceSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenArray_WhenGetResult_ThenResultAsExpected(int[] nums, int target, int[] expectedResult)
{
var solution = new Solution();
int[] result = solution.GetResult(nums, target);
int[] result = BruteForceSolution.GetResult(nums, target);
result.ShouldBeEquivalentTo(expectedResult);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using LeetCode.Challenges.TwoSum;
using Shouldly;
using Xunit;

namespace LeetCode.Challenges.UnitTests.TwoSum;

public class TwoPointerWithSortingSolutionTests
{
[Theory]
[MemberData(nameof(TestData))]
public void GivenArray_WhenGetResult_ThenResultAsExpected(int[] nums, int target, int[] expectedResult)
{
int[] result = TwoPointerWithSortingSolution.GetResult(nums, target);
result.ShouldBeEquivalentTo(expectedResult);
}

public static IEnumerable<object[]> TestData()
{
yield return [ new [] { 2, 7, 11, 15 }, 9, new [] { 0, 1 } ];
yield return [ new [] { 3, 2, 4 }, 6, new [] { 1, 2 } ];
yield return [ new [] { 3, 3 }, 6, new [] { 0, 1 } ];
}
}
Loading

0 comments on commit e4492fa

Please sign in to comment.