https://leetcode.com/problems/minimum-penalty-for-a-shop/description/
2483. Minimum Penalty for a Shop
给你一个阵列 customers,customers[i] 表示第 i 个小时的时候是否有顾客来消费
,开店的时候如果没客人会亏损,关店的时候有客人来也会亏,我们要挑选一个时间
打烊可以让店里的亏损最低,并且这个时间要尽可能的早。
Example 1:
Input: customers = "YYNY"
Output: 2
Explanation:
- Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is
earlier, the optimal closing time is 2.
Example 2:
Input: customers = "NNNNN"
Output: 0
Explanation: It is best to close the shop at the 0th hour as no customers
arrive.
思路:
1.先统计出所有小时关店时,会错过或没客人的数量,关店的时间点之前的
亏损会是该个时间点之前N的数量,而关店的时间点之后的亏损会是来客数量Y。
2.统计完之后从 "开到最后一刻才打烊" 遍历到 "一开始就不开店" 的所有结果中
,取出最小的亏损额,因为我们是从比较晚的时间开始往前算所以遇到一样亏损的
打烊时间就取新的就一定是时间最早的。
Java Code: