Skip to content

Commit 9333e95

Browse files
committed
new soln
1 parent b2e4e3f commit 9333e95

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

409.LongestPalindrome.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 409. Longest Palindrome
2+
// Given a string s which consists of lowercase or uppercase letters, return the length of the longest
3+
// palindrome that can be built with those letters.
4+
5+
// Letters are case sensitive, for example, "Aa" is not considered a palindrome.
6+
7+
// Example 1:
8+
// Input: s = "abccccdd"
9+
// Output: 7
10+
// Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
11+
// Example 2:
12+
// Input: s = "a"
13+
// Output: 1
14+
// Explanation: The longest palindrome that can be built is "a", whose length is 1.
15+
16+
// Constraints:
17+
// 1 <= s.length <= 2000
18+
// s consists of lowercase and/or uppercase English letters only.
19+
public class Solution {
20+
public int LongestPalindrome(string s) {
21+
Dictionary<char, int> freq = new();
22+
foreach(var c in s){
23+
if(freq.ContainsKey(c))
24+
freq[c]++;
25+
else
26+
freq[c] = 1;
27+
}
28+
bool odd = false; int size = 0;
29+
foreach(var item in freq){
30+
if(item.Value % 2 ==0){
31+
size += item.Value;
32+
}
33+
else
34+
{
35+
size += item.Value -1;
36+
odd = true;
37+
}
38+
}
39+
return odd ? size + 1: size;
40+
}
41+
}

895.FreqStack.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// 895. Maximum Frequency Stack
2+
// Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
3+
4+
// Implement the FreqStack class:
5+
6+
// FreqStack() constructs an empty frequency stack.
7+
// void push(int val) pushes an integer val onto the top of the stack.
8+
// int pop() removes and returns the most frequent element in the stack.
9+
// If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
10+
11+
// Example 1:
12+
// Input
13+
// ["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
14+
// [[], [5], [7], [5], [7], [4], [5], [], [], [], []]
15+
// Output
16+
// [null, null, null, null, null, null, null, 5, 7, 5, 4]
17+
18+
// Explanation
19+
// FreqStack freqStack = new FreqStack();
20+
// freqStack.push(5); // The stack is [5]
21+
// freqStack.push(7); // The stack is [5,7]
22+
// freqStack.push(5); // The stack is [5,7,5]
23+
// freqStack.push(7); // The stack is [5,7,5,7]
24+
// freqStack.push(4); // The stack is [5,7,5,7,4]
25+
// freqStack.push(5); // The stack is [5,7,5,7,4,5]
26+
// freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
27+
// freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
28+
// freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
29+
// freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
30+
31+
// Constraints:
32+
// 0 <= val <= 109
33+
// At most 2 * 104 calls will be made to push and pop.
34+
// It is guaranteed that there will be at least one element in the stack before calling pop.
35+
36+
37+
// Microsoft
38+
public class FreqStack {
39+
Dictionary<int, int> DictFreq;
40+
Dictionary<int, Stack<int>> DictRecent;
41+
int maxFreq;
42+
43+
public FreqStack() {
44+
DictFreq = new Dictionary<int, int>();
45+
DictRecent = new Dictionary<int, Stack<int>>();
46+
maxFreq = 0;
47+
}
48+
49+
public void Push(int val) {
50+
int freq;
51+
if(!DictFreq.ContainsKey(val)){
52+
freq= 1;
53+
}
54+
else{
55+
freq = DictFreq[val] + 1;
56+
}
57+
DictFreq[val] = freq;
58+
if(!DictRecent.ContainsKey(freq)){
59+
Stack<int> st = new Stack<int>();
60+
st.Push(val);
61+
DictRecent[freq] = st;
62+
}
63+
else{
64+
DictRecent[freq].Push(val);
65+
}
66+
maxFreq = Math.Max(maxFreq,freq);
67+
}
68+
69+
public int Pop() {
70+
int res;
71+
if(DictFreq.Count == 0)
72+
return -1;
73+
res = DictRecent[maxFreq].Pop();
74+
if(DictRecent[maxFreq].Count == 0){
75+
DictRecent.Remove(maxFreq);
76+
maxFreq--;
77+
}
78+
DictFreq[res]--;
79+
if(DictFreq[res] == 0){
80+
DictFreq.Remove(res);
81+
}
82+
return res;
83+
}
84+
}
85+
86+
/**
87+
* Your FreqStack object will be instantiated and called as such:
88+
* FreqStack obj = new FreqStack();
89+
* obj.Push(val);
90+
* int param_2 = obj.Pop();
91+
*/

0 commit comments

Comments
 (0)