Skip to content

Commit 67eb035

Browse files
committed
Apr-14
1 parent 8d92a2c commit 67eb035

11 files changed

+438
-38
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Large diffs are not rendered by default.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Tag: Array, Hash Table, Math, Sliding Window, Prefix Sum
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
8+
// Return the number of nice sub-arrays.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: nums = [1,1,2,1,1], k = 3
13+
// Output: 2
14+
// Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
15+
//
16+
// Example 2:
17+
//
18+
// Input: nums = [2,4,6], k = 1
19+
// Output: 0
20+
// Explanation: There are no odd numbers in the array.
21+
//
22+
// Example 3:
23+
//
24+
// Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
25+
// Output: 16
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= nums.length <= 50000
31+
// 1 <= nums[i] <= 10^5
32+
// 1 <= k <= nums.length
33+
//
34+
//
35+
36+
class Solution {
37+
public:
38+
int numberOfSubarrays(vector<int>& nums, int k) {
39+
return count(nums, k) - count(nums, k - 1);
40+
}
41+
42+
int count(vector<int>& nums, int k) {
43+
int n = nums.size();
44+
int i = 0;
45+
int odd = 0;
46+
int res = 0;
47+
for (int j = 0; j < n; j++) {
48+
odd += nums[j] % 2;
49+
while (i <= j && odd > k) {
50+
odd -= nums[i] % 2;
51+
i += 1;
52+
}
53+
res += j - i + 1;
54+
}
55+
return res;
56+
}
57+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Tag: Array, Hash Table, Math, Sliding Window, Prefix Sum
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
8+
# Return the number of nice sub-arrays.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: nums = [1,1,2,1,1], k = 3
13+
# Output: 2
14+
# Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
15+
#
16+
# Example 2:
17+
#
18+
# Input: nums = [2,4,6], k = 1
19+
# Output: 0
20+
# Explanation: There are no odd numbers in the array.
21+
#
22+
# Example 3:
23+
#
24+
# Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
25+
# Output: 16
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= nums.length <= 50000
31+
# 1 <= nums[i] <= 10^5
32+
# 1 <= k <= nums.length
33+
#
34+
#
35+
36+
class Solution:
37+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
38+
return self.count(nums, k) - self.count(nums, k - 1)
39+
40+
def count(self, nums: List[int], k: int) -> int:
41+
n = len(nums)
42+
res = 0
43+
i = 0
44+
odd = 0
45+
for j in range(n):
46+
odd += nums[j] % 2
47+
while i <= j and odd > k:
48+
odd -= nums[i] % 2
49+
i += 1
50+
res += j - i + 1
51+
return res

leetcode/1534.count-good-triplets.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Tag: Array, Enumeration
2+
// Time: O(N^3)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/cNpiisOiQ1o
7+
8+
// Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.
9+
// A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:
10+
//
11+
// 0 <= i < j < k < arr.length
12+
// |arr[i] - arr[j]| <= a
13+
// |arr[j] - arr[k]| <= b
14+
// |arr[i] - arr[k]| <= c
15+
//
16+
// Where |x| denotes the absolute value of x.
17+
// Return the number of good triplets.
18+
//  
19+
// Example 1:
20+
//
21+
// Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
22+
// Output: 4
23+
// Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
24+
//
25+
// Example 2:
26+
//
27+
// Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
28+
// Output: 0
29+
// Explanation: No triplet satisfies all conditions.
30+
//
31+
//  
32+
// Constraints:
33+
//
34+
// 3 <= arr.length <= 100
35+
// 0 <= arr[i] <= 1000
36+
// 0 <= a, b, c <= 1000
37+
//
38+
39+
class Solution {
40+
public:
41+
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
42+
int n = arr.size();
43+
int res = 0;
44+
for (int i = 0; i < n; i++) {
45+
for (int j = i + 1; j < n; j++) {
46+
for (int k = j + 1; k < n; k++) {
47+
if (abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) {
48+
res += 1;
49+
}
50+
}
51+
}
52+
}
53+
return res;
54+
}
55+
};

