Skip to content

feat: update solutions to lc problems: No.1508,1512 #3579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ tags:

<pre>
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>输出:</strong>13
<strong>输出:</strong>13
<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 。
</pre>

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

<!-- solution:start -->

### 方法一:排序
### 方法一:模拟

按照题意生成 `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$ 为题目给定的数组长度。

<!-- tabs:start -->

@@ -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;
}
```

Original file line number Diff line number Diff line change
@@ -30,8 +30,8 @@ tags:

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>Output:</strong> 13
<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.
<strong>Output:</strong> 13
<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.
</pre>

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

<!-- solution:start -->

### 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.

<!-- tabs:start -->

@@ -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;
}
```

Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
161 changes: 27 additions & 134 deletions solution/1500-1599/1512.Number of Good Pairs/README.md
Original file line number Diff line number Diff line change
@@ -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>) -> 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) {
<!-- solution:end -->
<!-- solution:start -->
### 方法二
<!-- tabs:start -->
#### 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<int>& 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>) -> 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;
}
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->
167 changes: 32 additions & 135 deletions solution/1500-1599/1512.Number of Good Pairs/README_EN.md
Original file line number Diff line number Diff line change
@@ -63,7 +63,11 @@ tags:

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

@@ -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>) -> 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:end -->
<!-- solution:start -->
### Solution 2
<!-- tabs:start -->
#### 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<int>& 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>) -> 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;
}
```
<!-- tabs:end -->
<!-- solution:end -->
<!-- problem:end -->
12 changes: 12 additions & 0 deletions solution/1500-1599/1512.Number of Good Pairs/Solution.js
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 4 additions & 4 deletions solution/1500-1599/1512.Number of Good Pairs/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
impl Solution {
pub fn num_identical_pairs(nums: Vec<i32>) -> 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
}
2 changes: 1 addition & 1 deletion solution/1500-1599/1512.Number of Good Pairs/Solution.ts
Original file line number Diff line number Diff line change
@@ -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]++;
11 changes: 0 additions & 11 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.c

This file was deleted.

14 changes: 0 additions & 14 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.go

This file was deleted.

13 changes: 0 additions & 13 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.java

This file was deleted.

4 changes: 0 additions & 4 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.py

This file was deleted.

13 changes: 0 additions & 13 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.rs

This file was deleted.

11 changes: 0 additions & 11 deletions solution/1500-1599/1512.Number of Good Pairs/Solution2.ts

This file was deleted.