• 문제 링크
Degree of an Array - LeetCode
Can you solve this real interview question? Degree of an Array - 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. Your task is to find the smallest possible leng
leetcode.com
• 풀이 코드
public class Solution {
public int findShortestSubArray(int[] nums) {
int n = 0;
for (int num : nums) if (num > n) n = num;
int[] cnt = new int[n + 1], anc = new int[n + 1];
int max = 0, len = nums.length;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
if (++cnt[num] == 1) anc[num] = i;
if (cnt[num] > max) {
max = cnt[num];
len = i - anc[num] + 1;
} else if (cnt[num] == max) len = Math.min(len, i - anc[num] + 1);
}
return len;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 703. Kth Largest Element in a Stream - Java (0) | 2026.09.28 |
|---|---|
| [LeetCode] 700. Search in a Binary Search Tree - Java (0) | 2026.09.27 |
| [LeetCode] 696. Count Binary Substrings - Java (0) | 2026.09.25 |
| [LeetCode] 693. Binary Number with Alternating Bits - Java (0) | 2026.09.24 |
| [LeetCode] 682. Baseball Game - Java (0) | 2026.09.23 |
댓글