-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 397 ms (100.00%), Memory - 5.3 MB …
…(100.00%)
- Loading branch information
1 parent
29c5938
commit 09d3016
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> | ||
|
||
<p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> | ||
|
||
<ul> | ||
<li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> | ||
<li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> | ||
</ul> | ||
|
||
<p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">4</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, which is the maximum.</p> | ||
</div> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><strong>Input:</strong> <span class="example-io">rewardValues = [1,6,4,3,2]</span></p> | ||
|
||
<p><strong>Output:</strong> <span class="example-io">11</span></p> | ||
|
||
<p><strong>Explanation:</strong></p> | ||
|
||
<p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> | ||
</div> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= rewardValues.length <= 2000</code></li> | ||
<li><code>1 <= rewardValues[i] <= 2000</code></li> | ||
</ul> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
func maxTotalReward(rewardValues []int) int { | ||
sort.Ints(rewardValues) | ||
m := map[int] bool {} | ||
res := 0 | ||
|
||
for _, v := range rewardValues { | ||
for z := range m { | ||
if v > z { | ||
m[v + z] = true | ||
if v + z > res { | ||
res = v + z | ||
} | ||
} | ||
} | ||
|
||
m[v] = true | ||
if v > res { | ||
res = v | ||
} | ||
} | ||
|
||
return res | ||
} |