Problem solution · Python

Minimum Relative Loss After Buying Chocolates

Minimum Relative Loss After Buying Chocolates: a Python solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
77 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Minimum Relative Loss After Buying Chocolates, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 77 lines of Python from the credited upstream file 2819.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMinimum Relative Loss After Buying Chocolates · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def minimumRelativeLosses(      self,      prices: list[int],      queries: list[list[int]],  ) -> list[int]:    ans = []     prices.sort()     prefix = list(itertools.accumulate(prices, initial=0))     for k, m in queries:      countFront = self._getCountFront(k, m, prices)      countBack = m - countFront      ans.append(self._getRelativeLoss(countFront, countBack, k, prefix))     return ans   def _getCountFront(      self,      k: int,      m: int,      prices: list[int],  ) -> int:    """Returns `countFront` for query (k, m).     Returns `countFront` for query (k, m) s.t. picking the first `countFront`    and the last `m - countFront` chocolates is optimal.     Define loss[i] := the relative loss of picking `prices[i]`.    1. For prices[i] <= k, Bob pays prices[i] while Alice pays 0.       Thus, loss[i] = prices[i] - 0 = prices[i].    2. For prices[i] > k, Bob pays k while Alice pays prices[i] - k.       Thus, loss[i] = k - (prices[i] - k) = 2 * k - prices[i].    By observation, we deduce that it is always better to pick from the front    or the back since loss[i] is increasing for 1. and is decreasing for 2.     Assume that picking `left` chocolates from the left and `right = m - left`    chocolates from the right is optimal. Therefore, we are selecting    chocolates from `prices[0..left - 1]` and `prices[n - right..n - 1]`.     To determine the optimal `left` in each iteration, we simply compare    `loss[left]` with `loss[n - right]` if `loss[left] < loss[n - right]`,    it's worth increasing `left`.    """    n = len(prices)    countNoGreaterThanK = bisect.bisect_right(prices, k)    l = 0    r = min(countNoGreaterThanK, m)     while l < r:      mid = (l + r) // 2      right = m - mid      # Picking prices[mid] is better than picking prices[n - right].      if prices[mid] < 2 * k - prices[n - right]:        l = mid + 1      else:        r = mid     return l   def _getRelativeLoss(      self,      countFront: int,      countBack: int,      k: int,      prefix: list[int],  ) -> int:    """    Returns the relative loss of picking `countFront` and `countBack`     chocolates.    """    lossFront = prefix[countFront]    lossBack = 2 * k * countBack - (prefix[-1] - prefix[-countBack - 1])    return lossFront + lossBack 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