Skip to content

Commit d22d952

Browse files
committed
Runtime: 687 ms (Top 46.29%) | Memory: 121.4 MB (Top 57.71%)
1 parent 89ad70e commit d22d952

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1+
// Runtime: 687 ms (Top 46.29%) | Memory: 121.4 MB (Top 57.71%)
12
class Solution {
2-
3+
34
private:
4-
5+
56
int MOD = 1e9 + 7;
6-
7+
78
public:
8-
9+
910
int sumOfFlooredPairs(vector<int>& nums) {
1011

1112
// first of all, we record the max value
1213
int max_n = INT_MIN;
1314
for(int n : nums) max_n = max(max_n, n);
14-
15+
1516
// then the occurences for each number in [0, max]
1617
vector<int> occs(max_n + 1, 0);
1718
for(int n : nums) occs[n]++;
18-
19+
1920
// prefix sum algorithm to accumulate the occurences
2021
vector<int> occs_acc(max_n + 1, 0);
2122
for(int i = 1; i < max_n + 1; ++i) {
22-
occs_acc[i] = occs[i] + occs_acc[i - 1];
23+
occs_acc[i] = occs[i] + occs_acc[i - 1];
2324
}
24-
25+
2526
// long long needed to prevent overflows
26-
long long ans = 0;
27+
long long ans = 0;
2728
for(int i = 0; i < max_n + 1; ++i) {
28-
29+
2930
// just handle numbers that occur at least once
3031
if(occs[i] != 0) {
31-
32+
3233
int k = 1;
3334
int k_next;
34-
35+
3536
// for each multiple of i
3637
do {
37-
38+
3839
k_next = k + 1;
3940

40-
// "right and left" multipliers in occs_acc
41+
// "right and left" multipliers in occs_acc
4142
int r = min(k_next * i - 1, max_n);
4243
int l = k * i - 1;
43-
44+
4445
ans += ((long long) occs_acc[r] - (long long) occs_acc[l]) * (long long) occs[i] * k++;
45-
46-
46+
4747
} while(k_next * i - 1 < max_n);
4848
}
4949
}
50-
50+
5151
return ans % MOD;
5252
}
53-
};
53+
};

0 commit comments

Comments
 (0)