1962. Remove Stones to Minimize the Total

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...

I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Data Structure & Algorithms : the most important and fundamental of any programmer. We will explore famous questions on datastructure & algorithms from various popular websites.
Problem Statement The problem asks us to find three numbers in an array whose sum is closest to a given target value. Example Input: nums = [-1, 2, 1, -4], **target = 1 **Expected Output**:2` Approach We can solve the problem using the Two Pointer T...
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
To solve the question you can visit the following link
The question is asking us to do one operation at a time, the operation is simply to reduce the number of piles to half. We can do at most K operations. We wan't to operate in a way that we minimize the number of piles after K operation.
We need to remove the maximum number of possible stones from the pipes which also means we need to operate on the maximum number of stones at any pile at any given time. The piles are kept in an unordered way which should be sorted but the problem with sorting the pile is that after removing the half, the pile will again become unsorted. So after each removal, we need to know which Pile has the maximum number of stones present.
The Data structure which can keep the max element always at the top is Max Heap. Let's look at the approach to solving it using Max Heap
We will put the elements (piles) on a Max Heap. At top of the max heap, we will have the pile with most of the elements. We will remove half of the pile, We will add half of the pile back to Max Heap.
We will keep doing the operation K times, whatever is left on the queue, the sum of it is our answer.
class Solution {
public int minStoneSum(int[] piles, int k) {
//Create a PriorityQueue
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
// Add all elements to PriorityQueue
for(int num: piles)
pq.add(num);
//Iterate for K times and half the number of piles at top
while(k!=0){
int top= pq.poll();
pq.add((int) Math.ceil((float)top/2 ));
k--;
}
int result=0;
// Sum of remaining is our answer.
while(!pq.isEmpty()){
result+=pq.remove();
}
return result;
}
}