Hard DP prob pseudocode algorithm












-1















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.










share|improve this question



























    -1















    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.










    share|improve this question

























      -1












      -1








      -1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 17:57









      DHHDHH

      65




      65
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer


























          • 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












          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer


























          • 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
















          1














          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.






          share|improve this answer


























          • 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














          1












          1








          1







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

          How to fix TextFormField cause rebuild widget in Flutter