leetcode/1534.count-good-triplets.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Tag: Array, Enumeration
2+
# Time: O(N^3)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/cNpiisOiQ1o
7+
8+
# Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.
9+
# A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:
10+
#
11+
# 0 <= i < j < k < arr.length
12+
# |arr[i] - arr[j]| <= a
13+
# |arr[j] - arr[k]| <= b
14+
# |arr[i] - arr[k]| <= c
15+
#
16+
# Where |x| denotes the absolute value of x.
17+
# Return the number of good triplets.
18+
#  
19+
# Example 1:
20+
#
21+
# Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
22+
# Output: 4
23+
# Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
24+
#
25+
# Example 2:
26+
#
27+
# Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1
28+
# Output: 0
29+
# Explanation: No triplet satisfies all conditions.
30+
#
31+
#  
32+
# Constraints:
33+
#
34+
# 3 <= arr.length <= 100
35+
# 0 <= arr[i] <= 1000
36+
# 0 <= a, b, c <= 1000
37+
#
38+
39+
class Solution:
40+
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
41+
n = len(arr)
42+
res = 0
43+
for i in range(n):
44+
for j in range(i + 1, n):
45+
for k in range(j + 1, n):
46+
if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
47+
res += 1
48+
49+
return res

leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@
4343
//
4444
//
4545

46+
class Solution {
47+
public:
48+
long long countOfSubstrings(string word, int k) {
49+
return count(word, k) - count(word, k + 1);
50+
}
51+
52+
long long count(string &word, int k) {
53+
int n = word.size();
54+
unordered_map<char, int> vowel;
55+
int count = 0;
56+
long long res = 0;
57+
int i = 0;
58+
for (int j = 0; j < n; j++) {
59+
if (is_consonant(word[j])) {
60+
vowel[word[j]] += 1;
61+
} else {
62+
count += 1;
63+
}
64+
65+
while (vowel.size() == 5 && count >= k) {
66+
res += n - j;
67+
if (is_consonant(word[i])) {
68+
vowel[word[i]] -= 1;
69+
if (vowel[word[i]] == 0) {
70+
vowel.erase(word[i]);
71+
}
72+
} else {
73+
count -= 1;
74+
}
75+
i += 1;
76+
}
77+
}
78+
return res;
79+
}
80+
81+
bool is_consonant(char ch) {
82+
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
83+
}
84+
};
85+
4686
class Solution {
4787
public:
4888
long long countOfSubstrings(string word, int k) {

leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@
4343
#
4444
#
4545

46+
from collections import defaultdict
47+
class Solution:
48+
def countOfSubstrings(self, word: str, k: int) -> int:
49+
return self.count(word, k) - self.count(word, k + 1)
50+
51+
def count(self, word: str, k: int) -> int:
52+
n = len(word)
53+
vowel = defaultdict(int)
54+
count = 0
55+
i = 0
56+
res = 0
57+
for j in range(n):
58+
if word[j] in 'aeiou':
59+
vowel[word[j]] += 1
60+
else:
61+
count += 1
62+
63+
while len(vowel) == 5 and count >= k:
64+
res += n - j
65+
if word[i] in 'aeiou':
66+
vowel[word[i]] -= 1
67+
if vowel[word[i]] == 0:
68+
del vowel[word[i]]
69+
else:
70+
count -= 1
71+
i += 1
72+
73+
return res
74+
4675
from collections import defaultdict
4776
class Solution:
4877
def countOfSubstrings(self, word: str, k: int) -> int:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Tag: Array, Hash Table, Sliding Window, Counting
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums and an integer k, return the number of good subarrays of nums.
8+
// A good array is an array where the number of different integers in that array is exactly k.
9+
//
10+
// For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3.
11+
//
12+
// A subarray is a contiguous part of an array.
13+
//  
14+
// Example 1:
15+
//
16+
// Input: nums = [1,2,1,2,3], k = 2
17+
// Output: 7
18+
// 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]
19+
//
20+
// Example 2:
21+
//
22+
// Input: nums = [1,2,1,3,4], k = 3
23+
// Output: 3
24+
// Explanation: Subarrays formed with exactly 3 different integers: [1,2,1,3], [2,1,3], [1,3,4].
25+
//
26+
//  
27+
// Constraints:
28+
//
29+
// 1 <= nums.length <= 2 * 104
30+
// 1 <= nums[i], k <= nums.length
31+
//
32+
//
33+
34+
class Solution {
35+
public:
36+
int subarraysWithKDistinct(vector<int>& nums, int k) {
37+
return count(nums, k) - count(nums, k + 1);
38+
}
39+
40+
int count(vector<int>& nums, int k) {
41+
int n = nums.size();
42+
int i = 0;
43+
int res = 0;
44+
unordered_map<int, int> counter;
45+
for (int j = 0; j < n; j++) {
46+
counter[nums[j]] += 1;
47+
while (counter.size() >= k) {
48+
res += n - j;
49+
counter[nums[i]] -= 1;
50+
if (counter[nums[i]] == 0) {
51+
counter.erase(nums[i]);
52+
}
53+
i += 1;
54+
}
55+
}
56+
return res;
57+
}
58+
};

0 commit comments

Comments
 (0)