[LintCode] Daily Temperatures

linspiration發表於2019-01-19

Problem

Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.

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

Example

Input:
temperatures = [73, 74, 75, 71, 69, 72, 76, 73]
Output:
[1, 1, 4, 2, 1, 1, 0, 0]

Solution

public class Solution {
    /**
     * @param temperatures: a list of daily temperatures
     * @return: a list of how many days you would have to wait until a warmer temperature
     */
    public int[] dailyTemperatures(int[] input) {
        // for input[i], return the number of days in the future 
        // that have a higher temperature
        
        int record[] = new int[input.length];
        for (int i = 0; i < record.length; i++) {
            for (int j = i+1; j < record.length; j++) {
                if (input[j] > input[i]) {
                    record[i] = j-i;
                    break;
                }
            }
        }
        
        return record;
    }
}