Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: waiting_customers = 0 wheel_rotations = 0 minimum_wheel_rotations = -1 max_profit = 0 profit = 0 for c in customers: wheel_rotations+=1 boarding_waiting = min(4, waiting_customers) waiting_customers -= boarding_waiting boarding_now = max(0, min(4, c) - boarding_waiting) waiting_customers += c-boarding_now total_boarding = boarding_waiting + boarding_now profit += total_boarding * boardingCost - runningCost if profit > max_profit: max_profit = profit minimum_wheel_rotations = wheel_rotations fullrevenue_rotations = (waiting_customers // 4) wheel_rotations+=fullrevenue_rotations # fullprofit = fullprofit_rotations * 4 * boardingCost - fullprofit_rotations * runningCost fullrevenue = fullrevenue_rotations * (4 * boardingCost - runningCost) profit += fullrevenue if profit > max_profit: max_profit = profit minimum_wheel_rotations = wheel_rotations remaining_customers = waiting_customers % 4 wheel_rotations+=1 remaining_revenue = remaining_customers * boardingCost - runningCost profit += remaining_revenue if profit > max_profit: max_profit = profit minimum_wheel_rotations = wheel_rotations return minimum_wheel_rotations
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: wt = 0 profit = 0 bp = 0 ret = -1 cr = 0 gondola = [0, 0, 0, 0] i = 0 while i < len(customers) or wt: cr += 1 if i < len(customers): e = customers[i] wt += e if wt: take = min(wt, 4) profit += boardingCost * take - runningCost wt -= take gondola = gondola[1:] + [take] else: if sum(gondola) == 0: profit -= runningCost if profit > bp: bp = profit ret = cr i += 1 return ret
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: max_profit = 0 max_step = -1 profit = 0 rest = 0 for i, c in enumerate(customers): customer = c + rest customer, rest = min(4, customer), max(0, customer - 4) profit += customer * boardingCost - runningCost if profit > max_profit: max_profit = profit max_step = i + 1 q, r = divmod(rest, 4) if q > 0: profit += q * 4 * boardingCost - q * runningCost if profit > max_profit: max_profit = profit max_step = len(customers) + q if r > 0: profit += r * boardingCost - runningCost if profit > max_profit: max_step = len(customers) + q + 1 return max_step
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: wating = 0 profit = 0 idx = -1 t_r = 0 max_profit = 0 for customer in customers: wating += customer if wating <= 4: profit += wating * boardingCost - runningCost wating = 0 else: profit += 4 * boardingCost - runningCost wating -= 4 t_r += 1 if profit > max_profit: idx = t_r max_profit = profit if wating > 0: if 4 * boardingCost > runningCost: idx += wating//4 wating = wating%4 if wating * boardingCost > runningCost: idx += 1 return idx
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: if not customers: return -1 if 4 * boardingCost <= runningCost: return -1 num = 0 profit = 0 sum_ppl = 0 for c in customers: sum_ppl += c cur_w = 0 for i in range(len(customers)): num += 1 cur_w += customers[i] n = 4 if cur_w >= 4 else cur_w profit += n * boardingCost - runningCost cur_w -= n rotates, left = cur_w// 4, cur_w % 4 num += rotates profit += rotates * 4 * boardingCost - runningCost * rotates if left * boardingCost > runningCost: num += 1 profit += left * boardingCost - runningCost if profit <= 0: return -1 return num
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
from typing import List, Dict, Tuple class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: current = 0 cost = 0 amari = 0 ans = -1 macost = 0 for customer in customers: amari += customer if amari >= 4: amari -= 4 cost += 4 * boardingCost - runningCost current += 1 if cost > macost: macost = cost ans = current else: tmp = cost tmp = cost cost += amari * boardingCost - runningCost amari = 0 current += 1 if cost > macost: macost = cost ans = current a, b = divmod(amari, 4) if 4 * boardingCost > runningCost: cost += a * boardingCost - runningCost current += a if cost > macost: macost = cost ans = current if b * boardingCost > runningCost: cost += b * boardingCost - runningCost current += 1 if cost > macost: macost = cost ans = current return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: ans = -1 most = pnl = waiting = 0 for i, x in enumerate(customers): waiting += x # more people waiting in line waiting -= (chg := min(4, waiting)) # boarding pnl += chg * boardingCost - runningCost if most < pnl: ans, most = i+1, pnl q, r = divmod(waiting, 4) if 4*boardingCost > runningCost: ans += q if r*boardingCost > runningCost: ans += 1 return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: count = 0 minCount = -1 profit = 0 maxProfit = None waiting = 0 for i, cnum in enumerate(customers): waiting += cnum if waiting >= 4: rounds = waiting // 4 count += rounds profit += (boardingCost * 4 - runningCost)*rounds waiting = waiting % 4 else: if count <= i: count += 1 profit += boardingCost * waiting - runningCost waiting = 0 if profit > 0 and (maxProfit is None or profit > maxProfit): maxProfit = profit minCount = count if waiting > 0: profit += boardingCost * waiting - runningCost count += 1 if profit > 0 and (maxProfit is None or profit > maxProfit): maxProfit = profit minCount = count return minCount
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: r=0 c=0 n=len(customers) total=0 ans=0 ind=-1 while c<n: if customers[c]>=4: r+=(customers[c]//4) total+=((customers[c]//4)*4) customers[c]-=((customers[c]//4)*4) if customers[c]==0: c+=1 res=total*boardingCost-r*runningCost if res>ans: ans=res ind=r else: if c==n-1 or c==r: total+=customers[c] r+=1 res=total*boardingCost-r*runningCost if res>ans: ans=res ind=r else: customers[c+1]+=customers[c] c+=1 return ind
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: profit = 0 waiting = 0 rotations = 0 onboard = 0 gondola_customers = deque([]) total = 0 maxprofit = 0 max_rotation = -1 for idx,arrival in enumerate(customers): # if onboard == 0 and waiting == 0 and arrival == 0: # continue if gondola_customers and gondola_customers[0][1]==idx: coming_down = gondola_customers.popleft()[0] onboard -= coming_down total = arrival if waiting >0: total = waiting + arrival board = min(total,4) profit += ((board*boardingCost) - runningCost) onboard += board gondola_customers.append([board,idx+4]) waiting += (arrival-board) rotations += 1 if profit > maxprofit: maxprofit = profit max_rotation = rotations profit += ((waiting//4)*((4*boardingCost)-runningCost)) rotations += (waiting//4) if profit > maxprofit: maxprofit = profit max_rotation = rotations profit += (((waiting%4)*boardingCost)-runningCost) rotations += ((waiting%4)>0) if profit > maxprofit: maxprofit = profit max_rotation = rotations return max_rotation if maxprofit > 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], B: int, R: int) -> int: turns = 0 boarded = 0 max_profit = -1 rem = 0 max_turns = -1 # print(sum(customers)) for i, c in enumerate(customers): t, rem = divmod(c+rem, 4) turns += t boarded += t * 4 if turns <= i: turns += 1 boarded += rem rem = 0 profit = boarded * B - turns * R if profit > max_profit: max_profit = profit max_turns = turns # print(i, c, boarded, rem, turns, res) if rem > 0: boarded += rem turns += 1 profit = boarded * B - turns * R if profit > max_profit: max_profit = profit max_turns = turns return max_turns if max_profit > 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: left = 0 cost = 0 step = 0 def ceildiv(a, b): return -(-a // b) for i in range(len(customers)): left += customers[i] if left >= 4: left -= 4 cost = 4*(boardingCost) - runningCost step +=1 else: cost = left*boardingCost - runningCost step +=1 left = 0 lefts = left // 4 leftc = left % 4 costl = leftc*boardingCost - runningCost cost = cost + left*boardingCost - runningCost*lefts step += lefts if cost > 0 : if costl > 0: return step +1 else : return step else : return -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: _max=float('-inf') queue,index,profit,rotations,buffer=0,0,0,0,0 _len=len(customers) while queue>=0 and index<_len: queue+=customers[index] profit+= ( ( min(queue,4)*boardingCost ) - ( runningCost ) ) queue-=min(queue,4) if profit>_max: rotations+=1 if profit==_max: buffer+=1 _max=max(_max,profit) index+=1 if index==_len and queue: profit+= ( ( ( (queue//4)*4 ) * boardingCost ) - ( (queue//4)*runningCost ) ) _max=max(_max,profit) rotations+=queue//4 queue-=((queue//4)*4) if queue: profit+= (( queue%4 * boardingCost ) - runningCost) if profit>_max: rotations+=1 _max=max(_max,profit) return rotations+buffer if _max>=0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: cur = 0 total = 0 profit1 = [0,0] profit2 = [0,0] stop = False left =0 if 4*boardingCost-runningCost<=0: return -1 while cur<len(customers): left+=customers[cur] if left>=4: left-=4 customer = 4 else: customer = left left = 0 if (customer*boardingCost-runningCost)+profit1[0]>profit1[0]: profit2[0]=(customer*boardingCost-runningCost)+profit1[0] profit2[1]=profit1[1]+1 profit1[0]=(customer*boardingCost-runningCost)+profit1[0] profit1[1]=profit1[1]+1 cur+=1 if left: profit1[0]+=(left//4)*(customer*boardingCost-runningCost) profit1[1]+=(left//4) if (left%4)*boardingCost-runningCost>0: profit1[0]+=(left%4)*boardingCost-runningCost profit1[1]+=1 if profit1[0]>profit2[0]: return profit1[1] elif profit1[0]<profit2[0]: return profit2[1] else: return min(profit1[1],profit2[1]) ''' while cur<len(customers): current = customers[cur] if current >=4: profit1[cur+1][0]= (current//4)*(4*boardingCost-runningCost)+profit1[cur][0] profit1[cur+1][1] = profit1[cur][1] +current//4 current = current%4 if profit1[cur][0]<profit1[cur+1][0]: profit2[cur+1][0]=profit1[cur+1][0] profit2[cur+1][1]=profit1[cur+1][1] else: profit2[cur+1][1]=profit1[cur][1] profit2[cur+1][0]=profit1[cur][0] if current>0: profit1[cur+1][0]=current*boardingCost-runningCost+profit1[cur+1][0] profit1[cur+1][1]=profit1[cur+1][1]+1 else: profit1[cur+1][0]=current*boardingCost-runningCost+profit1[cur][0] profit1[cur+1][1]=profit1[cur][1]+1 profit2[cur+1][1]=profit1[cur][1] profit2[cur+1][0]=profit1[cur][0] cur+=1 keys1 = list(range(len(customers)+1)) keys2= list(range(len(customers)+1)) keys1.sort(key=lambda x: (profit1[x][0],-profit1[x][1])) keys2.sort(key=lambda x:(profit2[x][0],-profit2[x][1])) key1= keys1[-1] key2 = keys2[-1] print(profit1) print(profit2) if profit1[key1][0]>profit2[key2][0]: return profit1[key1][1] elif profit1[key1][0]<profit2[key2][0]: return profit2[key1][1] else: return min(profit1[key1][1],profit2[key2][1]) '''
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: # Ugliest code ever dont even try reading this return self.min_operations_max_profit_recurse(customers, 0, boardingCost, runningCost, 0, 0)[1] def serve_remaining_people(self, waiting, boardingCost, runningCost, depth): profit = 0 num_rotations = waiting // 4 if num_rotations and 4 * boardingCost - runningCost > 0: depth += num_rotations profit += (4 * boardingCost - runningCost) * num_rotations if waiting % 4 and waiting % 4 * boardingCost - runningCost > 0: depth += 1 profit += waiting % 4 * boardingCost - runningCost return profit, depth def min_operations_max_profit_recurse(self, customers, index, boardingCost, runningCost, waiting, depth): if index == len(customers): return self.serve_remaining_people(waiting, boardingCost, runningCost, depth) # free gondola waiting += customers[index] gondola = min(waiting, 4) waiting -= gondola # try rotating profit, rotations = self.min_operations_max_profit_recurse(customers, index + 1, boardingCost, runningCost, waiting, depth + 1) profit += gondola * boardingCost - runningCost if profit <= 0: return -1, -1 # print(profit, rotations) return profit, rotations # [10,9,6] # 6 # 4 # 0: 4 * 6 - 4 = 24 - 4 # 1: 4 * 6 - 4 # 2: # waiting = 11 # index = 1 # gondola = 4 # depth = 1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: wait = 0 pro = 0 high = 0 res = -1 for i in range(len(customers)): vacc = 4 - wait if vacc <= 0: wait += customers[i] - 4 pro += 4 * boardingCost - runningCost # board all elif customers[i] <= vacc: # board=customers[i]+wait pro += boardingCost * (customers[i] + wait) - runningCost wait = 0 else: pro += boardingCost * 4 - runningCost wait += customers[i] - 4 if pro > high: high = pro res = i # determine after all arrives pro_per = boardingCost * 4 - runningCost if pro_per > 0: last = wait % 4 if wait >= 4: if boardingCost * last - runningCost > 0: return len(customers) + wait // 4 + 1 else: return len(customers) + wait // 4 if boardingCost * last - runningCost > 0: return len(customers) + 1 return res + 1 if res >= 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: return self.min_operations_max_profit_recurse(customers, 0, boardingCost, runningCost, 0, 0)[1] def serve_remaining_people(self, waiting, boardingCost, runningCost, depth): profit = 0 num_rotations = waiting // 4 if num_rotations and 4 * boardingCost - runningCost > 0: depth += num_rotations profit += (4 * boardingCost - runningCost) * num_rotations if waiting % 4 and waiting % 4 * boardingCost - runningCost > 0: depth += 1 profit += waiting % 4 * boardingCost - runningCost return profit, depth def min_operations_max_profit_recurse(self, customers, index, boardingCost, runningCost, waiting, depth): if index == len(customers): return self.serve_remaining_people(waiting, boardingCost, runningCost, depth) # free gondola waiting += customers[index] gondola = min(waiting, 4) waiting -= gondola # try rotating profit, rotations = self.min_operations_max_profit_recurse(customers, index + 1, boardingCost, runningCost, waiting, depth + 1) profit += gondola * boardingCost - runningCost if profit <= 0: return -1, -1 # print(profit, rotations) return profit, rotations # [10,9,6] # 6 # 4 # 0: 4 * 6 - 4 = 24 - 4 # 1: 4 * 6 - 4 # 2: # waiting = 11 # index = 1 # gondola = 4 # depth = 1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: nwaiting = 0 profit = 0 maxprofit = 0 res = - 1 for i,c in enumerate(customers,1): nwaiting += c onboard = min(4,nwaiting) nwaiting -= onboard profit += onboard*boardingCost - runningCost if maxprofit < profit: maxprofit = profit res = i if nwaiting > 0: roundn = nwaiting//4 nwaiting -= roundn*4 profit += roundn* (4*boardingCost - runningCost ) if maxprofit < profit: maxprofit = profit res += roundn if nwaiting > 0: profit += nwaiting*boardingCost - runningCost if maxprofit < profit: maxprofit = profit res += 1 return res if maxprofit > 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
from math import ceil class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: r = 0 boarded = 0 maxProfit = 0 maxR = -1 print(customers,boardingCost,runningCost) for i in range(len(customers)-1) : customers[i+1] += customers[i] - 4 if customers[i] > 4 else 0 r += 1 boarded += min(customers[i],4) profit = boarded * boardingCost - r * runningCost if profit > maxProfit : maxProfit = profit maxR = r r += ceil(customers[-1]/4) boarded += customers[-1] if customers[-1] % 4 > 0 and (customers[-1] % 4) * boardingCost - runningCost <= 0: r -= 1 boarded -= customers[-1] % 4 profit = boarded *boardingCost - r *runningCost if profit > maxProfit : maxProfit = profit maxR = r return maxR if maxProfit >0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def profit(self,s,customers,count,curr_profit,waiting,rot_cost,bill): # print(s,waiting,count) if curr_profit>self.profit_so_far: self.profit_so_far=curr_profit self.ans=count if waiting==0 and s>=len(customers): return if s>=len(customers) and waiting>4: n=waiting//4 waiting=waiting-(n*4) count+=(n) curr_profit+= (n*4*bill - (n)*rot_cost) self.profit(len(customers) ,customers,count,curr_profit,waiting, rot_cost,bill) return if s<len(customers):waiting+=customers[s] curr_profit-=rot_cost if waiting<=4: curr_profit+=waiting*bill waiting=0 else: waiting-=4 curr_profit+=4*bill self.profit(s+1,customers,count+1,curr_profit,waiting,rot_cost,bill) def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: self.ans=-1 self.profit_so_far=0 self.profit(0,customers,0,0,0,runningCost,boardingCost) return self.ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: avail, cur, high, high_idx = 0, 0, 0, -1 for i, c in enumerate(customers): avail += c if avail > 4: cur += 4 * boardingCost - runningCost avail -= 4 else: cur += avail * boardingCost - runningCost avail = 0 if cur > high: high, high_idx = cur, i + 1 if 4 * boardingCost - runningCost > 0: i += avail // 4 cur += (4 * boardingCost - runningCost) * (avail // 4) avail = avail % 4 high, high_idx = cur, i + 1 cur += avail * boardingCost - runningCost avail = 0 if cur > high: high, high_idx = cur, high_idx + 1 return high_idx
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: if boardingCost*4 <= runningCost: return -1 customers.append(0) profit = [] curProfit, runs = 0, 0 for i in range(len(customers)-1): if customers[i] >= 4: if i+1 < len(customers): customers[i+1] += customers[i]-4 customers[i] = 4 if customers[i]*boardingCost < runningCost: runs = runs + 1 profit.append((curProfit, runs)) continue curProfit += customers[i]*boardingCost - runningCost runs += 1 profit.append((curProfit, runs)) #print(customers, curProfit, runs) if customers[-1] > 0: runs = runs + (customers[-1]//4) curProfit += (customers[-1]//4)*boardingCost - (customers[-1]//4)//4*runningCost customers[-1] = customers[-1]%4 if customers[-1]*boardingCost > runningCost: runs = runs + 1 curProfit += customers[-1]*boardingCost - runningCost #print(\"w\", curProfit, runs) profit.append((curProfit, runs)) profit.sort(key = lambda x: (-x[0], x[1])) #print(profit) return profit[0][1] if profit[0][0] > 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: # use a list to record profits cs = [customers[0]] for tmp in customers[1:]: cs.append(cs[-1] + tmp) # first steps maxp = -1 maxn = -1 max_cap = 0 for i in range(len(customers)): max_cap = min(cs[i], max_cap + 4) cur_profit = max_cap * boardingCost - runningCost * (i + 1) if cur_profit > maxp: maxp = cur_profit maxn = i + 1 # how many people are left? ppl_left = cs[-1] - max_cap rounds = ppl_left // 4 cur_profit += rounds * (4 * boardingCost - runningCost) cur_round = len(customers) + rounds if cur_profit > maxp: maxp = cur_profit maxn = cur_round ppl_left2 = ppl_left % 4 cur_profit += (ppl_left2 * boardingCost - runningCost) cur_round += 1 if cur_profit > maxp: maxp = cur_profit maxn = cur_round return maxn
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: max_profit = times_rotated = float('-inf') total_customers = ran = left = i = 0 while i < len(customers) or left > 0: if i < len(customers): left += customers[i] if i < ran: i += 1 continue if left >= 4: times = left // 4 total_customers += 4 * times ran += times left -= 4 * times else: total_customers += left left = 0 ran += 1 curr_profit = total_customers * boardingCost - ran * runningCost if curr_profit > max_profit: max_profit = curr_profit times_rotated = ran i += 1 # print(total_customers, ran, curr_profit) if max_profit < 0: return -1 return times_rotated
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: if runningCost >= boardingCost*4: return -1 result = -1 maxProfit = 0 waiting = 0 current = 0 for i, customerCount in enumerate(customers): waiting += customerCount boarding = min(waiting, 4) waiting -= boarding current += boarding*boardingCost - runningCost if current > maxProfit: maxProfit = current result = i+1 if waiting > 0: fullRoundsLeft = waiting // 4 lastRoundQuantity = waiting % 4 current += fullRoundsLeft * (4*boardingCost - runningCost) turns = len(customers) + fullRoundsLeft if current > maxProfit: maxProfit = current result = turns current += lastRoundQuantity*boardingCost - runningCost turns += 1 if current > maxProfit: maxProfit = current result = turns return result if result >= 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: numCust = 0 rotation = 0 curWait = 0 maxProfit = 0 minRo = 0 ##8 for i, customer in enumerate(customers): while rotation < i: rotation += 1 curWait -= 4 curWait = max(0, curWait) curProfit = (numCust - curWait) * boardingCost - rotation * runningCost if curProfit > maxProfit: maxProfit = curProfit minRo = rotation numCust += customer curWait += customer rots = curWait // 4 rotation += rots curWait %= 4 curProfit = (numCust - curWait) * boardingCost - rotation * runningCost if curProfit > maxProfit: maxProfit = curProfit minRo = rotation if curWait > 0: rotation += 1 curProfit = numCust * boardingCost - rotation * runningCost if curProfit > maxProfit: maxProfit = curProfit minRo = rotation return minRo if maxProfit > 0 else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: profit = 0 waiting = 0 rotations = 0 ans = -1 for c in customers: waiting += c cap = 0 if waiting >= 4: waiting -= 4 cap += 4 else: cap = waiting waiting = 0 cur_profit = profit + (cap * boardingCost) - runningCost rotations += 1 if cur_profit > profit: ans = rotations profit = cur_profit if waiting > 0: req_rotations = math.ceil(waiting/4) ignore = waiting // 4 possible_profit = (waiting * boardingCost) - (req_rotations * runningCost) full_only = ((waiting - (waiting % 4)) * boardingCost) - (ignore * runningCost) if possible_profit > full_only: additional = req_rotations if profit + possible_profit > profit: ans = rotations + req_rotations else: additional = ignore if profit + full_only > profit: ans = rotations + ignore return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, A, BC, RC): ans=profit=t=0 maxprofit=0 wait=i=0 n=len(A) while i<n: if i<n: wait+=A[i] i+=1 t+=1 y=wait if wait<4 else 4 wait-=y profit+=y*BC profit-=RC if profit>maxprofit: maxprofit=profit ans=t profit+=wait//4*BC #profit-=RC*(wait+3)//4 if profit>maxprofit: ans+=wait//4 if wait%4*BC>RC: maxprofit+=wait%4*BC-RC ans+=1 if maxprofit<=0: return -1 else: return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: ans=-1 maxp=0 curr=0 c=0 track=0 for i in range(len(customers)): track+=customers[i] curr+=min(4,track)*boardingCost-runningCost track-=min(4,track) c+=1 if curr>maxp: maxp=curr ans=c if track>=4: curr+=(track-track%4)*boardingCost-(track//4)*runningCost c+=track//4 if curr>maxp: maxp=curr ans=c curr+=(track%4)*boardingCost-runningCost c+=1 if curr>maxp: maxp=curr ans=c return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: ans = -1 if boardingCost * 4 < runningCost: return ans cur_profit = 0 wait_num = 0 dic = {} for i in range(len(customers)): if customers[i] + wait_num < 5: cur_profit += boardingCost * (customers[i] + wait_num) - runningCost wait_num = 0 else: cur_profit += boardingCost * 4 - runningCost wait_num += customers[i] - 4 if cur_profit > ans: ans = cur_profit dic[ans] = i+1 if wait_num > 0: while wait_num > 3: wait_num -= 4 dic[ans] += 1 if wait_num * boardingCost > runningCost: dic[ans] += 1 return dic[ans]
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: if boardingCost*4 < runningCost: return -1 rot = 0 spot = 0 prof = max_prof = [0, 0] while spot < len(customers): rot += 1 curr = customers[spot] if curr > 4: if spot == len(customers) - 1: temp = prof[0] + 4*boardingCost*(curr//4) - runningCost*(curr//4) max_prof1 = max(max_prof, [temp, rot -1 + curr//4]) temp = prof[0] + 4*boardingCost*(curr//4) + boardingCost*(curr%4) - runningCost*(curr//4+bool(curr%4)) max_prof2 = max(max_prof, [temp, rot -1 + curr//4 + bool(curr%4)]) if max_prof1[0] != max_prof2[0]: return max(max_prof1, max_prof2)[1] if max_prof1[0] == max_prof2[0]: return max_prof1[1] else: customers[spot+1] += curr-4 prof[0] += 4*boardingCost - runningCost else: prof[0] += curr*boardingCost - runningCost max_prof = max(max_prof, [prof[0], rot]) spot += 1 return max_prof[1]
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: profit = 0 waiting = 0 rotation = 0 max_profit = 0 ans = None for customer in customers: customer += waiting rotation += 1 if customer>=4: profit += 4*boardingCost - runningCost waiting = customer-4 else: profit = customer*boardingCost - runningCost waiting = 0 if max_profit<profit: max_pprofit = profit ans = rotation if waiting>0: if waiting>4: while waiting>4: profit += 4*boardingCost - runningCost waiting = waiting-4 rotation += 1 #print(profit) if max_profit<profit: max_pprofit = profit ans = rotation profit = waiting*boardingCost - runningCost rotation+=1 if max_profit<profit: max_pprofit = profit ans = rotation return ans if ans else -1
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: profit = 0 maxProfit = -1 res = -1 currentCustomer = 0 for i, c in enumerate(customers): currentCustomer += c if currentCustomer <=4: profit += currentCustomer*boardingCost - runningCost currentCustomer = 0 else: profit += 4*boardingCost - runningCost currentCustomer -=4 if profit > maxProfit: maxProfit = profit res = i+1 rounds = currentCustomer // 4 left = currentCustomer % 4 if boardingCost*4 - runningCost > 0: profit += rounds*(boardingCost*4 - runningCost) if profit > maxProfit: maxProfit = profit res += rounds profit += boardingCost*left-runningCost if profit > maxProfit: maxProfit = profit res +=1 return res
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: minimum = runningCost//boardingCost+1 if minimum>4: return -1 max_value = 0 profit = 0 remain = 0 turns = 0 res = None for c in customers: turns += 1 temp = remain+c if temp>=4: remain = temp-4 profit += 4*boardingCost-runningCost else: remain = 0 profit += temp*boardingCost-runningCost if profit>max_value: res = turns max_value = profit print(turns, remain) while remain: turns += 1 if remain>=4: remain -= 4 profit += 4*boardingCost-runningCost else: profit += remain*boardingCost-runningCost remain = 0 if profit>max_value: res = turns max_value = profit return res
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, nums: List[int], pos: int, fee: int) -> int: if 4*pos <= fee: return -1 ans = cur = 0 s = sum(nums) best = -math.inf p = 0 for i, x in enumerate(nums): cur += x if cur >= 4: p += (4*pos - fee) cur -= 4 else: p += (cur * pos - fee) cur = 0 if p > best: best = p ans = i + 1 res = len(nums) while cur > 0: res += 1 if cur >= 4: p += (4*pos - fee) cur -= 4 else: p += (cur * pos - fee) cur = 0 if p > best: best = p ans = res if best <=0: return -1 return ans
You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars. You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again. You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation. Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.   Example 1: Input: customers = [8,3], boardingCost = 5, runningCost = 6 Output: 3 Explanation: The numbers written on the gondolas are the number of people currently there. 1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14. 2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28. 3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37. The highest profit was $37 after rotating the wheel 3 times. Example 2: Input: customers = [10,9,6], boardingCost = 6, runningCost = 4 Output: 7 Explanation: 1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20. 2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40. 3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60. 4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80. 5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100. 6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120. 7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122. The highest profit was $122 after rotating the wheel 7 times. Example 3: Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92 Output: -1 Explanation: 1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89. 2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177. 3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269. 4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 11 * $1 - 4 * $92 = -$357. 5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447. The profit was never positive, so return -1. Example 4: Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 Output: 9 Explanation: 1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4. 2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8. 3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12. 4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16. 5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20. 6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24. 7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28. 8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32. 9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36. 10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31. The highest profit was $36 after rotating the wheel 9 times.   Constraints: n == customers.length 1 <= n <= 105 0 <= customers[i] <= 50 1 <= boardingCost, runningCost <= 100
class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: inLine = 0 profit = 0 maxProf = -1 maxRoll = -1 rolls = 0 for i in customers: inLine += i if inLine >= 4: profit += 4*boardingCost - runningCost inLine -= 4 else: profit += inLine*boardingCost - runningCost inLine = 0 rolls += 1 if profit > maxProf: maxProf = profit maxRoll = rolls while inLine: if inLine >= 4: profit += 4*boardingCost - runningCost inLine -= 4 else: profit += inLine*boardingCost - runningCost inLine = 0 rolls += 1 # maxProf = max(maxProf, profit) if profit > maxProf: maxProf = profit maxRoll = rolls return maxRoll
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: V1 = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"] V2 = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"] V3 = ["Thousand", "Million", "Billion"] def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return "Zero" answer = self.convert_hundred(num % 1000) for i in range(3): num //= 1000 if num % 1000 > 0: following = " " + answer if answer else "" answer = self.convert_hundred(num % 1000) + " " + self.V3[i] + following return answer def convert_hundred(self, num): answer = "" a = num // 100 b = num % 100 c = num % 10 if b < 20: answer = self.V1[b] else: following = " " + self.V1[c] if c > 0 else "" answer = self.V2[b // 10] + following if a > 0: following = " " + answer if answer else "" answer = self.V1[a] + " Hundred" + following return answer
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def parseHundred(self, n): to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split() tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split() if n == 0: return '' w = "" while n > 0: if n > 99: digit = n//100 w += to19[digit-1] + ' ' + 'Hundred' n = n % 100 if n != 0: w += ' ' else: if n <= 19: w += to19[n-1] n = 0 else: digit = n// 10 w += tens[digit-2] n = n % 10 if n != 0: w += ' ' return w def numberToWords(self, num): """ :type num: int :rtype: str """ thousands = ['', ' Thousand ', ' Million ', ' Billion '] i = 0 w = "" if num == 0: return 'Zero' while num > 0: digits = num % 1000 if digits != 0: w = self.parseHundred(digits) + thousands[i] + w num = num//1000 i += 1 return w.strip()
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def numberToWords(self, num): return ' '.join(self.words(num)) or 'Zero' def words(self, n): print(n) to19 = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'] tens = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety',] if n < 20: return to19[n - 1: n] if n < 100: return [tens[n//10 - 2]] + self.words(n%10) if n < 1000: return [to19[n//100 - 1]] + ['Hundred'] + self.words(n%100) for p, w in enumerate(('Thousand', 'Million', 'Billion'), 1): if n < 1000**(p + 1): return self.words(n//1000**p) + [w] + self.words(n%1000**p) """ :type num: int :rtype: str """
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def numberToWords(self, num): """ :type num: int :rtype: str """ if num==0: return "Zero" res="" less20=["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"] tens=["Zero","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"] thousands=["","Thousand","Million","Billion"] for k in range(4): if num ==0: break cur="" curNum=num%1000 num//=1000 print(("num",num)) if curNum>=100: x=curNum//100 curNum%=100 cur=less20[x]+" Hundred" if curNum>0: if cur: cur+=" " if curNum<20: cur+=less20[curNum] else: x=curNum//10 curNum%=10 cur+=tens[x] if curNum!=0: cur+=" "+less20[curNum] print("cur") if cur: res=cur+((" "+thousands[k] if k else"")) +((" "+res if res else"")) return res
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def __init__(self): self.twenties = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'] self.tens = ['', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'] self.thousands = ['', 'Thousand', 'Million', 'Billion'] def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return 'Zero' result = '' for i in range(len(self.thousands)): if num % 1000 != 0: result = self.helper(num%1000) + self.thousands[i] + ' ' + result num //= 1000 return result.strip() def helper(self, num): if num == 0: return '' elif num < 20: return self.twenties[num] + ' ' elif num < 100: return self.tens[num//10] + ' ' + self.helper(num%10) else: return self.twenties[num//100] + ' Hundred ' + self.helper(num%100)
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def numberToWords(self, num): """ :type num: int :rtype: str """ # special case 0 def translate(num): if num == 0: return '' less20 = [ '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine' , 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen' , 'Eighteen', 'Nineteen' ] more20 = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'] res = '' if num // 100: res = less20[num // 100] + ' Hundred' + (' ' if num % 100 else '') num %= 100 res += less20[num] if num < 20 else more20[num // 10] + (' ' + less20[num % 10] if num % 10 else '') return res if num == 0: return 'Zero' unit = ['', 'Thousand', 'Million', 'Billion'] i = 0 res = [] while num > 0: t = translate(num % 1000) if t: res += [unit[i], t] num //= 1000 i += 1 return (' '.join(res[::-1])).strip()
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: digit = { 0: '', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine' } teen = { 10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen' } ten = { 2: 'Twenty', 3: 'Thirty', 4: 'Forty', 5: 'Fifty', 6: 'Sixty', 7: 'Seventy', 8: 'Eighty', 9: 'Ninety' } idx = { 0: '', 1: 'Thousand', 2: 'Million', 3: 'Billion', 4: 'Trillion', 5: 'Quadrillion' } def helper(self,num): result = "" while len(num) < 3: num = '0' + num if num[0] is not '0': result = self.digit[int(num[0])] + ' Hundred' if num[1] is not '0': if num[1] == '1': return result + " " + self.teen[int(num[1]+num[2])] else: return result + " " + self.ten[int(num[1])] + " " + self.digit[int(num[2])] if num[2] is not '0': result = result + " " + self.digit[int(num[2])] return result def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return 'Zero' result = "" rev = str(num)[::-1] slices = [rev[i:i+3] for i in range(0,len(str(num)),3)] for idx, slice in enumerate((slices)): if slice == '000': continue else: result = self.helper(str(slice[::-1])) + " " + self.idx[idx] + " "+ result result = result.split(' ') result = [ x for x in result if x is not ""] return ' '.join(result)
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def numberToWords(self, num): """ :type num: int :rtype: str """ num_lyst = ['', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'] tens_lyst = ['', '', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'] under20_lyst = ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'] large_scale = ['', 'Thousand', 'Million', 'Billion'] def convert(num): out_str = '' hundred = num // 100 ten_digit = num % 100 if hundred: out_str += num_lyst[hundred] + ' ' + 'Hundred ' if ten_digit: if ten_digit < 10: out_str += num_lyst[ten_digit] elif ten_digit < 20: out_str += under20_lyst[ten_digit % 10] else: out_str += tens_lyst[ten_digit // 10] + ' ' + num_lyst[ten_digit % 10] return out_str.strip() if not num: return 'Zero' res = num // 1000 # 商 last3 = num % 1000 # 余数,后三位 ans = '' while res or last3: if last3: ans = convert(last3) + ' ' + large_scale.pop(0) + ' '+ ans else: large_scale.pop(0) last3 = res % 1000 res = res //1000 return ans.strip()
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: def getEnglishThousand(self, n): if 1 <= n <= 9: return ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'][n-1] elif 10 <= n <= 19: return ['Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'][n-10] elif 20 <= n <= 99: eng = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'][(n//10)-2] if n % 10 > 0: return eng + ' ' + self.getEnglishThousand(n % 10) else: return eng else: hundred = self.getEnglishThousand(n // 100) + ' Hundred' if n % 100 > 0: return hundred + ' ' + self.getEnglishThousand(n % 100) else: return hundred def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return 'Zero' stack = ['Billion', 'Million', 'Thousand', None] english = [] while num: quantifier = stack.pop() if num % 1000 > 0: english.append(self.getEnglishThousand(num % 1000) + (' ' + quantifier if quantifier else '')) num //= 1000 return ' '.join(reversed(english))
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. Example 1: Input: 123 Output: "One Hundred Twenty Three" Example 2: Input: 12345 Output: "Twelve Thousand Three Hundred Forty Five" Example 3: Input: 1234567 Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" Example 4: Input: 1234567891 Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
class Solution: quantity_unit = ['', 'Thousand', 'Million', 'Billion'] digit_to_str = ['', ' One', ' Two', ' Three', ' Four', ' Five', ' Six', ' Seven', ' Eight', ' Nine'] ten_to_str = {10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty', 60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety', 0: ''} def read_three_digits(self, unit, ten, hundred, index): if ten == 1: str_of_num = ' ' + Solution.ten_to_str[ten * 10 + unit] else: str_of_num = (' ' + Solution.ten_to_str[ten * 10] + Solution.digit_to_str[unit]) if ten > 1 else Solution.digit_to_str[unit] str_of_num = (Solution.digit_to_str[hundred] + ' Hundred' if hundred > 0 else "") + str_of_num # print(str_of_num + str(index)) return str_of_num[1:] + ' ' + Solution.quantity_unit[index] if str_of_num else str_of_num[1:] def numberToWords(self, num): """ :type num: int :rtype: str """ if num == 0: return 'Zero' str_of_num = "" index = 0 while num > 0: part = num % 1000 unit = part % 10 ten = (part % 100) // 10 hundred = part // 100 str_of_num = self.read_three_digits(unit, ten, hundred, index) + ' ' + str_of_num.strip() num //= 1000 index += 1 return str_of_num.strip()
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def preferences_to_scores(self, preferences): scores = {} for u, up in enumerate(preferences): for s, v in enumerate(up): scores[(u, v)] = s return scores def unhappy_friends(self, scores, a, b): ret = set() for ai, aa in enumerate(a): af = a[1 - ai] for bi, bb in enumerate(b): bf = b[1 - bi] if scores[(aa, bb)] < scores[(aa, af)] and scores[(bb, aa)] < scores[(bb, bf)]: ret.add(aa) ret.add(bb) return ret def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: scores = self.preferences_to_scores(preferences) ret = set() for i, a in enumerate(pairs): for j in range(i): b = pairs[j] ret |= self.unhappy_friends(scores, a, b) return len(ret)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: preference_list = [[0 for _ in range(n)] for _ in range(n)] for x in range(n): for i, y in enumerate(preferences[x]): preference_list[x][y] = n-i-1 #print(preference_list) unhappy={} for i in range(n//2): x,y = pairs[i] for j in range(n//2): u,v = pairs[j] if i!=j: if ((preference_list[x][y]<preference_list[x][u] and preference_list[u][x]>preference_list[u][v]) or (preference_list[x][y]<preference_list[x][v] and preference_list[v][u]<preference_list[v][x])): #print(f'X-> x, y : {x, y}; u,v: {u,v}') unhappy[x]=1 if ((preference_list[y][x]<preference_list[y][u] and preference_list[u][y]>preference_list[u][v]) or (preference_list[y][x]<preference_list[y][v] and preference_list[v][u]<preference_list[v][y])): #print(f'Y-> y,x : {y,x}; u,v: {u,v}') unhappy[y]=1 #print(unhappy) res = len(unhappy) return res
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: pref = {} for i in range(len(preferences)): pref[i] = {} plist = preferences[i] for j in range(len(plist)): pref[i][plist[j]] = j print(pref) unhappy = set() for i in range(len(pairs)): for j in range(len(pairs)): if i == j: continue for x in pairs[i]: y = pairs[i][1] if x == pairs[i][0] else pairs[i][0] for u in pairs[j]: v = pairs[j][1] if u == pairs[j][0] else pairs[j][0] if (pref[x][y] > pref[x][u] and pref[u][v] > pref[u][x]): unhappy.add(x) return len(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, P: List[List[int]], A: List[List[int]]) -> int: # O(n^3) # have to look at nC2 pairs unhappy = set() for i in range(n//2): for j in range(i+1,n//2): Ai0, Ai1, Aj0, Aj1 = A[i][0], A[i][1], A[j][0], A[j][1] # check all 4 poss # O(n) if P[Ai0].index(Ai1) > P[Ai0].index(Aj0) and P[Aj0].index(Aj1) > P[Aj0].index(Ai0): unhappy.add(Ai0) unhappy.add(Aj0) Ai0, Ai1, Aj0, Aj1 = A[i][1], A[i][0], A[j][0], A[j][1] # check all 4 poss # O(n) if P[Ai0].index(Ai1) > P[Ai0].index(Aj0) and P[Aj0].index(Aj1) > P[Aj0].index(Ai0): unhappy.add(Ai0) unhappy.add(Aj0) Ai0, Ai1, Aj0, Aj1 = A[i][0], A[i][1], A[j][1], A[j][0] # check all 4 poss # O(n) if P[Ai0].index(Ai1) > P[Ai0].index(Aj0) and P[Aj0].index(Aj1) > P[Aj0].index(Ai0): unhappy.add(Ai0) unhappy.add(Aj0) Ai0, Ai1, Aj0, Aj1 = A[i][1], A[i][0], A[j][1], A[j][0] # check all 4 poss # O(n) if P[Ai0].index(Ai1) > P[Ai0].index(Aj0) and P[Aj0].index(Aj1) > P[Aj0].index(Ai0): unhappy.add(Ai0) unhappy.add(Aj0) return len(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: unhappy = [0] * n for i in range(n//2): a, b = pairs[i] b_a_idx, a_b_idx = preferences[b].index(a), preferences[a].index(b) for j in range(i+1, n//2): c, d = pairs[j] c_a_idx = preferences[c].index(a) c_b_idx = preferences[c].index(b) c_d_idx = preferences[c].index(d) d_a_idx = preferences[d].index(a) d_b_idx = preferences[d].index(b) d_c_idx = preferences[d].index(c) a_c_idx = preferences[a].index(c) a_d_idx = preferences[a].index(d) b_c_idx = preferences[b].index(c) b_d_idx = preferences[b].index(d) # a <-> c if c_a_idx < c_d_idx and a_c_idx < a_b_idx: unhappy[a] = unhappy[c] = 1 # a <-> d if d_a_idx < d_c_idx and a_d_idx < a_b_idx: unhappy[a] = unhappy[d] = 1 # b <-> c if c_b_idx < c_d_idx and b_c_idx < b_a_idx: unhappy[b] = unhappy[c] = 1 # b <-> d if d_b_idx < d_c_idx and b_d_idx < b_a_idx: unhappy[b] = unhappy[d] = 1 return sum(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: unhappy = [0] * n for i in range(n//2): a, b = pairs[i] b_a_idx, a_b_idx = preferences[b].index(a), preferences[a].index(b) for j in range(i+1, n//2): c, d = pairs[j] c_a_idx = preferences[c].index(a) c_b_idx = preferences[c].index(b) c_d_idx = preferences[c].index(d) d_a_idx = preferences[d].index(a) d_b_idx = preferences[d].index(b) d_c_idx = preferences[d].index(c) a_c_idx = preferences[a].index(c) a_d_idx = preferences[a].index(d) b_c_idx = preferences[b].index(c) b_d_idx = preferences[b].index(d) # a-c if c_a_idx < c_d_idx and a_c_idx < a_b_idx: unhappy[a] = unhappy[c] = 1 # a-d if d_a_idx < d_c_idx and a_d_idx < a_b_idx: unhappy[a] = unhappy[d] = 1 # b-c if c_b_idx < c_d_idx and b_c_idx < b_a_idx: unhappy[b] = unhappy[c] = 1 # b-d if d_b_idx < d_c_idx and b_d_idx < b_a_idx: unhappy[b] = unhappy[d] = 1 return sum(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: idx_table = collections.defaultdict(lambda: collections.defaultdict(int)) for i in range(n): for idx, person in enumerate(preferences[i]): idx_table[i][person] = idx unhappy = [0] * n for i in range(n//2): a, b = pairs[i] b_a_idx, a_b_idx = idx_table[b][a], idx_table[a][b] for j in range(i+1, n//2): c, d = pairs[j] c_a_idx = idx_table[c][a] c_b_idx = idx_table[c][b] c_d_idx = idx_table[c][d] d_a_idx = idx_table[d][a] d_b_idx = idx_table[d][b] d_c_idx = idx_table[d][c] a_c_idx = idx_table[a][c] a_d_idx = idx_table[a][d] b_c_idx = idx_table[b][c] b_d_idx = idx_table[b][d] if c_a_idx < c_d_idx and a_c_idx < a_b_idx: unhappy[a] = unhappy[c] = 1 # a & c prefer each other if d_a_idx < d_c_idx and a_d_idx < a_b_idx: unhappy[a] = unhappy[d] = 1 # a & d prefer each other if c_b_idx < c_d_idx and b_c_idx < b_a_idx: unhappy[b] = unhappy[c] = 1 # b & c prefer each other if d_b_idx < d_c_idx and b_d_idx < b_a_idx: unhappy[b] = unhappy[d] = 1 # b & d prefer each other return sum(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: res = set() mem = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(n-1): mem[i][preferences[i][j]] = j+1 for i in range(n//2): x = pairs[i][0] y = pairs[i][1] for j in range(i+1, n//2): u = pairs[j][0] v = pairs[j][1] if mem[x][u] < mem[x][y] and mem[u][x] < mem[u][v]: res.add(x) res.add(u) if mem[x][v] < mem[x][y] and mem[v][x] < mem[v][u]: res.add(x) res.add(v) if mem[y][u] < mem[y][x] and mem[u][y] < mem[u][v]: res.add(y) res.add(u) if mem[y][v] < mem[y][x] and mem[v][y] < mem[v][u]: res.add(y) res.add(v) return len(res)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
import numpy as np class Solution: def check_happy(self,x,y,u,v,fmatrix,unhappy): if(fmatrix[x][u]>fmatrix[x][y] and fmatrix[u][x]>fmatrix[u][v]): unhappy.add(x) unhappy.add(u) def unhappyFriends(self, n, preferences, pairs) -> int: if(n==2): return 0 unhappy = set() fmatrix = np.zeros((n,n),dtype=int) for i,friendpref in enumerate(preferences): for j,pref_ind in enumerate(friendpref): fmatrix[i][pref_ind] = n-1-j for i in range(n//2-1): for j in range(i+1, n//2): x,y,u,v = pairs[i][0],pairs[i][1],pairs[j][0],pairs[j][1] self.check_happy(x,y,u,v,fmatrix,unhappy) self.check_happy(x,y,v,u,fmatrix,unhappy) self.check_happy(y,x,u,v,fmatrix,unhappy) self.check_happy(y,x,v,u,fmatrix,unhappy) return len(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
import numpy as np class Solution: def check_happy(self,x,y,u,v,fmatrix,unhappy): if(fmatrix[x][u]>fmatrix[x][y] and fmatrix[u][x]>fmatrix[u][v]): unhappy.add(x) unhappy.add(u) def unhappyFriends(self, n, preferences, pairs) -> int: unhappy = set() fmatrix = np.zeros((n,n),dtype=int) for i,friendpref in enumerate(preferences): for j,pref_ind in enumerate(friendpref): fmatrix[i][pref_ind] = n-1-j for i in range(n//2-1): for j in range(i+1, n//2): x,y,u,v = pairs[i][0],pairs[i][1],pairs[j][0],pairs[j][1] self.check_happy(x,y,u,v,fmatrix,unhappy) self.check_happy(x,y,v,u,fmatrix,unhappy) self.check_happy(y,x,u,v,fmatrix,unhappy) self.check_happy(y,x,v,u,fmatrix,unhappy) return len(unhappy)
You are given a list of preferences for n friends, where n is always even. For each person i, preferences[i] contains a list of friends sorted in the order of preference. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from 0 to n-1. All the friends are divided into pairs. The pairings are given in a list pairs, where pairs[i] = [xi, yi] denotes xi is paired with yi and yi is paired with xi. However, this pairing may cause some of the friends to be unhappy. A friend x is unhappy if x is paired with y and there exists a friend u who is paired with v but: x prefers u over y, and u prefers x over v. Return the number of unhappy friends.   Example 1: Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]] Output: 2 Explanation: Friend 1 is unhappy because: - 1 is paired with 0 but prefers 3 over 0, and - 3 prefers 1 over 2. Friend 3 is unhappy because: - 3 is paired with 2 but prefers 1 over 2, and - 1 prefers 3 over 0. Friends 0 and 2 are happy. Example 2: Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]] Output: 0 Explanation: Both friends 0 and 1 are happy. Example 3: Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]] Output: 4   Constraints: 2 <= n <= 500 n is even. preferences.length == n preferences[i].length == n - 1 0 <= preferences[i][j] <= n - 1 preferences[i] does not contain i. All values in preferences[i] are unique. pairs.length == n/2 pairs[i].length == 2 xi != yi 0 <= xi, yi <= n - 1 Each person is contained in exactly one pair.
class Solution: def get_pair(self, i): for j in self.pairs: if i in j: for k in j: if k != i: return k def t_happy(self, t, i): pair = self.get_pair(t) for j in self.prefer[t]: if j == pair: return True if j == i: return False def happy(self, i, p): prefer = self.prefer[i] if prefer[0] == p: return True for j in range(1, len(prefer)): if not self.t_happy(prefer[j-1], i): return False if prefer[j] == p: return True def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int: self.prefer = preferences self.pairs = pairs count = 0 for i in range(n): p = self.get_pair(i) if not self.happy(i, p): count += 1 return count
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 0
class Solution: def findLength(self, A, B): def check(length): seen = {A[i:i+length] for i in range(len(A) - length + 1)} return any(B[j:j+length] in seen for j in range(len(B) - length + 1)) A = ''.join(map(chr, A)) B = ''.join(map(chr, B)) lo, hi = 0, min(len(A), len(B)) + 1 while lo < hi: mi = int((lo + hi) / 2) if check(mi): lo = mi + 1 else: hi = mi return lo - 1
Given an array of digits, you can write numbers using each digits[i] as many times as we want.  For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.   Example 1: Input: digits = ["1","3","5","7"], n = 100 Output: 20 Explanation: The 20 numbers that can be written are: 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77. Example 2: Input: digits = ["1","4","9"], n = 1000000000 Output: 29523 Explanation: We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array. Example 3: Input: digits = ["7"], n = 8 Output: 1   Constraints: 1 <= digits.length <= 9 digits[i].length == 1 digits[i] is a digit from '1' to '9'. All the values in digits are unique. 1 <= n <= 109
class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int: count = 0 length = 1 n_str = str(n) while length < len(n_str): count+= len(digits)**length length+=1 digits_sorted = sorted(digits) ## now length should equal to len(n), we compare the number with same length current_digit = 0 while current_digit < length: for digit in digits_sorted: next_round = False if digit < n_str[current_digit]: count+=len(digits)**(length-current_digit-1) elif digit > n_str[current_digit]: return count else: if current_digit == length-1: return count+1 else: current_digit+=1 next_round = True break if not next_round: return count return count
Given an array of digits, you can write numbers using each digits[i] as many times as we want.  For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.   Example 1: Input: digits = ["1","3","5","7"], n = 100 Output: 20 Explanation: The 20 numbers that can be written are: 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77. Example 2: Input: digits = ["1","4","9"], n = 1000000000 Output: 29523 Explanation: We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array. Example 3: Input: digits = ["7"], n = 8 Output: 1   Constraints: 1 <= digits.length <= 9 digits[i].length == 1 digits[i] is a digit from '1' to '9'. All the values in digits are unique. 1 <= n <= 109
class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int: ns = str(n) dp = 1 digits.sort() M, N = len(ns), len(digits) for i in range(M-1, -1, -1): dp2 = 0 for d in digits: if d>ns[i]: break if d<ns[i]: dp2 += N**(M-1-i) else: dp2 += dp dp = dp2 return dp + sum(N**i for i in range(1, M))
Given an array of digits, you can write numbers using each digits[i] as many times as we want.  For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'. Return the number of positive integers that can be generated that are less than or equal to a given integer n.   Example 1: Input: digits = ["1","3","5","7"], n = 100 Output: 20 Explanation: The 20 numbers that can be written are: 1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77. Example 2: Input: digits = ["1","4","9"], n = 1000000000 Output: 29523 Explanation: We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers, 81 four digit numbers, 243 five digit numbers, 729 six digit numbers, 2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers. In total, this is 29523 integers that can be written using the digits array. Example 3: Input: digits = ["7"], n = 8 Output: 1   Constraints: 1 <= digits.length <= 9 digits[i].length == 1 digits[i] is a digit from '1' to '9'. All the values in digits are unique. 1 <= n <= 109
class Solution: def atMostNGivenDigitSet(self, digits: List[str], n: int) -> int: def less(digits, d): return len([i for i in digits if i < d ]) cnt = 0 ld, ln = len(digits), len(str(n)) N = str(n) for i in range(ln-1): cnt += ld ** (i+1) for i in range(ln): cnt += less(digits, N[i]) * (ld ** (ln - i -1) ) if N[i] not in digits: return cnt return cnt + 1
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() i = 0 j = len(piles) - 1 max_coins = 0 for i in range(len(piles) // 3, len(piles), 2): max_coins += piles[i] return max_coins
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse=True) return sum(piles[1:int(len(piles)/3*2):2])
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles = sorted(piles) piles.reverse() ans = 0 for i in range(len(piles)//3): ans = ans + piles[2*i+1] return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: list.sort(piles , reverse=True) print(piles) init =0 for j in range((len(piles)//3)): init=init + piles[j*2+1] return init
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse=True) ans = 0 i = 1 j = len(piles)-1 while(j-i >= 1): ans+=piles[i] j-=1 i+=2 return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: rounds = [] piles.sort() for i in range((int)(len(piles)/3)): round_draw = [] round_draw.append(piles[i]) round_draw.append(piles[-i*2 - 2]) round_draw.append(piles[-i*2 - 1]) rounds.append(round_draw) sum = 0 for round_draw in rounds: sum += round_draw[1] return sum
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: result = 0 piles.sort(reverse = True) for i in range(1, 2*len(piles)//3, 2): result += piles[i] return result
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: # # With deque # piles.sort() # piles = deque(piles) # me = 0 # while len(piles) > 2: # piles.pop() # me += piles.pop() # if piles: # piles.popleft() # return me # With 2 pointers piles.sort() me = 0 start, end = 0, len(piles) - 1 while start < end: if end-1 > 0: me += piles[end-1] start, end = start+1, end-2 return me
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: from collections import deque def maxCoins(self, piles: List[int]) -> int: piles.sort() n = len(piles)//3 s = piles[n:n*3] return sum([s[i] for i in range(len(s)) if i%2==0])
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() left = 0 right = len(piles) - 2 total = 0 while(left < right): total += piles[right] left += 1 right -= 2 return total
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: n = len(piles) piles = sorted(piles) piles = deque(piles) ans = 0 while piles: piles.pop() ans += piles.pop() piles.popleft() return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(key=lambda k: k * -1) return sum(piles[1:len(piles)//3*2:2]) # 9, 8, 1 # 7, 6, 2 # 5, 4, 3
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles = sorted(piles, key=lambda x: -x) result = 0 for i in range(len(piles) // 3): result += piles[1 + 2 * i] return result
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles = sorted(piles,reverse = True) ans = 0 for i in range(len(piles)//3): ans = ans + piles[2*i+1] return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() piles = deque(piles) me = 0 while piles: alice = piles.pop() if piles: me += piles.pop() if piles: bob = piles.popleft() return me
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: rounds = len(piles) // 3 score = 0 piles.sort() for x in range(len(piles) - 2, rounds - 1 , -2): score += piles[x] return score
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles = sorted(piles) coins = 0 for i in range(len(piles) // 3, len(piles), 2): coins += piles[i] return coins
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, a: List[int]) -> int: a.sort() n = len(a) i = n // 3 ans = 0 while i < n: ans += a[i] i += 2 return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
import heapq class Solution: def maxCoins(self, piles: List[int]) -> int: choice = heapq.nlargest(len(piles)//3*2,piles) ans = 0 for i in range(1,len(choice),2): ans += choice[i] return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: # With deque piles.sort() piles = deque(piles) me = 0 while len(piles) > 2: piles.pop() me += piles.pop() if piles: piles.popleft() return me # # With 2 pointers # piles.sort() # me = 0 # start, end = 0, len(piles) - 1 # while start < end: # if end-1 > 0: # me += piles[end-1] # start, end = start+1, end-2 # return me
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() n = len(piles) // 3 piles = piles[n:] # remove Bob's return sum(piles[i*2] for i in range(n))
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse = True) i = 1 x = len(piles) ans = [] while i < x: ans.append(piles[i]) x = x- 1 i = i + 2 return sum(ans)
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: num=int(len(piles)/3) sor=sorted(piles) count=0 for i in range(1,num+1): count+=sor[num*3-i*2] return count
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: res = 0 piles.sort(reverse = True) for i in range(len(piles)//3): res += piles[2*(i+1)-1] return res
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: # [2,4,1,2,7,8] # [1,2,2,4,7,8] piles.sort() n = len(piles) if n < 3: return 0 current = n - 2 low = 0 total = 0 while current > low: total += piles[current] current -= 2 low += 1 return total
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
from heapq import heappop, heappush, heapify class Solution: def maxCoins(self, piles: List[int]) -> int: piles = sorted(piles) i, j = 0, 0 ans = 0 length = len(piles) while i + j < length - 1: i += 1 j += 2 ans += piles[-j] return ans
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() # piles = deque(piles) me = 0 start, end = 0, len(piles) - 1 # while len(piles) > 2: while start < end: # piles.pop() # me += piles.pop() # if piles: # piles.popleft() if end-1 > 0: me += piles[end-1] start, end = start+1, end-2 return me
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse=True) res = 0 for i in range(1, len(piles) // 3 * 2, 2): res += piles[i] return res
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: print(len(piles) % 3) piles = sorted(piles) print(piles) score = 0 if len(piles) % 3 == 0: tmp = piles[int(len(piles)/3):] nums = [tmp[i] for i in range(0, len(tmp), 2)] print(sum(nums)) return sum(nums) elif len(piles) % 3 == 2: tmp = piles[int(len(piles) / 3):] nums = [tmp[i] for i in range(0, len(tmp), 2)] print(sum(nums)) return sum(nums) else: tmp = piles[int(len(piles) / 3):] nums = [tmp[i] for i in range(1, len(tmp), 2)] print(sum(nums)) return sum(nums)
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() return sum(piles[len(piles) // 3::2])
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() i,j= 0, len(piles)-1 coins = 0 while i < j: coins += piles[j-1] i += 1 j -= 2 return coins
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() length=len(piles)//3 piles=piles[length:];me=0;alice=0 for i in range(len(piles)): if i%2==0: me+=piles[i] else: alice+=piles[i] return me
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: arr = sorted(piles) res = [] n = len(piles)//3 while len(arr) > n: arr.pop(-1) res.append(arr.pop(-1)) return sum(res)
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() first, last = 0, len(piles) - 1 total = 0 while first < last: total += piles[last-1] first += 1 last -= 2 return total
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: sort = sorted(piles) sort = sort[len(sort) // 3:] res = 0 for i in range(0, len(sort), 2): res += sort[i] return res
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse=True) # print(piles) res = 0 tims = len(piles)//3 for i in range(1,len(piles)-tims,2): res += piles[i] return res pass
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort() res = 0 i = len(piles)-2 count = 1 while count<=len(piles)/3: res +=piles[i] i-=2 count+=1 return res
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: piles.sort(reverse=True) print(piles) sumval = 0 for i in range(1,len(piles)*2//3,2): sumval += piles[i] return sumval
There are 3n piles of coins of varying size, you and your friends will take piles of coins as follows: In each step, you will choose any 3 piles of coins (not necessarily consecutive). Of your choice, Alice will pick the pile with the maximum number of coins. You will pick the next pile with maximum number of coins. Your friend Bob will pick the last pile. Repeat until there are no more piles of coins. Given an array of integers piles where piles[i] is the number of coins in the ith pile. Return the maximum number of coins which you can have.   Example 1: Input: piles = [2,4,1,2,7,8] Output: 9 Explanation: Choose the triplet (2, 7, 8), Alice Pick the pile with 8 coins, you the pile with 7 coins and Bob the last one. Choose the triplet (1, 2, 4), Alice Pick the pile with 4 coins, you the pile with 2 coins and Bob the last one. The maximum number of coins which you can have are: 7 + 2 = 9. On the other hand if we choose this arrangement (1, 2, 8), (2, 4, 7) you only get 2 + 4 = 6 coins which is not optimal. Example 2: Input: piles = [2,4,5] Output: 4 Example 3: Input: piles = [9,8,7,6,5,1,2,3,4] Output: 18   Constraints: 3 <= piles.length <= 10^5 piles.length % 3 == 0 1 <= piles[i] <= 10^4
class Solution: def maxCoins(self, piles: List[int]) -> int: rounds = len(piles) // 3 count = 0 piles.sort(reverse=True) for i in range(0, 2*rounds, 2): count += piles[i+1] return count