ucap-doc/documents/업무/8월/4째주/hackerank-expected-demand.js

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-09-08 12:43:10 +00:00
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'filledOrders' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY order
* 2. INTEGER k
*/
function filledOrders(order, k) {
// Write your code here
let total = 0;
let count = 0;
const ordersLength = order.length;
const sortedOrders = order.sort();
for (let i = 0; i < ordersLength; i++) {
if (total + sortedOrders[i] <= k) {
// if all orders able to be filled
if (total <= k && i === ordersLength - 1) return ordersLength;
total += sortedOrders[i];
count++;
} else {
return count;
}
}
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const orderCount = parseInt(readLine().trim(), 10);
let order = [];
for (let i = 0; i < orderCount; i++) {
const orderItem = parseInt(readLine().trim(), 10);
order.push(orderItem);
}
const k = parseInt(readLine().trim(), 10);
const result = filledOrders(order, k);
ws.write(result + '\n');
ws.end();
}