Created
November 17, 2016 07:36
-
-
Save jianminchen/9dda212873b0d90d0d646a543a198098 to your computer and use it in GitHub Desktop.
Kindergarten adventure - HackerRank -study code - JavaScript - score 30 (maximum score 30)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume(); | |
process.stdin.setEncoding('ascii'); | |
var input_stdin = ""; | |
var input_stdin_array = ""; | |
var input_currentline = 0; | |
process.stdin.on('data', function (data) { | |
input_stdin += data; | |
}); | |
process.stdin.on('end', function () { | |
input_stdin_array = input_stdin.split("\n"); | |
main(); | |
}); | |
function readLine() { | |
return input_stdin_array[input_currentline++]; | |
} | |
/////////////// ignore above this line //////////////////// | |
function main() { | |
var n = parseInt(readLine()); | |
var m = readLine().split(' '); | |
m = m.map(Number); | |
var add = []; | |
var remove = []; | |
for(var e=0; e<n; e++){ | |
if(m[e]>0){ | |
var s = e-m[e]+1; | |
if(s<0){ | |
add[s+n] ? add[s+n]++ : add[s+n]=1;//console.log('s = '+(s+n)); | |
remove[n-1] ? remove[n-1]++ : remove[n-1]=1;//console.log('e = '+(n-1)); | |
s = 0; | |
} | |
add[s] ? add[s]++ : add[s]=1;//console.log('s = '+s); | |
remove[e] ? remove[e]++ : remove[e]=1;//console.log('e = '+e); | |
} | |
} | |
var x = add[0]; | |
var min = x; | |
var minId = 1; | |
for(var i=1; i<n; i++){ | |
if(remove[i-1]){ | |
x -= remove[i-1]; | |
} | |
if(add[i]){ | |
x += add[i]; | |
} | |
if(x<min){ | |
min = x; | |
minId = i+1; | |
} | |
} | |
console.log(minId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment