Skip to content

Commit 02bb610

Browse files
committed
feat: leetcode 1636
1 parent 28a2168 commit 02bb610

File tree

8 files changed

+132
-1
lines changed

8 files changed

+132
-1
lines changed

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/-进度:498-green" alt="进度:498">
5+
<img src="https://img.shields.io/badge/-进度:499-green" alt="进度:499">
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>

assets/data/categories.json

+6
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,12 @@
24292429
"path": "./problemset/find-servers-that-handled-most-number-of-requests/README.md",
24302430
"difficulty": "困难"
24312431
},
2432+
{
2433+
"id": "1636",
2434+
"title": "按照频率将数组升序排序",
2435+
"path": "./problemset/sort-array-by-increasing-frequency/README.md",
2436+
"difficulty": "简单"
2437+
},
24322438
{
24332439
"id": "1656",
24342440
"title": "涉及有序流",

assets/data/topics.json

+10
Original file line numberDiff line numberDiff line change
@@ -4599,6 +4599,16 @@
45994599
"url": "https://leetcode.cn/problems/largest-substring-between-two-equal-characters/",
46004600
"path": "./problemset/largest-substring-between-two-equal-characters/README.md"
46014601
},
4602+
{
4603+
"id": "1636",
4604+
"title": {
4605+
"cn": "按照频率将数组升序排序",
4606+
"en": "sort-array-by-increasing-frequency"
4607+
},
4608+
"difficulty": "简单",
4609+
"url": "https://leetcode.cn/problems/sort-array-by-increasing-frequency/",
4610+
"path": "./problemset/sort-array-by-increasing-frequency/README.md"
4611+
},
46024612
{
46034613
"id": "1656",
46044614
"title": {

assets/docs/CATEGORIES.md

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
| 1582. [二进制矩阵中的特殊位置](../../problemset/special-positions-in-a-binary-matrix/README.md) | 简单 |
447447
| 1592. [重新排列单词间的空格](../../problemset/rearrange-spaces-between-words/README.md) | 简单 |
448448
| 1606. [找到处理最多请求的服务器](../../problemset/find-servers-that-handled-most-number-of-requests/README.md) | 困难 |
449+
| 1636. [按照频率将数组升序排序](../../problemset/sort-array-by-increasing-frequency/README.md) | 简单 |
449450
| 1656. [涉及有序流](../../problemset/design-an-ordered-stream/README.md) | 简单 |
450451
| 1672. [最富有客户的资产总量](../../problemset/richest-customer-wealth/README.md) | 简单 |
451452
| 1706. [球会落何处](../../problemset/where-will-the-ball-fall/README.md) | 中等 |

assets/docs/TOPICS.md

+2
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,8 @@
920920

921921
[1624. 两个相同字符之间的最长子字符串](../../problemset/largest-substring-between-two-equal-characters/README.md)
922922

923+
[1636. 按照频率将数组升序排序](../../problemset/sort-array-by-increasing-frequency/README.md)
924+
923925
[1656. 涉及有序流](../../problemset/design-an-ordered-stream/README.md)
924926

925927
[1663. 具有给定数值的最小字符串](../../problemset/smallest-string-with-a-given-numeric-value/README.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 按照频率将数组升序排序
2+
3+
> 难度:简单
4+
>
5+
> https://leetcode.cn/problems/sort-array-by-increasing-frequency/
6+
7+
## 题目
8+
9+
给你一个整数数组 `nums` ,请你将数组按照每个值的频率 **升序** 排序。如果有多个值的频率相同,请你按照数值本身将它们 **降序** 排序。 
10+
11+
请你返回排序后的数组。
12+
13+
### 示例
14+
15+
#### 示例 1:
16+
17+
```
18+
输入:nums = [1,1,2,2,2,3]
19+
输出:[3,1,1,2,2,2]
20+
解释:'3' 频率为 1,'1' 频率为 2,'2' 频率为 3 。
21+
```
22+
23+
#### 示例 2:
24+
25+
```
26+
输入:nums = [2,3,1,3,2]
27+
输出:[1,3,3,2,2]
28+
解释:'2' 和 '3' 频率都为 2 ,所以它们之间按照数值本身降序排序。
29+
```
30+
31+
#### 示例 3:
32+
33+
```
34+
输入:nums = [-1,1,-6,4,5,-6,1,4,1]
35+
输出:[5,-1,4,4,-6,-6,1,1,1]
36+
```
37+
38+
## 解题
39+
40+
```ts
41+
/**
42+
* 模拟
43+
* @desc 时间复杂度 O(NlogN) 空间复杂度 O(N)
44+
* @param nums
45+
* @returns
46+
*/
47+
export function frequencySort(nums: number[]): number[] {
48+
const cnt = new Map<number, number>()
49+
for (const num of nums)
50+
cnt.set(num, (cnt.get(num) || 0) + 1)
51+
52+
const list = [...nums]
53+
list.sort((a, b) => {
54+
const cnt1 = cnt.get(a)!
55+
const cnt2 = cnt.get(b)!
56+
return cnt1 !== cnt2 ? cnt1 - cnt2 : b - a
57+
})
58+
const length = nums.length
59+
for (let i = 0; i < length; i++)
60+
nums[i] = list[i]
61+
62+
return nums
63+
}
64+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { frequencySort } from '.'
3+
4+
describe('按照频率将数组升序排序', () => {
5+
testCase(frequencySort)
6+
})
7+
8+
function testCase(fn: (nums: number[]) => number[]) {
9+
it.each([
10+
[
11+
[1, 1, 2, 2, 2, 3],
12+
[3, 1, 1, 2, 2, 2],
13+
],
14+
[
15+
[2, 3, 1, 3, 2],
16+
[1, 3, 3, 2, 2],
17+
],
18+
[
19+
[-1, 1, -6, 4, 5, -6, 1, 4, 1],
20+
[5, -1, 4, 4, -6, -6, 1, 1, 1],
21+
],
22+
])('示例%#', (nums, expected) => {
23+
expect(fn(nums)).toStrictEqual(expected)
24+
})
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 模拟
3+
* @desc 时间复杂度 O(NlogN) 空间复杂度 O(N)
4+
* @param nums
5+
* @returns
6+
*/
7+
export function frequencySort(nums: number[]): number[] {
8+
const cnt = new Map<number, number>()
9+
for (const num of nums)
10+
cnt.set(num, (cnt.get(num) || 0) + 1)
11+
12+
const list = [...nums]
13+
list.sort((a, b) => {
14+
const cnt1 = cnt.get(a)!
15+
const cnt2 = cnt.get(b)!
16+
return cnt1 !== cnt2 ? cnt1 - cnt2 : b - a
17+
})
18+
const length = nums.length
19+
for (let i = 0; i < length; i++)
20+
nums[i] = list[i]
21+
22+
return nums
23+
}

0 commit comments

Comments
 (0)