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 6c1db62

Browse files
Refactor findPairs method: improve logic for counting pairs and handle edge cases
1 parent 819d42d commit 6c1db62

File tree

1 file changed

+72
-46
lines changed

1 file changed

+72
-46
lines changed

‎leetcode/532.k_diff_pairs_in_an_array.dart

Lines changed: 72 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,89 @@ void main(List<String> args) {
1010
}
1111

1212
class Solution {
13-
////! My Solution and solved the problem
14-
////! Hash Map
13+
////! Enhance My Hash Map solution
14+
////! AI help
15+
////! Best Time Solution
1516
int findPairs(List<int> nums, int k) {
16-
if (nums.length <=1) return 0;
17+
if (nums.isEmpty || k <0) return 0;
1718

18-
final Map<int, Set<int>> map = {};
1919
int pairsCount = 0;
20+
final Map<int, int> map = {};
2021

2122
for (int num in nums) {
22-
/// Both Negative Number and positive number are calculated and counted
23-
if (map[num]?.length == 2) {
24-
continue;
25-
}
23+
map[num] = (map[num] ?? 0) + 1;
24+
}
2625

27-
/// Postive number == negative number
26+
final mapKeys = map.keys;
27+
for (int num in mapKeys) {
2828
if (k == 0) {
29-
if (map[num] == null) {
30-
map[num] = {};
31-
} else if (map[num]?.isEmpty == true) {
32-
map[num]?.add(num);
33-
pairsCount++;
34-
}
35-
continue;
36-
}
37-
38-
if (map[num] == null) {
39-
map[num] = {};
40-
}
41-
42-
/// Find positive other number i - k = positive number
43-
int positiveNumber = num - k;
44-
45-
/// if we can add the current num to the positive number set
46-
/// then the positive number appeared before and its set isn't contains the current num
47-
if (map[positiveNumber] != null &&
48-
map[positiveNumber]?.contains(num) == false &&
49-
map[num]?.contains(positiveNumber) == false) {
50-
map[positiveNumber]?.add(num);
51-
map[num]?.add(positiveNumber);
52-
pairsCount++;
53-
}
54-
55-
/// Find negative other number
56-
int negativeNumber = num + k;
57-
58-
/// if we can add the current num to the negative number set
59-
/// then the negative number appeared before and its set isn't contains the current num
60-
if (map[negativeNumber] != null &&
61-
map[negativeNumber]?.contains(num) == false &&
62-
map[num]?.contains(negativeNumber) == false) {
63-
map[negativeNumber]?.add(num);
64-
map[num]?.add(negativeNumber);
65-
pairsCount++;
29+
if (map[num]! > 1) pairsCount++;
30+
} else {
31+
int otherNumber = num + k;
32+
if (map.containsKey(otherNumber)) pairsCount++;
6633
}
6734
}
6835
return pairsCount;
6936
}
37+
38+
////!
39+
////! My Solution and solved the problem
40+
////! Hash Map
41+
// int findPairs(List<int> nums, int k) {
42+
// if (nums.length <= 1) return 0;
43+
44+
// final Map<int, Set<int>> map = {};
45+
// int pairsCount = 0;
46+
47+
// for (int num in nums) {
48+
// /// Both Negative Number and positive number are calculated and counted
49+
// if (map[num]?.length == 2) {
50+
// continue;
51+
// }
52+
53+
// /// Postive number == negative number
54+
// if (k == 0) {
55+
// if (map[num] == null) {
56+
// map[num] = {};
57+
// } else if (map[num]?.isEmpty == true) {
58+
// map[num]?.add(num);
59+
// pairsCount++;
60+
// }
61+
// continue;
62+
// }
63+
64+
// if (map[num] == null) {
65+
// map[num] = {};
66+
// }
67+
68+
// /// Find positive other number i - k = positive number
69+
// int positiveNumber = num - k;
70+
71+
// /// if we can add the current num to the positive number set
72+
// /// then the positive number appeared before and its set isn't contains the current num
73+
// if (map[positiveNumber] != null &&
74+
// map[positiveNumber]?.contains(num) == false &&
75+
// map[num]?.contains(positiveNumber) == false) {
76+
// map[positiveNumber]?.add(num);
77+
// map[num]?.add(positiveNumber);
78+
// pairsCount++;
79+
// }
80+
81+
// /// Find negative other number
82+
// int negativeNumber = num + k;
83+
84+
// /// if we can add the current num to the negative number set
85+
// /// then the negative number appeared before and its set isn't contains the current num
86+
// if (map[negativeNumber] != null &&
87+
// map[negativeNumber]?.contains(num) == false &&
88+
// map[num]?.contains(negativeNumber) == false) {
89+
// map[negativeNumber]?.add(num);
90+
// map[num]?.add(negativeNumber);
91+
// pairsCount++;
92+
// }
93+
// }
94+
// return pairsCount;
95+
// }
7096
}
7197

7298
void runTests() {

0 commit comments

Comments
(0)

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