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 ...
随机推荐
- ie6下固定位置的实现
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python 多线程 ping
python 多线程 ping 多线程操作可按如下例子实现 #!/usr/bin/env python #encoding: utf8 import subprocess from threading ...
- Linux-yum只下载不安装
通过yum命令只下载rpm包不安装 经常遇到服务器没有网络的情况下部署环境,或者创建自己的 yum 仓库等.每次都是在网上搜搜搜,都是五花八门,自己整理了下自己用到的以下三种方式,这里没有太多废话,只 ...
- BZOJ 3000: Big Number (数学)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=3000 题解: 首先n很大,O(n)跑不过,那么就要用一些高端 而且没听过 的东西——sti ...
- 让pip使用python3而不是python2
步骤 ln -sf $(which pip3) $(which pip)
- Floyed-Warshall【弗洛伊德算法】
首先介绍一下有关最短路径的知识 从某顶点出发,沿图的边到达另一顶点所经过的路径中,各边上权值之和最小的一条路径叫做最短路径.解决最短路的问题有以下算法,Dijkstra算法,Bellman-Ford算 ...
- poj 2886 "Who Gets The Most Candies?"(树状数组)
传送门 参考资料: [1]:http://www.hankcs.com/program/algorithm/poj-2886-who-gets-the-most-candies.html 题意: 抢糖 ...
- ansible迭代/迭代嵌套/同步异步/特殊topic说明
tasks直接举例说明: ---- host: docker remote_user: root gather_facts: yes serial: 3 #表示同一时间控制主机数量(值可以是数值 ...
- 洛伦兹曲线(Lorenz curve)提升指数、提升表和提升图
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- freetype之PC机体验
目录 freetype之PC机体验 引入 中文教程 官方教程 代码结构 字体概念 PC上安装 官方例子 宽字符保存显示中文 坐标框架体系 字符坐标信息获取 title: freetype之PC机体验 ...