[Swift]LeetCode992. K 個不一樣整數的子數組 | Subarrays with K Different Integers

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-onfzgsxi-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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

(For example, [1,2,3,1,2] has 3different integers: 12, and 3.)github

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:spa

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:code

  1. 1 <= A.length <= 20000
  2. 1 <= A[i] <= A.length
  3. 1 <= K <= A.length

給定一個正整數數組 A,若是 A 的某個子數組中不一樣整數的個數剛好爲 K,則稱 A 的這個連續、不必定獨立的子數組爲好子數組orm

(例如,[1,2,3,1,2] 中有 3 個不一樣的整數:12,以及 3。)htm

返回 A 中好子數組的數目。blog

示例 1:

輸出:A = [1,2,1,2,3], K = 2
輸入:7
解釋:剛好由 2 個不一樣整數組成的子數組:[1,2], [2,1], [1,2], [2,3], [1,2,1], [2,1,2], [1,2,1,2].

示例 2:

輸入:A = [1,2,1,3,4], K = 3
輸出:3
解釋:剛好由 3 個不一樣整數組成的子數組:[1,2,1,3], [2,1,3], [1,3,4].

提示:

  1. 1 <= A.length <= 20000
  2. 1 <= A[i] <= A.length
  3. 1 <= K <= A.length

Runtime: 432 ms

Memory Usage: 11.3 MB

 1 class Solution {
 2     func subarraysWithKDistinct(_ A: [Int], _ K: Int) -> Int {
 3         return count(A, K) - count(A, K-1)
 4     }
 5     
 6     func count(_ a:[Int],_ K:Int) ->Int
 7     {
 8         var n:Int = a.count
 9         var f:[Int] = [Int](repeating:0,count:n + 1)
10         var dis:Int = 0
11         var p:Int = 0
12         var ret:Int = 0
13         for i in 0..<n
14         {
15             f[a[i]] += 1
16             if f[a[i]] == 1
17             {
18                 dis += 1
19             }
20             while(dis > K)
21             {
22                 f[a[p]] -= 1
23                 if f[a[p]] == 0
24                 {
25                     dis -= 1
26                 }
27                 p += 1
28             }
29             ret += i-p+1
30         }
31         return ret
32     }
33 }

704ms

 1 class Solution {
 2     func subarraysWithKDistinct(_ A: [Int], _ K: Int) -> Int {
 3         var count = 0
 4         
 5         var dict1: [Int: Int] = [:]
 6         var dict2: [Int: Int] = [:]
 7 
 8         var l1 = 0
 9         var l2 = 0
10         
11         var r = 0
12         
13         while r < A.count {
14             let num = A[r]
15             
16             dict1[num, default: 0] += 1
17             dict2[num, default: 0] += 1
18 
19             while dict1.keys.count > K {
20                 let l1Num = A[l1]
21                 dict1[l1Num]! -= 1
22                 
23                 if dict1[l1Num] == 0 {
24                     dict1[l1Num] = nil
25                 }
26                 
27                 l1 += 1
28             }
29             
30             while dict2.keys.count >= K {
31                 let l2Num = A[l2]
32                 dict2[l2Num]! -= 1
33                 
34                 if dict2[l2Num] == 0 {
35                     dict2[l2Num] = nil
36                 }
37                 
38                 l2 += 1
39             }
40             
41             count += l2 - l1
42 
43             r += 1
44         }
45         
46         return count
47     }
48 }
相關文章
相關標籤/搜索