-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
LeetCode.Tests/Q1561_MaximumNumberOfCoinsYouCanGetTests.cs
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,19 @@ | ||
namespace LeetCode.Tests; | ||
|
||
public class Q1561Tests | ||
{ | ||
[Theory] | ||
[InlineData(new int[] { 2, 4, 1, 2, 7, 8 }, 9)] | ||
[InlineData(new int[] { 2, 4, 5 }, 4)] | ||
[InlineData(new int[] { 9, 8, 7, 6, 5, 1, 2, 3, 4 }, 18)] | ||
public void MaxCoins_ValidInput_ReturnsCorrectResult(int[] piles, int expectedResult) | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var actualResult = Q1561.MaxCoins(piles); | ||
|
||
// Assert | ||
Assert.Equal(expectedResult, actualResult); | ||
} | ||
} |
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,19 @@ | ||
namespace LeetCode; | ||
|
||
public class Q1561 | ||
{ | ||
// Time Complexity: O(n log n) | ||
// Space Complexity: O(1) | ||
|
||
public static int MaxCoins(int[] piles) | ||
{ | ||
Array.Sort(piles); | ||
var chances = piles.Length / 3; | ||
var maxCoins = 0; | ||
for (var i = 0; i < chances; i++) | ||
{ | ||
maxCoins += piles[piles.Length - 2 - (i * 2)]; | ||
} | ||
return maxCoins; | ||
} | ||
} |