Created
April 19, 2020 07:59
-
-
Save jorgejr568/e1bdb994423ab8c35b92819d9f028163 to your computer and use it in GitHub Desktop.
Google Kick Start 2020 (Round A)
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
const readline = require('readline'); | |
function solution(input, it) | |
{ | |
let firstLine = input.shift(), | |
firstLineElements = firstLine.split(' '); | |
const quantityHouses = firstLineElements[0]; | |
let dollars = parseInt(firstLineElements[1]); | |
let secondLine = input.shift(), | |
secondLineElements = secondLine.split(' '); | |
let houses = secondLineElements.sort((a, b) => { | |
return a + b | |
}) | |
let answer = 0; | |
for(let lixo = 0; lixo < houses.length; lixo++) { | |
let actualHouse = parseInt(houses[lixo]); | |
if (dollars >= actualHouse) { | |
dollars = dollars - actualHouse; | |
++answer; | |
} | |
} | |
console.log(`Case #${it}: ${answer}`); | |
} | |
function main(){ | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
}); | |
let lineInterator = 0; | |
let waitingSize = 0; | |
let aux = []; | |
rl.on('line', function(line) { | |
if (lineInterator === 0) { | |
waitingSize = parseInt(line) * 2; | |
} | |
aux.push(line); | |
lineInterator++; | |
if(lineInterator > waitingSize) { | |
rl.close(); | |
let tests = parseInt(aux.shift()); | |
let it = 1; | |
while(tests--){ | |
solution(aux, it++); | |
} | |
} | |
}) | |
} | |
if (!module.parent) { | |
main() | |
} |
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
const readline = require('readline'); | |
function fill(i, j){ | |
let k = []; | |
for(l = 0; l<i;l++){ | |
k[l] = []; | |
for(m = 0;m<j;m++){ | |
k[l][m] =0; | |
} | |
} | |
return k; | |
} | |
function maxPlatesBeautyValue(values, n, k, p) { | |
let prefixSum = fill(n+1, p+1); | |
let lookup = fill(n+1,p+1); | |
for (let i = 1; i <= n; i++) { | |
for (let j = 1; j <= k; j++) { | |
prefixSum[i][j] = prefixSum[i][j - 1] + values[i - 1][j - 1]; | |
} | |
} | |
for (let i = 1; i <= n; i++) { | |
for (let j = 1; j <= p; j++) { | |
lookup[i][j] = 0; | |
for (let x = 0; x <= Math.min(j, k); x++) { | |
lookup[i][j] = Math.max(lookup[i][j], prefixSum[i][x] + lookup[i - 1][j - x]); | |
} | |
} | |
} | |
return lookup[n][p]; | |
} | |
function solution(input) | |
{ | |
let tests = parseInt(input.shift()); | |
let it = 1; | |
while(tests--){ | |
let firstLine = input.shift(), | |
firstLineElements = firstLine.split(' '); | |
let n = firstLineElements[0]; | |
let k = firstLineElements[1]; | |
let p = firstLineElements[2]; | |
let a = []; | |
for(let i=0;i<n;++i){ | |
a.push( | |
input | |
.shift() | |
.split(' ') | |
.map(b => (parseInt(b)) | |
) | |
); | |
} | |
answer = maxPlatesBeautyValue(a, n, k, p); | |
console.log(`Case #${it}: ${answer}`); | |
it++; | |
} | |
} | |
function main(){ | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
}); | |
let lineInterator = 0; | |
let expectedLines = 0; | |
let aux = []; | |
rl | |
.on('line', function(line) { | |
aux.push(line) | |
if(lineInterator > expectedLines){ | |
if(parseInt(line)){ | |
expectedLines += parseInt(line) | |
} | |
else{ | |
rl.close() | |
} | |
} | |
lineInterator++ | |
}) | |
.on('close', function(line){ | |
solution(aux) | |
}) | |
} | |
if (!module.parent) { | |
main() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment