diff --git a/.github/workflows/update-stats.yml b/.github/workflows/update-stats.yml index 8dae67d..0c29b4c 100644 --- a/.github/workflows/update-stats.yml +++ b/.github/workflows/update-stats.yml @@ -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) diff --git a/README.md b/README.md index f2c0bb9..2f6e1f9 100644 --- a/README.md +++ b/README.md @@ -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 🌊 @@ -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 diff --git a/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted.go b/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted.go new file mode 100644 index 0000000..4e465e0 --- /dev/null +++ b/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted.go @@ -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} +} \ No newline at end of file diff --git a/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted_test.go b/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted_test.go new file mode 100644 index 0000000..9aa5262 --- /dev/null +++ b/medium/167_two_sum_II_input_array_is_sorted/167_two_sum_II_input_array_is_sorted_test.go @@ -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) + } + }) + } +} \ No newline at end of file