Last active
March 5, 2024 12:07
-
-
Save amitasaurus/5a331ffab261880a6e0b2e69a9ff0c54 to your computer and use it in GitHub Desktop.
In a computer network with n servers numbered from 1 to n, each server has an associated security value denoted by security_val[i]. A hacker aims to compromise servers in the network to maximize their total security value.
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 gainMaxValue(security_val: number[], k: number): number { | |
let maxValue = Number.NEGATIVE_INFINITY; | |
for (let startNode = 0; startNode < security_val.length; startNode++) { | |
let currentNode = startNode; | |
let currentValue = security_val[startNode]; | |
while (currentNode + k < security_val.length) { | |
currentNode += k; | |
currentValue += security_val[currentNode]; | |
if (currentValue && currentValue > maxValue) maxValue = currentValue; | |
} | |
} | |
return maxValue; | |
} | |
console.log(gainMaxValue([2, -3, 4, 6, 1], 2)); | |
console.log(gainMaxValue([3, 5, -2, -4, 9, 16], 2)); | |
console.log(gainMaxValue([2, 5, -8, -6, -7], 3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In a computer network with n servers numbered from 1 to n, each server has an associated security value denoted by security_val[i]. A hacker aims to compromise servers in the network to maximize their total security value. The hacker starts from a chosen node and can jump to another node x+k, where k is a constant. The hack ends if the target node doesn't exist. Initially, the hacker has access to a set of servers with zero security value. The hacker accumulates the security values of compromised servers along the way, even if some values are negative. The objective is to strategically select the starting node to optimize the total accumulated security value during the hack.