# SE103 Week 3 Warm Up Solutions
[Daily temperatures](https://hackmd.io/SX_NCJw7Q2mTHScJ4XEj-A#Daily-Temperatures)
[Sort characters by frequency](https://hackmd.io/SX_NCJw7Q2mTHScJ4XEj-A#Sort-characters-by-frequency)
## Daily Temperatures
**Java**
```java
class Solution {
public int[] dailyTemperatures(int[] temperatures) {
Stack<Integer> stack = new Stack<>();
int[] ret = new int[temperatures.length];
for(int i = 0; i < temperatures.length; i++) {
while(!stack.isEmpty() &&
temperatures[i] > temperatures[stack.peek()]) {
int idx = stack.pop();
ret[idx] = i - idx;
}
stack.push(i);
}
return ret;
}
}
```
**Python**
```python
class Solution(object):
def dailyTemperatures(self, temperatures):
answer = [0 for _ in temperatures]
stack = [(0, temperatures[0])]
for i, t in enumerate(temperatures):
while stack and stack[-1][1] < t:
low_i = stack[-1][0]
answer[low_i] = i - low_i
stack.pop()
stack.append((i,t))
return answer
```
## Sort characters by frequency
**Java**
```Java
/*
Map each unique character to the number of times it appears in the string.
Create an array where the index of the array represents how many times that
character occurred in the String. Iterate from the end of the array to the
beginning – at each index, append each character to the result string that
number of times.
*/
class Solution {
public String frequencySort(String s) {
if (s == null) {
return null;
}
Map<Character, Integer> map = new HashMap();
char[] charArray = s.toCharArray();
int max = 0;
for (Character c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 0);
}
map.put(c, map.get(c) + 1);
max = Math.max(max, map.get(c));
}
List<Character>[] array = buildArray(map, max);
return buildString(array);
}
private List<Character>[] buildArray(Map<Character, Integer> map, int maxCount) {
List<Character>[] array = new List[maxCount + 1];
for (Character c : map.keySet()) {
int count = map.get(c);
if (array[count] == null) {
array[count] = new ArrayList();
}
array[count].add(c);
}
return array;
}
private String buildString(List<Character>[] array) {
StringBuilder sb = new StringBuilder();
for (int i = array.length - 1; i > 0; i--) {
List<Character> list = array[i];
if (list != null) {
for (Character c : list) {
for (int j = 0; j < i; j++) {
sb.append(c);
}
}
}
}
return sb.toString();
}
}
```
**Python**
```Python
'''
Map each unique character to the number of times it appears in the string. Sort
the table such that the entries are ordered by the number of times the appear
in the original string.
'''
class Solution(object):
def frequencySort(self, s):
table = {}
for l in s:
if l not in table:
table[l] = 1
else:
table[l] += 1
order = sorted(table, key = lambda x: -table[x])
res = ''
for letter in order:
res = res + letter * table[letter]
return res
```