Non-overlapping Matrix Sum











up vote
5
down vote

favorite












Non-overlapping Matrix Sum



Given k arrays of length n, output the maximum sum possible using one element from each array such that no two elements are from the same index. It is guaranteed that k<=n.



Input



A nonempty list of nonempty arrays of integers.



Output



An integer that represents the maximum sum.



Examples



Input -> Output
[[1]] -> 1
[[1, 3], [1, 3]] -> 4
[[1, 4, 2], [5, 6, 1]] -> 9
[[-2, -21],[18, 2]] -> 0
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 15
[[1, 2, 3, 4], [5, 4, 3, 2], [6, 2, 7, 1]] -> 16









share|improve this question


























    up vote
    5
    down vote

    favorite












    Non-overlapping Matrix Sum



    Given k arrays of length n, output the maximum sum possible using one element from each array such that no two elements are from the same index. It is guaranteed that k<=n.



    Input



    A nonempty list of nonempty arrays of integers.



    Output



    An integer that represents the maximum sum.



    Examples



    Input -> Output
    [[1]] -> 1
    [[1, 3], [1, 3]] -> 4
    [[1, 4, 2], [5, 6, 1]] -> 9
    [[-2, -21],[18, 2]] -> 0
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 15
    [[1, 2, 3, 4], [5, 4, 3, 2], [6, 2, 7, 1]] -> 16









    share|improve this question
























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      Non-overlapping Matrix Sum



      Given k arrays of length n, output the maximum sum possible using one element from each array such that no two elements are from the same index. It is guaranteed that k<=n.



      Input



      A nonempty list of nonempty arrays of integers.



      Output



      An integer that represents the maximum sum.



      Examples



      Input -> Output
      [[1]] -> 1
      [[1, 3], [1, 3]] -> 4
      [[1, 4, 2], [5, 6, 1]] -> 9
      [[-2, -21],[18, 2]] -> 0
      [[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 15
      [[1, 2, 3, 4], [5, 4, 3, 2], [6, 2, 7, 1]] -> 16









      share|improve this question













      Non-overlapping Matrix Sum



      Given k arrays of length n, output the maximum sum possible using one element from each array such that no two elements are from the same index. It is guaranteed that k<=n.



      Input



      A nonempty list of nonempty arrays of integers.



      Output



      An integer that represents the maximum sum.



      Examples



      Input -> Output
      [[1]] -> 1
      [[1, 3], [1, 3]] -> 4
      [[1, 4, 2], [5, 6, 1]] -> 9
      [[-2, -21],[18, 2]] -> 0
      [[1, 2, 3], [4, 5, 6], [7, 8, 9]] -> 15
      [[1, 2, 3, 4], [5, 4, 3, 2], [6, 2, 7, 1]] -> 16






      code-golf array-manipulation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Quintec

      1,3401620




      1,3401620






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          JavaScript (ES6), 74 bytes





          f=([a,...r],s=m=0,k,i=1)=>a+a?a.map(n=>k&(i*=2)||f(r,s+n,k|i))|m:m<s?m=s:m


          Try it online!






          share|improve this answer




























            up vote
            1
            down vote














            Jelly, 13 12 bytes



            ẈŒpQƑƇị"€¹§Ṁ


            Try it online!



            How it works



            ẈŒpQƑƇị"€¹§Ṁ  Main link. Argument: M (matrix)

            Ẉ Widths; compute the length of each row.
            For an n×m matrix, this yields an array m copies of n.
            Œp Cartesian product; promote each n to [1, ..., n], then form all arrays
            that pick one k out of all m copies of [1, ..., n].
            QƑƇ Comb by fixed unique; keep only arrays that do not change by
            deduplicating their entries.
            ¹ Identity; yield M.
            ị"€ For each of the arrays of unique elements, use its m entries to index
            into the m rows of M.
            § Take the sums of all resulting vectors.
            Ṁ Take the maximum.





            share|improve this answer























            • Ah... I almost posted this same answer with XLṗL instead of J€Œp.
              – Erik the Outgolfer
              1 hour ago




















            up vote
            0
            down vote














            Python 3, 94 90 89 84 bytes





            f=lambda x,y=:x>and max(e+f(x[1:],y+[i])for(i,e)in enumerate(x[0])if i not in y)


            Try it online!






            share|improve this answer























              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              });
              });
              }, "mathjax-editing");

              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: "200"
              };
              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',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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%2fcodegolf.stackexchange.com%2fquestions%2f177673%2fnon-overlapping-matrix-sum%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote













              JavaScript (ES6), 74 bytes





              f=([a,...r],s=m=0,k,i=1)=>a+a?a.map(n=>k&(i*=2)||f(r,s+n,k|i))|m:m<s?m=s:m


              Try it online!






              share|improve this answer

























                up vote
                1
                down vote













                JavaScript (ES6), 74 bytes





                f=([a,...r],s=m=0,k,i=1)=>a+a?a.map(n=>k&(i*=2)||f(r,s+n,k|i))|m:m<s?m=s:m


                Try it online!






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  JavaScript (ES6), 74 bytes





                  f=([a,...r],s=m=0,k,i=1)=>a+a?a.map(n=>k&(i*=2)||f(r,s+n,k|i))|m:m<s?m=s:m


                  Try it online!






                  share|improve this answer












                  JavaScript (ES6), 74 bytes





                  f=([a,...r],s=m=0,k,i=1)=>a+a?a.map(n=>k&(i*=2)||f(r,s+n,k|i))|m:m<s?m=s:m


                  Try it online!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Arnauld

                  71.6k688299




                  71.6k688299






















                      up vote
                      1
                      down vote














                      Jelly, 13 12 bytes



                      ẈŒpQƑƇị"€¹§Ṁ


                      Try it online!



                      How it works



                      ẈŒpQƑƇị"€¹§Ṁ  Main link. Argument: M (matrix)

                      Ẉ Widths; compute the length of each row.
                      For an n×m matrix, this yields an array m copies of n.
                      Œp Cartesian product; promote each n to [1, ..., n], then form all arrays
                      that pick one k out of all m copies of [1, ..., n].
                      QƑƇ Comb by fixed unique; keep only arrays that do not change by
                      deduplicating their entries.
                      ¹ Identity; yield M.
                      ị"€ For each of the arrays of unique elements, use its m entries to index
                      into the m rows of M.
                      § Take the sums of all resulting vectors.
                      Ṁ Take the maximum.





                      share|improve this answer























                      • Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                        – Erik the Outgolfer
                        1 hour ago

















                      up vote
                      1
                      down vote














                      Jelly, 13 12 bytes



                      ẈŒpQƑƇị"€¹§Ṁ


                      Try it online!



                      How it works



                      ẈŒpQƑƇị"€¹§Ṁ  Main link. Argument: M (matrix)

                      Ẉ Widths; compute the length of each row.
                      For an n×m matrix, this yields an array m copies of n.
                      Œp Cartesian product; promote each n to [1, ..., n], then form all arrays
                      that pick one k out of all m copies of [1, ..., n].
                      QƑƇ Comb by fixed unique; keep only arrays that do not change by
                      deduplicating their entries.
                      ¹ Identity; yield M.
                      ị"€ For each of the arrays of unique elements, use its m entries to index
                      into the m rows of M.
                      § Take the sums of all resulting vectors.
                      Ṁ Take the maximum.





                      share|improve this answer























                      • Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                        – Erik the Outgolfer
                        1 hour ago















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote










                      Jelly, 13 12 bytes



                      ẈŒpQƑƇị"€¹§Ṁ


                      Try it online!



                      How it works



                      ẈŒpQƑƇị"€¹§Ṁ  Main link. Argument: M (matrix)

                      Ẉ Widths; compute the length of each row.
                      For an n×m matrix, this yields an array m copies of n.
                      Œp Cartesian product; promote each n to [1, ..., n], then form all arrays
                      that pick one k out of all m copies of [1, ..., n].
                      QƑƇ Comb by fixed unique; keep only arrays that do not change by
                      deduplicating their entries.
                      ¹ Identity; yield M.
                      ị"€ For each of the arrays of unique elements, use its m entries to index
                      into the m rows of M.
                      § Take the sums of all resulting vectors.
                      Ṁ Take the maximum.





                      share|improve this answer















                      Jelly, 13 12 bytes



                      ẈŒpQƑƇị"€¹§Ṁ


                      Try it online!



                      How it works



                      ẈŒpQƑƇị"€¹§Ṁ  Main link. Argument: M (matrix)

                      Ẉ Widths; compute the length of each row.
                      For an n×m matrix, this yields an array m copies of n.
                      Œp Cartesian product; promote each n to [1, ..., n], then form all arrays
                      that pick one k out of all m copies of [1, ..., n].
                      QƑƇ Comb by fixed unique; keep only arrays that do not change by
                      deduplicating their entries.
                      ¹ Identity; yield M.
                      ị"€ For each of the arrays of unique elements, use its m entries to index
                      into the m rows of M.
                      § Take the sums of all resulting vectors.
                      Ṁ Take the maximum.






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 1 hour ago

























                      answered 1 hour ago









                      Dennis

                      186k32295735




                      186k32295735












                      • Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                        – Erik the Outgolfer
                        1 hour ago




















                      • Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                        – Erik the Outgolfer
                        1 hour ago


















                      Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                      – Erik the Outgolfer
                      1 hour ago






                      Ah... I almost posted this same answer with XLṗL instead of J€Œp.
                      – Erik the Outgolfer
                      1 hour ago












                      up vote
                      0
                      down vote














                      Python 3, 94 90 89 84 bytes





                      f=lambda x,y=:x>and max(e+f(x[1:],y+[i])for(i,e)in enumerate(x[0])if i not in y)


                      Try it online!






                      share|improve this answer



























                        up vote
                        0
                        down vote














                        Python 3, 94 90 89 84 bytes





                        f=lambda x,y=:x>and max(e+f(x[1:],y+[i])for(i,e)in enumerate(x[0])if i not in y)


                        Try it online!






                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote










                          Python 3, 94 90 89 84 bytes





                          f=lambda x,y=:x>and max(e+f(x[1:],y+[i])for(i,e)in enumerate(x[0])if i not in y)


                          Try it online!






                          share|improve this answer















                          Python 3, 94 90 89 84 bytes





                          f=lambda x,y=:x>and max(e+f(x[1:],y+[i])for(i,e)in enumerate(x[0])if i not in y)


                          Try it online!







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 1 hour ago

























                          answered 2 hours ago









                          BMO

                          11.1k21981




                          11.1k21981






























                              draft saved

                              draft discarded




















































                              If this is an answer to a challenge…




                              • …Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.


                              • …Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
                                Explanations of your answer make it more interesting to read and are very much encouraged.


                              • …Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.



                              More generally…




                              • …Please make sure to answer the question and provide sufficient detail.


                              • …Avoid asking for help, clarification or responding to other answers (use comments instead).






                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2fcodegolf.stackexchange.com%2fquestions%2f177673%2fnon-overlapping-matrix-sum%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

                              Quarter-circle Tiles

                              build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

                              Mont Emei