Skip to content

Commit 74275ed

Browse files
authored
Create 9 February Maximum path sum from any node
1 parent 02a56e3 commit 74275ed

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
long long countBadPairs(vector<int>& nums) {
4+
long long n = nums.size();
5+
long long totalPair = (n * (n - 1) / 2);
6+
long long cnt = 0;
7+
unordered_map<int, int> mp;
8+
for (int i = 0; i < n; i++) {
9+
int x = nums[i] - i;
10+
if (mp.find(x) != mp.end()) {
11+
cnt += mp[x];
12+
}
13+
mp[x]++;
14+
}
15+
return totalPair - cnt;
16+
}
17+
};

0 commit comments

Comments
 (0)