-
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.
Solve LeetCode #3264 'Final Array State After K Multiplication Operat…
…ions I' [Easy].
- Loading branch information
1 parent
d5e3624
commit 13abe01
Showing
3 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...Challenges/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/Solution.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,31 @@ | ||
namespace LeetCode.Challenges.Problems32xx.N_3264_FinalArrayStateAfterKMultiplicationOperationsI; | ||
|
||
public static class Solution | ||
{ | ||
public static int[] GetFinalState(int[] nums, int k, int multiplier) | ||
{ | ||
// Min-heap to store (index, value) and prioritize by value, then index. | ||
PriorityQueue<(int Index, int Value), (int Value, int Index)> minHeap = new(); | ||
|
||
// Populate the min-heap with initial array elements. | ||
for (var i = 0; i < nums.Length; i++) | ||
{ | ||
minHeap.Enqueue((i, nums[i]), (nums[i], i)); | ||
} | ||
|
||
// Perform 'k' operations. | ||
for (var i = 0; i < k; i++) | ||
{ | ||
// Extract the smallest element (first occurrence of the smallest value) | ||
var (index, value) = minHeap.Dequeue(); | ||
|
||
// Update the value by multiplying with the multiplier. | ||
nums[index] = value * multiplier; | ||
|
||
// Re-enqueue the updated value. | ||
minHeap.Enqueue((index, nums[index]), (nums[index], index)); | ||
} | ||
|
||
return nums; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Tests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/SolutionTests.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,16 @@ | ||
using FluentAssertions; | ||
using LeetCode.Challenges.Problems32xx.N_3264_FinalArrayStateAfterKMultiplicationOperationsI; | ||
using Xunit; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems32xx.N_3264_FinalArrayStateAfterKMultiplicationOperationsI; | ||
|
||
public class MaxHeapSolutionTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData))] | ||
public void GivenNumbersAndMultiplier_WhenGetFinalState_ThenResultAsExpected( | ||
int[] numbers, int k, int multiplier, int[] expectedResult) | ||
{ | ||
Solution.GetFinalState(numbers, k, multiplier).Should().BeEquivalentTo(expectedResult); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
....UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/TestData.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,14 @@ | ||
using System.Collections; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems32xx.N_3264_FinalArrayStateAfterKMultiplicationOperationsI; | ||
|
||
public class TestData : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return [new[] { 2, 1, 3, 5, 6 }, 5, 2, new[] { 8, 4, 6, 5, 6 }]; | ||
yield return [new[] { 1, 2 }, 3, 4, new[] { 16, 8 }]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} |