<p>One thing you could try:</p>

<ol>
<li>Take the first value in each input array, and the number of the input array.</li>
<li>Sort the array from (1.) based on value.</li>
<li>Your desired output is the input array number from the first element in the sorted array.</li>
<li>Remove the first element from the sorted array, and note its array number.</li>
<li>Add the next value from the noted array number into the sorted array, unless that array number now refers to an empty array.</li>
<li>Repeat (3.), (4.), (5.) until all array numbers/indices refer to empty arrays.</li>
</ol>

<p><strong>Example:</strong></p>

<p>Input source arrays:</p>

<pre><code>87643 = input array number 1
99732 = input array number 2
76534 = input array number 3
87651 = input array number 4
64311 = input array number 5
</code></pre>

<p><strong>1. Generate a 1-time input array:</strong></p>

<pre><code>[(8,1),(9,2),(7,3),(8,4),(6,5)]
</code></pre>

<p>Update the starting index for the source arrays (don't update the array content, just keep track of the value you're on for each array):</p>

<pre><code>7643
9732
6534
7651
4311
</code></pre>

<p><strong>2. Sort the input array:</strong></p>

<pre><code>[(9,2),(8,1),(8,4),(7,3),(6,5)]
</code></pre>

<p><strong>3. The first element contains your output:</strong></p>

<pre><code>out = (2[1])
</code></pre>

<p><strong>4. remove the first element:</strong></p>

<pre><code>[(8,1),(8,4),(7,3),(6,5)]

first element was from array number 2
</code></pre>

<p><strong>5. add/sort the next single value from the previously removed output index, unless that index is now empty:</strong></p>

<pre><code>7643
9732 = input array number 2
6534
7651
4311

[(9,2),(8,1),(8,4),(7,3),(6,5)]
</code></pre>

<p>This would only read/sort the first elements in each index once at the beginning. Then, you're just reading/sorting a single value with each successive iteration, which should be fast.</p>

CC BY-SA 4.0
2019-08-26T15:56:37.597Z
2019-08-26T15:56:37.597Z
bey
<p>(I hope this is on-topic for this site -- mods feel free to send this to another stack exchange if not )</p>

<p>I've got an optimization problem where I need to choose from one of several options to maximize a quantity.  </p>

<p>Each of those options is an array of numbers of arbitrary length, and each of the elements is strictly non-increasing (either decreasing or not changing), moving from left to right.</p>

<p>Here is an example, stacking them row-wise:</p>

<pre><code>87643
99732
76534
87651
64311
</code></pre>

<p>I can only choose the leftmost in the stack, and after I choose it, it's gone.  The naive way to do this would be to look for the maximum from the first column, take it, then redefine the problem with the removed element out and then continue.  </p>

<p>So:</p>

<pre><code>        87643
        9732
        76534
        87651
        64311
        val = 9
out = (2[1])

        87643
        732
        76534
        87651
        64311
        val = 18
out = (2[2])

        7643
        732
        76534
        87651
        64311
        val = 26
out = (2[2], 1[1])
</code></pre>

<p>and so on</p>

<p>This is sort of slow given the size of my array, and how it's likely to scale.  Are there any better alternatives?</p>

<p>Edit:</p>

<p>No idea why the downvotes but anyway I figured out that I can just concatenate a vector of indices (1 through N) to each array, concatenate the 2d arrays, then sort by the value. I select elements until the cumulative sum matches what I want.</p>

CC BY-SA 4.0
2019-08-27T10:47:38.027Z
Probably a better fit at cs.stackexchange or stackoverflow...Did you try having a priority queue, push the first element of each list (along with the index of which list it's in and where in that list), then repeatedly pop the front of the queue, take it, add the next item in its list to the queue.
usul
<ds.algorithms>
An efficient algorithm for maximizing gain by choosing from a set of options
generic_user
# An efficient algorithm for maximizing gain by choosing from a set of options

(Asked by: username_0 on 2019-08-13)

(I hope this is on-topic for this site -- mods feel free to send this to another stack exchange if not )
I've got an optimization problem where I need to choose from one of several options to maximize a quantity.  
Each of those options is an array of numbers of arbitrary length, and each of the elements is strictly non-increasing (either decreasing or not changing), moving from left to right.
Here is an example, stacking them row-wise:
87643
99732
76534
87651
64311

I can only choose the leftmost in the stack, and after I choose it, it's gone.  The naive way to do this would be to look for the maximum from the first column, take it, then redefine the problem with the removed element out and then continue.  
So:
        87643
        9732
        76534
        87651
        64311
        val = 9
out = (2[1])

        87643
        732
        76534
        87651
        64311
        val = 18
out = (2[2])

        7643
        732
        76534
        87651
        64311
        val = 26
out = (2[2], 1[1])

and so on
This is sort of slow given the size of my array, and how it's likely to scale.  Are there any better alternatives?
Edit:
No idea why the downvotes but anyway I figured out that I can just concatenate a vector of indices (1 through N) to each array, concatenate the 2d arrays, then sort by the value. I select elements until the cumulative sum matches what I want.

**Tags:** ds.algorithms

> **Comments:**
> **username_1**: Probably a better fit at cs.stackexchange or stackoverflow...Did you try having a priority queue, push the first element of each list (along with the index of which list it's in and where in that list), then repeatedly pop the front of the queue, take it, add the next item in its list to the queue.  

---

## 1 answers

### Answer by username_2 on 2019-08-26. Score: 1

One thing you could try:

Take the first value in each input array, and the number of the input array.
Sort the array from (1.) based on value.
Your desired output is the input array number from the first element in the sorted array.
Remove the first element from the sorted array, and note its array number.
Add the next value from the noted array number into the sorted array, unless that array number now refers to an empty array.
Repeat (3.), (4.), (5.) until all array numbers/indices refer to empty arrays.

Example:
Input source arrays:
87643 = input array number 1
99732 = input array number 2
76534 = input array number 3
87651 = input array number 4
64311 = input array number 5

1. Generate a 1-time input array:
[(8,1),(9,2),(7,3),(8,4),(6,5)]

Update the starting index for the source arrays (don't update the array content, just keep track of the value you're on for each array):
7643
9732
6534
7651
4311

2. Sort the input array:
[(9,2),(8,1),(8,4),(7,3),(6,5)]

3. The first element contains your output:
out = (2[1])

4. remove the first element:
[(8,1),(8,4),(7,3),(6,5)]

first element was from array number 2

5. add/sort the next single value from the previously removed output index, unless that index is now empty:
7643
9732 = input array number 2
6534
7651
4311

[(9,2),(8,1),(8,4),(7,3),(6,5)]

This would only read/sort the first elements in each index once at the beginning. Then, you're just reading/sorting a single value with each successive iteration, which should be fast.


---

