Hard DP prob pseudocode algorithm
So this is the question:
There’s an actor doing fan meeting who is trying to do free hugs to his fans lined up in a line. He starts at position 0 and there are fans to his right and left. Their position is depicted by both neg and pos number like -3, 5, etc. The ‘utility’ (economics) his fan gains from the hug starts at certain number, say 10, and decreases by 1 per 1 distance he walks. The actor wants to find an algorithm to maximize the utility his fans gain.
For example, the maximum utility the fans at pos 2, 4, 6 with initial utility of 10 can get is 8 + 6 + 4.
The number of fans N can be up to 100 and initial utility M can be up to 10000 (can’t be negative). The fans’ positions are between -10000 to 10000.
Please help solve this q in pseudocode given the initial utility, number of fans and fans’ position.
I somehow cant think of ways to work it through.
algorithm data-structures dynamic-programming
add a comment |
So this is the question:
There’s an actor doing fan meeting who is trying to do free hugs to his fans lined up in a line. He starts at position 0 and there are fans to his right and left. Their position is depicted by both neg and pos number like -3, 5, etc. The ‘utility’ (economics) his fan gains from the hug starts at certain number, say 10, and decreases by 1 per 1 distance he walks. The actor wants to find an algorithm to maximize the utility his fans gain.
For example, the maximum utility the fans at pos 2, 4, 6 with initial utility of 10 can get is 8 + 6 + 4.
The number of fans N can be up to 100 and initial utility M can be up to 10000 (can’t be negative). The fans’ positions are between -10000 to 10000.
Please help solve this q in pseudocode given the initial utility, number of fans and fans’ position.
I somehow cant think of ways to work it through.
algorithm data-structures dynamic-programming
add a comment |
So this is the question:
There’s an actor doing fan meeting who is trying to do free hugs to his fans lined up in a line. He starts at position 0 and there are fans to his right and left. Their position is depicted by both neg and pos number like -3, 5, etc. The ‘utility’ (economics) his fan gains from the hug starts at certain number, say 10, and decreases by 1 per 1 distance he walks. The actor wants to find an algorithm to maximize the utility his fans gain.
For example, the maximum utility the fans at pos 2, 4, 6 with initial utility of 10 can get is 8 + 6 + 4.
The number of fans N can be up to 100 and initial utility M can be up to 10000 (can’t be negative). The fans’ positions are between -10000 to 10000.
Please help solve this q in pseudocode given the initial utility, number of fans and fans’ position.
I somehow cant think of ways to work it through.
algorithm data-structures dynamic-programming
So this is the question:
There’s an actor doing fan meeting who is trying to do free hugs to his fans lined up in a line. He starts at position 0 and there are fans to his right and left. Their position is depicted by both neg and pos number like -3, 5, etc. The ‘utility’ (economics) his fan gains from the hug starts at certain number, say 10, and decreases by 1 per 1 distance he walks. The actor wants to find an algorithm to maximize the utility his fans gain.
For example, the maximum utility the fans at pos 2, 4, 6 with initial utility of 10 can get is 8 + 6 + 4.
The number of fans N can be up to 100 and initial utility M can be up to 10000 (can’t be negative). The fans’ positions are between -10000 to 10000.
Please help solve this q in pseudocode given the initial utility, number of fans and fans’ position.
I somehow cant think of ways to work it through.
algorithm data-structures dynamic-programming
algorithm data-structures dynamic-programming
asked Jan 2 at 17:57
DHHDHH
65
65
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
dp[r][l][b][i] = max utility you can get by having visited r as the rightmost fan, l as the leftmost fan, b is a boolean saying if you are at position of rightmost fan or left, and i is the remaining utility. The amount of possible states are 100 * 100 * 2 * 10000 = 200000000, should be doable to solve in less than a second.
Pseudocode: Separate fans in 2: the ones < 0 and the ones > 0.
solve(left, right, atRight, utility):
if left < 0 or right > totalFans or utility <= 0:
return 0
if dp[left][right][atRight][utility] != None:
return dp[left][right][atRight][utility]
if atRight == true:
dp[left][right][atRight][utility] = max(solve(left, right + 1, true, utility - distance(right, right + 1)), solve(left + 1, right, false, utility - distance(right, left + 1))) + utility
else:
dp[left][right][atRight][utility] = max(solve(left + 1, right, false, utility - distance(left, left + 1)), solve(left, right + 1, true, utility - distance(right + 1, left))) + utility
return dp[left][right][atRight][utility]
answer = max(solve(0, 1, true, initialUtility - distance(0, firstFan at dist > 0)), solve(1, 0, false, initialUtility - distance(0, firstFan at dist < 0)))
You basically try all possibilities, your state is where you are, the rightmost fan you already hugged, the leftmost fan you already hugged and the remaining utility. If leftmost fan is 10, that means you already hugged closest fan to 0 that was at pos < 0, second, third, 4th... up to 10th.
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010999%2fhard-dp-prob-pseudocode-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
dp[r][l][b][i] = max utility you can get by having visited r as the rightmost fan, l as the leftmost fan, b is a boolean saying if you are at position of rightmost fan or left, and i is the remaining utility. The amount of possible states are 100 * 100 * 2 * 10000 = 200000000, should be doable to solve in less than a second.
Pseudocode: Separate fans in 2: the ones < 0 and the ones > 0.
solve(left, right, atRight, utility):
if left < 0 or right > totalFans or utility <= 0:
return 0
if dp[left][right][atRight][utility] != None:
return dp[left][right][atRight][utility]
if atRight == true:
dp[left][right][atRight][utility] = max(solve(left, right + 1, true, utility - distance(right, right + 1)), solve(left + 1, right, false, utility - distance(right, left + 1))) + utility
else:
dp[left][right][atRight][utility] = max(solve(left + 1, right, false, utility - distance(left, left + 1)), solve(left, right + 1, true, utility - distance(right + 1, left))) + utility
return dp[left][right][atRight][utility]
answer = max(solve(0, 1, true, initialUtility - distance(0, firstFan at dist > 0)), solve(1, 0, false, initialUtility - distance(0, firstFan at dist < 0)))
You basically try all possibilities, your state is where you are, the rightmost fan you already hugged, the leftmost fan you already hugged and the remaining utility. If leftmost fan is 10, that means you already hugged closest fan to 0 that was at pos < 0, second, third, 4th... up to 10th.
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
add a comment |
dp[r][l][b][i] = max utility you can get by having visited r as the rightmost fan, l as the leftmost fan, b is a boolean saying if you are at position of rightmost fan or left, and i is the remaining utility. The amount of possible states are 100 * 100 * 2 * 10000 = 200000000, should be doable to solve in less than a second.
Pseudocode: Separate fans in 2: the ones < 0 and the ones > 0.
solve(left, right, atRight, utility):
if left < 0 or right > totalFans or utility <= 0:
return 0
if dp[left][right][atRight][utility] != None:
return dp[left][right][atRight][utility]
if atRight == true:
dp[left][right][atRight][utility] = max(solve(left, right + 1, true, utility - distance(right, right + 1)), solve(left + 1, right, false, utility - distance(right, left + 1))) + utility
else:
dp[left][right][atRight][utility] = max(solve(left + 1, right, false, utility - distance(left, left + 1)), solve(left, right + 1, true, utility - distance(right + 1, left))) + utility
return dp[left][right][atRight][utility]
answer = max(solve(0, 1, true, initialUtility - distance(0, firstFan at dist > 0)), solve(1, 0, false, initialUtility - distance(0, firstFan at dist < 0)))
You basically try all possibilities, your state is where you are, the rightmost fan you already hugged, the leftmost fan you already hugged and the remaining utility. If leftmost fan is 10, that means you already hugged closest fan to 0 that was at pos < 0, second, third, 4th... up to 10th.
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
add a comment |
dp[r][l][b][i] = max utility you can get by having visited r as the rightmost fan, l as the leftmost fan, b is a boolean saying if you are at position of rightmost fan or left, and i is the remaining utility. The amount of possible states are 100 * 100 * 2 * 10000 = 200000000, should be doable to solve in less than a second.
Pseudocode: Separate fans in 2: the ones < 0 and the ones > 0.
solve(left, right, atRight, utility):
if left < 0 or right > totalFans or utility <= 0:
return 0
if dp[left][right][atRight][utility] != None:
return dp[left][right][atRight][utility]
if atRight == true:
dp[left][right][atRight][utility] = max(solve(left, right + 1, true, utility - distance(right, right + 1)), solve(left + 1, right, false, utility - distance(right, left + 1))) + utility
else:
dp[left][right][atRight][utility] = max(solve(left + 1, right, false, utility - distance(left, left + 1)), solve(left, right + 1, true, utility - distance(right + 1, left))) + utility
return dp[left][right][atRight][utility]
answer = max(solve(0, 1, true, initialUtility - distance(0, firstFan at dist > 0)), solve(1, 0, false, initialUtility - distance(0, firstFan at dist < 0)))
You basically try all possibilities, your state is where you are, the rightmost fan you already hugged, the leftmost fan you already hugged and the remaining utility. If leftmost fan is 10, that means you already hugged closest fan to 0 that was at pos < 0, second, third, 4th... up to 10th.
dp[r][l][b][i] = max utility you can get by having visited r as the rightmost fan, l as the leftmost fan, b is a boolean saying if you are at position of rightmost fan or left, and i is the remaining utility. The amount of possible states are 100 * 100 * 2 * 10000 = 200000000, should be doable to solve in less than a second.
Pseudocode: Separate fans in 2: the ones < 0 and the ones > 0.
solve(left, right, atRight, utility):
if left < 0 or right > totalFans or utility <= 0:
return 0
if dp[left][right][atRight][utility] != None:
return dp[left][right][atRight][utility]
if atRight == true:
dp[left][right][atRight][utility] = max(solve(left, right + 1, true, utility - distance(right, right + 1)), solve(left + 1, right, false, utility - distance(right, left + 1))) + utility
else:
dp[left][right][atRight][utility] = max(solve(left + 1, right, false, utility - distance(left, left + 1)), solve(left, right + 1, true, utility - distance(right + 1, left))) + utility
return dp[left][right][atRight][utility]
answer = max(solve(0, 1, true, initialUtility - distance(0, firstFan at dist > 0)), solve(1, 0, false, initialUtility - distance(0, firstFan at dist < 0)))
You basically try all possibilities, your state is where you are, the rightmost fan you already hugged, the leftmost fan you already hugged and the remaining utility. If leftmost fan is 10, that means you already hugged closest fan to 0 that was at pos < 0, second, third, 4th... up to 10th.
edited Jan 2 at 23:24
answered Jan 2 at 18:34


juvianjuvian
13.4k22227
13.4k22227
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
add a comment |
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
Would u take care to explain it a bit more? I somehow still do not get your idea?
– DHH
Jan 2 at 21:27
@DHH added psuedocode
– juvian
Jan 2 at 23:24
@DHH added psuedocode
– juvian
Jan 2 at 23:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010999%2fhard-dp-prob-pseudocode-algorithm%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown