-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumFrequencyAfterSubarrayOperation.java
More file actions
218 lines (172 loc) · 8 KB
/
MaximumFrequencyAfterSubarrayOperation.java
File metadata and controls
218 lines (172 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package Algorithms.GreedyAlgorithms;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 05 July 2025
* @link 3434. Maximum Frequency After Subarray Operation <a href="https://leetcode.com/problems/maximum-frequency-after-subarray-operation">Leetcode link</a>
* @topics Array, Greedy, Dp, HashTable
* @companies amazon, google, microsoft
*/
public class MaximumFrequencyAfterSubarrayOperation {
public static void main(String[] args) {
int[] nums = {10, 2, 3, 10, 3, 3, 10, 3, 3};
int k = 10;
System.out.println("maxFrequency using Kadane's Algorithm: " + maxFrequencyUsingKadanesAlgorithm(nums, k));
System.out.println("maxFrequency 2: " + maxFrequency2(nums, k));
}
/**
* @TimeComplexity O(N*U), where N is the length of nums and U is the number of unique numbers
* @SpaceComplexity O(U)
* Using Kadane's Algorithm
* --> see {@link Algorithms.DivideAndConquer.MaximumSubArray#maxSubArrayUsingKadanesAlgorithm}
* and {@link Algorithms.DivideAndConquer.MaximumSubArray#maxSubArrayUsingKadanesAlgorithm2}
We know that if the numbers are different like 2,2,3,4
then even if we add x=2 to that numbers 4,4,5,6 the numbers will be still different
So, assume that we need max frequency of any number in the subArray and if we add x=k-num,
then we'll get the max frequency
nums=[10, 2, 3, 10, 3, 3, 10, 3, 3], target=10
set=[10, 2, 3]
when target = 10
[10, 2, 3, 10, 3, 3, 10, 3, 3]
skip as target == k == 10
when target = 2
[10, 2, 3, 10, 3, 3, 10, 3, 3]
i ---> count = -1 => 0, maxCount = 0
i ---> count = 1, maxCount = 1
i ---> count = 1, maxCount = 1
i ---> count = 0, maxCount = 1 ---> 1 target and 1 k ---> are canceled out
i ---> count = 0, maxCount = 1
i ---> count = 0, maxCount = 1
i ---> count = -1 => 0, maxCount = 1 ---> 1 target and 1 k ---> are canceled out
i ---> count = 0, maxCount = 1
i ---> count = 0, maxCount = 1
maxCount = 1
when target = 3
[10, 2, 3, 10, 3, 3, 10, 3, 3]
i ---> count = -1 => 0, maxCount = 0
i ---> count = 0, maxCount = 0
i ---> count = 1, maxCount = 1
i ---> count = 0, maxCount = 1 ---> 1 target and 1 k ---> are canceled out
i ---> count = 1, maxCount = 1
i ---> count = 2, maxCount = 2
i ---> count = 1, maxCount = 2 ---> 1 target and 1 k ---> are canceled out
i ---> count = 2, maxCount = 2
i ---> count = 3, maxCount = 3
maxCount = 3
*/
public static int maxFrequencyUsingKadanesAlgorithm(int[] nums, int k) {
final int n = nums.length;
int kCount = 0;
int res = 0;
Set<Integer> set = new HashSet<>();
// Count the number of k's and collect unique numbers
for (int num : nums) {
if(num == k) {
kCount++;
}
set.add(num);
}
if(kCount == n) {
return n;
}
// Try every number ≠ k as a conversion target
for (int target : set) {
if(target == k) {
continue;
}
int count = 0;
int maxCount = 0;
// Kadane's Algorithm
for(int num: nums) {
if(num == target) {
count++;
} else if(num == k) { // 1 target and 1 k ---> are canceled out
count--;
}
if (count < 0) {
count = 0;
}
maxCount = Math.max(maxCount, count);
}
res = Math.max(res, maxCount);
}
return res+kCount;
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
* int[] count = new int[51]; // as the problem stated "1 <= nums[i] <= 50" and skip count[0] 0th index ele
when target = 10
[10, 2, 3, 10, 3, 3, 10, 3, 3]
i ---> count[10] = max(count[10], count[10])+1 => max(0, 0)+1 => 1, count[10] = 1, res=max(0, 1-1)=0
i ---> count[2] = max(count[2], count[10])+1 => max(0, 1)+1 => 2, count[10] = 1, res=max(0, 2-1)=1
i ---> count[3] = max(count[3], count[10])+1 => max(0, 1)+1 => 2, count[10] = 1, res=max(0, 1-1)=0
i ---> count[10] = max(count[10], count[10])+1 => max(1, 1)+1 => 2, count[10] = 2, res=max(0, 1-1)=0
i ---> count[3] = max(count[3], count[10])+1 => max(2, 2)+1 => 3, count[10] = 2, res=max(0, 1-1)=0
i ---> count[3] = max(count[3], count[10])+1 => max(3, 2)+1 => 4, count[10] = 2, res=max(0, 1-1)=0
i ---> count[10] = max(count[10], count[10])+1 => max(2, 2)+1 => 3, count[10] = 3, res=max(0, 1-1)=0
i ---> count[3] = max(count[3], count[10])+1 => max(4, 3)+1 => 5, count[10] = 3, res=max(0, 1-1)=0
i ---> count[3] = max(count[3], count[10])+1 => max(5, 3)+1 => 6, count[10] = 3, res=max(0, 1-1)=0
*/
public static int maxFrequency2(int[] nums, int k) {
int[] count = new int[51]; // maxFrequency we achieve upto now if target in count[num]
int maxFreq = 0; // max frequency of num i.e., not including k
for (int num : nums) {
count[num] = Math.max(count[num], count[k]) + 1; // +1 for current num
maxFreq = Math.max(maxFreq, count[num] - count[k]);
}
return count[k] + maxFreq;
}
/**
* same like above {@link #maxFrequency2(int[], int)} but use HashMap instead of array
*/
public static int maxFrequency3(int[] nums, int k) {
Map<Integer, Integer> count = new HashMap<>();
int maxFreq = 0; // max frequency of num i.e., not including k
for (int num : nums) {
count.put(num, Math.max(count.getOrDefault(num, 0), count.getOrDefault(k, 0)) + 1); // +1 for current num
maxFreq = Math.max(maxFreq, count.getOrDefault(num, 0) - count.getOrDefault(k, 0));
}
return count.getOrDefault(k, 0) + maxFreq;
}
public static int maxFrequencyUsingKadanesAlgorithm2(int[] nums, int k) {
int n = nums.length;
int kCount = 0;
Set<Integer> set = new HashSet<>();
// Count the number of k's and collect unique numbers
for (int num : nums) {
if(num == k) {
kCount++;
}
set.add(num);
}
if(kCount == n) {
return n;
}
int res = 0;
for (int target : set) {
if(target == k) {continue;} // optional because in kadane() we 1st check (num==k) and then (num==target)
res = Math.max(res, kadane(nums, k, target));
}
return res + kCount;
}
private static int kadane(int[] nums, int k, int target) {
int maxCount = 0, count = 0;
for (int num : nums) {
if (num == k) {
count--;
}
else if (num == target) {
count++;
}
if (count < 0) {
count = 0;
}
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
}