# [Java] Easy Brute-Force

• In the first for-while loops, I just check the smallest (index wise) temperature to the right that is greater than the current one.

In the second for loop, I just handle the `0`s at the end.

``````class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int[] r = new int[temperatures.length];
if(temperatures == null || temperatures.length==0)
return r;

for(int i=0; i<temperatures.length; i++) {
int j=i+1;
while(j<temperatures.length && temperatures[j]<=temperatures[i]) {
j++;
}
if(j<temperatures.length)
r[i] = j-i;
}

int i=0;
for(i=temperatures.length-1; i>0; i--) {
if(temperatures[i] <= temperatures[i-1])
r[i]=0;
else break;
}
r[i]=0;
return r;
}
}
``````

I guess can definitely be done better!

• You can actually remove second loop, no need of it as when you initialise array the default values are zero.

class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int[] r = new int[temperatures.length];
if(temperatures == null || temperatures.length==0)
return r;

``````    for(int i=0; i<temperatures.length; i++) {
int j=i+1;
while(j<temperatures.length && temperatures[j]<=temperatures[i]) {
j++;
}
if(j<temperatures.length)
r[i] = j-i;
}

//   int i=0;
//    for(i=temperatures.length-1; i>0; i--) {
//       if(temperatures[i] <= temperatures[i-1])
//          r[i]=0;
//        else break;
``````

// }
// r[i]=0;
return r;
}
}

• I have tried to make the calculation little simple
public int[] dailyTemperatures(int[] temperatures) {
int res [] = new int [temperatures.length];
Arrays.fill(res,0);
int len = temperatures.length;
for(int i =0;i<len ;i++){
int j =i+1;
while(j < len && temperatures[i] >= temperatures[j] ){
j++;

``````        }
if( j != len )res[i] = j-i;
}return res;
}``````

• You can remove the second for loop. Just add an else condition setting r[i] = 0;

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