Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

739. Daily Temperatures #207

Open
Tcdian opened this issue Jun 11, 2020 · 1 comment
Open

739. Daily Temperatures #207

Tcdian opened this issue Jun 11, 2020 · 1 comment
Labels

Comments

@Tcdian
Copy link
Owner

Tcdian commented Jun 11, 2020

739. Daily Temperatures

根据每日 气温 列表,请重新生成一个列表,对应位置的输出是需要再等待多久温度才会升高超过该日的天数。如果之后都不会升高,请在该位置用 0 来代替。

例如,给定一个列表 temperatures = [73, 74, 75, 71, 69, 72, 76, 73],你的输出应该是 [1, 1, 4, 2, 1, 1, 0, 0]

提示:气温 列表长度的范围是 [1, 30000]。每个气温的值的均为华氏度,都是在 [30, 100] 范围内的整数。

@Tcdian
Copy link
Owner Author

Tcdian commented Jun 11, 2020

Solution

  • JavaScript Solution
/**
 * @param {number[]} T
 * @return {number[]}
 */
var dailyTemperatures = function(T) {
    const result = new Array(T.length).fill(0);
    const stack = [];
    for (let i = 0; i < T.length; i++) {
        while (stack.length !== 0 && T[stack[stack.length - 1]] < T[i]) {
            const d = stack.pop();
            result[d] = i - d;
        }
        stack.push(i);
    }
    return result;
};
  • TypeScript Solution
function dailyTemperatures(T: number[]): number[] {
    const result: number[] = new Array(T.length).fill(0);
    const stack: number[] = [];
    for (let i = 0; i < T.length; i++) {
        while (stack.length !== 0 && T[stack[stack.length - 1]] < T[i]) {
            const d = stack.pop() as number;
            result[d] = i - d;
        }
        stack.push(i);
    }
    return result;
}

@Tcdian Tcdian removed the Classic label Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant