[LeetCode, not perfect] 992. Subarrays with K Different Integers

K个不同整数的子数组。题意是给定一个正整数数组 A,如果 A 的某个子数组中不同整数的个数恰好为 K,则称 A 的这个连续、不一定独立的子数组为好子数组。返回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].

思路依然是滑动窗口,但是这一题我只理解一个没有普适性的解法,之后再做修正。回忆前面做的滑动窗口的题目,有求过子数组里面最多K个不同元素的题(340),所以这个解法的思路是求子数组里面最多K个不同元素的子数组的数量子数组里面最多K - 1个不同元素的子数组的数量。如果理解这个思路是怎么来的,代码就非常简单了,直接套用之前的模板即可。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int subarraysWithKDistinct(int[] A, int K) {
 3         return atMostK(A, K) - atMostK(A, K - 1);
 4     }
 5
 6     private int atMostK(int[] A, int K) {
 7         int i = 0;
 8         int res = 0;
 9         Map<Integer, Integer> map = new HashMap<>();
10         for (int j = 0; j < A.length; j++) {
11             if (map.getOrDefault(A[j], 0) == 0) {
12                 K--;
13             }
14             map.put(A[j], map.getOrDefault(A[j], 0) + 1);
15             while (K < 0) {
16                 map.put(A[i], map.get(A[i]) - 1);
17                 if (map.get(A[i]) == 0) {
18                     K++;
19                 }
20                 i++;
21             }
22             res += j - i + 1;
23         }
24         return res;
25     }
26 }

JavaScript实现

 1 /**
 2  * @param {number[]} A
 3  * @param {number} K
 4  * @return {number}
 5  */
 6 var subarraysWithKDistinct = function (A, K) {
 7     function atMostK(k) {
 8         let l = 0;
 9         let res = 0;
10         const count = {};
11
12         for (let r = 0; r < A.length; r++) {
13             if (count[A[r]] == null) count[A[r]] = 0;
14             if (count[A[r]] === 0) k--;
15             count[A[r]]++;
16
17             while (k < 0) {
18                 count[A[l]]--;
19                 if (count[A[l]] === 0) k++;
20                 l++;
21             }
22             res += r - l + 1;
23         }
24         return res;
25     }
26     return atMostK(K) - atMostK(K - 1);
27 };

原文地址:https://www.cnblogs.com/aaronliu1991/p/12630196.html

时间: 2024-11-03 16:26:33

[LeetCode, not perfect] 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 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 nu

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

LeetCode:数组中的第K个最大元素【215】

LeetCode:数组中的第K个最大元素[215] 题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4 说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度. 题目分析 我们主要来学习一个新的集合类型--优先队列.优先队列作用是保证每次取

LeetCode 第 371 题 (Sum of Two Integers)

LeetCode 第 371 题 (Sum of Two Integers) Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 不用加减法计算两个整数的和.这道题其实是考察一些基本的布尔代数知识.我们知道,二进制表示时: 0 + 0 = 00 1 + 0 = 01 0 +

【Leetcode】Perfect Squares

题目链接:https://leetcode.com/problems/perfect-squares/ 题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 

Leetcode 713 Subarray Product Less Than K (子数组乘积大于K的个数) (双指针)

目录 问题描述 例子 方法 Leetcode 713 问题描述 Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. 例子 Example 1: Input: nums = [10, 5, 2, 6], k

[LeetCode] Valid Perfect Square 检验完全平方数

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

[LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串

Given a string, find the length of the longest substring T that contains at most k distinct characters. For example, Given s = “eceba” and k = 2, T is "ece" which its length is 3. 这道题是之前那道Longest Substring with At Most Two Distinct Characters的拓展