Leetcode 992 Subarrays with K Different Integers
题目链接:https://leetcode.com/problems/subarrays-with-k-different-integers/
题意:已知一个全为正数的数组A,1<=A.length<=20000,1<=A[i]<=A.length,1<=K<=A.length,问A中恰好有K个不同的元素的子数组个数有多少个
思路:对于考虑到数据范围较大,暴力显然不可取。对于每个位置i,如果用min_k[i] 表示起始位置为i的子数组存在k个元素的最小结束位置,用min_k_1[i] 表示起始位置为i的子数组存在k+1个不同的元素的最小结束位置,则对于起始位置为i、存在k个不同元素的的子数组的个数为min_k_1[i]-min_k[i]。
至于如何求min_k[i]呢(和求min_k_1的方法一样)呢,可以维护一个滑动窗口,左边界为i,当滑动窗口不同元素个数小于k时,向右移动右边界,当等于k时,则左边界变为i+1,刚好用来求min_k[i+1],min_k[i+1]>=min_k[i],因此可在线性的时间内求出min_k和min_k_1,滑动窗口的不同元素个数可以用map求(无序的话用inordered_map更好)。
class Solution {
public:
int subarraysWithKDistinct(vector<int>& A, int K) {
if (K > A.size()||K==0)
return 0;int *min_k = new int[A.size()], *min_k_1 = new int[A.size()];
cal_min(A,min_k,A.size(),K); //计算min_k
cal_min(A, min_k_1, A.size(), K + 1); //计算min_k_1
int ans = 0;
for (int i = 0;i < A.size();i++) {
if (min_k_1[i] + min_k[i] == -2) //如果都未更新,则i起始的子数组不同元素个数一定小于K
continue;
else if(min_k_1[i]!=-1) //min_k_1更新
ans += min_k_1[i] - min_k[i];
else ans += A.size() - min_k[i]; //min_k_1未更新,min_k更新,最后一个元素依然长度为K
}
return ans;
}
void cal_min(vector<int>& A,int *Min,int len,int K) {
map<int, int> mp;
memset(Min, -1, 4 * len);
for (int i = 0;i < K;i++)
mp[A[i]]++;
int l = 0, r = K - 1;
while (r < A.size()) {
while (mp.size() == K) {
Min[l] = r;
mp[A[l]]--;
if (mp[A[l]] == 0)
mp.erase(A[l]);
l++;
}
r++;
if (r<A.size())
mp[A[r]]++;
}
}
};
Leetcode 992 Subarrays with K Different Integers的更多相关文章
- LC 992. Subarrays with K Different Integers
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A g ...
- [Swift]LeetCode992. K 个不同整数的子数组 | Subarrays with K Different Integers
Given an array A of positive integers, call a (contiguous, not necessarily distinct) subarray of A g ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- 乘风破浪:LeetCode真题_023_Merge k Sorted Lists
乘风破浪:LeetCode真题_023_Merge k Sorted Lists 一.前言 上次我们学过了合并两个链表,这次我们要合并N个链表要怎么做呢,最先想到的就是转换成2个链表合并的问题,然后解 ...
- LeetCode:移除K位数字【402】
LeetCode:移除K位数字[402] 题目描述 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. nu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)
[LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- SolidWorks装配体
- 存储类&作用域&生命周期&链接属性
链接属性 (1)大家知道程序从源代码到最终可执行程序,经历的过程:编译.链接. (2)编译阶段就是把源代码搞成.o目标文件,目标文件里面有很多符号和代码段.数据段.bss段等分段.符号就是编程中的变量 ...
- Linux 下安装idea,提示svn版本太低问题
在 RedHat 6.5 虚拟机上装了 Idea 2017, 将项目代码从 Windows 共享到虚拟机中,然后 Idea 提示 svn 版本太旧, 上网查资料说 Idea 2018 不支持1.7以下 ...
- found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, or `npm audit` for details
npm 安装包之后,如果出现类似下面的信息 found 12 vulnerabilities (7 moderate, 5 high) run `npm audit fix` to fix them, ...
- openflow控制器和交换机之间的消息
openflow控制器和交换机之间的消息 消息格式 openflow消息由64bit,8个字节组成 Openflow协议数据包由Openflow Header和Openflow Message两部分组 ...
- 第一模块:Python基础(二)
目录 1.变量 常量 2.用户交互和注释 程序交互 注释 字符串 布尔型(bool) 格式化输出 运算符 while 循环 @(开发基础) 1.变量 变量用于存储要在计算机程序中引用和操作的信息.它们 ...
- visp库中解决lapack库的问题
解决的办法是——绕过去,不要用这个库: 使用中发现如下代码抛出异常: //vpTemplateTracker.cpp try { initHessienDesired(I); ptTemplateSu ...
- LOJ#2085 循环之美
解:首先看这个纯循环到底是什么玩意..... 经过一番打表,发现纯循环小数就是分母与进制互质的既约分数. #include <bits/stdc++.h> std::bitset<& ...
- 浅析redis缓存 在spring中的配置 及其简单的使用
一:如果你需要在你的本地项目中配置redis.那么你首先得需要在你的本地安装redis 参考链接[http://www.runoob.com/redis/redis-install.html] 下载r ...
- min-max容斥
这玩意儿一般都是跟概率期望结合的吧,就是下面这个式子(\(max(S)\)代表集合\(S\)中的最大值,\(min(S)\)同理): \[max(S)=\sum\limits_{T\subseteq ...