Created
June 5, 2022 14:11
-
-
Save vzsoares/5f4d074b5b578e9239c0e2c9a2e12254 to your computer and use it in GitHub Desktop.
entrevista05062022
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
// @ts-nocheck | |
const preços = [1, 12, 5, 111, 200, 1000, 10]; | |
const k = 50; | |
// // TODO | |
// function sortList(list) { | |
// let sortedList = []; | |
// for (let index = 0; index < list.length; index++) { | |
// const element = list[index]; | |
// sortedList.push(element); | |
// if (sortedList[0] > element) { | |
// sortedList.shift(element); | |
// } | |
// } | |
// return sortedList; | |
// } | |
// console.log(sortList(preços)); | |
function maxToys(preços, k) { | |
const preçosOrdenados = preços.sort((a, b) => a - b); | |
let sum = 0; | |
for (let index = 0; index < preçosOrdenados.length; index++) { | |
const element = preçosOrdenados[index]; | |
sum += element; | |
if (sum > k) { | |
sum -= element; | |
return index; | |
} | |
} | |
return ; | |
} | |
console.log(maxToys(preços, k)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment