Skip to content

Commit fc8382f

Browse files
committed
Feb-16
1 parent 21056d6 commit fc8382f

12 files changed

+3392
-2714
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Tag: Hash Table, String, Sliding Window
2+
// Time: O(N)
3+
// Space: O(N^2)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a string s, return the maximum number of occurrences of any substring under the following rules:
8+
//
9+
// The number of unique characters in the substring must be less than or equal to maxLetters.
10+
// The substring size must be between minSize and maxSize inclusive.
11+
//
12+
//  
13+
// Example 1:
14+
//
15+
// Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
16+
// Output: 2
17+
// Explanation: Substring "aab" has 2 occurrences in the original string.
18+
// It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
19+
//
20+
// Example 2:
21+
//
22+
// Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
23+
// Output: 2
24+
// Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
25+
//
26+
//  
27+
// Constraints:
28+
//
29+
// 1 <= s.length <= 105
30+
// 1 <= maxLetters <= 26
31+
// 1 <= minSize <= maxSize <= min(26, s.length)
32+
// s consists of only lowercase English letters.
33+
//
34+
//
35+
36+
class Solution {
37+
public:
38+
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
39+
int n = s.size();
40+
unordered_map<char, int> counter;
41+
unordered_map<string, int> res_counter;
42+
int res = 0;
43+
int i = 0;
44+
for (int j = 0; j < n; j++) {
45+
counter[s[j]] += 1;
46+
while (j - i + 1 >= minSize) {
47+
if (j - i + 1 <= maxSize && counter.size() <= maxLetters) {
48+
string sub = s.substr(i, j - i + 1);
49+
res_counter[sub] += 1;
50+
res = max(res, res_counter[sub]);
51+
}
52+
counter[s[i]] -= 1;
53+
if (counter[s[i]] == 0) {
54+
counter.erase(s[i]);
55+
}
56+
i += 1;
57+
}
58+
}
59+
60+
return res;
61+
}
62+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Tag: Hash Table, String, Sliding Window
2+
# Time: O(N)
3+
# Space: O(N^2)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a string s, return the maximum number of occurrences of any substring under the following rules:
8+
#
9+
# The number of unique characters in the substring must be less than or equal to maxLetters.
10+
# The substring size must be between minSize and maxSize inclusive.
11+
#
12+
#  
13+
# Example 1:
14+
#
15+
# Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
16+
# Output: 2
17+
# Explanation: Substring "aab" has 2 occurrences in the original string.
18+
# It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).
19+
#
20+
# Example 2:
21+
#
22+
# Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
23+
# Output: 2
24+
# Explanation: Substring "aaa" occur 2 times in the string. It can overlap.
25+
#
26+
#  
27+
# Constraints:
28+
#
29+
# 1 <= s.length <= 105
30+
# 1 <= maxLetters <= 26
31+
# 1 <= minSize <= maxSize <= min(26, s.length)
32+
# s consists of only lowercase English letters.
33+
#
34+
#
35+
36+
from collections import defaultdict
37+
class Solution:
38+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
39+
n = len(s)
40+
counter = defaultdict(int)
41+
i = 0
42+
res = 0
43+
res_count = defaultdict(int)
44+
for j in range(n):
45+
counter[s[j]] += 1
46+
47+
while (j - i + 1 >= minSize):
48+
if j - i + 1 <= maxSize and len(counter) <= maxLetters:
49+
sub = s[i: j + 1]
50+
res_count[sub] += 1
51+
res = max(res, res_count[sub])
52+
counter[s[i]] -= 1
53+
if counter[s[i]] == 0:
54+
del counter[s[i]]
55+
i += 1
56+
57+
return res
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Tag: Array, Backtracking
2+
// Time: O(N!)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/3Z75z9sojwg
7+
8+
// Given an integer n, find a sequence that satisfies all of the following:
9+
//
10+
// The integer 1 occurs once in the sequence.
11+
// Each integer between 2 and n occurs twice in the sequence.
12+
// For every integer i between 2 and n, the distance between the two occurrences of i is exactly i.
13+
//
14+
// The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of their indices, |j - i|.
15+
// Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution.
16+
// A sequence a is lexicographically larger than a sequence b (of the same length) if in the first position where a and b differ, sequence a has a number greater than the corresponding number in b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they differ is at the third number, and 9 is greater than 5.
17+
//  
18+
// Example 1:
19+
//
20+
// Input: n = 3
21+
// Output: [3,1,2,3,2]
22+
// Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
23+
//
24+
// Example 2:
25+
//
26+
// Input: n = 5
27+
// Output: [5,3,1,4,3,5,2,4,2]
28+
//
29+
//  
30+
// Constraints:
31+
//
32+
// 1 <= n <= 20
33+
//
34+
//
35+
36+
class Solution {
37+
public:
38+
vector<int> constructDistancedSequence(int n) {
39+
vector<int> res(2 * n - 1, 0);
40+
vector<bool> used(n + 1, false);
41+
helper(0, res, used, n);
42+
return res;
43+
}
44+
45+
bool helper(int i, vector<int> &res, vector<bool> &used, int n) {
46+
if (i == res.size()) {
47+
return true;
48+
}
49+
50+
if (res[i] != 0) {
51+
return helper(i + 1, res, used, n);
52+
}
53+
54+
for (int num = n; num >= 1; num--) {
55+
if (used[num]) {
56+
continue;
57+
}
58+
int j = num == 1 ? i : i + num;
59+
if (j < res.size() && res[j] == 0) {
60+
used[num] = true;
61+
res[i] = res[j] = num;
62+
if (helper(i + 1, res, used, n)) {
63+
return true;
64+
}
65+
res[i] = res[j] = 0;
66+
used[num] = false;
67+
}
68+
}
69+
70+
return false;
71+
}
72+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tag: Array, Backtracking
2+
# Time: O(N!)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/3Z75z9sojwg
7+
8+
# Given an integer n, find a sequence that satisfies all of the following:
9+
#
10+
# The integer 1 occurs once in the sequence.
11+
# Each integer between 2 and n occurs twice in the sequence.
12+
# For every integer i between 2 and n, the distance between the two occurrences of i is exactly i.
13+
#
14+
# The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of their indices, |j - i|.
15+
# Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution.
16+
# A sequence a is lexicographically larger than a sequence b (of the same length) if in the first position where a and b differ, sequence a has a number greater than the corresponding number in b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they differ is at the third number, and 9 is greater than 5.
17+
#  
18+
# Example 1:
19+
#
20+
# Input: n = 3
21+
# Output: [3,1,2,3,2]
22+
# Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.
23+
#
24+
# Example 2:
25+
#
26+
# Input: n = 5
27+
# Output: [5,3,1,4,3,5,2,4,2]
28+
#
29+
#  
30+
# Constraints:
31+
#
32+
# 1 <= n <= 20
33+
#
34+
#
35+
36+
class Solution:
37+
def constructDistancedSequence(self, n: int) -> List[int]:
38+
res = [0 for i in range(2 * n - 1)]
39+
used = [False for i in range(n + 1)]
40+
self.helper(0, res, n, used)
41+
return res
42+
43+
def helper(self, i: int, res: list, n: int, used: list) -> bool:
44+
if i == len(res):
45+
return True
46+
47+
if res[i] != 0:
48+
return self.helper(i + 1, res, n, used)
49+
50+
for num in range(n, 0, -1):
51+
if used[num]:
52+
continue
53+
54+
j = i + num if num > 1 else i
55+
if j < len(res) and res[j] == 0:
56+
used[num] = True
57+
res[i] = res[j] = num
58+
if self.helper(i + 1, res, n, used):
59+
return True
60+
res[i] = res[j] = 0
61+
used[num] = False
62+
63+
return False
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Tag: Array, Hash Table, Sliding Window
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums containing n integers, find the beauty of each subarray of size k.
8+
// The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers.
9+
// Return an integer array containing n - k + 1 integers, which denote the beauty of the subarrays in order from the first index in the array.
10+
//
11+
//
12+
// A subarray is a contiguous non-empty sequence of elements within an array.
13+
//
14+
//
15+
//  
16+
// Example 1:
17+
//
18+
// Input: nums = [1,-1,-3,-2,3], k = 3, x = 2
19+
// Output: [-1,-2,-2]
20+
// Explanation: There are 3 subarrays with size k = 3.
21+
// The first subarray is [1, -1, -3] and the 2nd smallest negative integer is -1. 
22+
// The second subarray is [-1, -3, -2] and the 2nd smallest negative integer is -2. 
23+
// The third subarray is [-3, -2, 3] and the 2nd smallest negative integer is -2.
24+
// Example 2:
25+
//
26+
// Input: nums = [-1,-2,-3,-4,-5], k = 2, x = 2
27+
// Output: [-1,-2,-3,-4]
28+
// Explanation: There are 4 subarrays with size k = 2.
29+
// For [-1, -2], the 2nd smallest negative integer is -1.
30+
// For [-2, -3], the 2nd smallest negative integer is -2.
31+
// For [-3, -4], the 2nd smallest negative integer is -3.
32+
// For [-4, -5], the 2nd smallest negative integer is -4. 
33+
// Example 3:
34+
//
35+
// Input: nums = [-3,1,2,-3,0,-3], k = 2, x = 1
36+
// Output: [-3,0,-3,-3,-3]
37+
// Explanation: There are 5 subarrays with size k = 2.
38+
// For [-3, 1], the 1st smallest negative integer is -3.
39+
// For [1, 2], there is no negative integer so the beauty is 0.
40+
// For [2, -3], the 1st smallest negative integer is -3.
41+
// For [-3, 0], the 1st smallest negative integer is -3.
42+
// For [0, -3], the 1st smallest negative integer is -3.
43+
//  
44+
// Constraints:
45+
//
46+
// n == nums.length 
47+
// 1 <= n <= 105
48+
// 1 <= k <= n
49+
// 1 <= x <= k 
50+
// -50 <= nums[i] <= 50 
51+
//
52+
//
53+
54+
class Solution {
55+
public:
56+
vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x) {
57+
int n = nums.size();
58+
unordered_map<int, int> counter;
59+
vector<int> res(n - k + 1, 0);
60+
for (int i = 0; i < n; i++) {
61+
if (nums[i] < 0) {
62+
counter[nums[i]] += 1;
63+
}
64+
65+
if (i >= k - 1) {
66+
int count = 0;
67+
for (int num = -59; num < 0; num++) {
68+
count += counter[num];
69+
if (count >= x) {
70+
res[i - k + 1] = num;
71+
break;
72+
}
73+
}
74+
if (nums[i - k + 1] < 0) {
75+
counter[nums[i - k + 1]] -= 1;
76+
if (counter[nums[i - k + 1]] == 0) {
77+
counter.erase(nums[i - k + 1]);
78+
}
79+
80+
}
81+
}
82+
}
83+
return res;
84+
}
85+
};
86+
87+
class Solution {
88+
public:
89+
vector<int> getSubarrayBeauty(vector<int>& nums, int k, int x) {
90+
int n = nums.size();
91+
vector<int> counter(50, 0);
92+
vector<int> res(n - k + 1, 0);
93+
for (int i = 0; i < n; i++) {
94+
if (nums[i] < 0) {
95+
counter[nums[i] + 50] += 1;
96+
}
97+
98+
if (i >= k - 1) {
99+
int count = 0;
100+
for (int num = 0; num < 50; num++) {
101+
count += counter[num];
102+
if (count >= x) {
103+
res[i - k + 1] = num - 50;
104+
break;
105+
}
106+
}
107+
if (nums[i - k + 1] < 0) {
108+
counter[nums[i - k + 1] + 50] -= 1;
109+
}
110+
}
111+
}
112+
return res;
113+
}
114+
};

0 commit comments

Comments
 (0)