Skip to content

Commit 949ac6f

Browse files
authored
update: 添加问题“3296.移山所需的最少秒数”的代码和题解 (#1440)
* word: en 2026.3.9 * word: en 2026.3.10 * word: en 2026.3.12 * 3296: WA.cpp (#1439) * 3296: RE.cpp (#1439) * 3296: AC.cpp (#1439) - AC,29.22%,19.01% * update: 添加问题“3296.移山所需的最少秒数”的代码和题解 (#1440) Signed-off-by: LetMeFly666 <Tisfy@qq.com> --------- Signed-off-by: LetMeFly666 <Tisfy@qq.com>
1 parent dd8e431 commit 949ac6f

5 files changed

Lines changed: 374 additions & 0 deletions
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2026-03-13 22:49:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2026-03-13 22:57:56
6+
*/
7+
#ifdef _DEBUG
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
ll minNumberOfSeconds(int mountainHeight, vector<int>& workerTimes) {
15+
priority_queue<tuple<ll, ll, int>, vector<tuple<ll, ll, int>>, greater<>> pq;
16+
for (int t : workerTimes) {
17+
pq.push({t, 1, t});
18+
}
19+
ll ans = 0;
20+
while (mountainHeight--) {
21+
auto[now, times, once] = pq.top();
22+
pq.pop();
23+
ans = now;
24+
pq.push({now + ++times * once, times, once});
25+
}
26+
return ans;
27+
}
28+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@
10791079
|3280.将日期转换为二进制表示|简单|<a href="https://leetcode.cn/problems/convert-date-to-binary/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/01/LeetCode%203280.%E5%B0%86%E6%97%A5%E6%9C%9F%E8%BD%AC%E6%8D%A2%E4%B8%BA%E4%BA%8C%E8%BF%9B%E5%88%B6%E8%A1%A8%E7%A4%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144870892" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/convert-date-to-binary/solutions/3036032/letmefly-3280jiang-ri-qi-zhuan-huan-wei-149mv/" target="_blank">LeetCode题解</a>|
10801080
|3285.找到稳定山的下标|简单|<a href="https://leetcode.cn/problems/find-indices-of-stable-mountains/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/19/LeetCode%203285.%E6%89%BE%E5%88%B0%E7%A8%B3%E5%AE%9A%E5%B1%B1%E7%9A%84%E4%B8%8B%E6%A0%87/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144596618" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-indices-of-stable-mountains/solutions/3025068/letmefly-3285zhao-dao-wen-ding-shan-de-x-u3wc/" target="_blank">LeetCode题解</a>|
10811081
|3289.数字小镇中的捣蛋鬼|简单|<a href="https://leetcode.cn/problems/the-two-sneaky-numbers-of-digitville/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/31/LeetCode%203289.%E6%95%B0%E5%AD%97%E5%B0%8F%E9%95%87%E4%B8%AD%E7%9A%84%E6%8D%A3%E8%9B%8B%E9%AC%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/154214125" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/the-two-sneaky-numbers-of-digitville/solutions/3820570/letmefly-3289shu-zi-xiao-zhen-zhong-de-d-oz9r/" target="_blank">LeetCode题解</a>|
1082+
|3296.移山所需的最少秒数|中等|<a href="https://leetcode.cn/problems/minimum-number-of-seconds-to-make-mountain-height-zero/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/03/13/LeetCode%203296.%E7%A7%BB%E5%B1%B1%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E7%A7%92%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/159020023" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-seconds-to-make-mountain-height-zero/solutions/3924553/letmefly-3296yi-shan-suo-xu-de-zui-shao-jbmgy/" target="_blank">LeetCode题解</a>|
10821083
|3297.统计重新排列后包含另一个字符串的子字符串数目I|中等|<a href="https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/09/LeetCode%203297.%E7%BB%9F%E8%AE%A1%E9%87%8D%E6%96%B0%E6%8E%92%E5%88%97%E5%90%8E%E5%8C%85%E5%90%AB%E5%8F%A6%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E7%9B%AEI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145031494" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-i/solutions/3042787/letmefly-3297tong-ji-zhong-xin-pai-lie-h-8ele/" target="_blank">LeetCode题解</a>|
10831084
|3305.元音辅音字符串计数I|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/12/LeetCode%203305.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146197747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/solutions/3607571/letmefly-3305yuan-yin-fu-yin-zi-fu-chuan-ptkg/" target="_blank">LeetCode题解</a>|
10841085
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: 3296.移山所需的最少秒数:优先队列
3+
date: 2026-03-13 22:59:06
4+
tags: [题解, LeetCode, 中等, 贪心, 数组, 数学, 二分查找, 堆(优先队列), 优先队列]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3296.移山所需的最少秒数:优先队列
9+
10+
力扣题目链接:[https://leetcode.cn/problems/minimum-number-of-seconds-to-make-mountain-height-zero/](https://leetcode.cn/problems/minimum-number-of-seconds-to-make-mountain-height-zero/)
11+
12+
<p>给你一个整数 <code>mountainHeight</code> 表示山的高度。</p>
13+
14+
<p>同时给你一个整数数组 <code>workerTimes</code>,表示工人们的工作时间(单位:<strong>秒</strong>)。</p>
15+
16+
<p>工人们需要 <strong>同时 </strong>进行工作以 <strong>降低 </strong>山的高度。对于工人 <code>i</code> :</p>
17+
18+
<ul>
19+
<li>山的高度降低 <code>x</code>,需要花费 <code>workerTimes[i] + workerTimes[i] * 2 + ... + workerTimes[i] * x</code> 秒。例如:
20+
21+
<ul>
22+
<li>山的高度降低 1,需要 <code>workerTimes[i]</code> 秒。</li>
23+
<li>山的高度降低 2,需要 <code>workerTimes[i] + workerTimes[i] * 2</code> 秒,依此类推。</li>
24+
</ul>
25+
</li>
26+
</ul>
27+
28+
<p>返回一个整数,表示工人们使山的高度降低到 0 所需的 <strong>最少</strong> 秒数。</p>
29+
30+
<p>&nbsp;</p>
31+
32+
<p><strong class="example">示例 1:</strong></p>
33+
34+
<div class="example-block">
35+
<p><strong>输入:</strong> <span class="example-io">mountainHeight = 4, workerTimes = [2,1,1]</span></p>
36+
37+
<p><strong>输出:</strong> <span class="example-io">3</span></p>
38+
39+
<p><strong>解释:</strong></p>
40+
41+
<p>将山的高度降低到 0 的一种方式是:</p>
42+
43+
<ul>
44+
<li>工人 0 将高度降低 1,花费 <code>workerTimes[0] = 2</code> 秒。</li>
45+
<li>工人 1 将高度降低 2,花费 <code>workerTimes[1] + workerTimes[1] * 2 = 3</code> 秒。</li>
46+
<li>工人 2 将高度降低 1,花费 <code>workerTimes[2] = 1</code> 秒。</li>
47+
</ul>
48+
49+
<p>因为工人同时工作,所需的最少时间为 <code>max(2, 3, 1) = 3</code> 秒。</p>
50+
</div>
51+
52+
<p><strong class="example">示例 2:</strong></p>
53+
54+
<div class="example-block">
55+
<p><strong>输入:</strong> <span class="example-io">mountainHeight = 10, workerTimes = [3,2,2,4]</span></p>
56+
57+
<p><strong>输出:</strong> <span class="example-io">12</span></p>
58+
59+
<p><strong>解释:</strong></p>
60+
61+
<ul>
62+
<li>工人 0 将高度降低 2,花费 <code>workerTimes[0] + workerTimes[0] * 2 = 9</code> 秒。</li>
63+
<li>工人 1 将高度降低 3,花费 <code>workerTimes[1] + workerTimes[1] * 2 + workerTimes[1] * 3 = 12</code> 秒。</li>
64+
<li>工人 2 将高度降低 3,花费 <code>workerTimes[2] + workerTimes[2] * 2 + workerTimes[2] * 3 = 12</code> 秒。</li>
65+
<li>工人 3 将高度降低 2,花费 <code>workerTimes[3] + workerTimes[3] * 2 = 12</code> 秒。</li>
66+
</ul>
67+
68+
<p>所需的最少时间为 <code>max(9, 12, 12, 12) = 12</code> 秒。</p>
69+
</div>
70+
71+
<p><strong class="example">示例 3:</strong></p>
72+
73+
<div class="example-block">
74+
<p><strong>输入:</strong> <span class="example-io">mountainHeight = 5, workerTimes = [1]</span></p>
75+
76+
<p><strong>输出:</strong> <span class="example-io">15</span></p>
77+
78+
<p><strong>解释:</strong></p>
79+
80+
<p>这个示例中只有一个工人,所以答案是 <code>workerTimes[0] + workerTimes[0] * 2 + workerTimes[0] * 3 + workerTimes[0] * 4 + workerTimes[0] * 5 = 15</code> 秒。</p>
81+
</div>
82+
83+
<p>&nbsp;</p>
84+
85+
<p><strong>提示:</strong></p>
86+
87+
<ul>
88+
<li><code>1 &lt;= mountainHeight &lt;= 10<sup>5</sup></code></li>
89+
<li><code>1 &lt;= workerTimes.length &lt;= 10<sup>4</sup></code></li>
90+
<li><code>1 &lt;= workerTimes[i] &lt;= 10<sup>6</sup></code></li>
91+
</ul>
92+
93+
94+
95+
## 解题方法:优先队列
96+
97+
使用一个优先队列,每次选完工最早的工人降低1个山高。工人完工后可以再次进入队列,只不过再次进入队列后的下次工作耗时更久而已。
98+
99+
具体来说,优先队列中存放每个工人的`(完工时间, 共计降低高度, baseTime)``共计降低高度``baseTime`都是为了计算完工时间。
100+
101+
+ 时间复杂度$O(height\times \log n)$
102+
+ 空间复杂度$O(n)$
103+
104+
### AC代码
105+
106+
#### C++
107+
108+
```cpp
109+
/*
110+
* @LastEditTime: 2026-03-13 22:57:56
111+
*/
112+
typedef long long ll;
113+
class Solution {
114+
public:
115+
ll minNumberOfSeconds(int mountainHeight, vector<int>& workerTimes) {
116+
priority_queue<tuple<ll, ll, int>, vector<tuple<ll, ll, int>>, greater<>> pq;
117+
for (int t : workerTimes) {
118+
pq.push({t, 1, t});
119+
}
120+
ll ans = 0;
121+
while (mountainHeight--) {
122+
auto[now, times, once] = pq.top();
123+
pq.pop();
124+
ans = now;
125+
pq.push({now + ++times * once, times, once});
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/159020023)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2026/03/13/LeetCode%203296.%E7%A7%BB%E5%B1%B1%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E7%A7%92%E6%95%B0/)哦~
133+
>
134+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,13 @@ categories: [自用]
18651865
|mechanics|n. 力学,结构,技巧,机械学|
18661866
|pretentious|adj. 炫耀的,虚夸的,自命不凡的|
18671867
|indignant|adj. 愤慨的,愤怒的|
1868+
|||
1869+
|conceptive|adj. 概念上的,设想上的,〈罕〉会受孕的|
1870+
|groundless|adj. 无理由的,无根据的|
1871+
|||
1872+
|wreath|n. (祭奠)花圈,(圣诞节)花环|
1873+
|||
1874+
|prohibitive|adj. (法令)禁止的,贵得买不起的|
18681875

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

codestats.plugin.zsh

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# https://gitlab.com/code-stats/code-stats-zsh
2+
3+
_codestats_version="0.3.11"
4+
5+
zmodload zsh/datetime
6+
7+
declare -g -i _codestats_xp=0
8+
declare -g -i _codestats_pulse_time=${EPOCHSECONDS}
9+
10+
# Because each `curl` call is forked into a subshell to keep the interactive
11+
# shell responsive, the consecutive error count cannot be updated in a
12+
# variable. So we use a temp file, one error per line.
13+
_codestats_consecutive_errors=$(mktemp)
14+
15+
# Logging: write to file if CODESTATS_LOG_FILE is set and exists
16+
_codestats_log()
17+
{
18+
if [[ -w "${CODESTATS_LOG_FILE}" ]]; then
19+
# EPOCHSECONDS is an integer, so disable globbing/splitting warning
20+
# shellcheck disable=SC2086
21+
echo "$(\strftime %Y-%m-%dT%H:%M:%S ${EPOCHSECONDS}) ($$) $*" >> "${CODESTATS_LOG_FILE}"
22+
fi
23+
}
24+
25+
# Widget wrapper: add a keypress and call original widget
26+
_codestats_call_widget()
27+
{
28+
# Bracketed-paste-magic calls self-insert when pasting and sets $PASTED.
29+
# Don't add xp for pasting.
30+
31+
# shellcheck disable=SC2004
32+
if (( ! ${+PASTED} )); then
33+
_codestats_xp+=1
34+
fi
35+
36+
builtin zle "$@"
37+
}
38+
39+
# Rebind widgets
40+
_codestats_rebind_widgets()
41+
{
42+
local w
43+
for w in \
44+
self-insert \
45+
delete-char \
46+
backward-delete-char \
47+
accept-line
48+
do
49+
# call the internal version, ie. the one beginning with `.`
50+
eval "_codestats_${w} () { _codestats_call_widget .${w} -- \"\$@\" }"
51+
zle -N ${w} _codestats_${w}
52+
53+
_codestats_log "Wrapped and rebound the ${w} widget."
54+
done
55+
}
56+
57+
# Pulse sending function
58+
_codestats_send_pulse()
59+
{
60+
# Check that error count hasn't been exceeded
61+
local -i error_count=0
62+
if [[ -r "${_codestats_consecutive_errors}" ]]; then
63+
error_count=$(wc -l < "${_codestats_consecutive_errors}")
64+
fi
65+
if (( error_count > 4 )); then
66+
_codestats_log "Received too many consecutive errors! Stopping..."
67+
_codestats_stop "Received ${error_count} consecutive errors when trying to save XP."
68+
return
69+
fi
70+
71+
# If there's accumulated XP, send it
72+
if (( _codestats_xp > 0 )); then
73+
local url
74+
url="$(_codestats_pulse_url)"
75+
76+
_codestats_log "Sending pulse (${_codestats_xp} xp) to ${url}"
77+
78+
local payload
79+
payload=$(_codestats_payload ${_codestats_xp})
80+
81+
\curl \
82+
--max-time 5 \
83+
--header "Content-Type: application/json" \
84+
--header "X-API-Token: ${CODESTATS_API_KEY}" \
85+
--user-agent "code-stats-zsh/${_codestats_version}" \
86+
--data "${payload}" \
87+
--request POST \
88+
--silent \
89+
--output /dev/null \
90+
--write-out "%{http_code}" \
91+
"${url}" \
92+
| _codestats_handle_response_status \
93+
&|
94+
95+
_codestats_xp=0
96+
fi
97+
}
98+
99+
# Error handling based on HTTP status
100+
_codestats_handle_response_status()
101+
{
102+
local _status
103+
_status=$(\cat -)
104+
case ${_status} in
105+
000)
106+
_codestats_log "Network error!"
107+
# don't stop; maybe the network will start working eventually
108+
;;
109+
200 | 201 )
110+
_codestats_log "Success (${_status})!"
111+
# clear error count
112+
echo -n >! "${_codestats_consecutive_errors}"
113+
;;
114+
3* )
115+
_codestats_log "Unexpected redirect ${_status}!"
116+
_codestats_stop "Server responded with a redirect. Perhaps code-stats-zsh is out of date?"
117+
# this problem will probably not go away. stop immediately.
118+
;;
119+
4* | 5* )
120+
_codestats_log "Server responded with error ${_status}!"
121+
# some of 4xx and 5xx statuses may indicate a temporary problem
122+
echo "${_status}" >>! "${_codestats_consecutive_errors}"
123+
;;
124+
*)
125+
_codestats_log "Unexpected response status ${_status}!"
126+
# whatever happened, stop if it persists
127+
echo "${_status}" >>! "${_codestats_consecutive_errors}"
128+
;;
129+
esac
130+
}
131+
132+
_codestats_pulse_url()
133+
{
134+
echo "${CODESTATS_API_URL:-https://codestats.net}/api/my/pulses"
135+
}
136+
137+
# Create API payload
138+
_codestats_payload()
139+
{
140+
# shellcheck disable=SC2086
141+
cat <<EOF
142+
{
143+
"coded_at": "$(\strftime %Y-%m-%dT%H:%M:%S%z ${EPOCHSECONDS})",
144+
"xps": [{"language": "Terminal (Zsh)", "xp": $1}]
145+
}
146+
EOF
147+
}
148+
149+
# Check time since last pulse; maybe send pulse
150+
_codestats_poll()
151+
{
152+
if (( EPOCHSECONDS - _codestats_pulse_time > 10 )); then
153+
_codestats_send_pulse
154+
_codestats_pulse_time=${EPOCHSECONDS}
155+
fi
156+
}
157+
158+
_codestats_exit()
159+
{
160+
_codestats_log "Shell is exiting. Calling _codestats_send_pulse one last time."
161+
_codestats_send_pulse
162+
163+
# remove temp file
164+
rm -f "${_codestats_consecutive_errors}"
165+
}
166+
167+
_codestats_init()
168+
{
169+
_codestats_log "Initializing code-stats-zsh@${_codestats_version}..."
170+
171+
_codestats_rebind_widgets
172+
173+
# Call the polling function on each new prompt
174+
autoload -U add-zsh-hook
175+
add-zsh-hook precmd _codestats_poll
176+
177+
# Send pulse on shell exit
178+
add-zsh-hook zshexit _codestats_exit
179+
180+
_codestats_log "Initialization complete."
181+
}
182+
183+
# Stop because there was an error. Overwrite handler functions.
184+
_codestats_stop()
185+
{
186+
_codestats_log "Stopping zsh-code-stats. Overwriting hook functions with no-ops."
187+
>&2 echo "code-stats-zsh: $* Stopping."
188+
_codestats_poll() { true; }
189+
_codestats_exit() { true; }
190+
191+
# remove temp file
192+
rm -f "${_codestats_consecutive_errors}"
193+
}
194+
195+
if [[ -n "${CODESTATS_API_KEY}" ]]; then
196+
_codestats_init
197+
else
198+
echo "code-stats-zsh requires CODESTATS_API_KEY to be set!"
199+
false
200+
fi
201+
202+
if [[ -n "${CODESTATS_LOG_FILE}" && ! -w "${CODESTATS_LOG_FILE}" ]]; then
203+
echo "Warning: CODESTATS_LOG_FILE needs to exist and be writable!"
204+
fi

0 commit comments

Comments
 (0)