Skip to content

Commit

Permalink
Add Q1561 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ungtsuhan committed Nov 24, 2023
1 parent 7270ae2 commit 936c3f5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LeetCode.Tests/Q1561_MaximumNumberOfCoinsYouCanGetTests.cs
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);
}
}
19 changes: 19 additions & 0 deletions LeetCode/Q1561_MaximumNumberOfCoinsYouCanGet.cs
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;
}
}

0 comments on commit 936c3f5

Please sign in to comment.