Last active
August 18, 2019 09:42
-
-
Save empeje/539692ba2d756315f85f55866b634d27 to your computer and use it in GitHub Desktop.
CTCI Hackerrank
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
'use strict'; | |
const fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function() { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the rotLeft function below. | |
function rotLeft(a, d) { | |
const first = a.slice(0, d); | |
const rest = a.slice(d, a.length); | |
return [...rest, ...first]; | |
} | |
function main() { | |
const ws = fs.createWriteStream(process.env.OUTPUT_PATH); | |
const nd = readLine().split(' '); | |
const n = parseInt(nd[0], 10); | |
const d = parseInt(nd[1], 10); | |
const a = readLine().split(' ').map(aTemp => parseInt(aTemp, 10)); | |
const result = rotLeft(a, d); | |
ws.write(result.join(' ') + '\n'); | |
ws.end(); | |
} |
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
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', inputStdin => { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', _ => { | |
inputString = inputString.replace(/\s*$/, '') | |
.split('\n') | |
.map(str => str.replace(/\s*$/, '')); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
// Complete the minimumBribes function below. | |
function minimumBribes(q) { | |
let answer = 0; | |
for (let i = q.length - 1; i >= 0; i--) { | |
if (q[i] - (i + 1) > 2) { | |
console.log('Too chaotic'); | |
return; | |
} | |
for (let j = Math.max(0, q[i] - 2); j < i; j++) { | |
if (q[j] > q[i]) answer++; | |
} | |
} | |
console.log(answer); | |
} | |
function main() { | |
const t = parseInt(readLine(), 10); | |
for (let tItr = 0; tItr < t; tItr++) { | |
const n = parseInt(readLine(), 10); | |
const q = readLine().split(' ').map(qTemp => parseInt(qTemp, 10)); | |
minimumBribes(q); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment