Last active
September 15, 2020 06:57
-
-
Save lh0x00/f5d4530fe0631e971af17fefc8bcaea8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: