java.lang.Object
g1901_2000.s1962_remove_stones_to_minimize_the_total.Solution

public class Solution extends Object
1962 - Remove Stones to Minimize the Total.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>piles</code>, where <code>piles[i]</code> represents the number of stones in the <code>i<sup>th</sup></code> pile, and an integer <code>k</code>. You should apply the following operation <strong>exactly</strong> <code>k</code> times:</p> <ul> <li>Choose any <code>piles[i]</code> and <strong>remove</strong> <code>floor(piles[i] / 2)</code> stones from it.</li> </ul> <p><strong>Notice</strong> that you can apply the operation on the <strong>same</strong> pile more than once.</p> <p>Return <em>the <strong>minimum</strong> possible total number of stones remaining after applying the</em> <code>k</code> <em>operations</em>.</p> <p><code>floor(x)</code> is the <strong>greatest</strong> integer that is <strong>smaller</strong> than or <strong>equal</strong> to <code>x</code> (i.e., rounds <code>x</code> down).</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> piles = [5,4,9], k = 2</p> <p><strong>Output:</strong> 12</p> <p><strong>Explanation:</strong> Steps of a possible scenario are:</p> <ul> <li> <p>Apply the operation on pile 2. The resulting piles are [5,4,5].</p> </li> <li> <p>Apply the operation on pile 0. The resulting piles are [3,4,5].</p> </li> </ul> <p>The total number of stones in [3,4,5] is 12.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> piles = [4,3,6,7], k = 3</p> <p><strong>Output:</strong> 12</p> <p><strong>Explanation:</strong> Steps of a possible scenario are:</p> <ul> <li> <p>Apply the operation on pile 2. The resulting piles are [4,3,3,7].</p> </li> <li> <p>Apply the operation on pile 3. The resulting piles are [4,3,3,4].</p> </li> <li> <p>Apply the operation on pile 0. The resulting piles are [2,3,3,4].</p> </li> </ul> <p>The total number of stones in [2,3,3,4] is 12.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= piles.length <= 10<sup>5</sup></code></li> <li><code>1 <= piles[i] <= 10<sup>4</sup></code></li> <li><code>1 <= k <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minStoneSum

      public int minStoneSum(int[] piles, int k)