Skip to main content

Command Palette

Search for a command to run...

Maximal Score After Applying K Operations

Published
2 min read
Maximal Score After Applying K Operations
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.

In one operation:

  1. choose an index i such that 0 <= i < nums.length,

  2. increase your score by nums[i], and

  3. 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.

LeetCode Problem - 2530

class Solution {
    public long maxKelements(int[] nums, int k) {
        long answer = 0; // Variable to store the final answer (sum of top k elements)

        // Priority queue to store elements in descending order (max heap)
        PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());

        // Add all elements of the array to the priority queue
        for (int num : nums) {
            pq.add(num);
        }

        // Perform k iterations to find the maximum sum
        for (int i = 0; i < k; i++) {
            // Poll the maximum element from the priority queue
            int peek = pq.poll();
            // Add the maximum element to the result
            answer += (long) peek;

            // Calculate the new value by dividing the current element by 3 and taking the ceiling
            int ceilValue = (int) Math.ceil((double) peek / 3);
            // Add the modified value back into the priority queue
            pq.add(ceilValue);
        }

        // Return the accumulated answer after k operations
        return answer;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.