Skip to content

Commit 6f9439e

Browse files
committed
update: 添加问题“2483.商店的最少代价”的代码和题解 (#1285)
2483: AC.cpp (#1284) + word(en) AC,91.67%,55.95% cpp - AC,一次遍历,82.14%,98.81% Signed-off-by: LetMeFly666 <[email protected]>
1 parent 5446391 commit 6f9439e

11 files changed

+225
-124
lines changed

.commitTitleExtra

Lines changed: 0 additions & 3 deletions
This file was deleted.

.commitmsg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2483: AC.cpp (#1284) + word(en)
2+
3+
AC,91.67%,55.95%
4+
cpp - AC,一次遍历,82.14%,98.81%
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-26 18:39:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-26 18:42:18
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int bestClosingTime(string customers) {
14+
int maxium = 0, ans = 0;
15+
for (char c : customers) {
16+
maxium += c == 'Y';
17+
}
18+
int thisCost = maxium;
19+
for (int i = 0; i < customers.size(); i++) {
20+
if (customers[i] == 'Y') {
21+
thisCost--;
22+
} else {
23+
thisCost++;
24+
}
25+
if (thisCost < maxium) {
26+
maxium = thisCost;
27+
ans = i + 1;
28+
}
29+
}
30+
return ans;
31+
}
32+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-26 18:39:14
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-26 18:46:24
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int bestClosingTime(string customers) {
14+
int maxium = 0, ans = 0;
15+
int thisCost = 0;
16+
for (int i = 0; i < customers.size(); i++) {
17+
if (customers[i] == 'Y') {
18+
thisCost--;
19+
} else {
20+
thisCost++;
21+
}
22+
if (thisCost < maxium) {
23+
maxium = thisCost;
24+
ans = i + 1;
25+
}
26+
}
27+
return ans;
28+
}
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,7 @@
819819
|2476.二叉搜索树最近节点查询|中等|<a href="https://leetcode.cn/problems/closest-nodes-queries-in-a-binary-search-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/02/24/LeetCode%202476.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E6%9C%80%E8%BF%91%E8%8A%82%E7%82%B9%E6%9F%A5%E8%AF%A2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136269516" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/closest-nodes-queries-in-a-binary-search-tree/solutions/2652038/letmefly-2476er-cha-sou-suo-shu-zui-jin-dbuce/" target="_blank">LeetCode题解</a>|
820820
|2477.到达首都的最少油耗|中等|<a href="https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/12/05/LeetCode%202477.%E5%88%B0%E8%BE%BE%E9%A6%96%E9%83%BD%E7%9A%84%E6%9C%80%E5%B0%91%E6%B2%B9%E8%80%97/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134816086" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-fuel-cost-to-report-to-the-capital/solutions/2555162/letmefly-2477dao-da-shou-du-de-zui-shao-2r4od/" target="_blank">LeetCode题解</a>|
821821
|2481.分割圆的最少切割次数|简单|<a href="https://leetcode.cn/problems/minimum-cuts-to-divide-a-circle/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/17/LeetCode%202481.%E5%88%86%E5%89%B2%E5%9C%86%E7%9A%84%E6%9C%80%E5%B0%91%E5%88%87%E5%89%B2%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131257304" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-cuts-to-divide-a-circle/solutions/2311882/letmefly-2481fen-ge-yuan-de-zui-shao-qie-w27z/" target="_blank">LeetCode题解</a>|
822+
|2483.商店的最少代价|中等|<a href="https://leetcode.cn/problems/minimum-penalty-for-a-shop/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/26/LeetCode%202483.%E5%95%86%E5%BA%97%E7%9A%84%E6%9C%80%E5%B0%91%E4%BB%A3%E4%BB%B7/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156312519" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-penalty-for-a-shop/solutions/3866581/letmefly-2483shang-dian-de-zui-shao-dai-06zzq/" target="_blank">LeetCode题解</a>|
822823
|2485.找出中枢整数|简单|<a href="https://leetcode.cn/problems/find-the-pivot-integer/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/26/LeetCode%202485.%E6%89%BE%E5%87%BA%E4%B8%AD%E6%9E%A2%E6%95%B4%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131391141" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-pivot-integer/solutions/2320680/letmefly-2485zhao-chu-zhong-shu-zheng-sh-monv/" target="_blank">LeetCode题解</a>|
823824
|2487.从链表中移除节点|中等|<a href="https://leetcode.cn/problems/remove-nodes-from-linked-list/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/01/03/LeetCode%202487.%E4%BB%8E%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%A7%BB%E9%99%A4%E8%8A%82%E7%82%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135357617" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-nodes-from-linked-list/solutions/2588878/letmefly-2487cong-lian-biao-zhong-yi-chu-c4dy/" target="_blank">LeetCode题解</a>|
824825
|2490.回环句|简单|<a href="https://leetcode.cn/problems/circular-sentence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/30/LeetCode%202490.%E5%9B%9E%E7%8E%AF%E5%8F%A5/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131468453" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/circular-sentence/solutions/2325193/letmefly-2490hui-huan-ju-pan-duan-kong-g-z56k/" target="_blank">LeetCode题解</a>|
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: 2483.商店的最少代价:两次遍历 -> 一次遍历
3+
date: 2025-12-26 18:45:20
4+
tags: [题解, LeetCode, 中等, 字符串, 遍历]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】2483.商店的最少代价:两次遍历 -> 一次遍历
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-penalty-for-a-shop/](https://leetcode.cn/problems/minimum-penalty-for-a-shop/)
11+
12+
<p>给你一个顾客访问商店的日志,用一个下标从 <strong>0</strong>&nbsp;开始且只包含字符&nbsp;<code>'N'</code> 和&nbsp;<code>'Y'</code>&nbsp;的字符串&nbsp;<code>customers</code>&nbsp;表示:</p>
13+
14+
<ul>
15+
<li>如果第&nbsp;<code>i</code>&nbsp;个字符是&nbsp;<code>'Y'</code>&nbsp;,它表示第&nbsp;<code>i</code>&nbsp;小时有顾客到达。</li>
16+
<li>如果第&nbsp;<code>i</code>&nbsp;个字符是&nbsp;<code>'N'</code>&nbsp;,它表示第 <code>i</code>&nbsp;小时没有顾客到达。</li>
17+
</ul>
18+
19+
<p>如果商店在第&nbsp;<code>j</code>&nbsp;小时关门(<code>0 &lt;= j &lt;= n</code>),代价按如下方式计算:</p>
20+
21+
<ul>
22+
<li>在开门期间,如果某一个小时没有顾客到达,代价增加 <code>1</code>&nbsp;。</li>
23+
<li>在关门期间,如果某一个小时有顾客到达,代价增加&nbsp;<code>1</code>&nbsp;。</li>
24+
</ul>
25+
26+
<p>请你返回在确保代价 <strong>最小</strong>&nbsp;的前提下,商店的&nbsp;<strong>最早</strong>&nbsp;关门时间。</p>
27+
28+
<p>注意,商店在第 <code>j</code>&nbsp;小时关门表示在第 <code>j</code> 小时以及之后商店处于关门状态。</p>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong>示例 1:</strong></p>
33+
34+
<pre>
35+
<b>输入:</b>customers = "YYNY"
36+
<b>输出:</b>2
37+
<b>解释:</b>
38+
- 第 0 小时关门,总共 1+1+0+1 = 3 代价。
39+
- 第 1 小时关门,总共 0+1+0+1 = 2 代价。
40+
- 第 2 小时关门,总共 0+0+0+1 = 1 代价。
41+
- 第 3 小时关门,总共 0+0+1+1 = 2 代价。
42+
- 第 4 小时关门,总共 0+0+1+0 = 1 代价。
43+
在第 2 或第 4 小时关门代价都最小。由于第 2 小时更早,所以最优关门时间是 2 。
44+
</pre>
45+
46+
<p><strong>示例 2:</strong></p>
47+
48+
<pre>
49+
<b>输入:</b>customers = "NNNNN"
50+
<b>输出:</b>0
51+
<b>解释:</b>最优关门时间是 0 ,因为自始至终没有顾客到达。</pre>
52+
53+
<p><strong>示例 3:</strong></p>
54+
55+
<pre>
56+
<b>输入:</b>customers = "YYYY"
57+
<b>输出:</b>4
58+
<b>解释:</b>最优关门时间是 4 ,因为每一小时均有顾客到达。
59+
</pre>
60+
61+
<p>&nbsp;</p>
62+
63+
<p><strong>提示:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= customers.length &lt;= 10<sup>5</sup></code></li>
67+
<li><code>customers</code>&nbsp;只包含字符&nbsp;<code>'Y'</code>&nbsp;和&nbsp;<code>'N'</code>&nbsp;。</li>
68+
</ul>
69+
70+
71+
72+
## 解题方法一:两次遍历
73+
74+
先计算下直接不开门的话总代价是多少。
75+
76+
接着遍历顾客字符串,遍历到第i天时计算这个顾客走之后关门的总代价是多少。
77+
78+
取一个最小总代价对应的关门时间;如有相同则取最早的那个。
79+
80+
假设当前总代价为`x`,准备改为往后一个顾客时间再关门,那么就看下一个时间是`Y`还是`N`。如果为`Y`则总代价减1否则总代价加1。
81+
82+
+ 时间复杂度$O(len(customers))$
83+
+ 空间复杂度$O(1)$
84+
85+
### AC代码
86+
87+
#### C++
88+
89+
```cpp
90+
/*
91+
* @LastEditTime: 2025-12-26 18:42:18
92+
*/
93+
class Solution {
94+
public:
95+
int bestClosingTime(string customers) {
96+
int maxium = 0, ans = 0;
97+
for (char c : customers) {
98+
maxium += c == 'Y';
99+
}
100+
int thisCost = maxium;
101+
for (int i = 0; i < customers.size(); i++) {
102+
if (customers[i] == 'Y') {
103+
thisCost--;
104+
} else {
105+
thisCost++;
106+
}
107+
if (thisCost < maxium) {
108+
maxium = thisCost;
109+
ans = i + 1;
110+
}
111+
}
112+
return ans;
113+
}
114+
};
115+
```
116+
117+
## 解题方法二:一次遍历
118+
119+
不难发现第一次遍历计算得到的“直接不开门”状态下的代价其实没有意义,将其视为0且后续顾客在此基础上浮动即可。
120+
121+
+ 时间复杂度$O(len(customers))$
122+
+ 空间复杂度$O(1)$
123+
124+
### AC代码
125+
126+
#### C++
127+
128+
```cpp
129+
/*
130+
* @LastEditTime: 2025-12-26 18:46:24
131+
*/
132+
class Solution {
133+
public:
134+
int bestClosingTime(string customers) {
135+
int maxium = 0, ans = 0;
136+
int thisCost = 0;
137+
for (int i = 0; i < customers.size(); i++) {
138+
if (customers[i] == 'Y') {
139+
thisCost--;
140+
} else {
141+
thisCost++;
142+
}
143+
if (thisCost < maxium) {
144+
maxium = thisCost;
145+
ans = i + 1;
146+
}
147+
}
148+
return ans;
149+
}
150+
};
151+
```
152+
153+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/156312519)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/26/LeetCode%202483.%E5%95%86%E5%BA%97%E7%9A%84%E6%9C%80%E5%B0%91%E4%BB%A3%E4%BB%B7/)哦~
154+
>
155+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,10 @@ categories: [自用]
16961696
|||
16971697
|pursuance|n. 遵守,进行,实行|
16981698
|homogenous|adj. (生物)同质的,同类的|
1699+
|||
1700+
|sterling|n. 英镑<br/>adj. 优秀的,杰出的,纯银的|
1701+
|herbal|adj. 药草的,香草的<br/>n. 草本植物志|
1702+
|arbitration|n. 仲裁|
16991703

17001704
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
17011705
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

createPr.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

createPr2.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

getPrTitle.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)