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 <= 200001 <= A[i] <= A.length1 <= 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 ...
随机推荐
- 元类编程--__get__ __set__属性描述符
from datetime import date, datetime import numbers class IntField: #数据描述符,实现以下任意一个,都会变为属性描述符 def __g ...
- Linux下相关性能指标数据采集
1)如何查看某个服务进程所占本机物理内存及百分比情况?可以使用Linux命令行查看,也可以使用脚本实现.如下以查看本机kube-nginx服务进程为例: [root@kevin ~]# pidof k ...
- 【转】provisional headers are shown 知多少
前言 请求里面provisional headers are shown(显示临时报头) 出现的情况很多,但原因是多样的.如果你去直接匹配关键字搜索,得到的结果可能与你自己的情况大相径庭.网上大部分都 ...
- httpd基于域名不同的虚拟主机配置
apache2.2.x版本 1. 注释主配置文件/etc/httpd/conf/httpd.conf中的 DoucumentRoot #DocumentRoot "/var/www/html ...
- PAT_B1013
这道题就是一道打印素数表的题目,本人使用的是筛选法,用bool数组记录是否为素数,每一次筛掉本轮数字的倍数,如果当前数字bool数组对应位置为false,则为素数. 这道题的坑是:你不知道最大第100 ...
- Topshelf 使用
前言 在写后台代码的过程中,经常会遇到要写一些单独的服务.以前呢,直接用的是 .NET 下的 “Windows 服务” 控件开发的. 这个传统的控件开发起来很不方面,使用也不友好.发现有用 Topsh ...
- 网站入侵工具 SQL注入神器
0x 00 前言 SQLMAP 0x 01 注入原理 不说了 *****************************************结束分割线********** ...
- 常见的HTML5语义化标签
<title>:页面主体内容.<hn>:h1~h6,分级标题,<h1> 与 <title> 协调有利于搜索引擎优化.<ul>:无序列表. ...
- C# 调用cmd执行命令
private void CmdRun_Click(object sender, EventArgs e) { Process p = new Process(); ...
- 最短路--SPFA及其优化
SPFA Shortest Path Faster Algorithm 最短路径最快算法 算法思想 SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路 ...