Skip to content

Commit 33e0242

Browse files
committed
一刷697
1 parent 9e670f4 commit 33e0242

File tree

5 files changed

+108
-34
lines changed

5 files changed

+108
-34
lines changed

README.adoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,13 +4904,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
49044904
//|{doc_base_url}/0696-count-binary-substrings.adoc[题解]
49054905
//|Easy
49064906
//|
4907-
//
4908-
//|{counter:codes}
4909-
//|{leetcode_base_url}/degree-of-an-array/[697. Degree of an Array^]
4910-
//|{source_base_url}/_0697_DegreeOfAnArray.java[Java]
4911-
//|{doc_base_url}/0697-degree-of-an-array.adoc[题解]
4912-
//|Easy
4913-
//|
4907+
4908+
|{counter:codes}
4909+
|{leetcode_base_url}/degree-of-an-array/[697. Degree of an Array^]
4910+
|{source_base_url}/_0697_DegreeOfAnArray.java[Java]
4911+
|{doc_base_url}/0697-degree-of-an-array.adoc[题解]
4912+
|Easy
4913+
|
49144914

49154915
|{counter:codes}
49164916
|{leetcode_base_url}/partition-to-k-equal-sum-subsets/[698. Partition to K Equal Sum Subsets^]

docs/0697-degree-of-an-array.adoc

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
11
[#0697-degree-of-an-array]
2-
= 697. Degree of an Array
2+
= 697. 数组的度
33

4-
{leetcode}/problems/degree-of-an-array/[LeetCode - Degree of an Array^]
4+
https://leetcode.cn/problems/degree-of-an-array/[LeetCode - 697. 数组的度 ^]
55

6-
Given a non-empty array of non-negative integers `nums`, the *degree* of this array is defined as the maximum frequency of any one of its elements.
7-
Your task is to find the smallest possible length of a (contiguous) subarray of `nums`, that has the same degree as `nums`.
6+
给定一个非空且只包含非负数的整数数组 `nums`,数组的 *度* 的定义是指数组里任一元素出现频数的最大值。
87

9-
*Example 1:*
8+
你的任务是在 `nums` 中找到与 `nums` 拥有相同大小的度的最短连续子数组,返回其长度。
109

10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [1, 2, 2, 3, 1]
15-
*Output:* 2
16-
*Explanation:*
17-
The input array has a degree of 2 because both elements 1 and 2 appear twice.
18-
Of the subarrays that have the same degree:
12+
....
13+
输入:nums = [1,2,2,3,1]
14+
输出:2
15+
解释:
16+
输入数组的度是 2 ,因为元素 1 和 2 的出现频数最大,均为 2 。
17+
连续子数组里面拥有相同度的有如下所示:
1918
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
20-
The shortest length is 2. So return 2.
21-
----
22-
19+
最短连续子数组 [2, 2] 的长度为 2 ,所以返回 2 。
20+
....
2321

22+
*示例 2:*
2423

25-
*Example 2:*
24+
....
25+
输入:nums = [1,2,2,3,1,4,2]
26+
输出:6
27+
解释:
28+
数组的度是 3 ,因为元素 2 重复出现 3 次。
29+
所以 [2,2,3,1,4,2] 是最短子数组,因此返回 6 。
30+
....
2631

32+
*提示:*
2733

28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* [1,2,2,3,1,4,2]
31-
*Output:* 6
32-
----
33-
34+
* `nums.length``1``50,000` 范围内。
35+
* `nums[i]` 是一个在 `0``49,999` 范围内的整数。
3436
35-
*Note:*
36-
. `nums.length` will be between 1 and 50,000.</li>
37-
. `nums[i]` will be an integer between 0 and 49,999.</li>
3837
38+
== 思路分析
3939

4040

4141
[[src-0697]]
42+
[tabs]
43+
====
44+
一刷::
45+
+
46+
--
4247
[{java_src_attr}]
4348
----
4449
include::{sourcedir}/_0697_DegreeOfAnArray.java[tag=answer]
4550
----
51+
--
52+
53+
// 二刷::
54+
// +
55+
// --
56+
// [{java_src_attr}]
57+
// ----
58+
// include::{sourcedir}/_0697_DegreeOfAnArray_2.java[tag=answer]
59+
// ----
60+
// --
61+
====
62+
63+
64+
== 参考资料
65+
4666

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ include::0695-max-area-of-island.adoc[leveloffset=+1]
14761476

14771477
// include::0696-count-binary-substrings.adoc[leveloffset=+1]
14781478

1479-
// include::0697-degree-of-an-array.adoc[leveloffset=+1]
1479+
include::0697-degree-of-an-array.adoc[leveloffset=+1]
14801480

14811481
include::0698-partition-to-k-equal-sum-subsets.adoc[leveloffset=+1]
14821482

logbook/202503.adoc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,15 @@ endif::[]
544544
|✅ 贪心算法。最高位尽可能跟后面的最大数字进行交换。
545545

546546
|{counter:codes2503}
547-
|{leetcode_base_url}/two-sum-ii-input-array-is-sorted/[167. Two Sum II - Input array is sorted^]
547+
|{leetcode_base_url}/two-sum-ii-input-array-is-sorted/[167. 两数之和 II - 输入有序数组^]
548548
|{doc_base_url}/0167-two-sum-ii-input-array-is-sorted.adoc[题解]
549549
|✅ 双指针
550550

551+
|{counter:codes2503}
552+
|{leetcode_base_url}/degree-of-an-array/[697. 数组的度^]
553+
|{doc_base_url}/0697-degree-of-an-array.adoc[题解]
554+
|✅
555+
551556
|===
552557

553558
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _0697_DegreeOfAnArray {
9+
// tag::answer[]
10+
11+
/**
12+
* @author D瓜哥 · https://www.diguage.com
13+
* @since 2025-04-29 09:15:49
14+
*/
15+
public int findShortestSubArray(int[] nums) {
16+
Map<Integer, List<Integer>> numToIndex = new HashMap<>();
17+
Map<Integer, Integer> indexToDegre = new HashMap<>();
18+
int max = 0;
19+
20+
for (int i = 0; i < nums.length; i++) {
21+
int num = nums[i];
22+
List<Integer> index = numToIndex.get(num);
23+
if (index == null) {
24+
index = new ArrayList<>();
25+
index.add(i);
26+
index.add(i);
27+
numToIndex.put(num, index);
28+
} else {
29+
index.set(1, i);
30+
}
31+
int count = indexToDegre.getOrDefault(num, 0) + 1;
32+
max = Math.max(max, count);
33+
indexToDegre.put(num, count);
34+
}
35+
int minLength = Integer.MAX_VALUE;
36+
for (Map.Entry<Integer, Integer> entry : indexToDegre.entrySet()) {
37+
if (max == entry.getValue()) {
38+
List<Integer> index = numToIndex.get(entry.getKey());
39+
minLength = Math.min(minLength, index.get(1) - index.get(0));
40+
}
41+
}
42+
return minLength + 1;
43+
}
44+
// end::answer[]
45+
public static void main(String[] args) {
46+
new _0697_DegreeOfAnArray()
47+
.findShortestSubArray(new int[]{2, 1, 1, 2, 1, 3, 3, 3, 1, 3, 1, 3, 2});
48+
}
49+
}

0 commit comments

Comments
 (0)