Skip to content

Commit e03ae7b

Browse files
committed
+ problem 1862
1 parent 95d3b4c commit e03ae7b

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 1862. Sum of Floored Pairs
2+
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** <code>10<sup>9</sup> + 7</code>.
3+
4+
The `floor()` function returns the integer part of the division.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [2,5,9]
9+
<strong>Output:</strong> 10
10+
<strong>Explanation:</strong>
11+
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
12+
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
13+
floor(5 / 2) = 2
14+
floor(9 / 2) = 4
15+
floor(9 / 5) = 1
16+
We calculate the floor of the division for every pair of indices in the array then sum them up.
17+
</pre>
18+
19+
#### Example 2:
20+
<pre>
21+
<strong>Input:</strong> nums = [7,7,7,7,7,7,7]
22+
<strong>Output:</strong> 49
23+
</pre>
24+
25+
#### Constraints:
26+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
27+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
28+
29+
## Solutions (Python)
30+
31+
### 1. Solution
32+
```Python
33+
class Solution:
34+
def sumOfFlooredPairs(self, nums: List[int]) -> int:
35+
prevsum = 0
36+
ret = 0
37+
38+
nums.sort()
39+
40+
for j in range(len(nums)):
41+
if j > 0 and nums[j] == nums[j - 1]:
42+
ret = (ret + prevsum) % 1000000007
43+
continue
44+
45+
i = bisect.bisect(nums, nums[j] - 1)
46+
prevsum = 0
47+
48+
for x in range(1, nums[-1] // nums[j] + 1):
49+
k = bisect.bisect(nums, nums[j] * (x + 1) - 1, lo=i)
50+
prevsum = (prevsum + x * (k - i)) % 1000000007
51+
i = k
52+
53+
ret = (ret + prevsum) % 1000000007
54+
55+
return ret
56+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 1862. 向下取整数对和
2+
给你一个整数数组 `nums` ,请你返回所有下标对 `0 <= i, j < nums.length``floor(nums[i] / nums[j])` 结果之和。由于答案可能会很大,请你返回答案对<code>10<sup>9</sup> + 7</code> **取余** 的结果。
3+
4+
函数 `floor()` 返回输入数字的整数部分。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [2,5,9]
9+
<strong>输出:</strong> 10
10+
<strong>解释:</strong>
11+
floor(2 / 5) = floor(2 / 9) = floor(5 / 9) = 0
12+
floor(2 / 2) = floor(5 / 5) = floor(9 / 9) = 1
13+
floor(5 / 2) = 2
14+
floor(9 / 2) = 4
15+
floor(9 / 5) = 1
16+
我们计算每一个数对商向下取整的结果并求和得到 10 。
17+
</pre>
18+
19+
#### 示例 2:
20+
<pre>
21+
<strong>输入:</strong> nums = [7,7,7,7,7,7,7]
22+
<strong>输出:</strong> 49
23+
</pre>
24+
25+
#### 提示:
26+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
27+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
28+
29+
## 题解 (Python)
30+
31+
### 1. 题解
32+
```Python
33+
class Solution:
34+
def sumOfFlooredPairs(self, nums: List[int]) -> int:
35+
prevsum = 0
36+
ret = 0
37+
38+
nums.sort()
39+
40+
for j in range(len(nums)):
41+
if j > 0 and nums[j] == nums[j - 1]:
42+
ret = (ret + prevsum) % 1000000007
43+
continue
44+
45+
i = bisect.bisect(nums, nums[j] - 1)
46+
prevsum = 0
47+
48+
for x in range(1, nums[-1] // nums[j] + 1):
49+
k = bisect.bisect(nums, nums[j] * (x + 1) - 1, lo=i)
50+
prevsum = (prevsum + x * (k - i)) % 1000000007
51+
i = k
52+
53+
ret = (ret + prevsum) % 1000000007
54+
55+
return ret
56+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def sumOfFlooredPairs(self, nums: List[int]) -> int:
3+
prevsum = 0
4+
ret = 0
5+
6+
nums.sort()
7+
8+
for j in range(len(nums)):
9+
if j > 0 and nums[j] == nums[j - 1]:
10+
ret = (ret + prevsum) % 1000000007
11+
continue
12+
13+
i = bisect.bisect(nums, nums[j] - 1)
14+
prevsum = 0
15+
16+
for x in range(1, nums[-1] // nums[j] + 1):
17+
k = bisect.bisect(nums, nums[j] * (x + 1) - 1, lo=i)
18+
prevsum = (prevsum + x * (k - i)) % 1000000007
19+
i = k
20+
21+
ret = (ret + prevsum) % 1000000007
22+
23+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@
12201220
[1859][1859l]|[Sorting the Sentence][1859] |![py]
12211221
[1860][1860l]|[Incremental Memory Leak][1860] |![rs]
12221222
[1861][1861l]|[Rotating the Box][1861] |![rs]
1223+
[1862][1862l]|[Sum of Floored Pairs][1862] |![py]
12231224
[1863][1863l]|[Sum of All Subset XOR Totals][1863] |![py]
12241225
[1864][1864l]|[Minimum Number of Swaps to Make the Binary String Alternating][1864] |![rs]
12251226
[1865][1865l]|[Finding Pairs With a Certain Sum][1865] |![rs]
@@ -2906,6 +2907,7 @@
29062907
[1859]:Problemset/1859-Sorting%20the%20Sentence/README.md#1859-sorting-the-sentence
29072908
[1860]:Problemset/1860-Incremental%20Memory%20Leak/README.md#1860-incremental-memory-leak
29082909
[1861]:Problemset/1861-Rotating%20the%20Box/README.md#1861-rotating-the-box
2910+
[1862]:Problemset/1862-Sum%20of%20Floored%20Pairs/README.md#1862-sum-of-floored-pairs
29092911
[1863]:Problemset/1863-Sum%20of%20All%20Subset%20XOR%20Totals/README.md#1863-sum-of-all-subset-xor-totals
29102912
[1864]:Problemset/1864-Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README.md#1864-minimum-number-of-swaps-to-make-the-binary-string-alternating
29112913
[1865]:Problemset/1865-Finding%20Pairs%20With%20a%20Certain%20Sum/README.md#1865-finding-pairs-with-a-certain-sum
@@ -4586,6 +4588,7 @@
45864588
[1859l]:https://leetcode.com/problems/sorting-the-sentence/
45874589
[1860l]:https://leetcode.com/problems/incremental-memory-leak/
45884590
[1861l]:https://leetcode.com/problems/rotating-the-box/
4591+
[1862l]:https://leetcode.com/problems/sum-of-floored-pairs/
45894592
[1863l]:https://leetcode.com/problems/sum-of-all-subset-xor-totals/
45904593
[1864l]:https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/
45914594
[1865l]:https://leetcode.com/problems/finding-pairs-with-a-certain-sum/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@
12201220
[1859][1859l]|[将句子排序][1859] |![py]
12211221
[1860][1860l]|[增长的内存泄露][1860] |![rs]
12221222
[1861][1861l]|[旋转盒子][1861] |![rs]
1223+
[1862][1862l]|[向下取整数对和][1862] |![py]
12231224
[1863][1863l]|[找出所有子集的异或总和再求和][1863] |![py]
12241225
[1864][1864l]|[构成交替字符串需要的最小交换次数][1864] |![rs]
12251226
[1865][1865l]|[找出和为指定值的下标对][1865] |![rs]
@@ -2906,6 +2907,7 @@
29062907
[1859]:Problemset/1859-Sorting%20the%20Sentence/README_CN.md#1859-将句子排序
29072908
[1860]:Problemset/1860-Incremental%20Memory%20Leak/README_CN.md#1860-增长的内存泄露
29082909
[1861]:Problemset/1861-Rotating%20the%20Box/README_CN.md#1861-旋转盒子
2910+
[1862]:Problemset/1862-Sum%20of%20Floored%20Pairs/README_CN.md#1862-向下取整数对和
29092911
[1863]:Problemset/1863-Sum%20of%20All%20Subset%20XOR%20Totals/README_CN.md#1863-找出所有子集的异或总和再求和
29102912
[1864]:Problemset/1864-Minimum%20Number%20of%20Swaps%20to%20Make%20the%20Binary%20String%20Alternating/README_CN.md#1864-构成交替字符串需要的最小交换次数
29112913
[1865]:Problemset/1865-Finding%20Pairs%20With%20a%20Certain%20Sum/README_CN.md#1865-找出和为指定值的下标对
@@ -4586,6 +4588,7 @@
45864588
[1859l]:https://leetcode.cn/problems/sorting-the-sentence/
45874589
[1860l]:https://leetcode.cn/problems/incremental-memory-leak/
45884590
[1861l]:https://leetcode.cn/problems/rotating-the-box/
4591+
[1862l]:https://leetcode.cn/problems/sum-of-floored-pairs/
45894592
[1863l]:https://leetcode.cn/problems/sum-of-all-subset-xor-totals/
45904593
[1864l]:https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-binary-string-alternating/
45914594
[1865l]:https://leetcode.cn/problems/finding-pairs-with-a-certain-sum/

0 commit comments

Comments
 (0)