- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
java解法
- 解题思路
- 目标: 找到数组中出现频率最高的元素,并返回包含该元素的最短子数组的长度。
解题步骤:
记录频率:使用 freqMap 记录每个元素的出现次数。
记录首次出现位置:使用 firstPos 记录每个元素第一次出现的索引。
更新最长频率和最短长度:
如果当前元素的频率超过已知的最大频率,则更新 maxFreq 和对应的最短子数组长度 minLen。
如果频率相等,则比较当前子数组长度和已有的最短子数组长度,取较小值。
最终结果: 返回包含频率最高的元素的最短子数组的长度。
import java.util.HashMap;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println(findMinLength(arr));
}
public static int findMinLength(int[] arr) {
HashMap<Integer, Integer> freqMap = new HashMap<>();
HashMap<Integer, Integer> firstPos = new HashMap<>();
int maxFreq = 0;
int minLen = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
int num = arr[i];
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
if (!firstPos.containsKey(num)) {
firstPos.put(num, i);
}
int freq = freqMap.get(num);
if (freq > maxFreq) {
maxFreq = freq;
minLen = i - firstPos.get(num) + 1;
}
else if (freq == maxFreq) {
minLen = Math.min(minLen, i - firstPos.get(num) + 1);
}
}
return minLen;
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
C++解法
更新中
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
C解法
#include
#include
#include
#define MAX_ERRORS 10000
struct ErrorInfo {
int frequency;
int start;
int end;
};
int findMinLength(int* errors, int n) {
struct ErrorInfo errorMap[MAX_ERRORS] = {0};
int maxFreq = 0;
int minLen = INT_MAX;
for (int i = 0; i < n; i++) {
int error = errors[i];
errorMap[error].frequency++;
if (errorMap[error].frequency == 1) {
errorMap[error].start = i;
errorMap[error].end = i;
} else {
errorMap[error].end = i;
}
}
for (int i = 0; i < MAX_ERRORS; i++) {
if (errorMap[i].frequency > 0) {
int freq = errorMap[i].frequency;
int length = errorMap[i].end - errorMap[i].start + 1;
if (freq > maxFreq || (freq == maxFreq && length < minLen)) {
maxFreq = freq;
minLen = length;
}
}
}
return minLen;
}
int main() {
int n;
scanf("%d", &n);
int* errors = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &errors[i]);
}
printf("%d\n", findMinLength(errors, n));
free(errors);
return 0;
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
JS解法
function getMinSubArray(arr) {
const freq = {};
const positions = {};
for (let i = 0; i < arr.length; i++) {
const num = arr[i];
freq[num] = (freq[num] || 0) + 1;
if (!positions[num]) {
positions[num] = [i, i];
} else {
positions[num][1] = i;
}
}
let maxFreq = 0;
let minLen = Infinity;
for (const [key, count] of Object.entries(freq)) {
const [start, end] = positions[key];
const len = end - start + 1;
if (count > maxFreq || (count === maxFreq && len < minLen)) {
maxFreq = count;
minLen = len;
}
}
return minLen;
}
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let lines = [];
rl.on("line", (line) => {
lines.push(line);
if (lines.length === 2) {
const arr = lines[1].split(" ").map(Number);
console.log(getMinSubArray(arr));
lines = [];
}
});
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
注意:
如果发现代码有用例覆盖不到的情况,欢迎反馈!会在第一时间修正,更新。
解题不易,如对您有帮助,欢迎点赞/收藏
评论记录:
回复评论: