Skip to content

Commit a007714

Browse files
committedMar 3, 2025
Mar-3
1 parent 5791fa3 commit a007714

9 files changed

+443
-39
lines changed
 

‎README.md

Lines changed: 20 additions & 11 deletions
Large diffs are not rendered by default.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Tag: Array, Binary Search, Greedy, Sliding Window, Sorting, Prefix Sum
2+
// Time: O(NlogN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// The frequency of an element is the number of times it occurs in an array.
8+
// You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.
9+
// Return the maximum possible frequency of an element after performing at most k operations.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: nums = [1,2,4], k = 5
14+
// Output: 3
15+
// Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
16+
// 4 has a frequency of 3.
17+
// Example 2:
18+
//
19+
// Input: nums = [1,4,8,13], k = 5
20+
// Output: 2
21+
// Explanation: There are multiple optimal solutions:
22+
// - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
23+
// - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
24+
// - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
25+
//
26+
// Example 3:
27+
//
28+
// Input: nums = [3,9,6], k = 2
29+
// Output: 1
30+
//
31+
//  
32+
// Constraints:
33+
//
34+
// 1 <= nums.length <= 105
35+
// 1 <= nums[i] <= 105
36+
// 1 <= k <= 105
37+
//
38+
//
39+
40+
class Solution {
41+
public:
42+
int maxFrequency(vector<int>& nums, int k) {
43+
int n = nums.size();
44+
sort(nums.begin(), nums.end());
45+
int i = 0;
46+
long long total = 0;
47+
int res = 0;
48+
for (int j = 0; j < n; j++) {
49+
total += nums[j];
50+
while (nums[j] * (j - i + 1L) - total > k) {
51+
total -= nums[i];
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 maxFrequency(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+
long long total = 0;
69+
for (j = 0; j < n; j++) {
70+
total += nums[j];
71+
if (nums[j] * (j - i + 1L) - total > k) {
72+
total -= nums[i];
73+
i += 1;
74+
}
75+
}
76+
return j - i;
77+
}
78+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Tag: Array, Binary Search, Greedy, Sliding Window, Sorting, Prefix Sum
2+
# Time: O(NlogN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# The frequency of an element is the number of times it occurs in an array.
8+
# You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.
9+
# Return the maximum possible frequency of an element after performing at most k operations.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: nums = [1,2,4], k = 5
14+
# Output: 3
15+
# Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4].
16+
# 4 has a frequency of 3.
17+
# Example 2:
18+
#
19+
# Input: nums = [1,4,8,13], k = 5
20+
# Output: 2
21+
# Explanation: There are multiple optimal solutions:
22+
# - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
23+
# - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
24+
# - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
25+
#
26+
# Example 3:
27+
#
28+
# Input: nums = [3,9,6], k = 2
29+
# Output: 1
30+
#
31+
#  
32+
# Constraints:
33+
#
34+
# 1 <= nums.length <= 105
35+
# 1 <= nums[i] <= 105
36+
# 1 <= k <= 105
37+
#
38+
#
39+
40+
class Solution:
41+
def maxFrequency(self, nums: List[int], k: int) -> int:
42+
n = len(nums)
43+
nums.sort()
44+
i = 0
45+
res = 0
46+
total = 0
47+
for j in range(n):
48+
total += nums[j]
49+
while nums[j] * (j - i + 1) - total > k:
50+
total -= nums[i]
51+
i += 1
52+
53+
res = max(res, j - i + 1)
54+
55+
return res
56+
57+
class Solution:
58+
def maxFrequency(self, nums: List[int], k: int) -> int:
59+
n = len(nums)
60+
nums.sort()
61+
i = 0
62+
total = 0
63+
for j in range(n):
64+
total += nums[j]
65+
if nums[j] * (j - i + 1) - total > k:
66+
total -= nums[i]
67+
i += 1
68+
69+
return j - i + 1
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Tag: Array, Two Pointers, Simulation
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
8+
//
9+
// Every element less than pivot appears before every element greater than pivot.
10+
// Every element equal to pivot appears in between the elements less than and greater than pivot.
11+
// The relative order of the elements less than pivot and the elements greater than pivot is maintained.
12+
//
13+
// More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.
14+
//
15+
//
16+
//
17+
// Return nums after the rearrangement.
18+
//  
19+
// Example 1:
20+
//
21+
// Input: nums = [9,12,5,10,14,3,10], pivot = 10
22+
// Output: [9,5,3,10,10,12,14]
23+
// Explanation:
24+
// The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
25+
// The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
26+
// The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
27+
//
28+
// Example 2:
29+
//
30+
// Input: nums = [-3,4,3,2], pivot = 2
31+
// Output: [-3,2,4,3]
32+
// Explanation:
33+
// The element -3 is less than the pivot so it is on the left side of the array.
34+
// The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
35+
// The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
36+
//
37+
//  
38+
// Constraints:
39+
//
40+
// 1 <= nums.length <= 105
41+
// -106 <= nums[i] <= 106
42+
// pivot equals to an element of nums.
43+
//
44+
//
45+
46+
class Solution {
47+
public:
48+
vector<int> pivotArray(vector<int>& nums, int pivot) {
49+
int n = nums.size();
50+
vector<int> res(n, pivot);
51+
int l = 0;
52+
for (int i = 0; i < n; i++) {
53+
if (nums[i] < pivot) {
54+
res[l] = nums[i];
55+
l += 1;
56+
}
57+
}
58+
59+
int r = n - 1;
60+
for (int i = n - 1; i >= 0; i--) {
61+
if (nums[i] > pivot) {
62+
res[r] = nums[i];
63+
r -= 1;
64+
}
65+
}
66+
return res;
67+
}
68+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tag: Array, Two Pointers, Simulation
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/_tyJM7UjGlo
7+
8+
# You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
9+
#
10+
# Every element less than pivot appears before every element greater than pivot.
11+
# Every element equal to pivot appears in between the elements less than and greater than pivot.
12+
# The relative order of the elements less than pivot and the elements greater than pivot is maintained.
13+
#
14+
# More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.
15+
#
16+
#
17+
#
18+
# Return nums after the rearrangement.
19+
#  
20+
# Example 1:
21+
#
22+
# Input: nums = [9,12,5,10,14,3,10], pivot = 10
23+
# Output: [9,5,3,10,10,12,14]
24+
# Explanation:
25+
# The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
26+
# The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
27+
# The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
28+
#
29+
# Example 2:
30+
#
31+
# Input: nums = [-3,4,3,2], pivot = 2
32+
# Output: [-3,2,4,3]
33+
# Explanation:
34+
# The element -3 is less than the pivot so it is on the left side of the array.
35+
# The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
36+
# The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
37+
#
38+
#  
39+
# Constraints:
40+
#
41+
# 1 <= nums.length <= 105
42+
# -106 <= nums[i] <= 106
43+
# pivot equals to an element of nums.
44+
#
45+
#
46+
47+
class Solution:
48+
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
49+
n = len(nums)
50+
res = [pivot for i in range(n)]
51+
l = 0
52+
for i in range(n):
53+
if nums[i] < pivot:
54+
res[l] = nums[i]
55+
l += 1
56+
57+
r = n - 1
58+
for i in range(n - 1, -1, -1):
59+
if nums[i] > pivot:
60+
res[r] = nums[i]
61+
r -= 1
62+
63+
return res
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Tag: Hash Table, String, Sliding Window
2+
// Time: -
3+
// Space: -
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/_tyJM7UjGlo
7+
8+
// You are given a string s consisting of the characters 'a', 'b', and 'c' and a non-negative integer k. Each minute, you may take either the leftmost character of s, or the rightmost character of s.
9+
// Return the minimum number of minutes needed for you to take at least k of each character, or return -1 if it is not possible to take k of each character.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: s = "aabaaaacaabc", k = 2
14+
// Output: 8
15+
// Explanation:
16+
// Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
17+
// Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
18+
// A total of 3 + 5 = 8 minutes is needed.
19+
// It can be proven that 8 is the minimum number of minutes needed.
20+
//
21+
// Example 2:
22+
//
23+
// Input: s = "a", k = 1
24+
// Output: -1
25+
// Explanation: It is not possible to take one 'b' or 'c' so return -1.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= s.length <= 105
31+
// s consists of only the letters 'a', 'b', and 'c'.
32+
// 0 <= k <= s.length
33+
//
34+
//
35+
36+
class Solution {
37+
public:
38+
int takeCharacters(string s, int k) {
39+
int n = s.size();
40+
vector<int> counter(3, 0);
41+
for (char x: s) {
42+
counter[x - 'a'] += 1;
43+
}
44+
45+
for (int i = 0; i < 3; i++) {
46+
if (counter[i] < k) {
47+
return -1;
48+
}
49+
counter[i] -= k;
50+
}
51+
52+
int i = 0;
53+
int res = 0;
54+
vector<int> tmp(3, 0);
55+
for(int j = 0; j < n; j++) {
56+
tmp[s[j] - 'a'] += 1;
57+
while (tmp[s[j] - 'a'] > counter[s[j] - 'a']) {
58+
tmp[s[i] - 'a'] -= 1;
59+
i += 1;
60+
}
61+
res = max(res, j - i + 1);
62+
}
63+
64+
return n - res;
65+
}
66+
};

0 commit comments

Comments
 (0)