61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
'''
|
|
You have gifts, and each gift contains a certain number of candies and oranges. Your goal is to make all gifts identical in terms of the number of candies
|
|
and oranges. You are allowed to perform the following operations on any gift:
|
|
|
|
Remove one candy.
|
|
Remove one orange.
|
|
Remove one candy and one orange at the same time.
|
|
You cannot remove more than what is available in a gift (no negative values).
|
|
After performing some moves, all gifts must have the same number of candies and the same number of oranges (though candies and oranges do not need to be equal
|
|
to each other). Your task is to find the minimum number of moves required to achieve this.
|
|
|
|
Example 1
|
|
Input:
|
|
candies = [3, 5, 6]; oranges = [3, 2, 3]
|
|
Output:
|
|
6
|
|
'''
|
|
|
|
from typing import List
|
|
|
|
class Solution(object):
|
|
def solve(self, candies:List[int], oranges:List[int]) -> int:
|
|
"""
|
|
Calculates the minimum number of moves required to equalize the number of candies and oranges in all gifts.
|
|
|
|
In each move, you can either:
|
|
- Remove one candy from a gift,
|
|
- Remove one orange from a gift,
|
|
- Or remove one candy and one orange from the same gift simultaneously.
|
|
|
|
Args:
|
|
candies (List[int]): A list of integers representing the number of candies in each gift.
|
|
oranges (List[int]): A list of integers representing the number of oranges in each gift.
|
|
|
|
Returns:
|
|
int: The minimum number of moves required to make all gifts have the same number of candies and oranges.
|
|
"""
|
|
# Find the minimum candies and oranges in all gifts
|
|
min_candies = min(candies)
|
|
min_oranges = min(oranges)
|
|
moves = 0
|
|
for c, o in zip(candies, oranges):
|
|
# Calculate the extra candies and oranges to remove
|
|
extra_candies = c - min_candies
|
|
extra_oranges = o - min_oranges
|
|
# The minimum of the two can be removed together
|
|
both = min(extra_candies, extra_oranges)
|
|
moves += both
|
|
# Remove the rest individually
|
|
moves += (extra_candies - both) + (extra_oranges - both)
|
|
return moves
|
|
|
|
# usage example
|
|
|
|
s = Solution()
|
|
candies = [1, 2, 3, 4, 5]
|
|
oranges = [5, 4, 3, 2, 1]
|
|
print(s.solve(candies, oranges))
|
|
|
|
|
|
|