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 ...
随机推荐
- c# 串行化事件
- C++——构造函数 constructor
What is constructor C++中,如果你想要创建一个object,有一个函数会自动被调用(不需要programmer显式调用 ),这个函数就是constructor; construc ...
- Linux VPS搭建蚂蚁笔记Leanote私有云笔记存储平台
一.基础环境LNMP 安装nginx: yum install epel-release -y yum install nginx -y # 启动 nginx systemctl start ngin ...
- WebApi 接口恶意请求限制
为了防止爬虫以及恶意请求,我们适当的为API增加一个请求限制 WebApiThrottle限流框架 WebApiThrottle支持自定义配置各种限流策略.可以根据不同场景配置多个不同的限制 ...
- C# 中自定义配置
微软在ConfigurationManager类里面为我们提供了AppSetting和ConnectionStrings 两个常用配置, 但是有时候我们需要自定的配置,例如 <image lef ...
- JDK源码那些事儿之并发ConcurrentHashMap下篇
上一篇文章已经就ConcurrentHashMap进行了部分说明,介绍了其中涉及的常量和变量的含义,有些部分需要结合方法源码来理解,今天这篇文章就继续讲解并发ConcurrentHashMap 前言 ...
- js 面向对象之属性描述符
上回介绍了面向对象之构造器属性.这次介绍下属性描述符 遍历对象属性 let person = {name: "lisi"} for (key in person) { consol ...
- 60、springmvc-异步请求-返回Callable
60.springmvc-异步请求-返回Callable @Controller public class AsyncController { @RequestMapping("async0 ...
- [AngularJS] Extend Controller
/** * Module definition and dependencies */ angular.module('App.Child', []) /** * Component */ .comp ...
- SQL数据库调优
1.使用With As做数据库递归,调优树形表结构 例如:设计表结构简化如:ID.ParentID.Name:这里的ParentID就是这个表本身的某个ID WITH cte AS ( UNION A ...