Skip to content

Commit a5341b8

Browse files
committed
May-12
1 parent 9ebcd65 commit a5341b8

9 files changed

+381
-10
lines changed

README.md

Lines changed: 16 additions & 9 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Tag: Array
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/23ecGZB559Y
7+
8+
// Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: arr = [2,6,4,1]
13+
// Output: false
14+
// Explanation: There are no three consecutive odds.
15+
//
16+
// Example 2:
17+
//
18+
// Input: arr = [1,2,34,3,4,5,7,23,12]
19+
// Output: true
20+
// Explanation: [5,7,23] are three consecutive odds.
21+
//
22+
//  
23+
// Constraints:
24+
//
25+
// 1 <= arr.length <= 1000
26+
// 1 <= arr[i] <= 1000
27+
//
28+
//
29+
30+
class Solution {
31+
public:
32+
bool threeConsecutiveOdds(vector<int>& arr) {
33+
int count = 0;
34+
for (auto &x: arr) {
35+
if (x % 2 == 1) {
36+
count += 1;
37+
} else {
38+
count = 0;
39+
}
40+
41+
if (count == 3) {
42+
return true;
43+
}
44+
}
45+
return false;
46+
}
47+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Tag: Array
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/23ecGZB559Y
7+
8+
# Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: arr = [2,6,4,1]
13+
# Output: false
14+
# Explanation: There are no three consecutive odds.
15+
#
16+
# Example 2:
17+
#
18+
# Input: arr = [1,2,34,3,4,5,7,23,12]
19+
# Output: true
20+
# Explanation: [5,7,23] are three consecutive odds.
21+
#
22+
#  
23+
# Constraints:
24+
#
25+
# 1 <= arr.length <= 1000
26+
# 1 <= arr[i] <= 1000
27+
#
28+
#
29+
30+
class Solution:
31+
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
32+
count = 0
33+
for x in arr:
34+
if x % 2 == 1:
35+
count += 1
36+
else:
37+
count = 0
38+
39+
if count == 3:
40+
return True
41+
42+
return False
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Tag: Array, Two Pointers, Binary Search, Stack, Monotonic Stack
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing.
8+
// Return the length of the shortest subarray to remove.
9+
// A subarray is a contiguous subsequence of the array.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: arr = [1,2,3,10,4,2,3,5]
14+
// Output: 3
15+
// Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
16+
// Another correct solution is to remove the subarray [3,10,4].
17+
//
18+
// Example 2:
19+
//
20+
// Input: arr = [5,4,3,2,1]
21+
// Output: 4
22+
// Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
23+
//
24+
// Example 3:
25+
//
26+
// Input: arr = [1,2,3]
27+
// Output: 0
28+
// Explanation: The array is already non-decreasing. We do not need to remove any elements.
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// 1 <= arr.length <= 105
34+
// 0 <= arr[i] <= 109
35+
//
36+
//
37+
38+
class Solution {
39+
public:
40+
int findLengthOfShortestSubarray(vector<int>& arr) {
41+
int n = arr.size();
42+
int l = 0;
43+
int r = n - 1;
44+
while (l < r && arr[l] <= arr[l + 1]) {
45+
l += 1;
46+
}
47+
48+
while (l < r && arr[r] >= arr[r - 1]) {
49+
r -= 1;
50+
}
51+
52+
if (l >= r) {
53+
return 0;
54+
}
55+
56+
int res = min(n - l - 1, r);
57+
int i = 0;
58+
int j = r;
59+
while (i <= l && j <= n - 1) {
60+
if (arr[i] <= arr[j]) {
61+
res = min(res, j - i - 1);
62+
i += 1;
63+
} else {
64+
j += 1;
65+
}
66+
}
67+
68+
return res;
69+
}
70+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tag: Array, Two Pointers, Binary Search, Stack, Monotonic Stack
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining elements in arr are non-decreasing.
8+
# Return the length of the shortest subarray to remove.
9+
# A subarray is a contiguous subsequence of the array.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: arr = [1,2,3,10,4,2,3,5]
14+
# Output: 3
15+
# Explanation: The shortest subarray we can remove is [10,4,2] of length 3. The remaining elements after that will be [1,2,3,3,5] which are sorted.
16+
# Another correct solution is to remove the subarray [3,10,4].
17+
#
18+
# Example 2:
19+
#
20+
# Input: arr = [5,4,3,2,1]
21+
# Output: 4
22+
# Explanation: Since the array is strictly decreasing, we can only keep a single element. Therefore we need to remove a subarray of length 4, either [5,4,3,2] or [4,3,2,1].
23+
#
24+
# Example 3:
25+
#
26+
# Input: arr = [1,2,3]
27+
# Output: 0
28+
# Explanation: The array is already non-decreasing. We do not need to remove any elements.
29+
#
30+
#  
31+
# Constraints:
32+
#
33+
# 1 <= arr.length <= 105
34+
# 0 <= arr[i] <= 109
35+
#
36+
#
37+
38+
class Solution:
39+
def findLengthOfShortestSubarray(self, arr: List[int]) -> int:
40+
n = len(arr)
41+
l = 0
42+
r = n - 1
43+
44+
while l < r and arr[l] <= arr[l + 1]:
45+
l += 1
46+
47+
while l < r and arr[r] >= arr[r - 1]:
48+
r -= 1
49+
50+
if l >= r:
51+
return 0
52+
53+
res = min(n - l - 1, r)
54+
i = 0
55+
j = r
56+
while i <= l and j <= n - 1:
57+
if arr[i] <= arr[j]:
58+
res = min(res, j - i - 1)
59+
i += 1
60+
else:
61+
j += 1
62+
63+
return res
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Tag: Array, Hash Table, Sorting, Enumeration
2+
// Time: O(1)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/J_ODVpvGvUg
7+
8+
// You are given an integer array digits, where each element is a digit. The array may contain duplicates.
9+
// You need to find all the unique integers that follow the given requirements:
10+
//
11+
// The integer consists of the concatenation of three elements from digits in any arbitrary order.
12+
// The integer does not have leading zeros.
13+
// The integer is even.
14+
//
15+
// For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
16+
// Return a sorted array of the unique integers.
17+
//  
18+
// Example 1:
19+
//
20+
// Input: digits = [2,1,3,0]
21+
// Output: [102,120,130,132,210,230,302,310,312,320]
22+
// Explanation: All the possible integers that follow the requirements are in the output array.
23+
// Notice that there are no odd integers or integers with leading zeros.
24+
//
25+
// Example 2:
26+
//
27+
// Input: digits = [2,2,8,8,2]
28+
// Output: [222,228,282,288,822,828,882]
29+
// Explanation: The same digit can be used as many times as it appears in digits.
30+
// In this example, the digit 8 is used twice each time in 288, 828, and 882.
31+
//
32+
// Example 3:
33+
//
34+
// Input: digits = [3,7,5]
35+
// Output: []
36+
// Explanation: No even integers can be formed using the given digits.
37+
//
38+
//  
39+
// Constraints:
40+
//
41+
// 3 <= digits.length <= 100
42+
// 0 <= digits[i] <= 9
43+
//
44+
//
45+
46+
class Solution {
47+
public:
48+
vector<int> findEvenNumbers(vector<int>& digits) {
49+
vector<int> counter(10, 0);
50+
for (auto &x: digits) {
51+
counter[x] += 1;
52+
}
53+
54+
vector<int> res;
55+
helper(0, 0, res, counter);
56+
return res;
57+
}
58+
59+
void helper(int i, int ans, vector<int> &res, vector<int> &counter) {
60+
if (i == 3) {
61+
res.push_back(ans);
62+
return;
63+
}
64+
65+
for (int d = 0; d <= 9; d++) {
66+
if ((i == 0 && d == 0) || (i == 2 && d % 2 == 1) || counter[d] == 0) {
67+
continue;
68+
}
69+
70+
counter[d] -= 1;
71+
helper(i + 1, ans * 10 + d, res, counter);
72+
counter[d] += 1;
73+
}
74+
75+
}
76+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Tag: Array, Hash Table, Sorting, Enumeration
2+
# Time: O(1)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/J_ODVpvGvUg
7+
8+
# You are given an integer array digits, where each element is a digit. The array may contain duplicates.
9+
# You need to find all the unique integers that follow the given requirements:
10+
#
11+
# The integer consists of the concatenation of three elements from digits in any arbitrary order.
12+
# The integer does not have leading zeros.
13+
# The integer is even.
14+
#
15+
# For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
16+
# Return a sorted array of the unique integers.
17+
#  
18+
# Example 1:
19+
#
20+
# Input: digits = [2,1,3,0]
21+
# Output: [102,120,130,132,210,230,302,310,312,320]
22+
# Explanation: All the possible integers that follow the requirements are in the output array.
23+
# Notice that there are no odd integers or integers with leading zeros.
24+
#
25+
# Example 2:
26+
#
27+
# Input: digits = [2,2,8,8,2]
28+
# Output: [222,228,282,288,822,828,882]
29+
# Explanation: The same digit can be used as many times as it appears in digits.
30+
# In this example, the digit 8 is used twice each time in 288, 828, and 882.
31+
#
32+
# Example 3:
33+
#
34+
# Input: digits = [3,7,5]
35+
# Output: []
36+
# Explanation: No even integers can be formed using the given digits.
37+
#
38+
#  
39+
# Constraints:
40+
#
41+
# 3 <= digits.length <= 100
42+
# 0 <= digits[i] <= 9
43+
#
44+
#
45+
46+
from collections import Counter
47+
class Solution:
48+
def findEvenNumbers(self, digits: List[int]) -> List[int]:
49+
n = len(digits)
50+
counter = Counter(digits)
51+
res = []
52+
self.helper(0, counter, 0, res)
53+
return res
54+
55+
def helper(self, i: int, counter: dict, ans: int, res: list):
56+
if i == 3:
57+
res.append(ans)
58+
return
59+
60+
for d in range(0, 10):
61+
if (i == 0 and d == 0) or (i == 2 and d % 2 == 1) or counter[d] == 0:
62+
continue
63+
64+
counter[d] -= 1
65+
self.helper(i + 1, counter, ans * 10 + d, res)
66+
counter[d] += 1

list/endlesscheng.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
121. https://www.lintcode.com/problem/merge-operations-to-turn-array-into-a-palindrome/
141141
122. https://www.lintcode.com/problem/3sum-smaller/
142142

143-
- https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
143+
123. https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/ +Mark
144144
- https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii/
145145
- https://leetcode.com/problems/recover-the-original-array/
146146
- https://leetcode.com/problems/minimize-connected-groups-by-inserting-interval/

test.mov

184 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)