Skip to content

Commit 5fc23cc

Browse files
committed
Added 2 solutions
1 parent 7f4f94d commit 5fc23cc

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎Hard/Maximum Frequency Stack.java‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class FreqStack {
2+
3+
Stack<Integer> stack;
4+
Stack<Integer> freqStack;
5+
Map<Integer, Integer> map;
6+
int maxFreq;
7+
8+
public FreqStack() {
9+
stack = new Stack<>();
10+
freqStack = new Stack<>();
11+
map = new HashMap<>();
12+
maxFreq = Integer.MIN_VALUE;
13+
}
14+
15+
public void push(int x) {
16+
// Everytime increment the frequency of number being pushed in the stack
17+
map.put(x, map.getOrDefault(x, 0) + 1);
18+
stack.push(x);
19+
20+
// Update maxFreq if it changes after the push operation
21+
if (maxFreq < map.get(x)) {
22+
maxFreq = map.get(x);
23+
}
24+
25+
// Push maxFreq to the freqStack to keep track of maximum frequency after each push
26+
freqStack.push(maxFreq);
27+
}
28+
29+
public int pop() {
30+
31+
int ans = -1;
32+
33+
// If the top element in stack has the highest frequency then just return it by popping it
34+
if (map.get(stack.peek()) == maxFreq) {
35+
int temp = stack.pop();
36+
map.put(temp, map.getOrDefault(temp, 0) - 1);
37+
ans = temp;
38+
}
39+
// Traverse the stack till you find the element with frequency equal to maxFreq
40+
else {
41+
int count = 0;
42+
Stack<Integer> temp = new Stack<>();
43+
int t = -1;
44+
45+
while (!stack.empty()) {
46+
count++;
47+
t = stack.pop();
48+
freqStack.pop();
49+
50+
if (map.get(t) == maxFreq) {
51+
map.put(t, map.getOrDefault(t, 0) - 1);
52+
break;
53+
}
54+
else {
55+
temp.push(t);
56+
}
57+
}
58+
59+
// Put back the elements in the stack except the element with frequency equal to maxFreq
60+
while (!temp.empty()) {
61+
stack.push(temp.pop());
62+
}
63+
64+
// The element with frequency equal to maxFreq
65+
ans = t;
66+
67+
// Check if the freqStack had a value except the value for maxFreq which we popped
68+
int freqTemp = freqStack.isEmpty() ? 0 : freqStack.peek();
69+
70+
// Update freqStack
71+
while (count-- > 0) {
72+
freqStack.push(freqTemp);
73+
}
74+
}
75+
76+
freqStack.pop();
77+
78+
// Update maxFreq
79+
maxFreq = stack.isEmpty() ? Integer.MIN_VALUE : freqStack.peek();
80+
81+
return ans;
82+
}
83+
}

‎Medium/4Sum.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public static List<List<Integer>> fourSum(int[] nums, int target) {
3+
Set<List<Integer>> ans = new HashSet<>();
4+
5+
Arrays.sort(nums);
6+
7+
for (int i=0; i<nums.length; i++) {
8+
for (int j=i+1; j<nums.length; j++) {
9+
int temp = target - (nums[i] + nums[j]);
10+
int start = j+1;
11+
int end = nums.length-1;
12+
13+
while (start < end) {
14+
if (nums[start] + nums[end] == temp) {
15+
List<Integer> list = new ArrayList<>();
16+
list.add(nums[i]);
17+
list.add(nums[j]);
18+
list.add(nums[start]);
19+
list.add(nums[end]);
20+
21+
ans.add(list);
22+
start++;
23+
end--;
24+
}
25+
else if (nums[start] + nums[end] < temp) {
26+
start++;
27+
}
28+
else if (nums[start] + nums[end] > temp) {
29+
end--;
30+
}
31+
}
32+
}
33+
}
34+
35+
return new ArrayList<>(ans);
36+
}
37+
}

0 commit comments

Comments
 (0)