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: 12, 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. 1 <= A.length <= 20000
  2. 1 <= A[i] <= A.length
  3. 1 <= K <= A.length
Runtime: 128 ms, faster than 100.00% of C++ online submissions for Subarrays with K Different Integers.
Memory Usage: 25 MB, less than 100.00% of C++ online submissions for Subarrays with K Different Integers.
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的更多相关文章

  1. Leetcode 992 Subarrays with K Different Integers

    题目链接:https://leetcode.com/problems/subarrays-with-k-different-integers/ 题意:已知一个全为正数的数组A,1<=A.leng ...

  2. [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 ...

  3. 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 ...

  4. LeetCode编程训练 - 滑动窗口(Sliding Window)

    滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...

  5. 算法与数据结构基础 - 滑动窗口(Sliding Window)

    滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...

  6. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

  7. Slide Window 专题

    992. Subarrays with K Different Integers 给定一个正整数数组,计算刚好有K个不同数的子数组的个数.(For example, [1,2,3,1,2] has 3 ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. [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 ...

随机推荐

  1. 第十七篇:WEB服务器之HTTP协议

    本篇主要为为了实现WEB服务器,其中包含了HTTP协议的理解,以及TCP的三次握手.四次挥手等方面相关知识,同时还包含了关于web浏览器与服务器之间的通信过程. 一.web浏览器 通常在我们上网时会在 ...

  2. NTFS文件系统概述

    NTFS简介 NTFS是Windows NT家族1的限制级专用的文件系统2.Win95.Win98识别不了NTFS,只有支持NT内核的OS才能识别NTFS文件系统.当前,NTFS取代了老式的FAT文件 ...

  3. 剑指Offer(二十七):字符串的排列

    剑指Offer(二十七):字符串的排列 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  4. MySQL之三张表关联

    创建三张表 1.学生表 mysql> create table students( sid int primary key auto_increment, sname ) not null, a ...

  5. 转,sql server update set from inner 批量修改的使用

    SQL update select结合语句详解及应用   QL update select语句 最常用的update语法是: 1 2 UPDATE TABLE_NAME SET column_name ...

  6. MongoDB 分片管理(四)数据均衡

    通常来说,MongoDB会自动处理数据均衡. 1.1 集群分片的块的均衡 注意,均衡器只使用块的数量,而非数据大小,来作为衡量分片间是否均衡的指标. 1.2 均衡器 1.执行所有数据库管理操作前,都应 ...

  7. The backup set holds a backup of a database other than the existing ‘dbName’ database

     [Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than t ...

  8. learning scala pattern matching 03

    code: package com.aura.scala.day01 object patternMatching03 { //当不同类型对象需要调用不同方法时,仅匹配类型的模式非常有用. def g ...

  9. hihoCoder 2 * problem

    1792 模拟,转化为二进制后逐位比较 1819 线段树维护区间加 维护每个数加了多少 每次弹出栈顶元素后栈顶位置注意清空 1792 #include <iostream> #includ ...

  10. luogu P3386 【模板】二分图匹配

    二次联通门 : luogu P3386 [模板]二分图匹配 /* luogu P3386 [模板]二分图匹配 最大流 设置源点,汇点,连到每条边上 跑一边最大流即可 */ #include <i ...