LC 992. Subarrays with K Different Integers
Given an array A
of positive integers, call a (contiguous, not necessarily distinct) subarray of A
good if the number of different integers in that subarray is exactly K
.
(For example, [1,2,3,1,2]
has 3
different integers: 1
, 2
, and 3
.)
Return the number of good subarrays of A
.
Example 1:
Input: A = [1,2,1,2,3], K = 2
Output: 7
Explanation: Subarrays formed with exactly 2 different integers: [1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].
Example 2:
Input: A = [1,2,1,3,4], K = 3
Output: 3
Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
Note:
1 <= A.length <= 20000
1 <= A[i] <= A.length
1 <= K <= A.length
class Solution {
public:
int subarraysWithKDistinct(vector<int>& A, int K) {
return atMostK(A, K) - atMostK(A,K-);
}
int atMostK(vector<int>& A, int K) {
int i = ,res = ;
unordered_map<int,int> mp;
for(int j=; j<A.size(); j++) {
if(!mp[A[j]]++) K--;
while(K<){
if(!--mp[A[i]]) K++;
i++;
}
res += j - i + ;
}
return res;
}
};
LC 992. Subarrays with K Different Integers的更多相关文章
- Leetcode 992 Subarrays with K Different Integers
题目链接:https://leetcode.com/problems/subarrays-with-k-different-integers/ 题意:已知一个全为正数的数组A,1<=A.leng ...
- [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 ...
- LC 358. Rearrange String k Distance Apart
Given a non-empty string s and an integer k, rearrange the string such that the same characters are ...
- LeetCode编程训练 - 滑动窗口(Sliding Window)
滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...
- 算法与数据结构基础 - 滑动窗口(Sliding Window)
滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Slide Window 专题
992. Subarrays with K Different Integers 给定一个正整数数组,计算刚好有K个不同数的子数组的个数.(For example, [1,2,3,1,2] has 3 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
随机推荐
- Oracle加密解密
Oracle内部有专门的加密包,可以很方便的对内部数据进行加密(encrypt)和解密(decrypt). 介绍加密包之前,先简单说一下Oracle基本数据类型——RAW类型. RAW,用于保存位串的 ...
- 记录java+testng运行selenium(二)---定义元素类及浏览器
一: 元素类 整体思路: 1. 根据状态可分可见和不可见两种 2. 同一个路径可以查找单个元素或多个元素 3. 获取元素text或者指定的value值 4. selenium对元素操作有两种,一是通过 ...
- 【转】Lombok Pojo默认初始值问题
Lombok以注解形式来简化java代码,提高开发效率.比如我们常用的@Builder.@Data.@AllArgsConstructor.@NoArgsConstructor.@ToString等. ...
- linux下环境管理anaconda3
我之前在centos之安装单独python3.6,大家都知道centos自带python2.7,通过输入python,和python3来控制想要使用python2,或者python3,如今想要要在li ...
- 大数据之路week06--day07(虚拟机的克隆)
1.安装vmware,务必以管理员身份运行 操作系统(CentOS 6.5)的配置 准备工作:虚拟机安装三台linux 本次测试是 centos 6.5,(三台虚拟机的系统时间保持一致) *安装jd ...
- Jquery 操作DOM元素
一.文本输入框: text <input type=”text” value=”99.com” size=12 id=”input1” /> 1.获取文本值: $("#input ...
- 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor
33.[源码]-AOP原理-获取拦截器链-MethodInterceptor
- Laravel 报 Nginx 502 : Bad Gateway 错误
1 问题再现1.1 Laravel 6.1.0,在前端数据修改,标题中包含有中文符号:.或<>,Nginx 报502 错误. 1.2 在 TEXT 字段中保存则不存在此问题 2 排查过程 ...
- 四十六.系统审计 服务安全 Linux安全之打补丁
192.168.4.51 1.部署audit监控文件 使用audit监控/etc/ssh/sshd_config 当该文件发生任何变化即记录日志 通过手动和ausearch工具查看日志内容 1 ...
- 二十七. Keepalived热备 Keepalived+LVS 、 HAProxy服务器
1.Keepalived高可用服务器 proxy:192.168.4.5(客户端主机) web1:192.168.4.100(Web服务器,部署Keepalived高可用软件) web2:192.16 ...