Skip to content

Commit 20c932f

Browse files
committed
feat: leetcode 1775
1 parent 10320bf commit 20c932f

File tree

8 files changed

+185
-1
lines changed

8 files changed

+185
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<p align="center">
44
<!-- TOPICS COUNT START -->
5-
<img src="https://img.shields.io/badge/-进度:563-green" alt="进度:563">
5+
<img src="https://img.shields.io/badge/-进度:565-green" alt="进度:565">
66
<!-- TOPICS COUNT END -->
77
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
88
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>

Diff for: assets/data/categories.json

+12
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@
362362
"path": "./problemset/maximum-number-of-balls-in-a-box/README.md",
363363
"difficulty": "简单"
364364
},
365+
{
366+
"id": "1775",
367+
"title": "通过最少操作次数使数组的和相等",
368+
"path": "./problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md",
369+
"difficulty": "中等"
370+
},
365371
{
366372
"id": "1832",
367373
"title": "判断句子是否为全字母句",
@@ -4108,6 +4114,12 @@
41084114
"path": "./problemset/maximum-units-on-a-truck/README.md",
41094115
"difficulty": "简单"
41104116
},
4117+
{
4118+
"id": "1775",
4119+
"title": "通过最少操作次数使数组的和相等",
4120+
"path": "./problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md",
4121+
"difficulty": "中等"
4122+
},
41114123
{
41124124
"id": "2038",
41134125
"title": "如果相邻两个颜色均相同则删除当前颜色",

Diff for: assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,16 @@
52195219
"url": "https://leetcode.cn/problems/closest-dessert-cost/",
52205220
"path": "./problemset/closest-dessert-cost/README.md"
52215221
},
5222+
{
5223+
"id": "1775",
5224+
"title": {
5225+
"cn": "通过最少操作次数使数组的和相等",
5226+
"en": "equal-sum-arrays-with-minimum-number-of-operations"
5227+
},
5228+
"difficulty": "中等",
5229+
"url": "https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/",
5230+
"path": "./problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md"
5231+
},
52225232
{
52235233
"id": "1779",
52245234
"title": {

Diff for: assets/docs/CATEGORIES.md

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
| 1624. [两个相同字符之间的最长子字符串](../../problemset/largest-substring-between-two-equal-characters/README.md) | 简单 |
6565
| 1640. [能否连接形成数组](../../problemset/check-array-formation-through-concatenation/README.md) | 简单 |
6666
| 1742. [盒子中小球的最大数量](../../problemset/maximum-number-of-balls-in-a-box/README.md) | 简单 |
67+
| 1775. [通过最少操作次数使数组的和相等](../../problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md) | 中等 |
6768
| 1832. [判断句子是否为全字母句](../../problemset/check-if-the-sentence-is-pangram/README.md) | 简单 |
6869
| 2006. [差的绝对值为 K 的数对数目](../../problemset/count-number-of-pairs-with-absolute-difference-k/README.md) | 简单 |
6970
| 2013. [检测正方形](../../problemset/detect-squares/README.md) | 中等 |
@@ -755,6 +756,7 @@
755756
| 1413. [逐步求和得到正数的最小值](../../problemset/minimum-value-to-get-positive-step-by-step-sum/README.md) | 简单 |
756757
| 1663. [具有给定数值的最小字符串](../../problemset/smallest-string-with-a-given-numeric-value/README.md) | 中等 |
757758
| 1710. [卡车上的最大单元数](../../problemset/maximum-units-on-a-truck/README.md) | 简单 |
759+
| 1775. [通过最少操作次数使数组的和相等](../../problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md) | 中等 |
758760
| 2038. [如果相邻两个颜色均相同则删除当前颜色](../../problemset/remove-colored-pieces-if-both-neighbors-are-the-same-color/README.md) | 中等 |
759761

760762
## 二叉树

Diff for: assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,8 @@
10441044

10451045
[1774. 最接近目标价格的甜点成本](../../problemset/closest-dessert-cost/README.md)
10461046

1047+
[1775. 通过最少操作次数使数组的和相等](../../problemset/equal-sum-arrays-with-minimum-number-of-operations/README.md)
1048+
10471049
[1779. 找到最近的有相同 X 或 Y 坐标的点](../../problemset/find-nearest-point-that-has-the-same-x-or-y-coordinate/README.md)
10481050

10491051
[1784. 检查二进制字符串字段](../../problemset/check-if-binary-string-has-at-most-one-segment-of-ones/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 通过最少操作次数使数组的和相等
2+
3+
> 难度:中等
4+
>
5+
> https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/
6+
7+
## 题目
8+
9+
给你两个长度可能不等的整数数组 `nums1``nums2` 。两个数组中的所有值都在 `1``6` 之间(包含 `1``6`)。
10+
11+
每次操作中,你可以选择 **任意** 数组中的任意一个整数,将它变成 `1``6` 之间 任意 的值(包含 `1``6`)。
12+
13+
请你返回使 `nums1` 中所有数的和与 `nums2` 中所有数的和相等的最少操作次数。如果无法使两个数组的和相等,请返回 -1 。
14+
15+
### 示例
16+
17+
#### 示例 1:
18+
19+
```
20+
输入:nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
21+
输出:3
22+
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
23+
- 将 nums2[0] 变为 6 。 nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2] 。
24+
- 将 nums1[5] 变为 1 。 nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2] 。
25+
- 将 nums1[2] 变为 2 。 nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2] 。
26+
```
27+
28+
#### 示例 2:
29+
30+
```
31+
输入:nums1 = [1,1,1,1,1,1,1], nums2 = [6]
32+
输出:-1
33+
解释:没有办法减少 nums1 的和或者增加 nums2 的和使二者相等。
34+
```
35+
36+
#### 示例 3:
37+
38+
```
39+
输入:nums1 = [6,6], nums2 = [1]
40+
输出:3
41+
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
42+
- 将 nums1[0] 变为 2 。 nums1 = [2,6], nums2 = [1] 。
43+
- 将 nums1[1] 变为 2 。 nums1 = [2,2], nums2 = [1] 。
44+
- 将 nums2[0] 变为 4 。 nums1 = [2,2], nums2 = [4] 。
45+
```
46+
47+
## 解题
48+
49+
```ts
50+
/**
51+
* 贪心 + 哈希表
52+
* @desc 时间复杂度 O(N+M) 空间复杂度 O(C)
53+
* @param nums1
54+
* @param nums2
55+
* @returns
56+
*/
57+
export function minOperations(nums1: number[], nums2: number[]): number {
58+
const n = nums1.length; const m = nums2.length
59+
if (6 * n < m || 6 * m < n)
60+
return -1
61+
62+
const cnt1: number[] = new Array(7).fill(0)
63+
const cnt2: number[] = new Array(7).fill(0)
64+
let diff = 0
65+
for (const i of nums1) {
66+
++cnt1[i]
67+
diff += i
68+
}
69+
for (const i of nums2) {
70+
++cnt2[i]
71+
diff -= i
72+
}
73+
if (diff === 0)
74+
return 0
75+
76+
if (diff > 0)
77+
return help(cnt2, cnt1, diff)
78+
79+
return help(cnt1, cnt2, -diff)
80+
81+
function help(h1: number[], h2: number[], diff: number) {
82+
const h = new Array(7).fill(0)
83+
for (let i = 1; i < 7; ++i) {
84+
h[6 - i] += h1[i]
85+
h[i - 1] += h2[i]
86+
}
87+
let res = 0
88+
for (let i = 5; i > 0 && diff > 0; --i) {
89+
const t = Math.min(Math.floor((diff + i - 1) / i), h[i])
90+
res += t
91+
diff -= t * i
92+
}
93+
return res
94+
}
95+
}
96+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { minOperations } from '.'
3+
4+
describe('通过最少操作次数使数组的和相等', () => {
5+
testCase(minOperations)
6+
})
7+
8+
function testCase(fn: (nums1: number[], nums2: number[]) => number) {
9+
it.each([
10+
[[1, 2, 3, 4, 5, 6], [1, 1, 2, 2, 2, 2], 3],
11+
[[1, 1, 1, 1, 1, 1, 1], [6], -1],
12+
[[6, 6], [1], 3],
13+
])('示例%#', (nums1, nums2, expected) => {
14+
expect(fn(nums1, nums2)).toBe(expected)
15+
})
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* 贪心 + 哈希表
3+
* @desc 时间复杂度 O(N+M) 空间复杂度 O(C)
4+
* @param nums1
5+
* @param nums2
6+
* @returns
7+
*/
8+
export function minOperations(nums1: number[], nums2: number[]): number {
9+
const n = nums1.length; const m = nums2.length
10+
if (6 * n < m || 6 * m < n)
11+
return -1
12+
13+
const cnt1: number[] = new Array(7).fill(0)
14+
const cnt2: number[] = new Array(7).fill(0)
15+
let diff = 0
16+
for (const i of nums1) {
17+
++cnt1[i]
18+
diff += i
19+
}
20+
for (const i of nums2) {
21+
++cnt2[i]
22+
diff -= i
23+
}
24+
if (diff === 0)
25+
return 0
26+
27+
if (diff > 0)
28+
return help(cnt2, cnt1, diff)
29+
30+
return help(cnt1, cnt2, -diff)
31+
32+
function help(h1: number[], h2: number[], diff: number) {
33+
const h = new Array(7).fill(0)
34+
for (let i = 1; i < 7; ++i) {
35+
h[6 - i] += h1[i]
36+
h[i - 1] += h2[i]
37+
}
38+
let res = 0
39+
for (let i = 5; i > 0 && diff > 0; --i) {
40+
const t = Math.min(Math.floor((diff + i - 1) / i), h[i])
41+
res += t
42+
diff -= t * i
43+
}
44+
return res
45+
}
46+
}

0 commit comments

Comments
 (0)