Last active
September 15, 2020 06:57
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
function get_max_value( | |
k: number, | |
n: number, | |
weights: number[], | |
values: number[], | |
): number { | |
let max_value = 0 | |
const ratios = {} as { [i: number]: number } | |
for (let i = 0; i < n; i++) { | |
ratios[i] = values[i] / weights[i] | |
} | |
let total_weight = 0 | |
const ordered = Object.entries(ratios).sort((a, b) => b[1] - a[1]) | |
ordered.forEach(([idx, value]) => { | |
if (total_weight + weights[idx] <= k) { | |
max_value += values[idx] | |
total_weight += weights[idx] | |
} | |
}) | |
return max_value | |
} | |
get_max_value(10, 6, [5, 7, 6, 1, 2, 3], [1, 3, 2, 2, 3, 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As Javascript: