Skip to content

Commit

Permalink
feat: leetcode 2740
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jul 26, 2024
1 parent 1488802 commit 18d97e7
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<!-- TOPICS COUNT START -->
<img src="https://img.shields.io/badge/-进度:181-green" alt="进度:181">
<img src="https://img.shields.io/badge/-进度:182-green" alt="进度:182">
<!-- TOPICS COUNT END -->
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>
Expand Down
12 changes: 12 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@
"path": "./problemset/count-vowel-strings-in-ranges/README.md",
"difficulty": "中等"
},
{
"id": "2740",
"title": "找出分区值",
"path": "./problemset/find-the-value-of-the-partition/README.md",
"difficulty": "中等"
},
{
"id": "剑指 Offer II 076",
"title": "数组中的第 k 大的数字",
Expand Down Expand Up @@ -697,6 +703,12 @@
"path": "./problemset/largest-values-from-labels/README.md",
"difficulty": "中等"
},
{
"id": "2740",
"title": "找出分区值",
"path": "./problemset/find-the-value-of-the-partition/README.md",
"difficulty": "中等"
},
{
"id": "剑指 Offer II 076",
"title": "数组中的第 k 大的数字",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,16 @@
"url": "https://leetcode.cn/problems/modify-graph-edge-weights/",
"path": "./problemset/modify-graph-edge-weights/README.md"
},
{
"id": "2740",
"title": {
"cn": "找出分区值",
"en": "find-the-value-of-the-partition"
},
"difficulty": "中等",
"url": "https://leetcode.cn/problems/find-the-value-of-the-partition/description/?envType=daily-question&envId=2024-07-26",
"path": "./problemset/find-the-value-of-the-partition/README.md"
},
{
"id": "剑指 Offer II 076",
"title": {
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
| 2460. [对数组执行操作](../../problemset/apply-operations-to-an-array/README.md) | 简单 |
| 2475. [数组中不等三元组的数目](../../problemset/number-of-unequal-triplets-in-array/README.md) | 简单 |
| 2559. [统计范围内的元音字符串数](../../problemset/count-vowel-strings-in-ranges/README.md) | 中等 |
| 2740. [找出分区值](../../problemset/find-the-value-of-the-partition/README.md) | 中等 |
| 剑指 Offer II 076. [数组中的第 k 大的数字](../../problemset/xx4gt2/README.md) | 中等 |

## 字符串
Expand Down Expand Up @@ -149,6 +150,7 @@
| 215. [数组中的第K个最大元素](../../problemset/kth-largest-element-in-an-array/README.md) | 中等 |
| 524. [通过删除字母匹配到字典里最长单词](../../problemset/longest-word-in-dictionary-through-deleting/README.md) | 中等 |
| 1090. [受标签影响的最大值](../../problemset/largest-values-from-labels/README.md) | 中等 |
| 2740. [找出分区值](../../problemset/find-the-value-of-the-partition/README.md) | 中等 |
| 剑指 Offer II 076. [数组中的第 k 大的数字](../../problemset/xx4gt2/README.md) | 中等 |

## 数学
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/TOPICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@

[2699. 修改图中的边权](../../problemset/modify-graph-edge-weights/README.md)

[2740. 找出分区值](../../problemset/find-the-value-of-the-partition/README.md)

[剑指 Offer II 076. 数组中的第 k 大的数字](../../problemset/xx4gt2/README.md)

[剑指 Offer II 088. 爬楼梯的最少成本](../../problemset/gzcjip/README.md)
Expand Down
44 changes: 44 additions & 0 deletions problemset/find-the-value-of-the-partition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 找出分区值

> 难度:中等
>
> https://leetcode.cn/problems/find-the-value-of-the-partition/description/?envType=daily-question&envId=2024-07-26
## 题目

给你一个 正 整数数组 `nums`

`nums` 分成两个数组:`nums1``nums2` ,并满足下述条件:

- 数组 `nums` 中的每个元素都属于数组 `nums1` 或数组 `nums2`
- 两个数组都 非空 。
- 分区值 最小 。
分区值的计算方法是 `|max(nums1) - min(nums2)|`

其中,`max(nums1)` 表示数组 `nums1` 中的最大元素,`min(nums2)` 表示数组 `nums2` 中的最小元素。

返回表示分区值的整数。



### 示例 1:
```
输入:nums = [1,3,2,4]
输出:1
解释:可以将数组 nums 分成 nums1 = [1,2] 和 nums2 = [3,4] 。
- 数组 nums1 的最大值等于 2 。
- 数组 nums2 的最小值等于 3 。
分区值等于 |2 - 3| = 1 。
可以证明 1 是所有分区方案的最小值。
```

### 示例 2:
```
输入:nums = [100,1,10]
输出:9
解释:可以将数组 nums 分成 nums1 = [10] 和 nums2 = [100,1] 。
- 数组 nums1 的最大值等于 10 。
- 数组 nums2 的最小值等于 1 。
分区值等于 |10 - 1| = 9 。
可以证明 9 是所有分区方案的最小值。
```
16 changes: 16 additions & 0 deletions problemset/find-the-value-of-the-partition/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from 'vitest'
import { findValueOfPartition } from '.'

describe('找出分区值', () => {
testCase(findValueOfPartition)
})

function testCase(fn: (nums: number[]) => number) {
it.each([
// test cases
[[1, 3, 2, 4], 1],
[[100, 1, 10], 9],
])('示例%#', (nums, expected) => {
expect(fn(nums)).toBe(expected)
})
}
12 changes: 12 additions & 0 deletions problemset/find-the-value-of-the-partition/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { arraySort } from 'utils/array/array'
export function findValueOfPartition(nums: number[]): number {
const sortNums = arraySort.quickSort(nums);
let min = -1; // 差值
for (let i = 0; i < sortNums.length - 1; i++) {
const x = sortNums[i + 1] - sortNums[i];
if (min === -1 || x < min) {
min = x;
}
}
return min
}
19 changes: 19 additions & 0 deletions utils/array/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {
bubbleSort,
heapSort,
insertionSort,
mergeSort,
quickSort,
selectionSort,
shellSort,
} from './arraySort';

export const arraySort = {
quickSort,
mergeSort,
heapSort,
shellSort,
insertionSort,
selectionSort,
bubbleSort,
}
Loading

0 comments on commit 18d97e7

Please sign in to comment.