leetcode 992. K 個不同整數的子數組(滑動窗口)

給定一個正整數數組 A,如果 A 的某個子數組中不同整數的個數恰好為 K,則稱 A 的這個連續、不一定獨立的子數組為好子數組。

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

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

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

解題思路

這題與普通滑動窗口的不同之處在于:要統計[l,r]區間內的滿足不同整數的個數恰好為 K的子數組的個數,按照常規滑動窗口做法需要不斷l++,直到不同整數的個數不為 K,但是這樣的話,新得到的窗口就會丟失一部分元素,后面枚舉r的時候,就會有遺漏。
因此這題不直接移動l,而是選擇維護一個多重的滑動窗口,[l…l1…r]其中[l1,r]區間為滿足不同整數的個數恰好為 K-1的區間,
設區間末尾都是r
因為[l1…r]是恰好只有k-1個不同整數的,所以起始位置在l1前面的子數組,區間內的不同整數的個數都大于k-1
因為[l…r]是恰好只有k個不同整數的,所以起始位置在l前面的子數組,區間內的不同整數的個數都大于k
綜上所述,起始位置處于l和l1的子數組,區間內的不同整數的個數都剛好等于k
因此統計[l,r]區間內的滿足不同整數的個數恰好為 K的子數組的個數,只需要計算l1-l即可。

代碼

