add: 每日温度
This commit is contained in:
parent
4fca08df37
commit
ada2de5c1a
@ -371,6 +371,11 @@ LeetCode 与 LintCode 解题记录。此为个人练习仓库,代码中对重
|
|||||||
- LeetCode 238. 除自身以外数组的乘积 <https://leetcode-cn.com/problems/product-of-array-except-self/>
|
- LeetCode 238. 除自身以外数组的乘积 <https://leetcode-cn.com/problems/product-of-array-except-self/>
|
||||||
- LintCode 1310. 数组除了自身的乘积 <https://www.lintcode.com/problem/product-of-array-except-self/description>
|
- LintCode 1310. 数组除了自身的乘积 <https://www.lintcode.com/problem/product-of-array-except-self/description>
|
||||||
|
|
||||||
|
- [每日温度](src/array/daily-temperatures.js)
|
||||||
|
|
||||||
|
- LeetCode 739. 每日温度 <https://leetcode-cn.com/problems/daily-temperatures/>
|
||||||
|
- LintCode 1060. 每日温度 <https://www.lintcode.com/problem/daily-temperatures/description>
|
||||||
|
|
||||||
## 栈
|
## 栈
|
||||||
|
|
||||||
- [最大矩阵](src/stack/maximal-rectangle.js)
|
- [最大矩阵](src/stack/maximal-rectangle.js)
|
||||||
|
18
src/array/daily-temperatures.js
Normal file
18
src/array/daily-temperatures.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* @param {number[]} T
|
||||||
|
* @return {number[]}
|
||||||
|
*/
|
||||||
|
export const dailyTemperatures = function (T) {
|
||||||
|
const len = T.length
|
||||||
|
const ans = new Array(len).fill(0)
|
||||||
|
const stack = []
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
const temperature = T[i]
|
||||||
|
while (stack.length && temperature > T[stack[stack.length - 1]]) {
|
||||||
|
const prevIndex = stack.pop()
|
||||||
|
ans[prevIndex] = i - prevIndex
|
||||||
|
}
|
||||||
|
stack.push(i)
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
5
test/array/daily-temperatures.test.js
Normal file
5
test/array/daily-temperatures.test.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { dailyTemperatures } from '../../src/array/daily-temperatures.js'
|
||||||
|
|
||||||
|
test('每日温度', () => {
|
||||||
|
expect(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])).toEqual([1, 1, 4, 2, 1, 1, 0, 0])
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user