Skip to content

Commit

Permalink
Solve LeetCode #3264 'Final Array State After K Multiplication Operat…
Browse files Browse the repository at this point in the history
…ions I' [Easy].
  • Loading branch information
eminencegrs committed Dec 16, 2024
1 parent d5e3624 commit 13abe01
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
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;
}
}
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);
}
}
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();
}

0 comments on commit 13abe01

Please sign in to comment.