Skip to content

Commit 3038cc8

Browse files
authored
feat: add solutions to lc problem: No.1862 (#2041)
No.1862.Sum of Floored Pairs
1 parent 5ae0da4 commit 3038cc8

File tree

8 files changed

+504
-2
lines changed

8 files changed

+504
-2
lines changed

Diff for: solution/1800-1899/1862.Sum of Floored Pairs/README.md

+177-1
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,198 @@ floor(9 / 5) = 1
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47+
**方法一:值域前缀和 + 优化枚举**
48+
49+
我们先统计数组 $nums$ 中每个元素出现的次数,记录在数组 $cnt$ 中,然后计算数组 $cnt$ 的前缀和,记录在数组 $s$ 中,即 $s[i]$ 表示小于等于 $i$ 的元素的个数。
50+
51+
接下来,我们枚举分母 $y$ 以及商 $d$,利用前缀和数组计算得到分子的个数 $s[\min(mx, d \times y + y - 1)] - s[d \times y - 1]$,其中 $mx$ 表示数组 $nums$ 中的最大值。那么,我们将分子的个数,乘以分母的个数 $cnt[y]$,再乘以商 $d$,就可以得到所有满足条件的分式的值,将这些值累加起来,就可以得到答案。
52+
53+
时间复杂度 $O(M \times \log M)$,空间复杂度 $O(M)$,其中 $M$ 表示数组 $nums$ 中的最大值。
54+
4755
<!-- tabs:start -->
4856

4957
### **Python3**
5058

5159
<!-- 这里可写当前语言的特殊实现逻辑 -->
5260

5361
```python
54-
62+
class Solution:
63+
def sumOfFlooredPairs(self, nums: List[int]) -> int:
64+
mod = 10**9 + 7
65+
cnt = Counter(nums)
66+
mx = max(nums)
67+
s = [0] * (mx + 1)
68+
for i in range(1, mx + 1):
69+
s[i] = s[i - 1] + cnt[i]
70+
ans = 0
71+
for y in range(1, mx + 1):
72+
if cnt[y]:
73+
d = 1
74+
while d * y <= mx:
75+
ans += cnt[y] * d * (s[min(mx, d * y + y - 1)] - s[d * y - 1])
76+
ans %= mod
77+
d += 1
78+
return ans
5579
```
5680

5781
### **Java**
5882

5983
<!-- 这里可写当前语言的特殊实现逻辑 -->
6084

6185
```java
86+
class Solution {
87+
public int sumOfFlooredPairs(int[] nums) {
88+
final int mod = (int) 1e9 + 7;
89+
int mx = 0;
90+
for (int x : nums) {
91+
mx = Math.max(mx, x);
92+
}
93+
int[] cnt = new int[mx + 1];
94+
int[] s = new int[mx + 1];
95+
for (int x : nums) {
96+
++cnt[x];
97+
}
98+
for (int i = 1; i <= mx; ++i) {
99+
s[i] = s[i - 1] + cnt[i];
100+
}
101+
long ans = 0;
102+
for (int y = 1; y <= mx; ++y) {
103+
if (cnt[y] > 0) {
104+
for (int d = 1; d * y <= mx; ++d) {
105+
ans += 1L * cnt[y] * d * (s[Math.min(mx, d * y + y - 1)] - s[d * y - 1]);
106+
ans %= mod;
107+
}
108+
}
109+
}
110+
return (int) ans;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
int sumOfFlooredPairs(vector<int>& nums) {
121+
const int mod = 1e9 + 7;
122+
int mx = *max_element(nums.begin(), nums.end());
123+
vector<int> cnt(mx + 1);
124+
vector<int> s(mx + 1);
125+
for (int x : nums) {
126+
++cnt[x];
127+
}
128+
for (int i = 1; i <= mx; ++i) {
129+
s[i] = s[i - 1] + cnt[i];
130+
}
131+
long long ans = 0;
132+
for (int y = 1; y <= mx; ++y) {
133+
if (cnt[y]) {
134+
for (int d = 1; d * y <= mx; ++d) {
135+
ans += 1LL * cnt[y] * d * (s[min(mx, d * y + y - 1)] - s[d * y - 1]);
136+
ans %= mod;
137+
}
138+
}
139+
}
140+
return ans;
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func sumOfFlooredPairs(nums []int) (ans int) {
149+
mx := slices.Max(nums)
150+
cnt := make([]int, mx+1)
151+
s := make([]int, mx+1)
152+
for _, x := range nums {
153+
cnt[x]++
154+
}
155+
for i := 1; i <= mx; i++ {
156+
s[i] = s[i-1] + cnt[i]
157+
}
158+
const mod int = 1e9 + 7
159+
for y := 1; y <= mx; y++ {
160+
if cnt[y] > 0 {
161+
for d := 1; d*y <= mx; d++ {
162+
ans += d * cnt[y] * (s[min((d+1)*y-1, mx)] - s[d*y-1])
163+
ans %= mod
164+
}
165+
}
166+
}
167+
return
168+
}
169+
```
170+
171+
### **TypeScript**
172+
173+
```ts
174+
function sumOfFlooredPairs(nums: number[]): number {
175+
const mx = Math.max(...nums);
176+
const cnt: number[] = Array(mx + 1).fill(0);
177+
const s: number[] = Array(mx + 1).fill(0);
178+
for (const x of nums) {
179+
++cnt[x];
180+
}
181+
for (let i = 1; i <= mx; ++i) {
182+
s[i] = s[i - 1] + cnt[i];
183+
}
184+
let ans = 0;
185+
const mod = 1e9 + 7;
186+
for (let y = 1; y <= mx; ++y) {
187+
if (cnt[y]) {
188+
for (let d = 1; d * y <= mx; ++d) {
189+
ans += cnt[y] * d * (s[Math.min((d + 1) * y - 1, mx)] - s[d * y - 1]);
190+
ans %= mod;
191+
}
192+
}
193+
}
194+
return ans;
195+
}
196+
```
62197

198+
### **Rust**
199+
200+
```rust
201+
impl Solution {
202+
pub fn sum_of_floored_pairs(nums: Vec<i32>) -> i32 {
203+
const MOD: i32 = 1_000_000_007;
204+
let mut mx = 0;
205+
for &x in nums.iter() {
206+
mx = mx.max(x);
207+
}
208+
209+
let mut cnt = vec![0; (mx + 1) as usize];
210+
let mut s = vec![0; (mx + 1) as usize];
211+
212+
for &x in nums.iter() {
213+
cnt[x as usize] += 1;
214+
}
215+
216+
for i in 1..=mx as usize {
217+
s[i] = s[i - 1] + cnt[i];
218+
}
219+
220+
let mut ans = 0;
221+
for y in 1..=mx as usize {
222+
if cnt[y] > 0 {
223+
let mut d = 1;
224+
while d * y <= (mx as usize) {
225+
ans +=
226+
((cnt[y] as i64) *
227+
(d as i64) *
228+
(s[std::cmp::min(mx as usize, d * y + y - 1)] - s[d * y - 1])) %
229+
(MOD as i64);
230+
ans %= MOD as i64;
231+
d += 1;
232+
}
233+
}
234+
}
235+
236+
ans as i32
237+
}
238+
}
63239
```
64240

65241
### **...**

Diff for: solution/1800-1899/1862.Sum of Floored Pairs/README_EN.md

+177-1
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,194 @@ We calculate the floor of the division for every pair of indices in the array th
4040

4141
## Solutions
4242

43+
**Solution 1: Prefix Sum of Value Range + Optimized Enumeration**
44+
45+
First, we count the occurrences of each element in the array $nums$ and record them in the array $cnt$. Then, we calculate the prefix sum of the array $cnt$ and record it in the array $s$, i.e., $s[i]$ represents the count of elements less than or equal to $i$.
46+
47+
Next, we enumerate the denominator $y$ and the quotient $d$. Using the prefix sum array, we can calculate the count of the numerator $s[\min(mx, d \times y + y - 1)] - s[d \times y - 1]$, where $mx$ represents the maximum value in the array $nums$. Then, we multiply the count of the numerator by the count of the denominator $cnt[y]$, and then multiply by the quotient $d$. This gives us the value of all fractions that meet the conditions. By summing these values, we can get the answer.
48+
49+
The time complexity is $O(M \times \log M)$, and the space complexity is $O(M)$. Here, $M$ is the maximum value in the array $nums$.
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
4654

4755
```python
48-
56+
class Solution:
57+
def sumOfFlooredPairs(self, nums: List[int]) -> int:
58+
mod = 10**9 + 7
59+
cnt = Counter(nums)
60+
mx = max(nums)
61+
s = [0] * (mx + 1)
62+
for i in range(1, mx + 1):
63+
s[i] = s[i - 1] + cnt[i]
64+
ans = 0
65+
for y in range(1, mx + 1):
66+
if cnt[y]:
67+
d = 1
68+
while d * y <= mx:
69+
ans += cnt[y] * d * (s[min(mx, d * y + y - 1)] - s[d * y - 1])
70+
ans %= mod
71+
d += 1
72+
return ans
4973
```
5074

5175
### **Java**
5276

5377
```java
78+
class Solution {
79+
public int sumOfFlooredPairs(int[] nums) {
80+
final int mod = (int) 1e9 + 7;
81+
int mx = 0;
82+
for (int x : nums) {
83+
mx = Math.max(mx, x);
84+
}
85+
int[] cnt = new int[mx + 1];
86+
int[] s = new int[mx + 1];
87+
for (int x : nums) {
88+
++cnt[x];
89+
}
90+
for (int i = 1; i <= mx; ++i) {
91+
s[i] = s[i - 1] + cnt[i];
92+
}
93+
long ans = 0;
94+
for (int y = 1; y <= mx; ++y) {
95+
if (cnt[y] > 0) {
96+
for (int d = 1; d * y <= mx; ++d) {
97+
ans += 1L * cnt[y] * d * (s[Math.min(mx, d * y + y - 1)] - s[d * y - 1]);
98+
ans %= mod;
99+
}
100+
}
101+
}
102+
return (int) ans;
103+
}
104+
}
105+
```
106+
107+
### **C++**
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
int sumOfFlooredPairs(vector<int>& nums) {
113+
const int mod = 1e9 + 7;
114+
int mx = *max_element(nums.begin(), nums.end());
115+
vector<int> cnt(mx + 1);
116+
vector<int> s(mx + 1);
117+
for (int x : nums) {
118+
++cnt[x];
119+
}
120+
for (int i = 1; i <= mx; ++i) {
121+
s[i] = s[i - 1] + cnt[i];
122+
}
123+
long long ans = 0;
124+
for (int y = 1; y <= mx; ++y) {
125+
if (cnt[y]) {
126+
for (int d = 1; d * y <= mx; ++d) {
127+
ans += 1LL * cnt[y] * d * (s[min(mx, d * y + y - 1)] - s[d * y - 1]);
128+
ans %= mod;
129+
}
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func sumOfFlooredPairs(nums []int) (ans int) {
141+
mx := slices.Max(nums)
142+
cnt := make([]int, mx+1)
143+
s := make([]int, mx+1)
144+
for _, x := range nums {
145+
cnt[x]++
146+
}
147+
for i := 1; i <= mx; i++ {
148+
s[i] = s[i-1] + cnt[i]
149+
}
150+
const mod int = 1e9 + 7
151+
for y := 1; y <= mx; y++ {
152+
if cnt[y] > 0 {
153+
for d := 1; d*y <= mx; d++ {
154+
ans += d * cnt[y] * (s[min((d+1)*y-1, mx)] - s[d*y-1])
155+
ans %= mod
156+
}
157+
}
158+
}
159+
return
160+
}
161+
```
162+
163+
### **TypeScript**
164+
165+
```ts
166+
function sumOfFlooredPairs(nums: number[]): number {
167+
const mx = Math.max(...nums);
168+
const cnt: number[] = Array(mx + 1).fill(0);
169+
const s: number[] = Array(mx + 1).fill(0);
170+
for (const x of nums) {
171+
++cnt[x];
172+
}
173+
for (let i = 1; i <= mx; ++i) {
174+
s[i] = s[i - 1] + cnt[i];
175+
}
176+
let ans = 0;
177+
const mod = 1e9 + 7;
178+
for (let y = 1; y <= mx; ++y) {
179+
if (cnt[y]) {
180+
for (let d = 1; d * y <= mx; ++d) {
181+
ans += cnt[y] * d * (s[Math.min((d + 1) * y - 1, mx)] - s[d * y - 1]);
182+
ans %= mod;
183+
}
184+
}
185+
}
186+
return ans;
187+
}
188+
```
54189

190+
### **Rust**
191+
192+
```rust
193+
impl Solution {
194+
pub fn sum_of_floored_pairs(nums: Vec<i32>) -> i32 {
195+
const MOD: i32 = 1_000_000_007;
196+
let mut mx = 0;
197+
for &x in nums.iter() {
198+
mx = mx.max(x);
199+
}
200+
201+
let mut cnt = vec![0; (mx + 1) as usize];
202+
let mut s = vec![0; (mx + 1) as usize];
203+
204+
for &x in nums.iter() {
205+
cnt[x as usize] += 1;
206+
}
207+
208+
for i in 1..=mx as usize {
209+
s[i] = s[i - 1] + cnt[i];
210+
}
211+
212+
let mut ans = 0;
213+
for y in 1..=mx as usize {
214+
if cnt[y] > 0 {
215+
let mut d = 1;
216+
while d * y <= (mx as usize) {
217+
ans +=
218+
((cnt[y] as i64) *
219+
(d as i64) *
220+
(s[std::cmp::min(mx as usize, d * y + y - 1)] - s[d * y - 1])) %
221+
(MOD as i64);
222+
ans %= MOD as i64;
223+
d += 1;
224+
}
225+
}
226+
}
227+
228+
ans as i32
229+
}
230+
}
55231
```
56232

57233
### **...**

0 commit comments

Comments
 (0)