Skip to content

Commit 24e88e0

Browse files
authored
feat: update solutions to lc problems: No.1508,1512 (doocs#3579)
1 parent be9d7c6 commit 24e88e0

File tree

16 files changed

+91
-395
lines changed

16 files changed

+91
-395
lines changed

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131

3232
<pre>
3333
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
34-
<strong>输出:</strong>13
34+
<strong>输出:</strong>13
3535
<strong>解释:</strong>所有的子数组和为 1, 3, 6, 10, 2, 5, 9, 3, 7, 4 。将它们升序排序后,我们得到新的数组 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 1 到 ri = 5 的和为 1 + 2 + 3 + 3 + 4 = 13 。
3636
</pre>
3737

@@ -67,11 +67,11 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一:排序
70+
### 方法一:模拟
7171

72-
按照题意生成 `arr` 数组,排序后,对 $[left-1,.. right-1]$ 范围的所有元素求和,得到结果。
72+
我们可以按照题目的要求,生成数组 $\textit{arr}$,然后对数组进行排序,最后求出 $[\textit{left}-1, \textit{right}-1]$ 范围的所有元素的和,得到结果。
7373

74-
时间复杂度 $O(n^2\log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度。
74+
时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度。
7575

7676
<!-- tabs:start -->
7777

@@ -175,13 +175,8 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
175175
}
176176
}
177177

178-
let ans = 0;
179178
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
180-
for (const x of arr) {
181-
ans += x;
182-
}
183-
184-
return ans % mod;
179+
return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
185180
}
186181
```
187182

@@ -199,13 +194,8 @@ function rangeSum(nums, n, left, right) {
199194
}
200195
}
201196

202-
let ans = 0;
203197
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
204-
for (const x of arr) {
205-
ans += x;
206-
}
207-
208-
return ans % mod;
198+
return arr.reduce((acc, cur) => acc + cur, 0) % mod;
209199
}
210200
```
211201

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tags:
3030

3131
<pre>
3232
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
33-
<strong>Output:</strong> 13
34-
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
33+
<strong>Output:</strong> 13
34+
<strong>Explanation:</strong> All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
3535
</pre>
3636

3737
<p><strong class="example">Example 2:</strong></p>
@@ -65,11 +65,11 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1: Sorting
68+
### Solution 1: Simulation
6969

70-
According to the problem statement, generate the `arr` array, sort it, and then sum all the elements in the range $[left-1,.. right-1]$ to get the result.
70+
We can generate the array $\textit{arr}$ according to the problem's requirements, then sort the array, and finally calculate the sum of all elements in the range $[\textit{left}-1, \textit{right}-1]$ to get the result.
7171

72-
Time complexity is $O(n^2 \times \log n)$, and space complexity is $O(n^2)$. Here, $n$ is the length of the array given in the problem.
72+
The time complexity is $O(n^2 \times \log n)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the array given in the problem.
7373

7474
<!-- tabs:start -->
7575

@@ -173,13 +173,8 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
173173
}
174174
}
175175

176-
let ans = 0;
177176
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
178-
for (const x of arr) {
179-
ans += x;
180-
}
181-
182-
return ans % mod;
177+
return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
183178
}
184179
```
185180

@@ -197,13 +192,8 @@ function rangeSum(nums, n, left, right) {
197192
}
198193
}
199194

200-
let ans = 0;
201195
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
202-
for (const x of arr) {
203-
ans += x;
204-
}
205-
206-
return ans % mod;
196+
return arr.reduce((acc, cur) => acc + cur, 0) % mod;
207197
}
208198
```
209199

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ function rangeSum(nums, n, left, right) {
99
}
1010
}
1111

12-
let ans = 0;
1312
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
14-
for (const x of arr) {
15-
ans += x;
16-
}
17-
18-
return ans % mod;
13+
return arr.reduce((acc, cur) => acc + cur, 0) % mod;
1914
}

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
99
}
1010
}
1111

12-
let ans = 0;
1312
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
14-
for (const x of arr) {
15-
ans += x;
16-
}
17-
18-
return ans % mod;
13+
return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
1914
}

solution/1500-1599/1512.Number of Good Pairs/README.md