class Solution {public int subarraysWithKDistinct(int[] A, int K) {int n=A.length;int[] cnt1=new int[n+1],cnt2=new int[n+1];int n1=0,n2=0,l1=0,l2=0;int l=0,r=0,res=0;while (r<n){if(cnt1[A[r]]==0)n1++;cnt1[A[r]]++;if(cnt2[A[r]]==0)n2++;cnt2[A[r]]++;while (n2>K-1){cnt2[A[l2]]--;if(cnt2[A[l2]]==0)n2--;l2++;}while (n1>K){cnt1[A[l1]]--;if(cnt1[A[l1]]==0)n1--;l1++;}res+=l2-l1;r++;}return res;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391867.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391867.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391867.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

從完整的新手到通過TensorFlow開發人員證書考試

I recently graduated with a bachelor’s degree in Civil Engineering and was all set to start with a Master’s degree in Transportation Engineering this fall. Unfortunately, my plans got pushed to the winter term because of COVID-19. So as of January this y…

微信開發者平臺如何編寫代碼_編寫超級清晰易讀的代碼的初級開發者指南

微信開發者平臺如何編寫代碼Writing code is one thing, but writing clean, readable code is another thing. But what is “clean code?” I’ve created this short clean code for beginners guide to help you on your way to mastering and understanding the art of c…

【轉】PHP面試題總結

PHP面試總結 PHP基礎1&#xff1a;變量的傳值與引用。 2&#xff1a;變量的類型轉換和判斷類型方法。 3&#xff1a;php運算符優先級&#xff0c;一般是寫出運算符的運算結果。 4&#xff1a;PHP中函數傳參&#xff0c;閉包&#xff0c;判斷輸出的echo&#xff0c;print是不是函…

Winform控件WebBrowser與JS腳本交互

1&#xff09;在c#中調用js函數 如果要傳值&#xff0c;則可以定義object[]數組。 具體方法如下例子&#xff1a; 首先在js中定義被c#調用的方法: function Messageaa(message) { alert(message); } 在c#調用js方法Messageaa private void button1_Click(object …

從零開始擼一個Kotlin Demo

####前言 自從google將kotlin作為親兒子后就想用它擼一管app玩玩&#xff0c;由于工作原因一直沒時間下手&#xff0c;直到項目上線后才有了空余時間&#xff0c;期間又由于各種各樣煩人的事斷了一個月&#xff0c;現在終于開發完成項目分為服務器和客戶端&#xff1b;服務器用…

移動平均線ma分析_使用動態移動平均線構建交互式庫存量和價格分析圖

移動平均線ma分析I decided to code out my own stock tracking chart despite a wide array of freely available tools that serve the same purpose. Why? Knowledge gain, it’s fun, and because I recognize that a simple project can generate many new ideas. Even t…

敏捷開發創始人_開發人員和技術創始人如何將他們的想法轉化為UI設計

敏捷開發創始人by Simon McCade西蒙麥卡德(Simon McCade) 開發人員和技術創始人如何將他們的想法轉化為UI設計 (How developers and tech founders can turn their ideas into UI design) Discover how to turn a great idea for a product or service into a beautiful UI de…

在ubuntu怎樣修改默認的編碼格式

ubuntu修改系統默認編碼的方法是&#xff1a;1. 參考 /usr/share/i18n/SUPPORTED 編輯/var/lib/locales/supported.d/* gedit /var/lib/locales/supported.d/localgedit /var/lib/locales/supported.d/zh-hans如&#xff1a;more /var/lib/locales/supported.d/localzh_CN GB18…

JAVA中PO,BO,VO,DTO,POJO,Entity

https://my.oschina.net/liaodo/blog/2988512轉載于:https://www.cnblogs.com/dianzan/p/11311217.html

【Lolttery】項目開發日志 (三)維護好一個項目好難

項目的各種配置開始出現混亂的現象了 在只有一個人開發的情況下也開始感受到維護一個項目的難度。 之前明明還好用的東西&#xff0c;轉眼就各種莫名其妙的報錯&#xff0c;完全不知道為什么。 今天一天的工作基本上就是整理各種配置。 再加上之前數據庫設計出現了問題&#xf…

leetcode 567. 字符串的排列(滑動窗口)

給定兩個字符串 s1 和 s2&#xff0c;寫一個函數來判斷 s2 是否包含 s1 的排列。 換句話說&#xff0c;第一個字符串的排列之一是第二個字符串的子串。 示例1: 輸入: s1 “ab” s2 “eidbaooo” 輸出: True 解釋: s2 包含 s1 的排列之一 (“ba”). 解題思路 和s1每個字符…

靜態變數和非靜態變數_統計資料:了解變數

靜態變數和非靜態變數Statistics 101: Understanding the different type of variables.統計101&#xff1a;了解變量的不同類型。 As we enter the latter part of the year 2020, it is safe to say that companies utilize data to assist in making business decisions. F…

代碼走查和代碼審查_如何避免代碼審查陷阱降低生產率

代碼走查和代碼審查Code reviewing is an engineering practice used by many high performing teams. And even though this software practice has many advantages, teams doing code reviews also encounter quite a few code review pitfalls.代碼審查是許多高性能團隊使用…

Zabbix3.2安裝

一、環境 OS: CentOS7.0.1406 Zabbix版本: Zabbix-3.2 下載地址: http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm MySQL版本: 5.6.37 MySQL: http://repo.mysql.com/mysql-community-release-el7-5.noarch.r…

Warensoft Unity3D通信庫使用向導4-SQL SERVER訪問組件使用說明

Warensoft Unity3D通信庫使用向導4-SQL SERVER訪問組件使用說明 (作者:warensoft,有問題請聯系warensoft163.com) 在前一節《warensoft unity3d通信庫使用向導3-建立WarensoftDataService》中已經說明如何配置Warensoft Data Service&#xff0c;從本節開始&#xff0c;將說明…

01-gt;選中UITableViewCell后,Cell中的UILabel的背景顏色變成透明色

解決方案有兩種方法一 -> 新建一個UILabel類, 繼承UILabel, 然后重寫 setBackgroundColor: 方法, 在這個方法里不做任何操作, 讓UILabel的backgroundColor不發生改變.寫在最后, 感謝參考的出處:不是謝志偉StackOverflow: UITableViewCell makes labels background clear whe…

leetcode 703. 數據流中的第 K 大元素(堆)

設計一個找到數據流中第 k 大元素的類&#xff08;class&#xff09;。注意是排序后的第 k 大元素&#xff0c;不是第 k 個不同的元素。 請實現 KthLargest 類&#xff1a; KthLargest(int k, int[] nums) 使用整數 k 和整數流 nums 初始化對象。 int add(int val) 將 val 插…

不知道輸入何時停止_知道何時停止

不知道輸入何時停止In predictive analytics, it can be a tricky thing to know when to stop.在預測分析中&#xff0c;知道何時停止可能是一件棘手的事情。 Unlike many of life’s activities, there’s no definitive finishing line, after which you can say “tick, I…

移動認證_如何在移動設備上實施安全的生物特征認證

移動認證by Kathy Dinh凱西丁(Kathy Dinh) 如何在移動設備上實施安全的生物特征認證 (How to implement secure Biometric Authentication on mobile devices) A quick search for React Native biometric authentication would give you several tutorials. That was the fir…

[Luogu1890]gcd區間

原題鏈接https://www.luogu.org/problem/show?pid1890 暴力中的暴力。 對于每一組詢問l..r&#xff0c;我們先循環暴力枚舉l..r中最大值到1&#xff0c;再暴力循環l..r的每一個數&#xff0c;判斷前一重循環能否整除后一重&#xff0c;如果全部都能&#xff0c;則可判定它就是…