Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e1febd

Browse files
Add solution for Two Sum II - Input Array Is Sorted problem with comprehensive test cases
1 parent 6c1db62 commit 1e1febd

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
- Bad Question : I don't recommend to solve: https://leetcode.com/problems/next-permutation/description/
4444
- https://leetcode.com/problems/subarray-product-less-than-k/description/
4545
- https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
46+
- Two Pointers: with new idea: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
47+
-
4648
-
4749

4850
### Hard Problems
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
///
9+
stopwatch.stop();
10+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
11+
}
12+
13+
class Solution {
14+
////! Binary Search
15+
////! The more hard solution with Binary Search TC: O(n*log(n))
16+
////! Accepted And Faster
17+
List<int> twoSum(List<int> numbers, int target) {
18+
for (int i = 0; i < numbers.length - 1; i++) {
19+
final int otherNumber = target - numbers[i];
20+
int leftIndex = i + 1;
21+
int rightIndex = numbers.length - 1;
22+
23+
while (leftIndex <= rightIndex) {
24+
int mid = leftIndex + (rightIndex - leftIndex) ~/ 2;
25+
if (numbers[mid] == otherNumber) {
26+
return [i + 1, mid + 1];
27+
} else if (numbers[mid] > otherNumber) {
28+
rightIndex = mid - 1;
29+
} else {
30+
leftIndex = mid + 1;
31+
}
32+
}
33+
}
34+
35+
return [];
36+
}
37+
38+
////! Burte Force
39+
////! The easy solution --> TC: O(n^2)
40+
////! Accepted But Slow
41+
// List<int> twoSum(List<int> numbers, int target) {
42+
// for (int i = 0; i < numbers.length; i++) {
43+
// for (var j = i + 1; j < numbers.length; j++) {
44+
// if (numbers[i] + numbers[j] == target) {
45+
// return [i + 1, j + 1];
46+
// } else if (numbers[i] + numbers[j] > target) {
47+
// break;
48+
// }
49+
// }
50+
// }
51+
52+
// return [];
53+
// }
54+
}
55+
56+
void runTests() {
57+
final Solution s = Solution();
58+
59+
group('Two Sum II - Input Array Is Sorted', () {
60+
// Basic examples from problem statement
61+
test('Example 1: [2,7,11,15], target=9 → [1,2]', () {
62+
expect(s.twoSum([2, 7, 11, 15], 9), equals([1, 2]));
63+
});
64+
65+
test('Example 2: [2,3,4], target=6 → [1,3]', () {
66+
expect(s.twoSum([2, 3, 4], 6), equals([1, 3]));
67+
});
68+
69+
test('Example 3: [-1,0], target=-1 → [1,2]', () {
70+
expect(s.twoSum([-1, 0], -1), equals([1, 2]));
71+
});
72+
73+
// Edge cases
74+
test('Minimum length array: [1,2], target=3 → [1,2]', () {
75+
expect(s.twoSum([1, 2], 3), equals([1, 2]));
76+
});
77+
78+
test('Negative numbers: [-5,-3,-1], target=-8 → [1,2]', () {
79+
expect(s.twoSum([-5, -3, -1], -8), equals([1, 2]));
80+
});
81+
82+
test('Zero sum: [-2,0,1,2], target=0 → [1,4]', () {
83+
expect(s.twoSum([-2, 0, 1, 2], 0), equals([1, 4]));
84+
});
85+
86+
// Duplicate numbers
87+
test('Duplicate numbers: [1,1,2,3], target=2 → [1,2]', () {
88+
expect(s.twoSum([1, 1, 2, 3], 2), equals([1, 2]));
89+
});
90+
91+
test('Duplicate numbers at solution: [1,2,2,4], target=4 → [2,3]', () {
92+
expect(s.twoSum([1, 2, 2, 4], 4), equals([2, 3]));
93+
});
94+
95+
// Large numbers
96+
test('Large numbers: [1000,1000], target=2000 → [1,2]', () {
97+
expect(s.twoSum([1000, 1000], 2000), equals([1, 2]));
98+
});
99+
100+
// Large array
101+
test('Large array', () {
102+
final nums = List.generate(30000, (i) => i + 1);
103+
final target = 59999; // 30000 + 29999
104+
expect(s.twoSum(nums, target), equals([29999, 30000]));
105+
});
106+
107+
// First and last elements
108+
test('First and last elements: [1,2,3,4,5], target=6 → [1,5]', () {
109+
expect(s.twoSum([1, 2, 3, 4, 5], 6), equals([1, 5]));
110+
});
111+
112+
// Consecutive elements
113+
test('Consecutive elements: [1,2,3,4], target=7 → [3,4]', () {
114+
expect(s.twoSum([1, 2, 3, 4], 7), equals([3, 4]));
115+
});
116+
117+
test('Minimum target: [-1000,0,1000], target=-1000 → [1,2]', () {
118+
expect(s.twoSum([-1000, 0, 1000], -1000), equals([1, 2]));
119+
});
120+
121+
test('Maximum target: [-1000,0,1000], target=1000 → [2,3]', () {
122+
expect(s.twoSum([-1000, 0, 1000], 1000), equals([2, 3]));
123+
});
124+
125+
// Random cases
126+
test('Random case 2: [2,4,6,8,10], target=10 → [1,4]', () {
127+
expect(s.twoSum([2, 4, 6, 8, 10], 10), equals([1, 4]));
128+
});
129+
130+
/// Leet code
131+
test('Leet code: [0,0,3,4], target=0 → [1,2]', () {
132+
expect(s.twoSum([0, 0, 3, 4], 0), equals([1, 2]));
133+
});
134+
});
135+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /