Skip to content

Leetcode 167 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/update-stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,22 @@ jobs:
content = f.read()

# Add statistics section if it doesn't exist
stats_section = f"""
stats_section = f\"\"\"
## Statistics 📊

- Easy: {counts['easy']} solutions
- Medium: {counts['medium']} solutions
- Hard: {counts['hard']} solutions
- Total: {sum(counts.values())} solutions
"""
\"\"\"

if '## Statistics' in content:
# Replace existing statistics section
import re
content = re.sub(r'## Statistics.*?(?=##|$)', stats_section, content, flags=re.DOTALL)
else:
# Add statistics section before License
content = content.replace('## License', stats_section + '\n## License')
# Add statistics section before Solutions
content = content.replace('## Solutions', stats_section + '\\n## Solutions')

with open('README.md', 'w') as f:
f.write(content)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ go test ./easy/0001_two_sum
| [802](https://leetcode.com/problems/find-eventual-safe-states/description/?envType=daily-question&envId=2025-01-24) | [Find Eventual Safe States](/medium/802_find_eventual_safe_states) | medium | _`Depth-First Search`_ _`Adjacency List`_ | |
| [75](https://leetcode.com/problems/sort-colors/) | [Sort Colors](/medium/75_sort_colors) | medium | _`Three Pointers`_ _`DNF Algortithm`_ | Initalize 3 pointers. Left and mid at 0, high at len(nums) - 1. Do a while loop as long as mid <= high and swap elements based on 3 conditions. |
| [229](https://leetcode.com/problems/majority-element-ii/description/) | [Sort Colors](/medium/229_majority_element_II) | medium | _`Counter`_ _`Hashmap`_ | There can be at most 2 such elements with floor(n /3). Do a first pass to find all and eliminate all the non-qualify elements, then the second pass check how many time a each candidate appear in the original array. |
| [167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | [Two Sum II - Input Array Is Sorted](/medium/167_two_sum_II_input_array_is_sorted) | medium | _`Two Pointers`_ | We know that the array is sorted in asc order. We can track the total sum of 2 pointers left and right and increase or decrease their indices accordingly in a while loop |

## Workflow 🌊

Expand All @@ -64,7 +65,6 @@ go test ./easy/0001_two_sum
- Update `README.md` with current stats
- Automaticall commits and pushes the changes


## Statistics 📊

- Easy: 4 solutions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package two_sum_II_input_array_is_sorted

func twoSum(numbers []int, target int) []int {
l := 0
r := len(numbers) - 1

for {
if numbers[l] + numbers[r] < target {
l++
} else if numbers[l] + numbers[r] > target {
r--
} else {
break
}
}
return []int{l + 1, r + 1}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package two_sum_II_input_array_is_sorted

import (
"reflect"
"testing"
)

func TestTwoSumII(t *testing.T) {
tests := []struct {
name string
numbers []int
target int
expectedResult []int
} {
{
name: "Test Case 1",
numbers: []int{2,7,11,15},
target: 9,
expectedResult: []int{1,2},
},
{
name: "Test Case 2",
numbers: []int{2,3,4},
target: 6,
expectedResult: []int{1,3},
},
{
name: "Test Case 3",
numbers: []int{-1,0},
target: -1,
expectedResult: []int{1,2},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := twoSum(test.numbers, test.target)
if !reflect.DeepEqual(got, test.expectedResult) {
t.Errorf("twoSum(%v, %d) = %v ; want = %v", test.numbers, test.target, got, test.expectedResult)
}
})
}
}