Skip to content

Commit b370d0b

Browse files
committed
solve problem Daily Temperatures
1 parent 1759c29 commit b370d0b

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ All solutions will be accepted!
301301
|134|[Gas Station](https://leetcode-cn.com/problems/gas-station/description/)|[java/py/js](./algorithms/GasStation)|Medium|
302302
|238|[Product Of Array Except Self](https://leetcode-cn.com/problems/product-of-array-except-self/description/)|[java/py/js](./algorithms/ProductOfArrayExceptSelf)|Medium|
303303
|896|[Monotonic Array](https://leetcode-cn.com/problems/monotonic-array/description/)|[java/py/js](./algorithms/MonotonicArray)|Easy|
304+
|739|[Daily Temperatures](https://leetcode-cn.com/problems/daily-temperatures/description/)|[java/py/js](./algorithms/DailyTemperatures)|Medium|
304305

305306
# Database
306307
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Daily Temperatures
2+
We can solve this problem by stack and hashmap
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] dailyTemperatures(int[] temperatures) {
3+
int length = temperatures.length;
4+
int[] res = new int[length];
5+
LinkedList<Map<String, Integer>> stack = new LinkedList<Map<String, Integer>>();
6+
7+
for (int i = 0; i < length; i++) {
8+
while (stack.size() > 0 && stack.peek().get("value") < temperatures[i]) {
9+
Map<String, Integer> e = stack.pop();
10+
res[e.get("index")] = i - e.get("index");
11+
}
12+
Map<String, Integer> e = new HashMap<String, Integer>();
13+
e.put("index", i);
14+
e.put("value", temperatures[i]);
15+
stack.push(e);
16+
}
17+
18+
return res;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} temperatures
3+
* @return {number[]}
4+
*/
5+
var dailyTemperatures = function(temperatures) {
6+
let length = temperatures.length,
7+
res = new Array(length),
8+
stack = []
9+
res.fill(0)
10+
11+
for (let i = 0; i < length; i++) {
12+
while (stack.length > 0 && stack[stack.length - 1].value < temperatures[i]) {
13+
let e = stack.pop()
14+
res[e.index] = i - e.index
15+
}
16+
stack.push({ index: i, value: temperatures[i] })
17+
}
18+
19+
return res
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def dailyTemperatures(self, temperatures):
3+
"""
4+
:type temperatures: List[int]
5+
:rtype: List[int]
6+
"""
7+
length = len(temperatures)
8+
res = [0] * length
9+
stack = []
10+
11+
for i in xrange(length):
12+
while len(stack) > 0 and stack[-1]['value'] < temperatures[i]:
13+
e = stack.pop()
14+
res[e['index']] = i - e['index']
15+
stack.append({ 'index': i, 'value': temperatures[i] })
16+
17+
return res

0 commit comments

Comments
 (0)