Skip to content

Instantly share code, notes, and snippets.

@hueitan
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save hueitan/3763cc29defecc97f6ba to your computer and use it in GitHub Desktop.

Select an option

Save hueitan/3763cc29defecc97f6ba to your computer and use it in GitHub Desktop.
circle standing, who is taller challenge - https://codefights.com/feed/wG7zrPuKNMb9ppua6

Original answer

function Taller (N,M) {

var output = [];
// check every element
for (i=0;i<N;i++) {
   
   var temp = [];

   // check left
   for (j=i-1;;j--) {
       //
       if (j==i) {
           temp.push(-1);
           break;
       }
       // -1
       if (j==-1) {
           j = N;
       }
       // [0,N-1]
       else if (M[j]>M[i]) {
           temp.push(j+1);
           break;
       }
       
   }

   // check right
   for (j=i+1;;j++) {
       //
       if (j==i) {
           temp.push(-1);
           break;
       }
       // -1
       if (j==N) {
           j = -1;
       }
       // [0,N-1]
       else if (M[j]>M[i]) {
           temp.push(j+1);
           break;
       }
   }

    output.push(temp);
}

return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment