Skip to content

Commit 6a13875

Browse files
committed
Mar-9
1 parent 2cdde70 commit 6a13875

10 files changed

+527
-8
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Large diffs are not rendered by default.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Tag: Array, Hash Table, Binary Search, Sliding Window
2+
// Time: O(NlogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given an integer array nums. In one operation, you can replace any element in nums with any integer.
8+
// nums is considered continuous if both of the following conditions are fulfilled:
9+
//
10+
// All elements in nums are unique.
11+
// The difference between the maximum element and the minimum element in nums equals nums.length - 1.
12+
//
13+
// For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.
14+
// Return the minimum number of operations to make nums continuous.
15+
//  
16+
// Example 1:
17+
//
18+
// Input: nums = [4,2,5,3]
19+
// Output: 0
20+
// Explanation: nums is already continuous.
21+
//
22+
// Example 2:
23+
//
24+
// Input: nums = [1,2,3,5,6]
25+
// Output: 1
26+
// Explanation: One possible solution is to change the last element to 4.
27+
// The resulting array is [1,2,3,5,4], which is continuous.
28+
//
29+
// Example 3:
30+
//
31+
// Input: nums = [1,10,100,1000]
32+
// Output: 3
33+
// Explanation: One possible solution is to:
34+
// - Change the second element to 2.
35+
// - Change the third element to 3.
36+
// - Change the fourth element to 4.
37+
// The resulting array is [1,2,3,4], which is continuous.
38+
//
39+
//  
40+
// Constraints:
41+
//
42+
// 1 <= nums.length <= 105
43+
// 1 <= nums[i] <= 109
44+
//
45+
//
46+
47+
class Solution {
48+
public:
49+
int minOperations(vector<int>& nums) {
50+
int n = nums.size();
51+
sort(nums.begin(), nums.end());
52+
nums.erase(unique(nums.begin(), nums.end()), nums.end());
53+
int res = n;
54+
for (int i = 0, j = 0; i < nums.size(); i++) {
55+
while (j < nums.size() && nums[j] < nums[i] + n) {
56+
int unique = j - i + 1;
57+
res = min(res, n - unique);
58+
j += 1;
59+
}
60+
}
61+
return res;
62+
}
63+
};
64+
65+
class Solution {
66+
public:
67+
int minOperations(vector<int>& nums) {
68+
int n = nums.size();
69+
sort(nums.begin(), nums.end());
70+
nums.erase(unique(nums.begin(), nums.end()), nums.end());
71+
int res = n;
72+
for (int i = 0; i < nums.size(); i++) {
73+
int j = upper_bound(nums.begin(), nums.end(), nums[i] + n - 1) - nums.begin();
74+
int unique = j - i;
75+
res = min(res, n - unique);
76+
}
77+
return res;
78+
}
79+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Tag: Array, Hash Table, Binary Search, Sliding Window
2+
# Time: O(NlogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given an integer array nums. In one operation, you can replace any element in nums with any integer.
8+
# nums is considered continuous if both of the following conditions are fulfilled:
9+
#
10+
# All elements in nums are unique.
11+
# The difference between the maximum element and the minimum element in nums equals nums.length - 1.
12+
#
13+
# For example, nums = [4, 2, 5, 3] is continuous, but nums = [1, 2, 3, 5, 6] is not continuous.
14+
# Return the minimum number of operations to make nums continuous.
15+
#  
16+
# Example 1:
17+
#
18+
# Input: nums = [4,2,5,3]
19+
# Output: 0
20+
# Explanation: nums is already continuous.
21+
#
22+
# Example 2:
23+
#
24+
# Input: nums = [1,2,3,5,6]
25+
# Output: 1
26+
# Explanation: One possible solution is to change the last element to 4.
27+
# The resulting array is [1,2,3,5,4], which is continuous.
28+
#
29+
# Example 3:
30+
#
31+
# Input: nums = [1,10,100,1000]
32+
# Output: 3
33+
# Explanation: One possible solution is to:
34+
# - Change the second element to 2.
35+
# - Change the third element to 3.
36+
# - Change the fourth element to 4.
37+
# The resulting array is [1,2,3,4], which is continuous.
38+
#
39+
#  
40+
# Constraints:
41+
#
42+
# 1 <= nums.length <= 105
43+
# 1 <= nums[i] <= 109
44+
#
45+
#
46+
47+
class Solution:
48+
def minOperations(self, nums: List[int]) -> int:
49+
n = len(nums)
50+
nums = sorted(set(nums))
51+
res = n
52+
j = 0
53+
for i in range(len(nums)):
54+
while j < len(nums) and nums[j] < nums[i] + n:
55+
unique = j - i + 1
56+
res = min(res, n - unique)
57+
j += 1
58+
59+
return res
60+
61+
import bisect
62+
class Solution:
63+
def minOperations(self, nums: List[int]) -> int:
64+
n = len(nums)
65+
nums = sorted(set(nums))
66+
res = n
67+
68+
for i in range(len(nums)):
69+
j = bisect.bisect_right(nums, nums[i] + n - 1)
70+
unique = j - i
71+
res = min(res, n - unique)
72+
73+
return res
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Tag: Array, Binary Search, Sliding Window, Dynamic Programming
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer k.
8+
// You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.
9+
//
10+
// For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.
11+
//
12+
// Return the maximum number of prizes you can win if you choose the two segments optimally.
13+
//  
14+
// Example 1:
15+
//
16+
// Input: prizePositions = [1,1,2,2,3,3,5], k = 2
17+
// Output: 7
18+
// Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
19+
//
20+
// Example 2:
21+
//
22+
// Input: prizePositions = [1,2,3,4], k = 0
23+
// Output: 2
24+
// Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.
25+
//
26+
//  
27+
// Constraints:
28+
//
29+
// 1 <= prizePositions.length <= 105
30+
// 1 <= prizePositions[i] <= 109
31+
// 0 <= k <= 109
32+
// prizePositions is sorted in non-decreasing order.
33+
//
34+
//  
35+
//
36+
//
37+
38+
class Solution {
39+
public:
40+
int maximizeWin(vector<int>& prizePositions, int k) {
41+
int n = prizePositions.size();
42+
vector<int> dp(n + 1, 0);
43+
int res = 0;
44+
int i = 0;
45+
for (int j = 0; j < n; j++) {
46+
while (prizePositions[j] - prizePositions[i] > k) {
47+
i += 1;
48+
}
49+
50+
dp[j + 1] = max(dp[j], j - i + 1);
51+
res = max(res, j - i + 1 + dp[i]);
52+
}
53+
54+
return res;
55+
}
56+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Tag: Array, Binary Search, Sliding Window, Dynamic Programming
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer k.
8+
# You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.
9+
#
10+
# For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.
11+
#
12+
# Return the maximum number of prizes you can win if you choose the two segments optimally.
13+
#  
14+
# Example 1:
15+
#
16+
# Input: prizePositions = [1,1,2,2,3,3,5], k = 2
17+
# Output: 7
18+
# Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
19+
#
20+
# Example 2:
21+
#
22+
# Input: prizePositions = [1,2,3,4], k = 0
23+
# Output: 2
24+
# Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.
25+
#
26+
#  
27+
# Constraints:
28+
#
29+
# 1 <= prizePositions.length <= 105
30+
# 1 <= prizePositions[i] <= 109
31+
# 0 <= k <= 109
32+
# prizePositions is sorted in non-decreasing order.
33+
#
34+
#  
35+
#
36+
#
37+
38+
class Solution:
39+
def maximizeWin(self, prizePositions: List[int], k: int) -> int:
40+
n = len(prizePositions)
41+
dp = [0 for i in range(n + 1)]
42+
i = 0
43+
res = 0
44+
for j in range(n):
45+
while prizePositions[j] - prizePositions[i] > k:
46+
i += 1
47+
48+
dp[j + 1] = max(dp[j], j - i + 1)
49+
res = max(res, j - i + 1 + dp[i])
50+
51+
return res
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Tag: Array, Sliding Window
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:
8+
//
9+
// colors[i] == 0 means that tile i is red.
10+
// colors[i] == 1 means that tile i is blue.
11+
//
12+
// Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.
13+
// Return the number of alternating groups.
14+
// Note that since colors represents a circle, the first and the last tiles are considered to be next to each other.
15+
//  
16+
// Example 1:
17+
//
18+
// Input: colors = [1,1,1]
19+
// Output: 0
20+
// Explanation:
21+
//
22+
//
23+
// Example 2:
24+
//
25+
// Input: colors = [0,1,0,0,1]
26+
// Output: 3
27+
// Explanation:
28+
//
29+
// Alternating groups:
30+
//
31+
//
32+
//  
33+
// Constraints:
34+
//
35+
// 3 <= colors.length <= 100
36+
// 0 <= colors[i] <= 1
37+
//
38+
//
39+
40+
class Solution {
41+
public:
42+
int numberOfAlternatingGroups(vector<int>& colors) {
43+
int n = colors.size();
44+
int k = 3;
45+
int i = 0;
46+
int res = 0;
47+
for (int j = 1; i < n; j++) {
48+
if (colors[j % n] == colors[(j - 1) % n]) {
49+
i = j;
50+
}
51+
52+
if (j - i + 1 == k) {
53+
res += 1;
54+
i += 1;
55+
}
56+
}
57+
return res;
58+
}
59+
};

leetcode/3206.alternating-groups-i.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Tag: Array, Sliding Window
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# There is a circle of red and blue tiles. You are given an array of integers colors. The color of tile i is represented by colors[i]:
8+
#
9+
# colors[i] == 0 means that tile i is red.
10+
# colors[i] == 1 means that tile i is blue.
11+
#
12+
# Every 3 contiguous tiles in the circle with alternating colors (the middle tile has a different color from its left and right tiles) is called an alternating group.
13+
# Return the number of alternating groups.
14+
# Note that since colors represents a circle, the first and the last tiles are considered to be next to each other.
15+
#  
16+
# Example 1:
17+
#
18+
# Input: colors = [1,1,1]
19+
# Output: 0
20+
# Explanation:
21+
#
22+
#
23+
# Example 2:
24+
#
25+
# Input: colors = [0,1,0,0,1]
26+
# Output: 3
27+
# Explanation:
28+
#
29+
# Alternating groups:
30+
#
31+
#
32+
#  
33+
# Constraints:
34+
#
35+
# 3 <= colors.length <= 100
36+
# 0 <= colors[i] <= 1
37+
#
38+
#
39+
40+
class Solution:
41+
def numberOfAlternatingGroups(self, colors: List[int]) -> int:
42+
n = len(colors)
43+
k = 3
44+
i = 0
45+
j = 1
46+
res = 0
47+
while i < n:
48+
if colors[j % n] == colors[(j - 1) % n]:
49+
i = j
50+
51+
if j - i + 1 == k:
52+
res += 1
53+
i += 1
54+
55+
j += 1
56+
57+
return res

0 commit comments

Comments
 (0)