天天看點

滑動視窗-Slide Window

滑動視窗模版:

Medium

Given an array of positive integers <code>nums</code> and a positive integer <code>target</code>, return the minimal length of a contiguous subarray <code>[numsl, numsl+1, ..., numsr-1, numsr]</code> of which the sum is greater than or equal to <code>target</code>. If there is no such subarray, return <code>0</code> instead.

Example 1:

Example 2:

Example 3:

Constraints:

<code>1 &lt;= target &lt;= 109</code>

<code>1 &lt;= nums.length &lt;= 105</code>

<code>1 &lt;= nums[i] &lt;= 105</code>

Follow up: If you have figured out the <code>O(n)</code> solution, try coding another solution of which the time complexity is <code>O(n log(n))</code>.

Medium

Given a string <code>s</code>, find the length of the longest substring without repeating characters.

Example 1:

Example 2:

Example 3:

Example 4:

Constraints:

<code>0 &lt;= s.length &lt;= 5 * 104</code>

<code>s</code> consists of English letters, digits, symbols and spaces.

 時間複雜度:O(N)

Algorithms

Medium

Description

Given a string, find the length of the longest substring T that contains at most <code>2</code> distinct characters.

Example

時間複雜度:O(N)

Description

Given a string S, find the length of the longest substring T that contains at most k distinct characters.

Example

Example 1:

Example 2:

時間複雜度:O(N)

Medium

You are given a string <code>s</code> and an integer <code>k</code>. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most <code>k</code> times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

Example 1:

Example 2:

Constraints:

<code>1 &lt;= s.length &lt;= 105</code>

<code>s</code> consists of only uppercase English letters.

<code>0 &lt;= k &lt;= s.length</code>

時間複雜度: O(26N) -&gt; O(N) 

Medium

Given a binary array <code>nums</code>, you should delete one element from it.

Return the size of the longest non-empty subarray containing only <code>1</code>'s in the resulting array. Return <code>0</code> if there is no such subarray.

Example 1:

Example 2:

Example 3:

Example 4:

Example 5:

Constraints:

<code>1 &lt;= nums.length &lt;= 105</code>

<code>nums[i]</code> is either <code>0</code> or <code>1</code>.

Medium

Given a string <code>s</code> and an integer <code>k</code>, return the length of the longest substring of <code>s</code> such that the frequency of each character in this substring is greater than or equal to <code>k</code>.

Example 1:

Example 2:

Constraints:

<code>1 &lt;= s.length &lt;= 104</code>

<code>s</code> consists of only lowercase English letters.

<code>1 &lt;= k &lt;= 105</code>

時間複雜度:O(26N) -&gt; O(N)

描述

給定字元串S,找到最多有k個不同字元的最長子串T。

樣例

樣例 1:

樣例 2:

挑戰

O(n) 時間複雜度

FollowUp: 如果你拿到的是一個非常大的檔案,我們隻能用Stream逐漸讀取, 而不是有限長度的String, 如何解決?

 eabaacebdefc   k=3

這個時候因為太長是以不可能把string 都儲存在記憶體中,也就是我們無法記錄滑動視窗中 i 的位置

我們可以增加一個LRU來作為輔助,使用LRU來存儲元素最後一次出現的位置,當字母個數超過k時,尾部待remove的元素記錄的即為i的位置 

Hard

Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return the minimum window substring of <code>s</code> such that every character in <code>t</code> (including duplicates) is included in the window. If there is no such substring, return the empty string <code>""</code>.

The testcases will be generated such that the answer is unique.

A substring is a contiguous sequence of characters within the string.

Example 1:

Example 2:

Example 3:

Constraints:

<code>m == s.length</code>

<code>n == t.length</code>

<code>1 &lt;= m, n &lt;= 105</code>

<code>s</code> and <code>t</code> consist of uppercase and lowercase English letters.

Follow up: Could you find an algorithm that runs in <code>O(m + n)</code> time?

992. Subarrays with K Different Integers

Hard

243933Add to ListShare

Given an integer array <code>nums</code> and an integer <code>k</code>, return the number of good subarrays of <code>nums</code>.

A good array is an array where the number of different integers in that array is exactly <code>k</code>.

For example, <code>[1,2,3,1,2]</code> has <code>3</code> different integers: <code>1</code>, <code>2</code>, and <code>3</code>.

A subarray is a contiguous part of an array.

Example 1:

Example 2:

Constraints:

<code>1 &lt;= nums.length &lt;= 2 * 104</code>

<code>1 &lt;= nums[i], k &lt;= nums.length</code>

時間複雜度: O(N)

1248. Count Number of Nice Subarrays

Medium

Given an array of integers <code>nums</code> and an integer <code>k</code>. A continuous subarray is called nice if there are <code>k</code> odd numbers on it.

Return the number of nice sub-arrays. 

Example 1:

Example 2:

Example 3:

Constraints:

<code>1 &lt;= nums.length &lt;= 50000</code>

<code>1 &lt;= nums[i] &lt;= 10^5</code>

<code>1 &lt;= k &lt;= nums.length</code>

解法:這題跟上面一道是類似的解法

時間複雜度: O(N)

Medium

You are given an array representing a row of <code>seats</code> where <code>seats[i] = 1</code> represents a person sitting in the <code>ith</code> seat, and <code>seats[i] = 0</code> represents that the <code>ith</code> seat is empty (0-indexed).

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. 

Return that maximum distance to the closest person.

Example 1:

滑動視窗-Slide Window

Example 2:

Example 3:

Constraints:

<code>2 &lt;= seats.length &lt;= 2 * 104</code>

<code>seats[i]</code> is <code>0</code> or <code>1</code>.

At least one seat is empty.

At least one seat is occupied.

解法一:不推薦該解法,1.沒有使用模版,代碼寫起來容易出錯 2.使用了3個循環,實際該題可以1pass

推薦解法: