-
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.
Add a solution to the following problems: Two Sum and Two Sum II.
- Loading branch information
1 parent
3c70d39
commit e4492fa
Showing
30 changed files
with
187 additions
and
35 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...Code.Challenges/AddTwoNumbers/Solution.cs → ...enges/AddTwoNumbers/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...Largest Time for Given Digits/Solution.cs → ...me for Given Digits/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...ode.Challenges/ClimbingStairs/Solution.cs → ...nges/ClimbingStairs/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...nges/ClimbingStairs_MyVersion/Solution.cs → ...ingStairs_MyVersion/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...hallenges/GenerateParentheses/Solution.cs → ...GenerateParentheses/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...Code.Challenges/IsSubsequence/Solution.cs → ...enges/IsSubsequence/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...e.Challenges/LengthOfLastWord/Solution.cs → ...es/LengthOfLastWord/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...s/LongestPalindromicSubstring/Solution.cs → ...alindromicSubstring/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...gestSubstringWithoutRepeating/Solution.cs → ...ingWithoutRepeating/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...enges/MedianOfTwoSortedArrays/Solution.cs → ...anOfTwoSortedArrays/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...e.Challenges/MergeSortedArray/Solution.cs → ...es/MergeSortedArray/BruteForceSolution.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
2 changes: 1 addition & 1 deletion
2
...ode.Challenges/ReverseInteger/Solution.cs → ...nges/ReverseInteger/BruteForceSolution.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
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
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
33 changes: 33 additions & 0 deletions
33
LeetCode/src/LeetCode.Challenges/TwoSum/TwoPointerWithSortingSolution.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 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"); | ||
} | ||
} |
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,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
30
LeetCode/src/LeetCode.Challenges/TwoSum2/TwoPointerSolution.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,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"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...e.Challenges/ValidParentheses/Solution.cs → ...es/ValidParentheses/BruteForceSolution.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
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
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
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
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
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
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
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
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
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
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
23 changes: 23 additions & 0 deletions
23
LeetCode/tests/LeetCode.Challenges.UnitTests/TwoSum/TwoPointerWithSortingSolutionTests.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,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 } ]; | ||
} | ||
} |
Oops, something went wrong.