697. Degree of an Array

2023-12-21 16:18
文章标签 array degree 697

本文主要是介绍697. Degree of an Array,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

697. 数组的度

给定一个非空且只包含非负数的整数数组 nums, 数组的度的定义是指数组里任一元素出现频数的最大值。

你的任务是找到与 nums 拥有相同大小的度的最短连续子数组,返回其长度。

示例 1:

输入: [1, 2, 2, 3, 1]
输出: 2
解释: 
输入数组的度是2,因为元素1和2的出现频数最大,均为2.
连续子数组里面拥有相同度的有如下所示:
[1, 2, 2, 3, 1], [1, 2, 2, 3], [2, 2, 3, 1], [1, 2, 2], [2, 2, 3], [2, 2]
最短连续子数组[2, 2]的长度为2,所以返回2.

示例 2:

输入: [1,2,2,3,1,4,2]
输出: 6

注意:

  • nums.length 在1到50,000区间范围内。
  • nums[i] 是一个在0到49,999范围内的整数。

解法一

//时间复杂度O(n), 空间复杂度O(n)
class Solution {
public:int findShortestSubArray(vector<int>& nums) {int n = nums.size(), res = 0;unordered_map<int, vector<int>> um;//<value, <count, first, last> >for(int i = 0; i < n; i++) {if(!um.count(nums[i])) um[nums[i]] = {1, i, i};else {um[nums[i]][0]++;um[nums[i]][2] = i;}}int maxCount = 0;for(auto p : um) {if(p.second[0] > maxCount) {maxCount = p.second[0];res = p.second[2] - p.second[1] + 1;}else if(p.second[0] == maxCount)res = min(res, p.second[2] - p.second[1] + 1);}return res;}
};

思路:

思路很直白,对每一个数字,记录其出现次数,第一个及最后一个出现的位置,用一个哈希表um表示,其中um的键是数字,值是个长为3的列表(count, firstIndex, endIndex)。

首选遍历一次,记录元素的信息,存入um。然后遍历um,找出其中出现次数最多last - first最小的元素,返回其last - first + 1,这个值就是拥有相同大小的度的最短连续子数组的长度。

2019/06/23 13:44

这篇关于697. Degree of an Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520660

相关文章

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

做一个问卷考试,标准答案对比用户填写的答案,array_diff 进行差集比对

if( empty(array_diff($answer_mark, $answer)) && empty(array_diff( $answer,$answer_mark))){//用户答题正确}else{// 答题错误} 做一个问卷考试,标准答案对比用户填写的答案,array_diff  进行差集比对   如用户填写的答案变量为answer   标准答案为answer_mark

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

[LeetCode] 215. Kth Largest Element in an Array

题:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not th

MyBatis - 使用foreach迭代List/Array的说明

在 MyBatis 中的 foreach 元素,主要用于迭代 集合数据 以动态生成执行语句;主要有 item、index、collection、open、separator、close 等属性 属性说明         collection:要迭代的数据集对象,必填项         item:迭代出的元素的别名,必填项         index:元素的序号(map时为k

LeetCode - 33. Search in Rotated Sorted Array

33. Search in Rotated Sorted Array  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个数组,这个数组是由两个有序的数组拼接成的(无重复元素),在这个数组中查找元素k的下标. anal

[置顶]后缀数组(suffix array)详解

写在前面 在字符串处理当中,后缀树和后缀数组都是非常有力的工具。 其中后缀树大家了解得比较多,关于后缀数组则很少见于国内的资料。 其实后缀数组是后缀树的一个非常精巧的替代品,它比后缀树容易编程实现, 能够实现后缀树的很多功能而时间复杂度也不太逊色,并且,它比后缀树所占用的空间小很多。 可以说,在信息学竞赛中后缀数组比后缀树要更为实用! 因此在本文中笔者想介绍一下后缀数组的基本概念、构造

【CodeForces】266E More Queries to Array... 线段树

E. More Queries to Array... time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You've got an array, consisting of n

c/c++: warning: ISO C90 forbids variable length array ‘a’

文章目录 介绍C99安全问题类似的alloca安全问题的防护 介绍 https://en.cppreference.com/w/c/language/array @item -Wvla @opindex Wvla @opindex Wno-vla Warn if a variable-length array is used in the code. @option{-Wno-v