-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
王洋
committed
Jul 26, 2024
1 parent
1488802
commit 18d97e7
Showing
10 changed files
with
371 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 是所有分区方案的最小值。 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
Oops, something went wrong.