πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

or each operation, we choose the element i with the highest value? 
therefore the floor calculation should result the biggest reduction in stones per iteration

πŸ€”What Did I Struggle With?

the floor calculation is not 1:1 to the question description
integer division in C++ already floors the results, so floor is unecessary

πŸ’‘ Explanation of Solution

init a priority queue iterate through piles, 
inserting it into the pq iterate k times, getting the max element from the pq, 
performing the floor calculation, 
pushing it back into the queue, and repeating k times

βŒ› Complexity Analysis

Heap Init : O(n log n) for inserting all elements into the heap
Space Complexity: O(n)

πŸ’» Implementation of Solution

class Solution {
 
public:
 
Β  Β  int minStoneSum(vector<int>& piles, int k) {
Β  Β  Β  Β  priority_queue<int> pq;
 
Β  Β  Β  Β  for(int i=0; i < piles.size(); i++) {
Β  Β  Β  Β  Β  Β  pq.push(piles[i]);
Β  Β  Β  Β  }
 
Β  Β  Β  Β  for(int i=0; i < k; i++) {
Β  Β  Β  Β  Β  Β  int currLargest = pq.top();
Β  Β  Β  Β  Β  Β  pq.pop();
Β  Β  Β  Β  Β  Β  pq.push(currLargest - currLargest / 2);
Β  Β  Β  Β  }
  
Β  Β  Β  Β  int minTotal = 0;
  
Β  Β  Β  Β  while(!pq.empty()){
Β  Β  Β  Β  Β  Β  minTotal += pq.top();
Β  Β  Β  Β  Β  Β  pq.pop();
Β  Β  Β  Β  }
Β  Β  Β  Β  return minTotal;
Β  Β  }
};