diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md
index 25286920cca4f..41a8792547f05 100644
--- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md
+++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md
@@ -31,7 +31,7 @@ tags:
输入:nums = [1,2,3,4], n = 4, left = 1, right = 5
-输出:13
+输出:13
解释:所有的子数组和为 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 。
@@ -67,11 +67,11 @@ tags:
-### 方法一:排序
+### 方法一:模拟
-按照题意生成 `arr` 数组,排序后,对 $[left-1,.. right-1]$ 范围的所有元素求和,得到结果。
+我们可以按照题目的要求,生成数组 $\textit{arr}$,然后对数组进行排序,最后求出 $[\textit{left}-1, \textit{right}-1]$ 范围的所有元素的和,得到结果。
-时间复杂度 $O(n^2\log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度。
+时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度。
@@ -175,13 +175,8 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
}
```
@@ -199,13 +194,8 @@ function rangeSum(nums, n, left, right) {
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => acc + cur, 0) % mod;
}
```
diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md
index 6645ba6d28c43..c11ebd6eecc33 100644
--- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md
+++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md
@@ -30,8 +30,8 @@ tags:
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5
-Output: 13
-Explanation: 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.
+Output: 13
+Explanation: 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.
Example 2:
@@ -65,11 +65,11 @@ tags:
-### Solution 1: Sorting
+### Solution 1: Simulation
-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.
+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.
-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.
+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.
@@ -173,13 +173,8 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
}
```
@@ -197,13 +192,8 @@ function rangeSum(nums, n, left, right) {
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => acc + cur, 0) % mod;
}
```
diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js
index 83f07fdce38b7..ee496cb207c08 100644
--- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js
+++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js
@@ -9,11 +9,6 @@ function rangeSum(nums, n, left, right) {
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => acc + cur, 0) % mod;
}
diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts
index 22e1796afe0a9..22dd44876b7e5 100644
--- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts
+++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts
@@ -9,11 +9,6 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
}
}
- let ans = 0;
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
- for (const x of arr) {
- ans += x;
- }
-
- return ans % mod;
+ return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
}
diff --git a/solution/1500-1599/1512.Number of Good Pairs/README.md b/solution/1500-1599/1512.Number of Good Pairs/README.md
index be1308541d89b..275710512ae47 100644
--- a/solution/1500-1599/1512.Number of Good Pairs/README.md
+++ b/solution/1500-1599/1512.Number of Good Pairs/README.md
@@ -132,7 +132,7 @@ func numIdenticalPairs(nums []int) (ans int) {
```ts
function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
+ const cnt: number[] = Array(101).fill(0);
let ans = 0;
for (const x of nums) {
ans += cnt[x]++;
@@ -146,17 +146,34 @@ function numIdenticalPairs(nums: number[]): number {
```rust
impl Solution {
pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
let mut ans = 0;
- for &num in nums.iter() {
- ans += cnt[num as usize];
- cnt[num as usize] += 1;
+ let mut cnt = [0; 101];
+ for &x in nums.iter() {
+ ans += cnt[x as usize];
+ cnt[x as usize] += 1;
}
ans
}
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var numIdenticalPairs = function (nums) {
+ const cnt = Array(101).fill(0);
+ let ans = 0;
+ for (const x of nums) {
+ ans += cnt[x]++;
+ }
+ return ans;
+};
+```
+
#### PHP
```php
@@ -166,15 +183,12 @@ class Solution {
* @return Integer
*/
function numIdenticalPairs($nums) {
- $arr = array_values(array_unique($nums));
- for ($i = 0; $i < count($nums); $i++) {
- $v[$nums[$i]] += 1;
+ $ans = 0;
+ $cnt = array_fill(0, 101, 0);
+ foreach ($nums as $x) {
+ $ans += $cnt[$x]++;
}
- $rs = 0;
- for ($j = 0; $j < count($arr); $j++) {
- $rs += ($v[$arr[$j]] * ($v[$arr[$j]] - 1)) / 2;
- }
- return $rs;
+ return $ans;
}
}
```
@@ -196,125 +210,4 @@ int numIdenticalPairs(int* nums, int numsSize) {
-
-
-### 方法二
-
-
-
-#### Python3
-
-```python
-class Solution:
- def numIdenticalPairs(self, nums: List[int]) -> int:
- cnt = Counter(nums)
- return sum(v * (v - 1) for v in cnt.values()) >> 1
-```
-
-#### Java
-
-```java
-class Solution {
- public int numIdenticalPairs(int[] nums) {
- int[] cnt = new int[101];
- for (int x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-}
-```
-
-#### C++
-
-```cpp
-class Solution {
-public:
- int numIdenticalPairs(vector& nums) {
- int cnt[101]{};
- for (int& x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-};
-```
-
-#### Go
-
-```go
-func numIdenticalPairs(nums []int) (ans int) {
- cnt := [101]int{}
- for _, x := range nums {
- cnt[x]++
- }
- for _, v := range cnt {
- ans += v * (v - 1) / 2
- }
- return
-}
-```
-
-#### TypeScript
-
-```ts
-function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
- for (const x of nums) {
- ++cnt[x];
- }
- let ans = 0;
- for (const v of cnt) {
- ans += v * (v - 1);
- }
- return ans >> 1;
-}
-```
-
-#### Rust
-
-```rust
-impl Solution {
- pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
- for &num in nums.iter() {
- cnt[num as usize] += 1;
- }
- let mut ans = 0;
- for &v in cnt.iter() {
- ans += (v * (v - 1)) / 2;
- }
- ans
- }
-}
-```
-
-#### C
-
-```c
-int numIdenticalPairs(int* nums, int numsSize) {
- int cnt[101] = {0};
- for (int i = 0; i < numsSize; i++) {
- cnt[nums[i]]++;
- }
- int ans = 0;
- for (int i = 0; i < 101; ++i) {
- ans += cnt[i] * (cnt[i] - 1) / 2;
- }
- return ans;
-}
-```
-
-
-
-
-
diff --git a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md
index 5a33e5d94f4cf..ff295f5cb6171 100644
--- a/solution/1500-1599/1512.Number of Good Pairs/README_EN.md
+++ b/solution/1500-1599/1512.Number of Good Pairs/README_EN.md
@@ -63,7 +63,11 @@ tags:
-### Solution 1
+### Solution 1: Counting
+
+Traverse the array, and for each element $x$, count how many elements before it are equal to $x$. This count represents the number of good pairs formed by $x$ and the previous elements. After traversing the entire array, we obtain the answer.
+
+The time complexity is $O(n)$, and the space complexity is $O(C)$. Here, $n$ is the length of the array, and $C$ is the range of values in the array. In this problem, $C = 101$.
@@ -128,7 +132,7 @@ func numIdenticalPairs(nums []int) (ans int) {
```ts
function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
+ const cnt: number[] = Array(101).fill(0);
let ans = 0;
for (const x of nums) {
ans += cnt[x]++;
@@ -142,17 +146,34 @@ function numIdenticalPairs(nums: number[]): number {
```rust
impl Solution {
pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
let mut ans = 0;
- for &num in nums.iter() {
- ans += cnt[num as usize];
- cnt[num as usize] += 1;
+ let mut cnt = [0; 101];
+ for &x in nums.iter() {
+ ans += cnt[x as usize];
+ cnt[x as usize] += 1;
}
ans
}
}
```
+#### JavaScript
+
+```js
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var numIdenticalPairs = function (nums) {
+ const cnt = Array(101).fill(0);
+ let ans = 0;
+ for (const x of nums) {
+ ans += cnt[x]++;
+ }
+ return ans;
+};
+```
+
#### PHP
```php
@@ -162,15 +183,12 @@ class Solution {
* @return Integer
*/
function numIdenticalPairs($nums) {
- $arr = array_values(array_unique($nums));
- for ($i = 0; $i < count($nums); $i++) {
- $v[$nums[$i]] += 1;
- }
- $rs = 0;
- for ($j = 0; $j < count($arr); $j++) {
- $rs += ($v[$arr[$j]] * ($v[$arr[$j]] - 1)) / 2;
+ $ans = 0;
+ $cnt = array_fill(0, 101, 0);
+ foreach ($nums as $x) {
+ $ans += $cnt[$x]++;
}
- return $rs;
+ return $ans;
}
}
```
@@ -192,125 +210,4 @@ int numIdenticalPairs(int* nums, int numsSize) {
-
-
-### Solution 2
-
-
-
-#### Python3
-
-```python
-class Solution:
- def numIdenticalPairs(self, nums: List[int]) -> int:
- cnt = Counter(nums)
- return sum(v * (v - 1) for v in cnt.values()) >> 1
-```
-
-#### Java
-
-```java
-class Solution {
- public int numIdenticalPairs(int[] nums) {
- int[] cnt = new int[101];
- for (int x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-}
-```
-
-#### C++
-
-```cpp
-class Solution {
-public:
- int numIdenticalPairs(vector& nums) {
- int cnt[101]{};
- for (int& x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-};
-```
-
-#### Go
-
-```go
-func numIdenticalPairs(nums []int) (ans int) {
- cnt := [101]int{}
- for _, x := range nums {
- cnt[x]++
- }
- for _, v := range cnt {
- ans += v * (v - 1) / 2
- }
- return
-}
-```
-
-#### TypeScript
-
-```ts
-function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
- for (const x of nums) {
- ++cnt[x];
- }
- let ans = 0;
- for (const v of cnt) {
- ans += v * (v - 1);
- }
- return ans >> 1;
-}
-```
-
-#### Rust
-
-```rust
-impl Solution {
- pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
- for &num in nums.iter() {
- cnt[num as usize] += 1;
- }
- let mut ans = 0;
- for &v in cnt.iter() {
- ans += (v * (v - 1)) / 2;
- }
- ans
- }
-}
-```
-
-#### C
-
-```c
-int numIdenticalPairs(int* nums, int numsSize) {
- int cnt[101] = {0};
- for (int i = 0; i < numsSize; i++) {
- cnt[nums[i]]++;
- }
- int ans = 0;
- for (int i = 0; i < 101; ++i) {
- ans += cnt[i] * (cnt[i] - 1) / 2;
- }
- return ans;
-}
-```
-
-
-
-
-
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution.js b/solution/1500-1599/1512.Number of Good Pairs/Solution.js
new file mode 100644
index 0000000000000..e4586ffdc80e5
--- /dev/null
+++ b/solution/1500-1599/1512.Number of Good Pairs/Solution.js
@@ -0,0 +1,12 @@
+/**
+ * @param {number[]} nums
+ * @return {number}
+ */
+var numIdenticalPairs = function (nums) {
+ const cnt = Array(101).fill(0);
+ let ans = 0;
+ for (const x of nums) {
+ ans += cnt[x]++;
+ }
+ return ans;
+};
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution.rs b/solution/1500-1599/1512.Number of Good Pairs/Solution.rs
index 2d6329d025e37..c9e69ccc66d6c 100644
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution.rs
+++ b/solution/1500-1599/1512.Number of Good Pairs/Solution.rs
@@ -1,10 +1,10 @@
impl Solution {
pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
let mut ans = 0;
- for &num in nums.iter() {
- ans += cnt[num as usize];
- cnt[num as usize] += 1;
+ let mut cnt = [0; 101];
+ for &x in nums.iter() {
+ ans += cnt[x as usize];
+ cnt[x as usize] += 1;
}
ans
}
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution.ts b/solution/1500-1599/1512.Number of Good Pairs/Solution.ts
index e48f9b86d8735..8b13f5f86d0a5 100644
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution.ts
+++ b/solution/1500-1599/1512.Number of Good Pairs/Solution.ts
@@ -1,5 +1,5 @@
function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
+ const cnt: number[] = Array(101).fill(0);
let ans = 0;
for (const x of nums) {
ans += cnt[x]++;
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.c b/solution/1500-1599/1512.Number of Good Pairs/Solution2.c
deleted file mode 100644
index a15ad28a7ed56..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.c
+++ /dev/null
@@ -1,11 +0,0 @@
-int numIdenticalPairs(int* nums, int numsSize) {
- int cnt[101] = {0};
- for (int i = 0; i < numsSize; i++) {
- cnt[nums[i]]++;
- }
- int ans = 0;
- for (int i = 0; i < 101; ++i) {
- ans += cnt[i] * (cnt[i] - 1) / 2;
- }
- return ans;
-}
\ No newline at end of file
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.cpp b/solution/1500-1599/1512.Number of Good Pairs/Solution2.cpp
deleted file mode 100644
index eee7d0bca33b1..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-class Solution {
-public:
- int numIdenticalPairs(vector& nums) {
- int cnt[101]{};
- for (int& x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-};
\ No newline at end of file
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.go b/solution/1500-1599/1512.Number of Good Pairs/Solution2.go
deleted file mode 100644
index 9221177f22a7d..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.go
+++ /dev/null
@@ -1,10 +0,0 @@
-func numIdenticalPairs(nums []int) (ans int) {
- cnt := [101]int{}
- for _, x := range nums {
- cnt[x]++
- }
- for _, v := range cnt {
- ans += v * (v - 1) / 2
- }
- return
-}
\ No newline at end of file
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.java b/solution/1500-1599/1512.Number of Good Pairs/Solution2.java
deleted file mode 100644
index ded87607f2db9..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.java
+++ /dev/null
@@ -1,13 +0,0 @@
-class Solution {
- public int numIdenticalPairs(int[] nums) {
- int[] cnt = new int[101];
- for (int x : nums) {
- ++cnt[x];
- }
- int ans = 0;
- for (int v : cnt) {
- ans += v * (v - 1) / 2;
- }
- return ans;
- }
-}
\ No newline at end of file
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.py b/solution/1500-1599/1512.Number of Good Pairs/Solution2.py
deleted file mode 100644
index fd0d38656d18d..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.py
+++ /dev/null
@@ -1,4 +0,0 @@
-class Solution:
- def numIdenticalPairs(self, nums: List[int]) -> int:
- cnt = Counter(nums)
- return sum(v * (v - 1) for v in cnt.values()) >> 1
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.rs b/solution/1500-1599/1512.Number of Good Pairs/Solution2.rs
deleted file mode 100644
index 555ad4f47400f..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-impl Solution {
- pub fn num_identical_pairs(nums: Vec) -> i32 {
- let mut cnt = [0; 101];
- for &num in nums.iter() {
- cnt[num as usize] += 1;
- }
- let mut ans = 0;
- for &v in cnt.iter() {
- ans += (v * (v - 1)) / 2;
- }
- ans
- }
-}
diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution2.ts b/solution/1500-1599/1512.Number of Good Pairs/Solution2.ts
deleted file mode 100644
index bf835619d39e1..0000000000000
--- a/solution/1500-1599/1512.Number of Good Pairs/Solution2.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-function numIdenticalPairs(nums: number[]): number {
- const cnt = new Array(101).fill(0);
- for (const x of nums) {
- ++cnt[x];
- }
- let ans = 0;
- for (const v of cnt) {
- ans += v * (v - 1);
- }
- return ans >> 1;
-}