-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1862-SumOfFlooredPairs.go
More file actions
73 lines (65 loc) · 2.06 KB
/
1862-SumOfFlooredPairs.go
File metadata and controls
73 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
// 1862. Sum of Floored Pairs
// Given an integer array nums,
// return the sum of floor(nums[i] / nums[j]) for all pairs of indices 0 <= i, j < nums.length in the array.
// Since the answer may be too large, return it modulo 10^9 + 7.
// The floor() function returns the integer part of the division.
// Example 1:
// Input: nums = [2,5,9]
// Output: 10
// Explanation:
// floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
// floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
// floor(5 / 2) = 2
// floor(9 / 2) = 4
// floor(9 / 5) = 1
// We calculate the floor of the division for every pair of indices in the array then sum them up.
// Example 2:
// Input: nums = [7,7,7,7,7,7,7]
// Output: 49
// Constraints:
// 1 <= nums.length <= 10^5
// 1 <= nums[i] <= 10^5
import "fmt"
func sumOfFlooredPairs(nums []int) int {
res, mx, mod := 0, -1, 1_000_000_007
for _, v := range nums { // 找到最大值
if v > mx { mx = v }
}
freq := make([]int, mx + 1)
for _, v := range nums {
freq[v]++
}
prefix := make([]int, mx + 1)
for i := 1; i <= mx; i++ {
prefix[i] = prefix[i - 1] + freq[i]
}
for i := 0; i <= mx; i++ {
if freq[i] == 0 { continue }
sum := 0
for j := 1; j * i <= mx; j++ {
left, right := j * i, (j + 1) * i - 1
if right > mx { right = mx }
sum = (sum + (j * (prefix[right] - prefix[left - 1]))) % mod
}
res = (res + freq[i] * sum) % mod
}
return res
}
func main() {
// Example 1:
// Input: nums = [2,5,9]
// Output: 10
// Explanation:
// floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
// floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
// floor(5 / 2) = 2
// floor(9 / 2) = 4
// floor(9 / 5) = 1
// We calculate the floor of the division for every pair of indices in the array then sum them up.
fmt.Println(sumOfFlooredPairs([]int{2,5,9})) // 10
// Example 2:
// Input: nums = [7,7,7,7,7,7,7]
// Output: 49
fmt.Println(sumOfFlooredPairs([]int{7,7,7,7,7,7,7})) // 49
}