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 4d40192

Browse files
Refactor 3Sum solution to use two-pointer technique and update README for clarity
1 parent 6f72b25 commit 4d40192

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
- Map In Dart (Hash Map ---- Linked Hash Map ---- Splay Tree Map)
8383
- Hash Map (Not Ordered) -- Hash Table (Ordered)
8484
- Hash Table
85+
8586
A Hash table is defined as a data structure used to insert, look up, and remove key-value pairs quickly.
8687
It operates on the hashing concept,
8788
where each key is translated by a hash function into a distinct index in an array.
@@ -90,4 +91,4 @@
9091

9192
- Find Sum of 2 Numbers in sorted List
9293
1. Brute Force (Bad)
93-
2. 2 Pointers
94+
2. Two Pointers

‎leetcode/15.3sum.dart

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,72 @@ void main(List<String> args) {
66
final stopwatch = Stopwatch()..start();
77
runTests();
88

9+
print(Solution().threeSum([-4, -2, -2, -2, 0, 1, 2, 2, 2, 3, 3, 4, 4, 6, 6]));
10+
911
stopwatch.stop();
1012
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro s');
1113
}
1214

1315
class Solution {
14-
////! Time Limit Solution --> TC: O(n^3)
15-
//// ! Brute Force .
16+
////! Success Submitted TC: O(n^2) + Sort TC
1617
List<List<int>> threeSum(List<int> nums) {
1718
nums.sort();
1819

1920
final Map<String, List<int>> result = {};
2021
for (int i = 0; i < nums.length - 2; i++) {
2122
if (nums[i] > 0) break;
22-
23-
for (int j = i + 1; j < nums.length - 1; j++) {
24-
if (nums[i] + nums[j] > 0) break;
25-
26-
for (int k = j + 1; k < nums.length; k++) {
27-
int sum = nums[i] + nums[j] + nums[k];
28-
29-
if (sum == 0) {
30-
result.putIfAbsent(
31-
"${nums[i]}${nums[j]}${nums[k]}",
32-
() => [nums[i], nums[j], nums[k]],
33-
);
34-
} else if (sum > 0) {
35-
break;
36-
}
23+
int target = -1 * nums[i];
24+
int left = i + 1;
25+
int right = nums.length - 1;
26+
27+
while (left < right) {
28+
int sum = nums[right] + nums[left];
29+
if (sum == target) {
30+
result.putIfAbsent(
31+
"${nums[i]}${nums[left]}${nums[right]}",
32+
() => [nums[i], nums[left], nums[right]],
33+
);
34+
right--;
35+
left++;
36+
} else if (sum > target) {
37+
right--;
38+
} else {
39+
left++;
3740
}
3841
}
3942
}
4043
return result.values.toList();
4144
}
4245

46+
////! Time Limit Solution --> TC: O(n^3) + Sort TC
47+
//// ! Brute Force .
48+
// List<List<int>> threeSum(List<int> nums) {
49+
// nums.sort();
50+
51+
// final Map<String, List<int>> result = {};
52+
// for (int i = 0; i < nums.length - 2; i++) {
53+
// if (nums[i] > 0) break;
54+
55+
// for (int j = i + 1; j < nums.length - 1; j++) {
56+
// if (nums[i] + nums[j] > 0) break;
57+
58+
// for (int k = j + 1; k < nums.length; k++) {
59+
// int sum = nums[i] + nums[j] + nums[k];
60+
61+
// if (sum == 0) {
62+
// result.putIfAbsent(
63+
// "${nums[i]}${nums[j]}${nums[k]}",
64+
// () => [nums[i], nums[j], nums[k]],
65+
// );
66+
// } else if (sum > 0) {
67+
// break;
68+
// }
69+
// }
70+
// }
71+
// }
72+
// return result.values.toList();
73+
// }
74+
4375
/// Wrong Solution
4476
/// Will make a duplicate
4577
// List<List<int>> threeSum(List<int> nums) {
@@ -166,8 +198,8 @@ void runTests() {
166198
test('All same number except one: [1,1,1,1,1,-2] → [[1,1,-2]]', () {
167199
expect(
168200
s.threeSum([1, 1, 1, 1, 1, -2]),
169-
equals([
170-
[1, 1, -2]
201+
unorderedMatches([
202+
[1, 1, -2]..sort()
171203
]));
172204
});
173205

@@ -194,10 +226,7 @@ void runTests() {
194226
];
195227
final result = s.threeSum(nums);
196228

197-
// Verify all expected triplets are present
198-
for (final triplet in expected) {
199-
expect(result, contains(triplet));
200-
}
229+
expect(result, unorderedMatches(expected));
201230

202231
// Verify no extra triplets are present
203232
expect(result.length, equals(expected.length));

0 commit comments

Comments
(0)

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