Java implements LeetCode 739 daily temperature (violent cycle)

739. Daily temperature

According to the daily temperature list, please regenerate a list. The output of the corresponding position is how long to wait for the temperature to rise more than the number of days of the day. If it does not increase later, please use 0 instead.

For example, given a list of temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].

Tip: The range of the temperature list length is [1, 30000]. The value of each temperature is Fahrenheit, which is an integer in the range of [30, 100].

class Solution {
   public int[] dailyTemperatures(int[] T) {
        int len = T.length;
        int[] dp = new int[len];
        dp[len - 1] = 0;
        int j = 0;
        for (int i = len - 2; i >= 0; i--){
            j = i + 1;
            while (j < len){
                if (T[i] < T[j]){
                    dp[i] = j - i;
                    break;
                }else if (dp[j] == 0){
                    dp[i] = 0;
                    break;
                }else j = j + dp[j];
            }
        }
        return dp;
    }
}
1784 original articles published · 30,000 likes + · 4.43 million views

Guess you like

Origin blog.csdn.net/a1439775520/article/details/105481108