有一類題,看起來很像同向雙指針,但是不完全是,一般都是求exactly K
同向雙指針教會了我們如何求at most K, 現在我們要求exactly K. 那麼我們可以轉換爲 exactly K = atMost(K) - atMost(K - 1);
這類題有好多,如下:
Substrings of size K with K distinct chars
Given a string s
and an int k
, return an int representing the number of substrings (not unique) of s
with exactly k
distinct characters. If the given string doesn't have k
distinct characters, return 0.
https://leetcode.com/problems/subarrays-with-k-different-integers
Example 1:
Input: s = "pqpqs", k = 2
Output: 7
Explanation: ["pq", "pqp", "pqpq", "qp", "qpq", "pq", "qs"]
思路:這題思路很巧妙,我是看了上面那個 subarrays with k different integers 的花花視頻才懂的。
直接求很難,但是我們可以轉換成 f(k) - f(k-1) f(k)表示最多不大於k(at most k)的char的string的個數, at most k, 可以用sliding window的雙指針來做,count 就是 j - i,表示以j - 1 爲尾巴的subarray的個數;
public class numberOfUniqueChars {
public int getAtMostKCharacters(String s, int k) {
if(s == null || s.length() == 0) {
return 0;
}
int j = 0;
int[] counts = new int[256];
int c = 0;
int ans = 0;
for(int i = 0; i < s.length(); i++) {
// move j;
while(j < s.length() && c <= k) {
if(counts[s.charAt(j)] == 0 ) {
if(c == k) {
break;
}
c++;
}
counts[s.charAt(j)]++;
j++;
}
//update result;
ans += j - i;
// remove i;
counts[s.charAt(i)]--;
if(counts[s.charAt(i)] == 0) {
c--;
}
}
return ans;
}
public static void main(String[] args) {
/*
*
* Input: s = "pqpqs", k = 2
Output: 7
Explanation: ["pq", "pqp", "pqpq", "qp", "qpq", "pq", "qs"]
* */
numberOfUniqueChars numberOfUniqueChars = new numberOfUniqueChars();
String s = "pqpqs";
int ans1 = numberOfUniqueChars.getAtMostKCharacters(s, 2) - numberOfUniqueChars.getAtMostKCharacters(s, 1);
// ans = 7;
System.out.println("ans: " + ans1);
}
}
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 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].
思路:跟 Substrings with exactly K distinct chars ,把問題轉換爲 at most k的count,這樣就好做,然後用公式f(k) - f(k-1);
res += j - i ; 代表的是以 j -1 爲結尾的,滿足at most k chars的個數;
class Solution {
public int subarraysWithKDistinct(int[] A, int K) {
if(A == null || A.length == 0) {
return 0;
}
return getAtMostK(A, K) - getAtMostK(A, K - 1);
}
private int getAtMostK(int[] A, int k) {
int res = 0;
int j = 0;
int count = 0;
int[] counts = new int[A.length + 1];
for(int i = 0; i < A.length; i++) {
// move j;
while(j < A.length && count <= k) {
if(counts[A[j]] == 0) {
if(count == k) {
break;
}
count++;
}
counts[A[j]]++;
j++;
}
// update result;
res += j - i;
// move i;
counts[A[i]]--;
if(counts[A[i]] == 0) {
count--;
}
}
return res;
}
}
Substrings of size K with K distinct chars
Given a string s
and an int k
, return an int representing the number of substrings (not unique) of s
with exactly k
distinct characters. If the given string doesn't have k
distinct characters, return 0.
https://leetcode.com/problems/subarrays-with-k-different-integers
Example 1:
Input: s = "pqpqs", k = 2
Output: 7
Explanation: ["pq", "pqp", "pqpq", "qp", "qpq", "pq", "qs"]
思路:這題思路很巧妙,我是看了上面那個 subarrays with k different integers 的花花視頻才懂的。
直接求很難,但是我們可以轉換成 f(k) - f(k-1) f(k)表示最多不大於k(at most k)的char的string的個數, at most k, 可以用sliding window的雙指針來做,count 就是 j - i,表示以j - 1 爲尾巴的subarray的個數;
public class numberOfUniqueChars {
public int getAtMostKCharacters(String s, int k) {
if(s == null || s.length() == 0) {
return 0;
}
int j = 0;
int[] counts = new int[256];
int c = 0;
int ans = 0;
for(int i = 0; i < s.length(); i++) {
// move j;
while(j < s.length() && c <= k) {
if(counts[s.charAt(j)] == 0 ) {
if(c == k) {
break;
}
c++;
}
counts[s.charAt(j)]++;
j++;
}
//update result;
ans += j - i;
// remove i;
counts[s.charAt(i)]--;
if(counts[s.charAt(i)] == 0) {
c--;
}
}
return ans;
}
public static void main(String[] args) {
/*
*
* Input: s = "pqpqs", k = 2
Output: 7
Explanation: ["pq", "pqp", "pqpq", "qp", "qpq", "pq", "qs"]
* */
numberOfUniqueChars numberOfUniqueChars = new numberOfUniqueChars();
String s = "pqpqs";
int ans1 = numberOfUniqueChars.getAtMostKCharacters(s, 2) - numberOfUniqueChars.getAtMostKCharacters(s, 1);
// ans = 7;
System.out.println("ans: " + ans1);
}
}
Count Number of Nice Subarrays
Given an array of integers nums
and an integer k
. A subarray is called nice if there are k
odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
思路:sliding window讓我們學會了如何求At Most K的區間,那麼Exactly K
times = at most K
times - at most K - 1
times
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
if(nums == null || nums.length == 0) {
return 0;
}
return getAtMostK(nums, k) - getAtMostK(nums, k - 1);
}
private int getAtMostK(int[] nums, int k) {
// two pointers scan;
int j = 0;
int oddcount = 0;
int res = 0;
for(int i = 0; i < nums.length; i++) {
// move j;
while(j < nums.length && oddcount <= k) {
if(nums[j] % 2 == 1) {
if(oddcount == k) {
break;
}
oddcount++;
}
j++;
}
//update result
res += j - i;
//move i;
if(nums[i] % 2 == 1) {
oddcount--;
}
}
return res;
}
}