Skip to content

Commit f23c324

Browse files
authored
update: 添加问题“3494.酿造药水需要的最少总时间”的代码和题解 (#1165)
* 3494: WA.cpp (#1164) * 3494: AC.cpp (#1164) - AC,83.33%,50.00% 待会儿hello for initing codes * 3494: WA.cpp (#1164) - failed optimization * 3494: AC.cpp (#1164) - AC,reverse,83.33%,64.28% * 3494: TLE.py (#1164) - what??? * 3494: AC.py (#1164) - AC,14209ms击败5.77%,76.92% 把len(skill)提前用变量n存起来就过了。。。 看评论官解也TLE: https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/solutions/3800529/niang-zao-yao-shui-xu-yao-de-zui-shao-zo-ojii/comments/3139053/ * 3494: AC.rust (#1164) - AC,86ms击败100.00%,2.24MB击败100.00% 好小的内存占用好短的时间消耗 * 3494: AC.go (#1164) - AC,75.00%,100.00% * 3494: AC.java (#1164) - AC,11.76%,41.18% * update: 添加问题“3494.酿造药水需要的最少总时间”的代码和题解 (#1165) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent ca02bdd commit f23c324

11 files changed

+524
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-09 22:19:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-09 22:49:27
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
ll minTime(vector<int>& skill, vector<int>& mana) {
15+
vector<ll> times(skill.size());
16+
for (int m : mana) {
17+
ll delay = 0;
18+
ll nowFinish = times[0] + skill[0] * m;
19+
for (int i = 1; i < skill.size(); i++) {
20+
delay += max(0ll, times[i] - nowFinish);
21+
nowFinish = max(nowFinish, times[i]) + skill[i] * m;
22+
}
23+
times[0] += delay + skill[0] * m;
24+
for (int i = 1; i < skill.size(); i++) {
25+
times[i] = times[i - 1] + skill[i] * m;
26+
}
27+
// cout << times[0] << endl; // *****
28+
}
29+
return times.back();
30+
}
31+
};
32+
33+
#if defined(_WIN32) || defined(__APPLE__)
34+
/*
35+
[1,5,2,4]
36+
[5,1,4,2]
37+
38+
110
39+
40+
巫师0完成时间:
41+
5
42+
53
43+
58
44+
88
45+
*/
46+
int main() {
47+
string a, b;
48+
while (cin >> a >> b) {
49+
vector<int> va = stringToVector(a), vb = stringToVector(b);
50+
Solution sol;
51+
cout << sol.minTime(va, vb) << endl;
52+
}
53+
return 0;
54+
}
55+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-09 22:52:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-09 23:32:19
6+
*/
7+
package main
8+
9+
func minTime(skill []int, mana []int) int64 {
10+
n := len(skill)
11+
times := make([]int64, n)
12+
for _, m := range mana {
13+
lastTime := times[0] + int64(skill[0] * m)
14+
for i := 1; i < n; i++ {
15+
lastTime = max3494(times[i], lastTime) + int64(skill[i] * m)
16+
}
17+
times[n-1] = lastTime
18+
for i := n - 2; i >= 0; i-- {
19+
times[i] = times[i + 1] - int64(skill[i + 1] * m)
20+
}
21+
}
22+
return times[n-1]
23+
}
24+
25+
func max3494(a, b int64) int64 {
26+
if a > b {
27+
return a
28+
}
29+
return b
30+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-09 22:52:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-09 23:34:16
6+
*/
7+
class Solution {
8+
public long minTime(int[] skill, int[] mana) {
9+
int n = skill.length;
10+
long[] times = new long[n];
11+
for (int m : mana) {
12+
long last = times[0] + skill[0] * m;
13+
for (int i = 1; i < n; i++) {
14+
last = Math.max(last, times[i]) + skill[i] * m;
15+
}
16+
times[n-1] = last;
17+
for (int i = n - 2; i >= 0; i--) {
18+
times[i] = times[i + 1] - skill[i + 1] * m;
19+
}
20+
}
21+
return times[n-1];
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-10-09 22:52:05
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-10-09 23:11:26
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minTime(self, skill: List[int], mana: List[int]) -> int:
11+
n = len(skill)
12+
times = [0] * n
13+
for m in mana:
14+
lastTime = times[0] + skill[0] * m
15+
for i in range(1, n):
16+
lastTime = max(lastTime, times[i]) + skill[i] * m
17+
times[-1] = lastTime
18+
for i in range(n - 2, -1, -1):
19+
times[i] = times[i + 1] - skill[i + 1] * m
20+
return times[-1]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-09 22:52:05
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-09 23:18:22
6+
*/
7+
impl Solution {
8+
pub fn min_time(skill: Vec<i32>, mana: Vec<i32>) -> i64 {
9+
let n: usize = skill.len();
10+
let mut times: Vec<i64> = vec![0; n];
11+
for m in mana {
12+
let mut last_time: i64 = times[0] + (skill[0] * m) as i64;
13+
for i in 1..n {
14+
last_time = last_time.max(times[i]) + (skill[i] * m) as i64;
15+
}
16+
times[n-1] = last_time;
17+
for i in (0..n-1).rev() {
18+
times[i] = times[i + 1] - (skill[i + 1] * m) as i64;
19+
}
20+
}
21+
times[n-1]
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-10-09 22:19:32
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-10-09 23:01:17
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
typedef long long ll;
12+
class Solution {
13+
public:
14+
ll minTime(vector<int>& skill, vector<int>& mana) {
15+
vector<ll> times(skill.size());
16+
for (int m : mana) {
17+
ll nowFinish = times[0] + skill[0] * m;
18+
for (int i = 1; i < skill.size(); i++) {
19+
nowFinish = max(nowFinish, times[i]) + skill[i] * m;
20+
}
21+
times.back() = nowFinish;
22+
for (int i = skill.size() - 2; i >= 0; i--) {
23+
times[i] = times[i + 1] - skill[i + 1] * m;
24+
}
25+
}
26+
return times.back();
27+
}
28+
};

Codes/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2025-08-07 14:11:36
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-10-01 20:40:27
5+
* @LastEditTime: 2025-10-09 23:13:33
66
*/
77
pub struct Solution;
8-
include!("0778-swim-in-rising-water.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("3494-find-the-minimum-amount-of-time-to-brew-potions.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,7 @@
10331033
|3439.重新安排会议得到最多空余时间I|中等|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/13/LeetCode%203439.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E4%BC%9A%E8%AE%AE%E5%BE%97%E5%88%B0%E6%9C%80%E5%A4%9A%E7%A9%BA%E4%BD%99%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149317430" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/solutions/3722937/letmefly-3439zhong-xin-an-pai-hui-yi-de-r3t9e/" target="_blank">LeetCode题解</a>|
10341034
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
10351035
|3459.最长V形对角线段的长度|困难|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/31/LeetCode%203459.%E6%9C%80%E9%95%BFV%E5%BD%A2%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%AE%B5%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151050078" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/solutions/3768545/letmefly-3459zui-chang-v-xing-dui-jiao-x-gu8q/" target="_blank">LeetCode题解</a>|
1036+
|3494.酿造药水需要的最少总时间|中等|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/10/09/LeetCode%203494.%E9%85%BF%E9%80%A0%E8%8D%AF%E6%B0%B4%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E6%80%BB%E6%97%B6%E9%97%B4/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/152847907" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-minimum-amount-of-time-to-brew-potions/solutions/3801656/letmefly-3494niang-zao-yao-shui-xu-yao-d-bjxv/" target="_blank">LeetCode题解</a>|
10361037
|3508.设计路由器|中等|<a href="https://leetcode.cn/problems/implement-router/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/20/LeetCode%203508.%E8%AE%BE%E8%AE%A1%E8%B7%AF%E7%94%B1%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151901838" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/implement-router/solutions/3787451/letmefly-3508she-ji-lu-you-qi-stltao-stl-ehkf/" target="_blank">LeetCode题解</a>|
10371038
|3516.找到最近的人|简单|<a href="https://leetcode.cn/problems/find-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/04/LeetCode%203516.%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151184074" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-closest-person/solutions/3772009/letmefly-3516zhao-dao-zui-jin-de-ren-ji-8rlbz/" target="_blank">LeetCode题解</a>|
10381039
|3541.找到频率最高的元音和辅音|简单|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/13/LeetCode%203541.%E6%89%BE%E5%88%B0%E9%A2%91%E7%8E%87%E6%9C%80%E9%AB%98%E7%9A%84%E5%85%83%E9%9F%B3%E5%92%8C%E8%BE%85%E9%9F%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151653999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/solutions/3780744/letmefly-3541zhao-dao-pin-lu-zui-gao-de-yv5vs/" target="_blank">LeetCode题解</a>|

0 commit comments

Comments
 (0)