From 8582cb937dbbf8ec2adba824341b20c680c6d33b Mon Sep 17 00:00:00 2001 From: kevinzb56 <143kevinshah@gmail.com> Date: Mon, 14 Oct 2024 20:21:58 +0530 Subject: [PATCH 1/2] Added the solution for potd of 14-10-2024 --- october_2024/potd_14_10_2024.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 october_2024/potd_14_10_2024.cpp diff --git a/october_2024/potd_14_10_2024.cpp b/october_2024/potd_14_10_2024.cpp new file mode 100644 index 0000000..91726a6 --- /dev/null +++ b/october_2024/potd_14_10_2024.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + long long maxKelements(vector& nums, int k) { + + + long long score = 0; + priority_queue pq; + for(int i=0; i < nums.size(); i++){ + pq.push(nums[i]); + } + + while(k > 0){ + int max = pq.top(); + if(max == 1) //this condition helps to exit early hence more efficient + return score + k; + score += max; + pq.pop(); + pq.push(ceil(max / 3.0)); + k--; + } + + return score; + + } +}; \ No newline at end of file From 3691657efb35e3eeabcfa5b29d39b7b44f4681e3 Mon Sep 17 00:00:00 2001 From: kevinzb56 <143kevinshah@gmail.com> Date: Mon, 14 Oct 2024 20:28:48 +0530 Subject: [PATCH 2/2] Added the solution for potd of 14-10-2024 --- october_2024/potd_14_10_2024.cpp | 45 ++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/october_2024/potd_14_10_2024.cpp b/october_2024/potd_14_10_2024.cpp index 91726a6..921394e 100644 --- a/october_2024/potd_14_10_2024.cpp +++ b/october_2024/potd_14_10_2024.cpp @@ -1,11 +1,46 @@ +/* + +Question: 2530. Maximal Score After Applying K Operations + +Question Description: +You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0. + +In one operation: + +choose an index i such that 0 <= i < nums.length, +increase your score by nums[i], and +replace nums[i] with ceil(nums[i] / 3). +Return the maximum possible score you can attain after applying exactly k operations. + +The ceiling function ceil(val) is the least integer greater than or equal to val. + + + +Example 1: + +Input: nums = [10,10,10,10,10], k = 5 +Output: 50 +Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50. +Example 2: + +Input: nums = [1,10,3,3,3], k = 3 +Output: 17 +Explanation: You can do the following operations: +Operation 1: Select i = 1, so nums becomes [1,4,3,3,3]. Your score increases by 10. +Operation 2: Select i = 1, so nums becomes [1,2,3,3,3]. Your score increases by 4. +Operation 3: Select i = 2, so nums becomes [1,1,1,3,3]. Your score increases by 3. +The final score is 10 + 4 + 3 = 17. + +*/ + class Solution { public: long long maxKelements(vector& nums, int k) { - long long score = 0; - priority_queue pq; - for(int i=0; i < nums.size(); i++){ + long long score = 0; + priority_queue pq; //create a priority queue "pq" + for(int i=0; i < nums.size(); i++){ //add the elements to pq pq.push(nums[i]); } @@ -13,9 +48,9 @@ class Solution { int max = pq.top(); if(max == 1) //this condition helps to exit early hence more efficient return score + k; - score += max; + score += max; pq.pop(); - pq.push(ceil(max / 3.0)); + pq.push(ceil(max / 3.0)); //insert this element in place of max k--; }