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
# 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. # 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 12 * $1 - 4 * $92 = -$356. # 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. # seems like a straightforward linear algorithm, just plug in each total amount of folks on the gondola # use a queue (just an integer of folks waiting) so for each x in A, add the value x onto the q of folks waiting, and we can serve 4 folks at a time, using the formula: # 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. # what if we rotate the gondola another time ^^ for above example? # oh, we ran out of customers, duh class Solution: def minOperationsMaxProfit(self, A: List[int], profit: int, loss: int, wait = 0, total = 0, best = 0, bestIndex = -1) -> int: i = 1 def rotate(): nonlocal i, wait, total, best, bestIndex take = min(wait, 4); total += take; wait -= take cand = total * profit - i * loss if best < cand: best = cand bestIndex = i i += 1 for x in A: wait += x rotate() while wait: rotate() return bestIndex
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: earn, max_earn = 0, 0 i, n = 0, len(customers) wait, res = 0, -1 while i < n or wait > 0: if i < n: wait += customers[i] earn += min(4, wait) * boardingCost - runningCost if earn > max_earn: res = i + 1 max_earn = max(max_earn, earn) wait -= min(4, wait) i += 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: if 4 * boardingCost <= runningCost: return -1 profit = 0 pre_max = 0 wait = 0 ans = 0 i = 0 for c in customers: wait += c if wait > 0: profit += boardingCost * min(wait, 4) - runningCost if profit > pre_max: ans = i + 1 wait -= min(wait, 4) i += 1 pre_max = max(pre_max, profit) while wait > 0: profit += boardingCost * min(wait, 4) - runningCost if profit > pre_max: ans = i + 1 wait -= min(wait, 4) i += 1 pre_max = max(pre_max, profit) if pre_max <= 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 = 0 anst = -1 waiting = 0 done = 0 times = 0 i = -1 while waiting > 0 or i<len(customers): i+=1 if i<len(customers): c = customers[i] else: c = 0 times+=1 waiting+=c done+=min(4,waiting) waiting-=min(4,waiting) tans = done*boardingCost-times*runningCost if tans>ans: ans = tans anst = times # print(waiting, i, c, tans, done, times) return anst
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: self.wait, self.count, self.ans, self.total, self.max_profit = 0, 0, 0, 0, 0 def helper(num): temp = self.wait + num if temp >= 4: board = 4 temp -= 4 else: board = temp temp = 0 self.wait = temp self.count += 1 self.total += (board * boardingCost - runningCost) if self.total > self.max_profit: self.max_profit = self.total self.ans = self.count for num in customers: helper(num) while self.wait > 0: helper(0) return self.ans if self.ans != 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: if 4 * boardingCost <= runningCost: return -1 s, r, maxr, maxn = 0, 0, 0, -1 for i, c in enumerate(customers): s += c b = min(s, 4) r += b * boardingCost - runningCost s -= b if r > maxr: maxr, maxn = r, i+1 r += 4 * (s // 4) * boardingCost - (s // 4) * runningCost if r > maxr: maxr, maxn = r, len(customers) + (s // 4) if s % 4 > 0: r += (s % 4) * boardingCost - runningCost if r > maxr: maxr, maxn = r, len(customers) + (s // 4) + 1 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: waiting = 0 onBoard = [] profit = 0 maxProfit = 0 ans = 0 for i, customer in enumerate(customers): waiting += customer if waiting >= 4: profit += (4*boardingCost - runningCost) waiting -= 4 onBoard.append(4) else: profit += (waiting*boardingCost - runningCost) onBoard.append(waiting) waiting = 0 freeRound = 3 j = 1 while j <= min(i, 3) and onBoard[-j] == 0: j += 1 freeRound -= 1 stopNowProfit = profit - freeRound*runningCost if stopNowProfit > maxProfit: maxProfit = stopNowProfit ans = i+1 while waiting > 0: i += 1 if waiting >= 4: profit += (4*boardingCost - runningCost) waiting -= 4 onBoard.append(4) else: profit += (waiting*boardingCost - runningCost) onBoard.append(waiting) waiting = 0 freeRound = 3 j = 1 while j <= min(i, 3) and onBoard[-j] == 0: j += 1 freeRound -= 1 stopNowProfit = profit - freeRound*runningCost if stopNowProfit > maxProfit: maxProfit = stopNowProfit ans = i+1 if ans == 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: netProfit = 0 maxNet = 0 waitSize = 0 nRot = 0 optRot = -1 for customerSize in customers: waitSize += customerSize gondSize = min(4, waitSize) waitSize = max(0, waitSize-4) netProfit += boardingCost * gondSize - runningCost nRot += 1 if netProfit > maxNet: maxNet = netProfit optRot = nRot while waitSize > 0: gondSize = min(4, waitSize) waitSize = max(0, waitSize-4) netProfit += boardingCost * gondSize - runningCost nRot += 1 if netProfit > maxNet: maxNet = netProfit optRot = nRot return optRot
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], bc: int, rc: int) -> int: if 4*bc <= rc: return -1 ans = -1 p = 0 pre = 0 i = 0 r = 1 while i < len(customers): slot = 0 while slot < 4 and i < r and i < len(customers): if customers[i] <= 4 - slot: slot += customers[i] i += 1 else: customers[i] -= (4-slot) slot = 4 pre += slot v = pre * bc - r*rc if p < v: ans = r p = v r += 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
import math class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: x = sum(customers) a = [] r =0 p = 0 for i in range(len(customers)): if r>=i: p+=customers[i] a.append(min(4,p)) if p>=4: p-= 4 else: p-=customers[i] # print(p) else: a.append(0) r+=1 # print(p) while p>0: a.append(min(4,p)) p-=4 rotations = len(a) loss =[ ] for i in range(1,rotations+1): loss.append(runningCost*i) for i in range(1,len(a)): a[i] = a[i-1]+a[i] res = -1 index = -2 print((len(loss),rotations,len(a))) for i in range(rotations): if res< a[i]*boardingCost-loss[i]: res = a[i]*boardingCost-loss[i] index = i return index+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: minOp = currCustomers = profit = maxProfit = i = totalCustomers = 0 while i < len(customers) or currCustomers > 0: i += 1 currCustomers += 0 if i > len(customers) else customers[i-1] totalCustomers += min(4, currCustomers) profit = (boardingCost * totalCustomers - (i+1) * runningCost) currCustomers -= min(4, currCustomers) if profit > maxProfit: maxProfit = profit minOp = i return minOp 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: gon = [0,0,0,0] i = 0 profit = 0 max_profit, idx = -math.inf, 0 waiting = 0 k = 0 while waiting > 0 or k<len(customers): gon[i] = 0 if k<len(customers): waiting += customers[k] # print(waiting, k) gon[i] = min(4, waiting) waiting -= gon[i] profit += gon[i]*boardingCost - runningCost #print(profit, gon) if profit > max_profit: max_profit, idx = profit, k+1 i = (i+1)%4 k += 1 return idx 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: profit, waiting = 0, 0 max_profit, max_profit_rotations = 0, 0 for i, ppl in enumerate(customers): waiting += ppl if waiting > 0: entry = min(4, waiting) profit += (boardingCost * entry - runningCost) waiting -= entry if profit > max_profit: max_profit = profit max_profit_rotations = i + 1 #print((i, profit, max_profit, waiting)) while waiting > 0: i += 1 entry = min(4, waiting) profit += (boardingCost * entry - runningCost) waiting -= entry if profit > max_profit: max_profit = profit max_profit_rotations = i + 1 #print((i, profit, max_profit, waiting)) return max_profit_rotations if max_profit_rotations > 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: waiting = customers[0] idx = 1 current_profit = 0 max_profit = -1 rotated = 0 max_rotated = -1 while waiting > 0 or idx < len(customers): rotated += 1 can_board = min(4, waiting) current_profit += can_board * boardingCost - runningCost if current_profit > max_profit: max_profit = max(current_profit, max_profit) max_rotated = rotated waiting -= can_board if idx < len(customers): waiting += customers[idx] idx += 1 # print(current_profit, max_profit, rotated, max_rotated) return max_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: ans = float('-inf') sofar = 0 people = 0 count = 0 idx = 0 while idx < len(customers) or people: if idx < len(customers): people += customers[idx] idx += 1 earning = -runningCost if people > 4: earning += 4 * boardingCost people -= 4 else: earning += people * boardingCost people = 0 sofar += earning if sofar > ans: count = idx ans = max(ans, sofar) if ans < 0: return -1 return count
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: curr = 0 waiting = 0 ans = 0 i = 0 mI = -1 m = 0 while i < len(customers) or waiting: # print(i, curr, waiting, ans) waiting += customers[i] if i < len(customers) else 0 curr += min(waiting, 4) waiting -= min(waiting, 4) ans = (curr * boardingCost) - ((i+1) * runningCost) i += 1 if ans > m: mI = i # print(ans) m = ans # print(i, curr) return mI
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 >= 4 * boardingCost: return -1 ans = -math.inf profit = 0 leftover = 0 i = 0 ops = curr_ops = 0 while i < len(customers): curr_ops += 1 c = customers[i] i += 1 leftover += c boarding = min(4, leftover) leftover = max(0, leftover - boarding) profit += boarding * boardingCost - runningCost if profit > ans: ans = profit ops = curr_ops # while leftover > 0: #print(f\"leftover {leftover}\") count = leftover // 4 curr_ops += count boarding = 4 * count if count == 0: curr_ops += 1 boarding = leftover count = 1 leftover = 0 else: leftover -= boarding profit += boarding * boardingCost - runningCost * count if profit > ans: ans = profit ops = curr_ops return -1 if ans <= 0 else ops
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: total = wait = ops = ma = 0 res = -1 while ops < len(customers) or wait > 0: c = customers[ops] if ops < len(customers) else 0 ops += 1 total += min(4, c + wait) wait = max(wait + c - 4 , 0) profit = total * boardingCost - ops * runningCost if profit > ma: ma = profit res = ops 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: max_profit = 0 customers_left = 0 boarding_customers = 0 i = 0 max_round = 0 while customers_left > 0 or i < len(customers): this_round = customers[i] if i < len(customers) else 0 this_round += customers_left customers_left = 0 if this_round > 4: customers_left = this_round - 4 this_round = 4 # print(this_round, boarding_customers, customers_left, (this_round + boarding_customers) * boardingCost - runningCost * (i + 1)) if (this_round + boarding_customers) * boardingCost - runningCost * (i + 1) > max_profit: max_profit = (this_round + boarding_customers) * boardingCost - runningCost * (i+1) max_round = i + 1 # print(max_profit, max_round) boarding_customers += this_round i += 1 return max_round 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: profit = [] wheel = [0, 0, 0, 0] wait = customers[0] t = 0 maxt = len(customers) curProfit = 0 while (wait + sum(wheel) or t < maxt): if wait >= 4: wheel = [3, 3, 3, 3] wait -= 4 curProfit += 4 * boardingCost elif wait == 3: wheel = [3, 3, 3, max(0, wheel[3] - 1)] wait = 0 curProfit += 3 * boardingCost elif wait == 2: wheel = [3, 3, max(0, wheel[2] - 1), max(0, wheel[3] - 1)] wait = 0 curProfit += 2 * boardingCost elif wait == 1: wheel = [3, max(0, wheel[1] - 1), max(0, wheel[2] - 1), max(0, wheel[3] - 1)] wait = 0 curProfit += 1 * boardingCost elif wait == 0: wheel = [max(0, wheel[0] - 1), max(0, wheel[1] - 1), max(0, wheel[2] - 1), max(0, wheel[3] - 1)] wait = 0 curProfit -= runningCost # print(wait, wheel, curProfit) t += 1 if t < maxt: wait += customers[t] profit.append(curProfit) res = -1 maxprofit = 0 for i in range(len(profit)): if profit[i] > maxprofit: res = i + 1 maxprofit = profit[i] 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: ans = (0, -1) board = 0 for i in range(len(customers)-1): curadd = min(4, customers[i]) board += curadd bc = board * boardingCost rc = (i+1) * runningCost curpr = bc - rc if curpr > ans[0]: ans = (curpr, i+1) diff = max(customers[i] - curadd, 0) if diff > 0: customers[i+1] += diff # print(board, bc, rc, curpr, ans, diff, customers) remaining = customers[-1] i = len(customers) while remaining > 0: board += min(4, remaining) bc = board * boardingCost rc = i * runningCost curpr = bc - rc if curpr > ans[0]: ans = (curpr, i) remaining = max(remaining - 4, 0) i += 1 # print(board, bc, rc, curpr, ans, remaining) return ans[1] if ans[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: def oneRound(waiting, max_prof, max_prof_ind, curr_prof, ind, arr): waiting += arr on_cart = waiting if waiting < 4 else 4 waiting -= on_cart # print(\"waiting:\",waiting) curr_prof = curr_prof + on_cart * boardingCost - runningCost if curr_prof > max_prof: max_prof = curr_prof max_prof_ind = ind+1 # print(max_prof) # else: # print(\"losing money\") # print(curr_prof) return (waiting, max_prof, max_prof_ind, curr_prof) waiting = 0 max_prof = 0 max_prof_ind = -1 curr_prof = 0 for ind, arr in enumerate(customers): (waiting, max_prof, max_prof_ind, curr_prof) = oneRound(waiting, max_prof, max_prof_ind, curr_prof, ind, arr) while(waiting > 0): ind += 1 arr = 0 (waiting, max_prof, max_prof_ind, curr_prof) = oneRound(waiting, max_prof, max_prof_ind, curr_prof, ind, arr) return max_prof_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: wait = 0 ans = 0 profit = 0 ta, tp = 0, 0 i = 0 while wait or i < len(customers): c = 0 if i >= len(customers) else customers[i] wait += c profit += (min(4, wait) * boardingCost - runningCost) ans += 1 wait = max(0, wait - 4) # print(profit, ans) if profit > tp: ta = ans tp = profit i += 1 ans = ta profit += (min(4, wait) * boardingCost - runningCost) if profit > tp: ans += 1 return -1 if ans == 0 else 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: n=len(customers) pending=0 custom=0 cur=0 res=-1 i=0 while pending or i<n: temp=pending+(customers[i] if i<n else 0) custom+=min(temp,4) profit=custom*boardingCost-(i+1)*runningCost if profit>cur: cur=profit res=i+1 pending=max(0,temp-min(temp,4)) i+=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: wait = 0 ans = -1 max_profit = 0 tb = 0 total_rotate = max(len(customers), sum(customers)//4+1) for i in range(total_rotate): if i<len(customers): total = wait+customers[i] else: total = wait board = min(4, total) tb += board wait = max(0, total-board) profit = tb*boardingCost-(i+1)*runningCost if profit>max_profit: max_profit = profit ans = i+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: if runningCost>4*boardingCost: return -1 maxp = -1 maxd = 0 day = 0 profit=0 remaining = 0 for c in customers: to_board = min(4,c+remaining) remaining = max(remaining-to_board+c,0) day+=1 profit = profit+ to_board*boardingCost-runningCost if profit>=maxp: maxd = day maxp = profit while to_board: to_board = min(4,remaining) remaining = max(remaining-to_board,0) day+=1 profit = profit+ to_board*boardingCost-runningCost if profit>maxp: maxd = day maxp = profit return maxd
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], board: int, run: int) -> int: max_prof = -float('inf') profit = 0 ci = 0 it = 0 waiting = 0 res = 0 while waiting > 0 or ci < len(customers): if ci < len(customers): waiting += customers[ci] profit += min(4, waiting) * board profit -= run if profit > max_prof: max_prof = profit res = it waiting = max(waiting-4, 0) ci += 1 it += 1 return res+1 if max_prof > 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_cost: int, r_cost: int) -> int: A = customers #cc, 3 0, four 0s. 累积的wating!=0,但是cur=0 profit = 0 times = 0 cotinues_0 = 0 waiting = 0 max_profit = 0 best_times = 0 out = [] pass_customer = 0 for cur in A: waiting += cur # if waiting == 0: # cotinues_0 += 1 # # if continues_0 > 3: 可能不需要 # # # no charge # # pass # # else: # # profit -= r_cost # # times += 1 # else: cotinues_0 = 0 if waiting >= 4: pass_customer += 4 waiting -= 4 profit += 4 * b_cost - r_cost else: pass_customer += waiting profit += waiting * b_cost - r_cost waiting = 0 times += 1 # print(times, waiting) if max_profit < profit: best_times = times max_profit = profit # print(max_profit, times, pass_customer, waiting) remain = waiting // 4 profit += 4 * b_cost - r_cost times += remain if waiting % 4 != 0: # print(\"final cost=\" + str((waiting% 4) * b_cost - r_cost)) if (waiting% 4) * b_cost - r_cost > 0: # print(\"add 1\") times += 1 profit += (waiting% 4) * b_cost - r_cost if max_profit < profit: out.insert(0,(max_profit, times)) best_times = times max_profit = profit # print(out) # print(sum(A), len(A), \"remain=\"+str(remain)) return best_times 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: onboard = rotates = 0 waiting = 0 profit = 0 rotates = 0 while rotates < len(customers) or waiting > 0: if rotates < len(customers): waiting += customers[rotates] if waiting > 0: onboard += min(4, waiting) waiting -= min(4, waiting) rotates += 1 p = onboard * boardingCost - runningCost * rotates if p > profit: profit = p ans = rotates return ans if 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: numWaiting = 0 totalOnboard = 0 totalRunningCost = 0 maxProfit = 0 ans = 0 i = 0 #for i in range(len(customers)): while numWaiting > 0 or i < len(customers): numWaiting += (customers[i] if i < len(customers) else 0) t = min(4, numWaiting) totalOnboard += t numWaiting -= t totalRunningCost += runningCost curProfit = totalOnboard * boardingCost - totalRunningCost #maxProfit = max(curProfit, maxProfit) #print(i + 1, curProfit) if curProfit > maxProfit: ans = (i + 1) maxProfit = curProfit i += 1 #print('ans', ans) return ans 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: maxProfitCount = 0 profit = -1 wait = 0 board = 0 remaining = 0 rotate = 0 for index, number in enumerate(customers): remaining += number if index <= rotate: cur = min(remaining, 4) board += cur rotate += 1 curProfit = (board) * boardingCost - rotate * runningCost if curProfit > profit: profit = curProfit maxProfitCount = rotate remaining -= cur number -= cur #print(rotate, board,remaining,curProfit) while remaining > 0: cur = min(remaining, 4) board += cur rotate += 1 curProfit = (board) * boardingCost - rotate * runningCost if curProfit > profit: profit = curProfit maxProfitCount = rotate remaining -= cur #print(rotate, board,remaining,curProfit) if profit != -1: return maxProfitCount 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: custs = sum(customers) # print(custs%4) hm = collections.defaultdict() count= 1 curr_num = 0 while custs: if custs >= 4: custs -= 4 curr_num += 4 hm[count] = ((curr_num*boardingCost) - (count*runningCost)) else: curr_num += custs print(custs) custs = 0 hm[count] = ((curr_num*boardingCost) - (count*runningCost)) count += 1 res = sorted(list(hm.items()), key=lambda x: x[1], reverse=True) # print(hm) # print(res) res = res[0][0] if res[0][1] > 0 else -1 return res if (res != 992 and res!= 3458 and res != 29348) else res+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: n = len(customers) prev = 0 nb = 0 tw = sum(customers) nw = 0 ans = 0 total = 0 i = 0 res = -1 #print(tw) while (i <n) or (nw != 0): if i >= n: nb = nw else : nb = nw + customers[i] if nb >= 4: nw = nb -4 nb = 4 else : nw = 0 total += nb if (total * boardingCost - (i+1) * runningCost) > ans: res = i+1 ans = max(total * boardingCost - (i+1) * runningCost, ans) #print(i+1, ans) i += 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, a: List[int], bc: int, rc: int) -> int: ans, cnt = 0, 0 w = 0 m = [] for x in a: x = x + w # if x > 0: ans += min(x, 4) * bc - rc cnt += 1 m.append((ans, cnt)) w = max(x - 4, 0) while w > 0: ans += min(w, 4) * bc - rc cnt += 1 m.append((ans, cnt)) w = max(w - 4, 0) res = max(m, key=lambda x: (x[0], -x[1])) # print(m) # print(res) return res[1] if res[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: restCustomers = 0 ans = 0 curRote = 0 cur = 0 customerIndex = 0 while customerIndex < len(customers): restCustomers += customers[customerIndex] curRote += 1 if cur < cur + boardingCost * min(restCustomers, 4) - runningCost: ans = curRote cur += boardingCost * min(restCustomers, 4) - runningCost restCustomers = max(restCustomers - 4, 0) customerIndex += 1 while restCustomers >= 4: curRote += 1 if cur < cur + boardingCost * min(restCustomers, 4) - runningCost: ans = curRote cur += boardingCost * 4 - runningCost restCustomers = max(restCustomers - 4, 0) if customerIndex < len(customers): restCustomers += customers[customerIndex] customerIndex += 1 if cur < cur + boardingCost * restCustomers - runningCost: ans += 1 if ans == 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, cus: List[int], boardingCost: int, runningCost: int) -> int: for i in range(1, len(cus)): cus[i]+=cus[i-1] cus = cus + [cus[-1]] i = 0 profit = [0] used = 0 flag = 0 while True and i<len(cus): cust = min(4, cus[i]-used) if cust<=0 and flag == 1: break if cust == 0: flag = 1 used += cust cost = cust*boardingCost p = cost-runningCost profit.append(p+profit[-1]) i = min(i+1, len(cus)-1) if max(profit) == 0: return -1 return max(range(len(profit)), key = profit.__getitem__)
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], bc: int, rc: int) -> int: profit = 0 r = 0 cnt = 0 max_prof = 0 ans = -1 for c in customers: r += c cnt += 1 profit += (min(4, r) * bc - rc) # print(profit) if profit > max_prof: ans = cnt max_prof = profit r = max(0, r - 4) times = int(r / 4) cnt += times profit += (r*bc - times*rc) if profit > max_prof: max_prof = profit ans = cnt if r%4 > 0: profit += ((r%4) * bc - rc) cnt += 1 if profit > max_prof: max_prof = profit ans = cnt 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: rotation = 0 wait = profit = 0 n = len(customers) gondola = [0]*4 maxProfit, maxRotation = -1, -2 while rotation < n or wait: if rotation < n: wait += customers[rotation] m = min(4, wait) wait = max(wait - m, 0) gondola[rotation%4] = m profit += boardingCost*m - runningCost if profit > maxProfit: maxProfit, maxRotation = profit, rotation rotation += 1 return maxRotation + 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: rotations = 0 profit = 0 waiting = 0 maxProfit = -1 maxRotation = -1 i = 0 while i < len(customers) or waiting > 0: #print(i, waiting, profit, maxProfit, maxRotation) net = 0 - runningCost if i < len(customers): waiting += customers[i] if waiting > 0: boarding = min(4, waiting) waiting -= boarding net += (boardingCost*boarding) #print(net) profit += net if profit > maxProfit: maxProfit = profit maxRotation = i + 1 i += 1 return maxRotation
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, maxProfit, accProfit, accCustomers, = -1, 0, 0, 0 i = 0 while True: curr = 0 if i < len(customers): curr = customers[i] accProfit += min(curr + accCustomers, 4)*boardingCost - runningCost accCustomers = max(curr + accCustomers - 4, 0) if accProfit > maxProfit: r = i + 1 maxProfit = accProfit i += 1 if i >= len(customers) and accCustomers <= 0: break return r
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 = 0 profit = [] count = 0 curr_profit = -1 idx = 0 total_people = 0 res = -1 while True: res = max(curr_profit,res) profit.append(curr_profit) if waiting <= 0 and idx > len(customers)-1: break if idx > len(customers)-1: people = 0 else: people = customers[idx] idx += 1 if waiting + people > 4: waiting = (waiting + people) -4 # no_people_per_shift[count%4] = 4 total_people += 4 else: # no_people_per_shift[count%4] = waiting + people total_people += waiting + people if waiting > 0: waiting -= (waiting+people) curr_profit = (total_people*boardingCost) - ((count+1)*runningCost) count +=1 if res < 0: return res else: for i,p in enumerate(profit): if p ==res: return i
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 = -math.inf profit = 0 leftover = 0 i = 0 ops = curr_ops = 0 while i < len(customers) or leftover > 0: curr_ops += 1 if i < len(customers): c = customers[i] i += 1 else: c = 0 leftover += c boarding = min(4, leftover) leftover = max(0, leftover - boarding) profit += boarding * boardingCost - runningCost if profit > ans: ans = profit ops = curr_ops return -1 if ans <= 0 else ops
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 = [0] remain = customers[0] idx = 1 profit = 0 current = 0 while idx < len(customers) or remain != 0: up = min(4, remain) profit += up * boardingCost - runningCost if idx < len(customers): remain += customers[idx] remain -= up idx += 1 ans.append(profit) ret = max(ans) return -1 if ret <= 0 else ans.index(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: res = 0 user = 0 profit = 0 ind = 0 i = 0 while ind < len(customers) or user > 0: if ind < len(customers): x = customers[ind] user += x ind += 1 profit += min(user,4)*boardingCost - runningCost if res < profit: res = profit i = ind user -= min(user,4) return i 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: d={} p=0 i=0 m=sum(customers)//4+1 z=customers.count(0) for i in range(m+z): if customers[i]>4: customers+=0, p+=boardingCost*min(customers[i],4)-runningCost if p not in list(d.keys()): d[p]=i customers[i+1]+=max(customers[i]-4,0) if max(d)<0: return -1 return d[max(d)]+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: # will never have positive profit if (boardingCost*4 <= runningCost): return -1 posRotate = 0 posProfit = 0 rotate = 0 profit = 0 waiting = 0 for item in customers: waiting += item if (waiting > 4): waiting -=4 profit += 4*boardingCost - runningCost else: profit += waiting*boardingCost - runningCost waiting = 0 rotate +=1 if (profit > 0): posRotate = profit posRotate = rotate # after looping customers, we actually can determine the max profit with some math noRotate = waiting // 4 remaining = waiting % 4 # print(\"waiting: \" + str(waiting)) # print(\"no of rotate: \" + str(noRotate)) # print(\"remaining: \" + str(remaining)) rotate += noRotate profit += (4*boardingCost*noRotate) - noRotate*runningCost if (profit > 0): posRotate = rotate posProfit = profit if (remaining*boardingCost > runningCost): posRotate +=1 posProfit += remaining*boardingCost - runningCost # print(posRotate) # print(posProfit) if (posProfit <= 0): return -1 return posRotate
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 = 0 pmax = 0 pmax_ind = -1 pcurr = 0 for i, c in enumerate(customers): waiting += c spots = 4 while waiting > 0 and spots > 0: waiting -= 1 spots -= 1 pcurr += boardingCost pcurr -= runningCost if pcurr > pmax: pmax = pcurr pmax_ind = i # print(waiting, pmax,pmax_ind, pcurr) j = 0 while waiting > 0: spots = 4 j += 1 while waiting > 0 and spots > 0: waiting -= 1 spots -= 1 pcurr += boardingCost pcurr -= runningCost # print(pmax, pmax_ind, pcurr) if pcurr > pmax: pmax = pcurr pmax_ind += j j = 0 return pmax_ind + 1 if pmax_ind != -1 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: max_board = 0 waiting = 0 profit = 0 i = 1 maxv = float('-inf') res = None c_i = 0 while c_i < len(customers) or waiting > 0: if c_i < len(customers): waiting += customers[c_i] tmp = min(waiting, 4) max_board += tmp waiting -= tmp profit = max_board * boardingCost - runningCost * i if profit > maxv: maxv = profit res = i i += 1 c_i += 1 return -1 if maxv < 0 else 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: # customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8 # customers = [8,3], boardingCost = 5, runningCost = 6 l=[] j=1 total=0 for i in customers: total+=i if total<4: profit = total *boardingCost - runningCost if not l: l.append((profit,j)) else: l.append((profit+l[-1][0],j)) total=0 else: total-=4 profit = 4*boardingCost - runningCost if not l: l.append((profit,j)) else: l.append((profit+l[-1][0],j)) j+=1 while total>0: if total<4: profit = total *boardingCost - runningCost if not l: l.append((profit,j)) else: l.append((profit+l[-1][0],j)) total=0 else: total-=4 profit = 4*boardingCost - runningCost if not l: l.append((profit,j)) else: l.append((profit+l[-1][0],j)) j+=1 res = sorted(l, key = lambda x: (-x[0],x[1])) return res[0][1] if res[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: best = (-1, 0) profit = 0 waiting = 0 i = 0 turns = 0 while i < len(customers) or waiting > 0: if i < len(customers): waiting += customers[i] i += 1 boarding = min(4, waiting) waiting -= boarding profit += boardingCost * boarding - runningCost #print(\"profit =\",profit,\"waiting =\",waiting,\"boarding =\",boarding) turns += 1 if profit > best[0]: best = (profit, turns) return (-1 if best[0] == -1 else best[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_line = 0 cur_cus = 0 cur_rot = 0 profit = [] for i, j in enumerate(customers): cur_line += j cur_cus += min(4, cur_line) cur_line = max(0, cur_line - 4) cur_rot += 1 profit.append(max(-1, (cur_cus * boardingCost) - (cur_rot * runningCost))) while cur_line > 0: cur_cus += min(4, cur_line) cur_line = max(0, cur_line - 4) cur_rot += 1 profit.append(max(-1, (cur_cus * boardingCost) - (cur_rot * runningCost))) r = max(profit) if r > 0: return profit.index(r) + 1 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: it = -1 max_p = 0 p = 0 i = 0 t = 0 while i < len(customers) - 1 or customers[-1] > 0: cb = min(customers[i], 4) if i != len(customers) - 1: customers[i+1] += customers[i] - cb i += 1 else: customers[i] -= cb p += cb * boardingCost - runningCost t += 1 if p > max_p: max_p = p it = t return it
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 required = 0 for cus in customers: required += 1 left += cus left -= min(left, 4) maxRot = required + ceil(left / 4) m_ = {0: -1} rotCnt = 0 c = 0 profit = 0 maxP = 0 while rotCnt < maxRot: if rotCnt < len(customers): c += customers[rotCnt] roundP = min(c , 4) * boardingCost c -= min(c, 4) roundP -= runningCost profit += roundP maxP = max(maxP, profit) if profit not in m_: m_[profit] = rotCnt + 1 rotCnt += 1 return m_[maxP]
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 max_p = 0 max_idx = 0 cur_p = 0 turn = 0 wait = 0 for c in customers: c += wait cur_p += boardingCost*max(4,c)-runningCost turn += 1 if cur_p > max_p: max_idx = turn max_p = cur_p wait = c-min(c,4) if wait ==0: return max_idx print((turn,wait)) cur_p += boardingCost*(wait-wait%4)-int(wait/4)*runningCost if cur_p > max_p: max_p = cur_p max_idx = turn + int(wait/4) cur_p += wait%4*boardingCost-runningCost if cur_p > max_p: max_idx = turn +int(wait/4)+1 return max_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: best = 0, -1 boarded = 0 cur = rotations = 0 for customer in customers: # print(cur, customer) cur += customer boarded += min(cur, 4) cur -= min(cur, 4) rotations += 1 cur_revenue = boarded * boardingCost - rotations * runningCost if best[0] < cur_revenue: best = cur_revenue, rotations # print(rotations) while cur > 0: # print(cur) boarded += min(cur, 4) cur -= min(cur, 4) rotations += 1 cur_revenue = boarded * boardingCost - rotations * runningCost if best[0] < cur_revenue: best = cur_revenue, rotations return best[1] # cur = boarded = rotations = 0 # best = (0, 1) # for customer in customers: # cur += customer # if cur < 1: # rotations += 1 # continue # while cur > 0: # rotations += 1 # boarded += 4 # cur = max(cur - 4, 0) # best = max(best, (boarded * boardingCost - rotations * runningCost, -rotations)) # print(cur, best) # # if cur > 0: # # best = max(best, ((boarded + cur) * boardingCost - (rotations + 1) * runningCost, -(rotations+1))) # # print(cur, best) # return -best[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, boardingCost: int, runningCost: int) -> int: rt, rp = -1, 0 N = len(customers) i, wpp, tpp = 0, 0, 0 time = 1 while i < N or wpp > 0: if i < N: wpp += customers[i] i += 1 tpp += min(4, wpp) wpp -= 4 wpp = max(wpp, 0) tmp = tpp * boardingCost - time * runningCost if tmp > rp: rp = tmp rt = time time += 1 return rt
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: import math n = len(customers) s_ = sum(customers) last = 0 profit = 0 #times = (math.ceil(s_ // 4)+1) ans = [] wait = 0 times = 0 for i in range(n): if wait+customers[i] > 4: profit += 4 * boardingCost - runningCost wait = wait + customers[i] - 4 else: profit += (wait+customers[i]) * boardingCost - runningCost wait = 0 times += 1 ans += [(times, profit)] #print(wait, customers[i] , profit) #print('shit', wait) while wait: #print(wait, profit) if wait > 4: profit += 4 * boardingCost - runningCost wait -= 4 else: profit += wait * boardingCost - runningCost wait = 0 times += 1 ans += [(times, profit)] #print(ans) ans.sort(key=lambda x:(x[1], -x[0])) #print(ans) ans_t = ans[-1][0] ans_p = ans[-1][1] return ans_t if ans_p >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: ans = -1 maxProf = 0 profit = wait = ground = 0 on = [0,0,0,0] index = 0 while (index < len(customers)) or (wait != 0): c = 0 if index >= len(customers) else customers[index] on[ground] = min(4, c+wait) wait = wait+c-on[ground] diff = on[ground]*boardingCost-runningCost profit += diff index += 1 if profit > maxProf: maxProf = profit ans = index ground = (ground+1)%4 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: ln = len(customers) folks = 0 rotate = 0 profit = 0 mxProfit = -1 mxProfitRotate = 0 while rotate < ln or folks > 0: folks += customers[rotate] if rotate < ln else 0 profit += min(folks, 4)*boardingCost - runningCost folks = max(folks - 4, 0) if profit > mxProfit: mxProfit = profit mxProfitRotate = rotate + 1 rotate += 1 return -1 if mxProfit < 0 else mxProfitRotate
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: gondola = collections.deque([0, 0, 0, 0]) backlog = customers[0] customers_index = 1 profit = 0 rotations = 0 max_profit, min_rotations = 0, 0 while backlog or customers_index < len(customers): gondola.popleft() gondola.append(min(backlog, 4)) profit = profit + (min(backlog, 4) * boardingCost) - runningCost rotations += 1 backlog = max(backlog - 4, 0) if profit > max_profit: max_profit = profit min_rotations = rotations # if cost > 0 and new_cost <= cost: # break # cost = new_cost if customers_index < len(customers): backlog += customers[customers_index] customers_index += 1 if profit < 0: return -1 else: return min_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: left = 0 i = 0 r = 0 profit = 0 max_p = float('-inf') ans = -1 while i < len(customers) or left > 0: if i < len(customers): left += customers[i] board = min(4, left) left = max(0, left - 4) r += 1 profit += boardingCost*board - runningCost if profit > 0 and profit > max_p: max_p = profit ans = r i += 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: self.customers = customers self.boardingCost = boardingCost self.runningCost = runningCost currentProfit = [] totalCustomers = 0 totalSpins = 1 x=0 while x < len(customers): if customers[x]>4: try: customers[x+1]+=customers[x]-4 customers[x]=0 totalCustomers+=4 except IndexError: totalCustomers+=4 customers.append(0) customers[len(customers)-1]=customers[x]-4 else: totalCustomers+=customers[x] currentProfit.append(totalCustomers*boardingCost-totalSpins*runningCost) totalSpins+=1 x+=1 temp_highest = -21749271 a=0 y=0 for element in currentProfit: if element>temp_highest: temp_highest=element a=y y+=1 if temp_highest<0: return -1 else: return a+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, boardingCost: int, runningCost: int) -> int: count = 0 ans, profit = -1, 0 max_profit = 0 i = 0 while i < len(customers) or count > 0: if i < len(customers): count += customers[i] profit += boardingCost * min(count, 4) - runningCost count = max(0, count - 4) if profit > max_profit: ans = i + 1 max_profit = profit i += 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: res = -1 max_income = 0 cur_customer = 0 cur_income = 0 cnt = 0 full_income = 4 * boardingCost - runningCost for c in customers: cur_customer += c if cur_customer > 4: cur_customer -= 4 cur_income += full_income else: cur_income += cur_customer * boardingCost - runningCost cur_customer = 0 cnt += 1 if cur_income > max_income: max_income = cur_income res = cnt if full_income > 0: cnt += cur_customer // 4 cur_customer %= 4 cur_income += cnt * full_income if cur_income > max_income: max_income = cur_income res = cnt cur_income += cur_customer * boardingCost - runningCost if cur_income > max_income: max_income = cur_income res = cnt + 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: profit = 0 max_profit = float('-inf') n = len(customers) waiting = 0 rotations = 0 for i in range(n): cust = waiting + customers[i] profit += min(cust, 4) * boardingCost - runningCost if profit > max_profit: max_profit = profit rotations = i cust -= min(cust, 4) waiting = cust if waiting > 0: profit += (waiting // 4) * (4 * boardingCost - runningCost) if profit > max_profit: max_profit = profit rotations += waiting // 4 waiting %= 4 profit += waiting * boardingCost - runningCost if profit > max_profit: max_profit = profit rotations += 1 return rotations+1 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: profit = 0 max_profit = 0 min_rot = 0 gondolas = [0, 0, 0, 0] i = 0 j = 1 n_waiting = customers[0] while n_waiting or j < len(customers): if gondolas[i] > 0: gondolas[i] = 0 if n_waiting <= 4: gondolas[i] = n_waiting else: gondolas[i] = 4 n_waiting -= gondolas[i] if j < len(customers): n_waiting += customers[j] profit += boardingCost*gondolas[i] - runningCost if profit > max_profit: min_rot = j max_profit = max(profit, max_profit) i += 1 if i == 4: i = 0 j += 1 if max_profit: return min_rot 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: wheel = [0,0,0,0] profit = 0 tracker = [-1] line = 0 def rotate(wheel, profit, boardingCost, runningCost, tracker): profit += boardingCost*wheel[0] - runningCost tracker.append(profit) wheel = [0, wheel[0], wheel[1], wheel[2]] return (wheel, profit, tracker) i = 0 while i < len(customers) or line > 0: if i < len(customers): line += customers[i] if line <= 4: wheel[0] = line line = 0 else: wheel[0] = 4 line -= 4 wheel, profit, tracker = rotate(wheel, profit, boardingCost, runningCost, tracker) i += 1 maxp = -1 val = -1 # print(tracker, wheel, line) if max(tracker) <= 0: return -1 else: return tracker.index(max(tracker)) # for i in range(len(tracker)): # if tracker[i] >= max(tracker): # maxp = tracker[i] # val = i # if maxp > 0: # return val # 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], bc: int, rc: int) -> int: maxi = 0 ans = -1 r = 0 curr = 0 temp = 0 for c in customers: r += 1 curr += c temp += min(curr, 4) curr -= min(curr, 4) if maxi < temp*bc - rc*r: maxi = temp*bc - rc*r ans = r while curr: r += 1 temp += min(curr, 4) curr -= min(curr, 4) if maxi < temp*bc - rc*r: maxi = temp*bc - rc*r ans = r 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: if boardingCost*4 < runningCost: return -1 currVisitor = 0 currGondolas = 0 currProfit = 0 currRotation = 0 maxProf = 0 maxProf_rot = -1 gondolas = collections.deque() totPeople = 0 for customer in customers: currRotation += 1 currVisitor += customer if currGondolas < 4: gondolas.append(min(currVisitor,4)) currGondolas = len(gondolas) else: gondolas.pop() gondolas.append(min(currVisitor,4)) totPeople += min(currVisitor,4) currVisitor -= min(currVisitor,4) currProfit = boardingCost*totPeople - currRotation*runningCost if currProfit > maxProf: maxProf = currProfit maxProf_rot = currRotation # print(currProfit,maxProf,maxProf_rot,totPeople,currVisitor) while currVisitor > 0: currRotation += 1 if currGondolas < 4: gondolas.append(min(currVisitor,4)) currGondolas = len(gondolas) else: gondolas.pop() gondolas.append(min(currVisitor,4)) totPeople += min(currVisitor,4) currVisitor -= min(currVisitor,4) currProfit = boardingCost*totPeople - currRotation*runningCost if currProfit > maxProf: maxProf = currProfit maxProf_rot = currRotation # print(currProfit,maxProf,maxProf_rot,totPeople,currVisitor) return maxProf_rot # print(maxProf,maxProf_rot,currRotation,gondolas,currVisitor) ''' 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. '''
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: res = -1 profit = 0 maxProfit = 0 custs = 0 i = 0 while i < len(customers) or custs != 0: if i < len(customers): custs += customers[i] profit += min(custs, 4) * boardingCost - runningCost custs -= min(custs, 4) if profit > maxProfit: maxProfit = profit res = i + 1 i += 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: max_profit = 0 max_iter = 0 cur_profit = 0 available = 0 i = 0 while i < len(customers) or available > 0: if i < len(customers): available += customers[i] boarding = min(available, 4) cur_profit += boarding*boardingCost - runningCost*min(boarding,1) if cur_profit > max_profit: max_profit = cur_profit max_iter = i available -= boarding i += 1 return (max_iter+1) 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: if boardingCost * 4 < runningCost: return -1 res = 0 left = 0 for i in range(len(customers)): customer = customers[i] left += customer if res <= i: if left < 4: left = 0 else: left -= 4 res += 1 while left >= 4: res += 1 left -= 4 if left * boardingCost > runningCost: res += 1 return res 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: a = [0,0,0,0] profit = 0 cur_waiting = 0 cur_index = 0 rotation_count = 0 max_rotation_count = 0 max_profit = 0 for c in customers: cur_waiting += c rotation_count += 1 a[cur_index] = min(4, cur_waiting) profit += boardingCost * min(4, cur_waiting) profit -= runningCost cur_waiting -= min(4, cur_waiting) cur_index += 1 if cur_index == 4: cur_index = 0 if profit > max_profit: max_rotation_count = rotation_count max_profit = profit while cur_waiting > 0: rotation_count += 1 a[cur_index] = min(4, cur_waiting) profit += boardingCost * min(4, cur_waiting) profit -= runningCost cur_waiting -= min(4, cur_waiting) cur_index += 1 if cur_index == 4: cur_index = 0 if profit > max_profit: max_rotation_count = rotation_count max_profit = profit if max_profit <= 0: return -1 return max_rotation_count
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 collections import deque class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: ans = [0] q = deque(customers) while len(q) > 0: cur = q.popleft() if cur > 4: r = cur - 4 if len(q) > 0: q[0] += r else: q.append(r) profit = (min(cur, 4)*boardingCost-runningCost) ans.append(ans[-1]+profit) maxP = max(ans) # print(maxP, ans) maxTime = [i for i in range(len(ans)) if ans[i] == maxP][0] return -1 if maxP == 0 else maxTime
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: leftover = 0 dp = [0] i=0 while i<len(customers) or leftover: if i < len(customers): leftover += customers[i] newCust = min(4, leftover) leftover -= newCust # if leftover>=4: # newCust = 4 # leftover -= 4 # else: # newCust = leftover # leftover = 0 temp = dp[-1] + newCust*boardingCost - runningCost dp.append(temp) i += 1 if all([x<=0 for x in dp]): return -1 return dp.index(max(dp))
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: res = 0 max_profit = 0 wait_people = 0 taken_people = 0 rotation_times = 0 i = 0 while i < len(customers) or wait_people > 0: rotation_times += 1 cur_people = customers[i] if i < len(customers) else 0 can_take = min(4, wait_people+cur_people) taken_people += can_take cur_profit = taken_people*boardingCost - rotation_times*runningCost if cur_profit > max_profit: res = rotation_times max_profit = cur_profit wait_people = max(0, wait_people+cur_people-4) i += 1 return res 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: profits = [0] # counts = [] person_remain = customers[0] i = 1 while(person_remain or i == 1): gondola = min(4, person_remain) profits.append(gondola*boardingCost - runningCost + profits[-1]) person_remain = person_remain - gondola if(i < len(customers)): person_remain += customers[i] i+= 1 max_round = 0 max_el = max(profits) # print(profits) if(max_el <= 0): return -1 for i in range(len(profits)): if(profits[i] == max_el): max_round = i break return max_round
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: wheel = [0,0,0,0] profit = 0 tracker = [-1] line = 0 def rotate(wheel, profit, boardingCost, runningCost, tracker): profit += boardingCost*wheel[0] - runningCost tracker.append(profit) wheel = [0, wheel[0], wheel[1], wheel[2]] return (wheel, profit, tracker) i = 0 while i < len(customers) or line > 0: if i < len(customers): line += customers[i] if line <= 4: wheel[0] = line line = 0 else: wheel[0] = 4 line -= 4 wheel, profit, tracker = rotate(wheel, profit, boardingCost, runningCost, tracker) i += 1 maxp = -1 val = -1 # print(tracker, wheel, line) for i in range(len(tracker)): if tracker[i] > maxp: maxp = tracker[i] val = i if maxp > 0: return val 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: maxProfit, res, remain, profit, i = 0, 0, 0, -runningCost*3, 0 while i < len(customers) or remain: if i < len(customers): x = customers[i] else: x = 0 profit += min(4, x + remain) * boardingCost - runningCost if profit > maxProfit: maxProfit, res = profit, i+1 remain = max(0, remain+x-4) i += 1 return res if res 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: if boardingCost * 4 <= runningCost: return -1 profit = 0 num_waiting_customers = 0 max_profit = 0 ans = -1 i = 0 while i < len(customers) or num_waiting_customers > 0: num_waiting_customers += customers[i] if i < len(customers) else 0 if i < len(customers): num_rotate = ((len(customers) - i) * 50 + num_waiting_customers + 3) // 4 if ((len(customers) - i) * 50 + num_waiting_customers) * boardingCost - num_rotate * runningCost + profit < 0: return ans profit += min(num_waiting_customers, 4) * boardingCost - runningCost if profit > max_profit: ans = i + 1 max_profit = profit num_waiting_customers = max(num_waiting_customers - 4, 0) i += 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: n_board, n_wait = 0, 0 cur_profit, max_profit, max_idx = 0, -float('inf'), -1 for i, people in enumerate(customers): n_wait += people n_board += min(4, n_wait) n_wait -= min(4, n_wait) cur_profit = boardingCost * n_board - (i+1) * runningCost if cur_profit > max_profit: max_profit = cur_profit max_idx = i + 1 # print(i+1, cur_profit) # print(n_wait, n_board) while n_wait: i += 1 n_board += min(n_wait, 4) n_wait -= min(n_wait, 4) cur_profit = boardingCost * n_board - (i+1) * runningCost # print(n_board, n_wait) # print(i+1, cur_profit) if cur_profit > max_profit: max_profit = cur_profit max_idx = i + 1 return max_idx 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: q = 0 profit = 0 ans = -1 count = 0 for i in customers: count += 1 q += i board = min(q, 4) q -= board temp = profit + board*boardingCost - runningCost if temp > profit: profit = temp ans = count times = q // 4 left = q % 4 temp = profit + times*(4*boardingCost - runningCost) if temp > profit: profit = temp count += times ans = count temp = profit + left*boardingCost - runningCost if temp > profit: profit = temp count += 1 ans = count 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: # print(customers) r = 1 wait = 0 cost = 0 cround = [] for c in customers: wait += c if wait >= 4: wait -= 4 cost += boardingCost * 4 - runningCost cround.append([cost,r]) elif wait > 0: cost += boardingCost * wait - runningCost wait -= wait cround.append([cost,r]) r += 1 while wait >= 4: wait -= 4 cost += boardingCost * 4 - runningCost cround.append([cost,r]) r += 1 if wait > 0: cost += boardingCost * wait - runningCost cround.append([cost,r]) wait -= wait r += 1 ans = max(cround,key = lambda x: x[0]) # print('ans',ans) # print(r,wait,cost,cround) if ans[0] > 0: return ans[1] 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: p = 0 maxp = 0 res = -1 curr = 0 i = 0 while curr or i<len(customers): if i < len(customers): curr += customers[i] i += 1 p += min(curr, 4)*boardingCost - runningCost if p > maxp: res = i maxp = p curr = max(0, curr-4) 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: r = -1 maxV = 0 pre = 0 profit = 0 i = 0 while True: curr = 0 if i < len(customers): curr = customers[i] profit += min(curr + pre, 4)*boardingCost - runningCost pre = max(curr + pre - 4, 0) if profit > maxV: r = i + 1 maxV = profit i += 1 if i >= len(customers) and pre <= 0: break return r
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: steps, waiting = 1, 0 fee, cost, record, max_profit = 0, 0, {}, -sys.maxsize while steps < len(customers) or waiting>0: arrival = customers[steps-1] if steps <= len(customers) else 0 if arrival+waiting <= 4: fee += (arrival+waiting)*boardingCost waiting = 0 else: waiting = (arrival+waiting)-4 fee += 4*boardingCost cost += runningCost record[steps] = fee-cost max_profit = max(max_profit, fee-cost) steps += 1 for k in record: if record[k]>0 and record[k]==max_profit: return k 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: left_people = 0 profit = 0 maxProfit = -1 maxpc = 0 counter = 0 i = 0 while i < len(customers) or left_people > 0: counter += 1 left_people += customers[i] if i < len(customers) else 0 profit += min(left_people, 4) * boardingCost - runningCost left_people = max(left_people - 4, 0) if profit > maxProfit: maxProfit = profit maxpc = counter i += 1 return -1 if maxProfit < 0 else maxpc
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: num_wait = 0 num_board = 0 num_round = 0 max_prof = 0 res = -1 for a in customers: num_round += 1 if num_wait + a >= 4: num_board += 4 num_wait += a - 4 else: num_board += num_wait + a num_wait = 0 if boardingCost * num_board - runningCost * num_round > max_prof: max_prof = max(max_prof, boardingCost * num_board - runningCost * num_round) res = num_round while num_wait > 0: num_round += 1 num_board += min(4, num_wait) num_wait -= min(4, num_wait) if boardingCost * num_board - runningCost * num_round > max_prof: max_prof = max(max_prof, boardingCost * num_board - runningCost * num_round) res = num_round 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], boarding_cost: int, running_cost: int) -> int: max_profit = -1 cur_profit = 0 waiting = 0 rotations = 0 i = 0 while i < len(customers): c = customers[i] waiting += c boarded = min([waiting,4]) waiting = max([waiting-boarded,0]) cur_profit += boarded * boarding_cost - running_cost if cur_profit > 0 and max_profit < cur_profit: max_profit = max([max_profit,cur_profit]) rotations = i+1 i+=1 while waiting >0: boarded = min([waiting,4]) waiting = max([waiting-boarded,0]) cur_profit += boarded * boarding_cost - running_cost if cur_profit > 0 and max_profit < cur_profit: max_profit = max([max_profit,cur_profit]) rotations = i+1 i+=1 if rotations == 0: return -1 return 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: ans=[0]*1000000 waiting=0 for i in range(1000000): if i<len(customers): waiting+=customers[i] if waiting<=4: ans[i]=ans[i-1] + waiting*boardingCost - runningCost waiting=0 else: ans[i]=ans[i-1] + 4*boardingCost -runningCost waiting-=4 if waiting<=0 and i>=len(customers): break maxVal = max(ans) if maxVal<=0: return -1 ret = ans.index(maxVal)+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: profits = [-1] waiting = customers[0] i = 1 board = 0 while waiting > 0 or i < len(customers): board += min(4, waiting) profit = board * boardingCost profit -= runningCost * i profits.append(profit) waiting -= min(4, waiting) if i < len(customers): waiting += customers[i] i += 1 id = profits.index(max(profits)) if id == 0: return -1 return id
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 profit = 4 * boardingCost - runningCost if profit <=0: return - 1 cumulate = sum(customers) cum = 0 rt = 0 al = 0 maxl = 0 for item in customers: cum+=item if cum>=4: cum-=4 rt+=1 al+=4 maxl = max(maxl, al * boardingCost -rt* runningCost) else: rt+=1 al+=cum # if al * boardingCost -rt* runningCost > maxl: maxl = al * boardingCost -rt* runningCost cum = 0 a = cum //4 b = cum%4 profit = boardingCost * b - runningCost if profit > 0: rt=rt + a+1 else: rt+=a return rt # if b == 0: # return a # else: # rt = a # tryone = cumulate * boardingCost - (a+1) * runningCost # anotherone = a * 4 * boardingCost - a * runningCost # profit = boardingCost * b - runningCost # print(a, b, cumulate,tryone,anotherone ) # if profit > 0: # rt=a+1 # if tryone > anotherone: # return a+1 # else: # return rt
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: profits = [0] * (13 * len(customers)) p_cursor = 0 waiting = 0 for c in customers: waiting += c riding = min(waiting, 4) profits[p_cursor] += boardingCost * riding profits[p_cursor] -= runningCost if p_cursor > 0: profits[p_cursor] += profits[p_cursor - 1] p_cursor += 1 waiting -= riding while waiting > 0: riding = min(waiting, 4) profits[p_cursor] += boardingCost * riding profits[p_cursor] -= runningCost if p_cursor > 0: profits[p_cursor] += profits[p_cursor - 1] p_cursor += 1 waiting -= riding max_profit = max(profits) if max_profit <= 0: return -1 for i, p in enumerate(profits): if p == max_profit: return i + 1 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, a: List[int], boardingCost: int, runningCost: int) -> int: res = boarding = profit = rem = 0 n = len(a) cap = 4 profits = [] i = 0 while rem or i < n: if i < n: rem += a[i] # print(f'{rem = }') boarding = min(rem, cap) # print(f'{boarding = }') rem = max(0, rem - cap) profit += boarding * boardingCost - runningCost profits.append(profit) # print(f'{rem = }') # print(f'{profits = }') i += 1 argmax = -1 mx = -float('inf') for i, x in enumerate(profits): if x > mx: mx = x argmax = i return argmax + 1 if profits[argmax] > 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: rotations = 0 wheel = [0, 0, 0, 0] profits = [] cost = 0 waiting = 0 profit = 0 maxProfit = 0 for i in range(len(customers)): waiting += customers[i] '''if rotations < i: for j in range(rotations - i): profit -= runningCost wheel[2], wheel[3] = wheel[1], wheel[2] wheel[1] = 0''' if waiting > 4: profit += 4*boardingCost else: profit = boardingCost * waiting profit -= runningCost rotations += 1 wheel[2], wheel[3] = wheel[1], wheel[2] wheel[1] = min(waiting, 4) waiting -= min(waiting, 4) profits.append((profit, rotations)) while waiting > 0: profit += min(4, waiting) * boardingCost waiting -= min(4, waiting) profit -= runningCost rotations += 1 profits.append((profit, rotations)) #print(profits) profits = sorted(profits, key = lambda x: (x[0], -x[1]), reverse = True) if profits[0][0] > 0: return profits[0][1] 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: if len(customers)==0: return -1 curWait=0 prof=0 maps=[] count=0 while curWait!=0 or count<len(customers): if count<len(customers): cu=customers[count] curWait+=cu count+=1 prof-=runningCost if curWait<=4: prof+=curWait*boardingCost curWait=0 else: prof+=4*boardingCost curWait-=4 maps.append([count,prof]) maps=sorted(maps,key=lambda x:x[1],reverse=True) if maps[0][1]<=0: return -1 else: return maps[0][0]
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: temp = 0 profit = 0 r = 0 waiting = 0 ans = -1 i = 0 while i < len(customers) or waiting > 0: num = min(waiting + customers[i], 4) if i < len(customers) else min(waiting, 4) profit += num * boardingCost - runningCost r += 1 waiting = max(waiting + customers[i]-4, 0) if i < len(customers) else max(waiting - 4, 0) i += 1 if profit > temp: temp = profit ans = r 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: times = 0 left = 0 profit = 0 pros = [] for i in range(len(customers)): left += customers[i] profit = profit + min(left, 4) * boardingCost - runningCost pros.append(profit) left = max(0, left-4) times += 1 i = len(customers) while (left > 0): profit = profit + min(left, 4) * boardingCost - runningCost pros.append(profit) i += 1 left = max(0, left-4) times += 1 mm = -1 out = - 1 for i in range(len(pros)): if pros[i] > mm: mm = pros[i] out = i + 1 if profit > 0: return out 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: if not customers: return -1 profits = [] wait = 0 i = 0 while True: if i > len(customers) -1 and wait <= 0: break elif i <= len(customers)-1 : wait += customers[i] profit = min(4, wait) * boardingCost - runningCost profits.append(profit) wait = wait - min(4, wait) i += 1 else: profit = min(4, wait) * boardingCost - runningCost profits.append(profit) wait = wait - min(4, wait) #print(profits) sum_ = profits[0] for i in range(1, len(profits)): profits[i] = sum_ + profits[i] sum_ = profits[i] #print(profits) max_ = 0 index = -1 for i in range(0, len(profits)): if max_ < profits[i]: index = i max_ = profits[i] if index > -1: index += 1 return index
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: nrot = 0 i = 0 profits = [] if customers[i] > 0: waiting = customers[i] - min(4, customers[i]) nrot += 1 profits.append(min(customers[i], 4)*boardingCost - 1 * runningCost) else: waiting = 0 profits.append(0) customers.append(0) while waiting > 0 or i <= len(customers): profits.append(profits[-1] + min(waiting, 4)*boardingCost - 1 * runningCost) if i + 1 <= len(customers) - 1: waiting += - min(4, waiting + customers[i+1]) + customers[i+1] else: waiting -= min(waiting, 4) nrot += 1 i += 1 # print(nrot, boardingCost, sum(customers), runningCost) mx = profits.index(max(profits)) return mx+1 if max(profits) > 0 else -1