Skip to content

Commit 2fe4514

Browse files
committed
Feb 2 and added hint
1 parent 4e965cc commit 2fe4514

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ Solutions <a href = "https://github.com/aliasvishnu/leetcode/tree/master/Solutio
8989
| 150 | Stack | Use stack to store operands and whenever operator is seen, evaluate. Use stoi() to convert string to number. | O(n) |
9090
| 152 | Array | Maintain max and min values while iterating through the array. If A[i] is -ve, then swap max and min, as min is now a candidate to become max (after multiplying). While multiplying with A[i], only multiply if it can increase max or decrease min. If it cannot, then that subarray ends there and must start with A[i]. | O(n) |
9191
| 161 | BF | When there is mismatch, check if A[i+1:] == B[j:], A[i+1:] == B[j+1:], A[i:] == B[j+1:]. | O(n) |
92+
| 164 | Bucketing | Try to bucket numbers b/w min and max with gap = ceil(max-min / n-1). This way minimum gap is atleast according to the formula. If the numbers are in the same bucket then they cannot form the answer. So for every bucket you only have to maintain the min and max, compare max[i-1] with min[i] for all i, as they would be consecutive in the final sorting order. Note that maybe you can't place largest number inside some bucket, so keep an extra check later. | O(n) |
9293
| 168 | BF | If n <= 26, then this is easy. Otherwise it is F(n) = F(n/26)+ (char)n%26; something like this. Calculate the exact relation. | O(log(n)) |
9394
| 169 | Array | Assume A[0] is the majority element i.e. curmax = 0, whenever A[i] != A[curmax], reduce the count, if the count becomes -ve at any point, set curmax = i. Idea is that we are cancelling all non-equal elements, if some element occurs more than half times, then in the end curmax should point to that element. | O(n) |
9495
| 172 | Math | [n/5] + [n/25] + [n/125] and so on .. where [] is floor operator. | O(log(n))|

Solutions/*164-MaximumGap.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <math.h>
2+
class Solution {
3+
public:
4+
int maximumGap(vector<int>& nums) {
5+
int len = nums.size();
6+
7+
if(len < 2) return 0;
8+
9+
vector<int> mins (len, INT_MAX);
10+
vector<int> maxs (len, INT_MIN);
11+
12+
int mn = *min_element(nums.begin(), nums.end());
13+
int mx = *max_element(nums.begin(), nums.end());
14+
15+
int gap = ceil( (mx - mn)*1.0 / (len-1) );
16+
17+
for(int i: nums){
18+
if(i == mn || i == mx) continue;
19+
20+
int ind = (i - mn)/gap;
21+
22+
mins[ind] = (mins[ind] < i)? mins[ind]: i;
23+
maxs[ind] = (maxs[ind] > i)? maxs[ind]: i;
24+
}
25+
26+
int prev = mn, answer = 0;
27+
28+
for(int i = 0; i < len-1; i++){
29+
if(mins[i] == INT_MAX) continue;
30+
31+
answer = max(answer, mins[i]-prev);
32+
prev = maxs[i];
33+
}
34+
35+
answer = max(answer, mx-prev);
36+
return answer;
37+
}
38+
};

0 commit comments

Comments
 (0)