Skip to content

Commit e00e407

Browse files
committed
Mar-1
1 parent 231d46a commit e00e407

12 files changed

+637
-45
lines changed

README.md

Lines changed: 17 additions & 10 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Tag: Array, Two Pointers, Simulation
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/l-KC4smk5sM
7+
8+
// You are given a 0-indexed array nums of size n consisting of non-negative integers.
9+
// You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:
10+
//
11+
// If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.
12+
//
13+
// After performing all the operations, shift all the 0's to the end of the array.
14+
//
15+
// For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
16+
//
17+
// Return the resulting array.
18+
// Note that the operations are applied sequentially, not all at once.
19+
//  
20+
// Example 1:
21+
//
22+
// Input: nums = [1,2,2,1,1,0]
23+
// Output: [1,4,2,0,0,0]
24+
// Explanation: We do the following operations:
25+
// - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
26+
// - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
27+
// - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
28+
// - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
29+
// - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
30+
// After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
31+
//
32+
// Example 2:
33+
//
34+
// Input: nums = [0,1]
35+
// Output: [1,0]
36+
// Explanation: No operation can be applied, we just shift the 0 to the end.
37+
//
38+
//  
39+
// Constraints:
40+
//
41+
// 2 <= nums.length <= 2000
42+
// 0 <= nums[i] <= 1000
43+
//
44+
//
45+
46+
class Solution {
47+
public:
48+
vector<int> applyOperations(vector<int>& nums) {
49+
int n = nums.size();
50+
for (int i = 0; i < n - 1; i++) {
51+
if (nums[i] == nums[i + 1]) {
52+
nums[i] *= 2;
53+
nums[i + 1] = 0;
54+
}
55+
}
56+
int j = 0;
57+
for (int i = 0; i < n; i++) {
58+
if (nums[i]) {
59+
swap(nums[i], nums[j]);
60+
j += 1;
61+
}
62+
}
63+
return nums;
64+
}
65+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Tag: Array, Two Pointers, Simulation
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/l-KC4smk5sM
7+
8+
# You are given a 0-indexed array nums of size n consisting of non-negative integers.
9+
# You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:
10+
#
11+
# If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.
12+
#
13+
# After performing all the operations, shift all the 0's to the end of the array.
14+
#
15+
# For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].
16+
#
17+
# Return the resulting array.
18+
# Note that the operations are applied sequentially, not all at once.
19+
#  
20+
# Example 1:
21+
#
22+
# Input: nums = [1,2,2,1,1,0]
23+
# Output: [1,4,2,0,0,0]
24+
# Explanation: We do the following operations:
25+
# - i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
26+
# - i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
27+
# - i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
28+
# - i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
29+
# - i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
30+
# After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
31+
#
32+
# Example 2:
33+
#
34+
# Input: nums = [0,1]
35+
# Output: [1,0]
36+
# Explanation: No operation can be applied, we just shift the 0 to the end.
37+
#
38+
#  
39+
# Constraints:
40+
#
41+
# 2 <= nums.length <= 2000
42+
# 0 <= nums[i] <= 1000
43+
#
44+
#
45+
46+
class Solution:
47+
def applyOperations(self, nums: List[int]) -> List[int]:
48+
n = len(nums)
49+
for i in range(n - 1):
50+
if nums[i] == nums[i + 1]:
51+
nums[i] *= 2
52+
nums[i + 1] = 0
53+
54+
j = 0
55+
for i in range(n):
56+
if nums[i] > 0:
57+
nums[i], nums[j] = nums[j], nums[i]
58+
j += 1
59+
60+
return nums
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Tag: Array, Binary Search, Sliding Window, Sorting
2+
// Time: O(NlogN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed array nums and a non-negative integer k.
8+
// In one operation, you can do the following:
9+
//
10+
// Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].
11+
// Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].
12+
//
13+
// The beauty of the array is the length of the longest subsequence consisting of equal elements.
14+
// Return the maximum possible beauty of the array nums after applying the operation any number of times.
15+
// Note that you can apply the operation to each index only once.
16+
// A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
17+
//  
18+
// Example 1:
19+
//
20+
// Input: nums = [4,6,1,2], k = 2
21+
// Output: 3
22+
// Explanation: In this example, we apply the following operations:
23+
// - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
24+
// - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
25+
// After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
26+
// It can be proven that 3 is the maximum possible length we can achieve.
27+
//
28+
// Example 2:
29+
//
30+
// Input: nums = [1,1,1,1], k = 10
31+
// Output: 4
32+
// Explanation: In this example we don't have to apply any operations.
33+
// The beauty of the array nums is 4 (whole array).
34+
//
35+
//  
36+
// Constraints:
37+
//
38+
// 1 <= nums.length <= 105
39+
// 0 <= nums[i], k <= 105
40+
//
41+
//
42+
43+
class Solution {
44+
public:
45+
int maximumBeauty(vector<int>& nums, int k) {
46+
int n = nums.size();
47+
sort(nums.begin(), nums.end());
48+
int i = 0;
49+
int res = 0;
50+
for (int j = 0; j < n; j++) {
51+
while (nums[j] - nums[i] > 2 * k) {
52+
i += 1;
53+
}
54+
55+
res = max(res, j - i + 1);
56+
}
57+
return res;
58+
}
59+
};
60+
61+
class Solution {
62+
public:
63+
int maximumBeauty(vector<int>& nums, int k) {
64+
int n = nums.size();
65+
sort(nums.begin(), nums.end());
66+
int i = 0;
67+
int j = 0;
68+
for (j = 0; j < n; j++) {
69+
if (nums[j] - nums[i] > 2 * k) {
70+
i += 1;
71+
}
72+
}
73+
return j - i;
74+
}
75+
};
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Tag: Array, Binary Search, Sliding Window, Sorting
2+
# Time: O(NlogN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a 0-indexed array nums and a non-negative integer k.
8+
# In one operation, you can do the following:
9+
#
10+
# Choose an index i that hasn't been chosen before from the range [0, nums.length - 1].
11+
# Replace nums[i] with any integer from the range [nums[i] - k, nums[i] + k].
12+
#
13+
# The beauty of the array is the length of the longest subsequence consisting of equal elements.
14+
# Return the maximum possible beauty of the array nums after applying the operation any number of times.
15+
# Note that you can apply the operation to each index only once.
16+
# A subsequence of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the order of the remaining elements.
17+
#  
18+
# Example 1:
19+
#
20+
# Input: nums = [4,6,1,2], k = 2
21+
# Output: 3
22+
# Explanation: In this example, we apply the following operations:
23+
# - Choose index 1, replace it with 4 (from range [4,8]), nums = [4,4,1,2].
24+
# - Choose index 3, replace it with 4 (from range [0,4]), nums = [4,4,1,4].
25+
# After the applied operations, the beauty of the array nums is 3 (subsequence consisting of indices 0, 1, and 3).
26+
# It can be proven that 3 is the maximum possible length we can achieve.
27+
#
28+
# Example 2:
29+
#
30+
# Input: nums = [1,1,1,1], k = 10
31+
# Output: 4
32+
# Explanation: In this example we don't have to apply any operations.
33+
# The beauty of the array nums is 4 (whole array).
34+
#
35+
#  
36+
# Constraints:
37+
#
38+
# 1 <= nums.length <= 105
39+
# 0 <= nums[i], k <= 105
40+
#
41+
#
42+
43+
class Solution:
44+
def maximumBeauty(self, nums: List[int], k: int) -> int:
45+
n = len(nums)
46+
nums.sort()
47+
i = 0
48+
res = 0
49+
for j in range(n):
50+
while nums[j] - nums[i] > 2 * k:
51+
i += 1
52+
53+
res = max(res, j - i + 1)
54+
55+
return res
56+
57+
class Solution:
58+
def maximumBeauty(self, nums: List[int], k: int) -> int:
59+
n = len(nums)
60+
nums.sort()
61+
i = 0
62+
for j in range(n):
63+
if nums[j] - nums[i] > 2 * k:
64+
i += 1
65+
66+
return j - i + 1
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Tag: Array, Greedy, Sliding Window
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given an integer eventTime denoting the duration of an event, where the event occurs from time t = 0 to time t = eventTime.
8+
// You are also given two integer arrays startTime and endTime, each of length n. These represent the start and end time of n non-overlapping meetings, where the ith meeting occurs during the time [startTime[i], endTime[i]].
9+
// You can reschedule at most k meetings by moving their start time while maintaining the same duration, to maximize the longest continuous period of free time during the event.
10+
// The relative order of all the meetings should stay the same and they should remain non-overlapping.
11+
// Return the maximum amount of free time possible after rearranging the meetings.
12+
// Note that the meetings can not be rescheduled to a time outside the event.
13+
//  
14+
// Example 1:
15+
//
16+
// Input: eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5]
17+
// Output: 2
18+
// Explanation:
19+
//
20+
// Reschedule the meeting at [1, 2] to [2, 3], leaving no meetings during the time [0, 2].
21+
//
22+
// Example 2:
23+
//
24+
// Input: eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10]
25+
// Output: 6
26+
// Explanation:
27+
//
28+
// Reschedule the meeting at [2, 4] to [1, 3], leaving no meetings during the time [3, 9].
29+
//
30+
// Example 3:
31+
//
32+
// Input: eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]
33+
// Output: 0
34+
// Explanation:
35+
// There is no time during the event not occupied by meetings.
36+
//
37+
//  
38+
// Constraints:
39+
//
40+
// 1 <= eventTime <= 109
41+
// n == startTime.length == endTime.length
42+
// 2 <= n <= 105
43+
// 1 <= k <= n
44+
// 0 <= startTime[i] < endTime[i] <= eventTime
45+
// endTime[i] <= startTime[i + 1] where i lies in the range [0, n - 2].
46+
//
47+
//
48+
49+
class Solution {
50+
public:
51+
int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {
52+
int start = 0;
53+
vector<int> slots;
54+
for (int i = 0; i < startTime.size(); i++) {
55+
slots.push_back(startTime[i] - start);
56+
start = endTime[i];
57+
}
58+
slots.push_back(eventTime - start);
59+
if (k + 1 >= slots.size()) {
60+
return accumulate(slots.begin(), slots.end(), 0);
61+
}
62+
63+
int res = 0;
64+
int tmp = 0;
65+
for (int i = 0; i < slots.size(); i++) {
66+
tmp += slots[i];
67+
if (i >= k) {
68+
res = max(res, tmp);
69+
tmp -= slots[i - k];
70+
}
71+
72+
}
73+
74+
return res;
75+
}
76+
};

0 commit comments

Comments
 (0)