From 13abe0145dca744b22c8802175d217fccc86de48 Mon Sep 17 00:00:00 2001 From: eminencegrs Date: Mon, 16 Dec 2024 16:57:19 +0100 Subject: [PATCH] Solve LeetCode #3264 'Final Array State After K Multiplication Operations I' [Easy]. --- .../Solution.cs | 31 +++++++++++++++++++ .../SolutionTests.cs | 16 ++++++++++ .../TestData.cs | 14 +++++++++ 3 files changed, 61 insertions(+) create mode 100644 LeetCode/src/LeetCode.Challenges/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/Solution.cs create mode 100644 LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/SolutionTests.cs create mode 100644 LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/TestData.cs diff --git a/LeetCode/src/LeetCode.Challenges/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/Solution.cs b/LeetCode/src/LeetCode.Challenges/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/Solution.cs new file mode 100644 index 0000000..7bbc765 --- /dev/null +++ b/LeetCode/src/LeetCode.Challenges/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/Solution.cs @@ -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; + } +} diff --git a/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/SolutionTests.cs b/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/SolutionTests.cs new file mode 100644 index 0000000..b848aa0 --- /dev/null +++ b/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/SolutionTests.cs @@ -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); + } +} diff --git a/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/TestData.cs b/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/TestData.cs new file mode 100644 index 0000000..d34336c --- /dev/null +++ b/LeetCode/tests/LeetCode.Challenges.UnitTests/Problems32xx/N_3264_FinalArrayStateAfterKMultiplicationOperationsI/TestData.cs @@ -0,0 +1,14 @@ +using System.Collections; + +namespace LeetCode.Challenges.UnitTests.Problems32xx.N_3264_FinalArrayStateAfterKMultiplicationOperationsI; + +public class TestData : IEnumerable +{ + public IEnumerator 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(); +}