Lines changed: 27 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func numIdenticalPairs(nums []int) (ans int) {
132132

133133
```ts
134134
function numIdenticalPairs(nums: number[]): number {
135-
const cnt = new Array(101).fill(0);
135+
const cnt: number[] = Array(101).fill(0);
136136
let ans = 0;
137137
for (const x of nums) {
138138
ans += cnt[x]++;
@@ -146,17 +146,34 @@ function numIdenticalPairs(nums: number[]): number {
146146
```rust
147147
impl Solution {
148148
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
149-
let mut cnt = [0; 101];
150149
let mut ans = 0;
151-
for &num in nums.iter() {
152-
ans += cnt[num as usize];
153-
cnt[num as usize] += 1;
150+
let mut cnt = [0; 101];
151+
for &x in nums.iter() {
152+
ans += cnt[x as usize];
153+
cnt[x as usize] += 1;
154154
}
155155
ans
156156
}
157157
}
158158
```
159159

160+
#### JavaScript
161+
162+
```js
163+
/**
164+
* @param {number[]} nums
165+
* @return {number}
166+
*/
167+
var numIdenticalPairs = function (nums) {
168+
const cnt = Array(101).fill(0);
169+
let ans = 0;
170+
for (const x of nums) {
171+
ans += cnt[x]++;
172+
}
173+
return ans;
174+
};
175+
```
176+
160177
#### PHP
161178

162179
```php
@@ -166,15 +183,12 @@ class Solution {
166183
* @return Integer
167184
*/
168185
function numIdenticalPairs($nums) {
169-
$arr = array_values(array_unique($nums));
170-
for ($i = 0; $i < count($nums); $i++) {
171-
$v[$nums[$i]] += 1;
186+
$ans = 0;
187+
$cnt = array_fill(0, 101, 0);
188+
foreach ($nums as $x) {
189+
$ans += $cnt[$x]++;
172190
}
173-
$rs = 0;
174-
for ($j = 0; $j < count($arr); $j++) {
175-
$rs += ($v[$arr[$j]] * ($v[$arr[$j]] - 1)) / 2;
176-
}
177-
return $rs;
191+
return $ans;
178192
}
179193
}
180194
```
@@ -196,125 +210,4 @@ int numIdenticalPairs(int* nums, int numsSize) {
196210
197211
<!-- solution:end -->
198212
199-
<!-- solution:start -->
200-
201-
### 方法二
202-
203-
<!-- tabs:start -->
204-
205-
#### Python3
206-
207-
```python
208-
class Solution:
209-
def numIdenticalPairs(self, nums: List[int]) -> int:
210-
cnt = Counter(nums)
211-
return sum(v * (v - 1) for v in cnt.values()) >> 1
212-
```
213-
214-
#### Java
215-
216-
```java
217-
class Solution {
218-
public int numIdenticalPairs(int[] nums) {
219-
int[] cnt = new int[101];
220-
for (int x : nums) {
221-
++cnt[x];
222-
}
223-
int ans = 0;
224-
for (int v : cnt) {
225-
ans += v * (v - 1) / 2;
226-
}
227-
return ans;
228-
}
229-
}
230-
```
231-
232-
#### C++
233-
234-
```cpp
235-
class Solution {
236-
public:
237-
int numIdenticalPairs(vector<int>& nums) {
238-
int cnt[101]{};
239-
for (int& x : nums) {
240-
++cnt[x];
241-
}
242-
int ans = 0;
243-
for (int v : cnt) {
244-
ans += v * (v - 1) / 2;
245-
}
246-
return ans;
247-
}
248-
};
249-
```
250-
251-
#### Go
252-
253-
```go
254-
func numIdenticalPairs(nums []int) (ans int) {
255-
cnt := [101]int{}
256-
for _, x := range nums {
257-
cnt[x]++
258-
}
259-
for _, v := range cnt {
260-
ans += v * (v - 1) / 2
261-
}
262-
return
263-
}
264-
```
265-
266-
#### TypeScript
267-
268-
```ts
269-
function numIdenticalPairs(nums: number[]): number {
270-
const cnt = new Array(101).fill(0);
271-
for (const x of nums) {
272-
++cnt[x];
273-
}
274-
let ans = 0;
275-
for (const v of cnt) {
276-
ans += v * (v - 1);
277-
}
278-
return ans >> 1;
279-
}
280-
```
281-
282-
#### Rust
283-
284-
```rust
285-
impl Solution {
286-
pub fn num_identical_pairs(nums: Vec<i32>) -> i32 {
287-
let mut cnt = [0; 101];
288-
for &num in nums.iter() {
289-
cnt[num as usize] += 1;
290-
}
291-
let mut ans = 0;
292-
for &v in cnt.iter() {
293-
ans += (v * (v - 1)) / 2;
294-
}
295-
ans
296-
}
297-
}
298-
```
299-
300-
#### C
301-
302-
```c
303-
int numIdenticalPairs(int* nums, int numsSize) {
304-
int cnt[101] = {0};
305-
for (int i = 0; i < numsSize; i++) {
306-
cnt[nums[i]]++;
307-
}
308-
int ans = 0;
309-
for (int i = 0; i < 101; ++i) {
310-
ans += cnt[i] * (cnt[i] - 1) / 2;
311-
}
312-
return ans;
313-
}
314-
```
315-
316-
<!-- tabs:end -->
317-
318-
<!-- solution:end -->
319-
320213
<!-- problem:end -->

0 commit comments

Comments
 (0)