-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAmazon_MaxDeviationKadaneAlgo.cpp
107 lines (83 loc) · 2.97 KB
/
Amazon_MaxDeviationKadaneAlgo.cpp
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
/*
question:
Let's have a string: abbbcacbcdce
For substring abbb, you have most frequent letter b -> 3 and least frequent letter a -> 1.
So the deviation is = most frequent - least frequent = 3 - 1 = 2. You need to look at all the substrings and find the max deviation.
Here substring cacbcdc has the max deviation.
Frequency is like below:
c -> 4, a ->1, b->1, d->1.
So max freq - min freq = 4 - 1 = 3.
Among all substrings deviation, this is the max. So need to return it.
String length is 10^4. So you can't check each substring.
Idea
We take all possible distinct as ( c1 & c2 pairs ) characters possible. eg. 'a' & 'b' , 'a' & 'c' ... 'z' & 'z'
We consider c1 as the character with maximum Freq and c2 as the character with minimum Freq
Then we construct array of only c1 and c2 from the string( coz we are only interested in max and min Freq characters).
We take 1 for c1 and -1 for c2.
Further if we have consecutive c1's we simply add their frequency.
eg. ababcccaac
c1 = 'c' and c2 = 'a'
the array formed is -1 -1 3 -1 -1 1
Now the deviation will be the length of maximum sum of subarray in the generate array.
Note : that the size of subarray should be greater than 1.
Why? In above example max subarray sum is 3( of length 1 ) but this is not correct as all characters will be same( result should be 0)
So consider subarray of size>1.
*/
#include<bits/stdc++.h>
using namespace std;
int modifiedKadane( vector<int> &arr , int k ){
if( arr.size() < k )
return 0;
int n = arr.size();
vector<int> maxSum(n);
// use kadane's
maxSum[0] = arr[0];
for (int i = 1 ; i < arr.size(); i++) {
maxSum[i] = max(arr[i], maxSum[i - 1] + arr[i]);
}
int sum = 0 ;
for (int i = 0 ; i < k; i++) {
sum += arr[i];
}
int ans = sum;
for (int i = k ; i < arr.size(); i++) {
sum = sum + arr[i] - arr[i - k];
ans = max(ans, sum);
ans = max(ans, sum + maxSum[i - k]);
}
return ans;
}
int maxDeviation( string str ){
int ans = 0 ;
for( char c1 = 'a' ; c1<='z' ; c1++ ){
for( char c2 = 'a' ; c2<='z' ; c2++ ){
if ( c1 == c2 )
continue;
vector<int> arr;
// we consider c1 as character with maxFreq and c2 with minFreq
for( auto c : str ){
if( c==c1 ){
// We shall include all consecutive c1's in our array so we add their frequency
if( arr.size() && arr.back() != -1 ){
arr.back() += 1;
}
else{
arr.push_back( 1 );
}
}
else if( c==c2 ){
// we take distinct c2
arr.push_back( -1 );
}
}
ans= max( ans , modifiedKadane(arr, 2) );
}
}
return ans;
}
int main(){
// string str = "abacccabab";
string str = "baaa";
cout<<maxDeviation(str)<<"\n";
return 0;
}