Java, Simple Code to understand.


  • 0
    V

    This is kind of Brute Force attack. For each of the element, scan the right side in the array and find the next bigger element. This is not good on performance, but easy to understand. Once this is understood, one can try to get better algorithm.

    class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        
        if (temperatures == null || temperatures.length == 0){
            return temperatures;
        }
        
        int len = temperatures.length;
        int result[] = new int[len];
        int count = 0;
        for(int i = 0 ; i < len - 1;i++){
            count = 0;
            for(int j = i + 1 ; j < len; j++){
                if(temperatures[i] >= temperatures[j]){
                    count++;
                }
                else{
                    count++;
                    result[i] = count;
                    break;
                }
            }
        }
        return result;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.