zoukankan      html  css  js  c++  java
  • Leetcode 992 具有K个不同整数的子数组 (滑动窗口)

    Leetcode 992

    问题描述

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

    方法一

    ** Solution Java **
    ** 54ms, 22.96% **
    ** 43MB, 15.79% **
    class Solution {
        public int subarraysWithKDistinct(int[] A, int K) {
            return atMostK(A, K) - atMostK(A, K - 1);
        }
        private int atMostK(int[] A, int K) {
            int i = 0, res = 0;
            Map<Integer, Integer> map = new HashMap<>();
            for (int j = 0; j < A.length; ++j) {
                if (map.getOrDefault(A[j], 0) == 0)
                    --K;
                map.put(A[j], map.getOrDefault(A[j], 0) + 1);
                while (K < 0) {
                    map.put(A[i], map.get(A[i]) - 1);
                    if (map.get(A[i]) == 0)
                        ++K;
                    ++i;
                }
                res += j - i + 1;
            }
            return res;
        }
    }
    
    ** Solution Python3 **
    ** 636ms, 39.53% **
    ** 16.1MB, 100.00% **
    class Solution:
        def subarraysWithKDistinct(self, A, K):
            return self.atMostK(A, K) - self.atMostK(A, K - 1)
    
        def atMostK(self, A, K):
            count = collections.Counter()
            res = i = 0
            for j in range(len(A)):
                if count[A[j]] == 0: K -= 1
                count[A[j]] += 1
                while K < 0:
                    count[A[i]] -= 1
                    if count[A[i]] == 0: K += 1
                    i += 1
                res += j - i + 1
            return res
    

    方法二

    ** Solution Java **
    ** 4ms, 97.35% **
    ** 42.7MB, 21.05% **
    class Solution {
        public int subarraysWithKDistinct(int[] A, int K) {
            int res = 0, pre = 0;
            int[] map = new int[A.length + 1];
            for (int i = 0, j = 0, cnt = 0; i < A.length; ++i) {
                if (map[A[i]]++ == 0)
                    ++cnt;
                if (cnt > K) {
                    --map[A[j++]];
                    --cnt;
                    pre = 0;
                }
                while (map[A[j]] > 1) {
                    ++pre;
                    --map[A[j++]];
                }
                if (cnt == K)
                    res += pre + 1;
            }
            return res;
        }
    }
    
  • 相关阅读:
    JBPM工作流(四)——管理流程定义
    JBPM工作流(三)——ProcessEngine与Service API
    JBPM工作流(二)——数据库表说明
    JBPM工作流(一)——实现一个简单的工作流例子
    jbpm与spring hibernate struts整合
    SpringMVC12拦截器
    SpringMVC11文件上传
    阅读代码的方法
    关于linux系统的资料
    关于图灵机的介绍(相见恨晚,太赞了)
  • 原文地址:https://www.cnblogs.com/willwuss/p/12508248.html
Copyright © 2011-2022 走看看