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 ...
随机推荐
- [ffmpeg] 滤波
ffmpeg中有很多已经实现好的滤波器,这些滤波器的实现位于libavfilter目录之下,用户需要进行滤波时,就是是调用这些滤波器来实现的.ffmpeg对于调用滤波器有一整套的调用机制. 基本结构 ...
- sql server查看表是否死锁
1,查看那个表死锁 select object_name(resource_associated_entity_id) as tableName, request_session_id as pid ...
- C++ 中 double、 long double、long 和 long long
double 属于浮点类型,具体为双精度浮点类型,通常为 IEEE-754 64 位浮点类型. long double 也属于浮点类型,具体为扩展精度浮点类型,其精度不低于double,具体由编译器和 ...
- 江西财经大学第二届程序设计竞赛同步赛 H大时钟 (扩展欧几里得)
链接:https://ac.nowcoder.com/acm/contest/635/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- opencv + cuda编译
#获取最新代码git clone "https://github.com/opencv/opencv.git" #build目录mkdir buildcd build #使用ccm ...
- Sublime Text3—设置快捷键打开浏览器
在不同浏览器查看代码效果可谓是家常便饭,所以用不同快捷键对应打开不同浏览器可以大大提高工作效率. 本篇分享个简单的方法只需二步: 一.安装插件SideBarEnhancements ctrl+shif ...
- Linux核心命令使用方法
一.Linux命令行常用快捷键 ctrl + c cancel 取消当前的操作 ctrl + l (小写字母L) clear(命令)清空当前屏幕 ctrl + d 退出当前用户 ctrl + r 查找 ...
- anacodna/python 安装 tensorflow
study from : https://www.cnblogs.com/HongjianChen/p/8385547.html 执行1-6 7 安装jupyter 每次使用tensorflow,都要 ...
- 服务器配置 ssl 证书
最近因为公司的 服务器 ssl证书即将到期(服务器 和 ssl证书管理都在 腾讯云上), 所以为了能顺利的 重新申请 ssl证书 ,我和小伙伴 在他的个人服务器上尝试了一波(我们居然都不会 ...) ...
- Linux安装Oracle JDK替换OpenJDK详解
转自http://www.lsychina.com/howto_install_oracle-jdk_replace_openjdk_on_linux.html 众所周知,由于Oracle公司的JDK ...