Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0f90258

Browse files
committed
add new solutions
1 parent2c4fc40 commit0f90258

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

‎script/[134]加油站.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
2+
#
3+
# 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
4+
#
5+
# 给定两个整数数组 gas 和 cost ,如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。
6+
#
7+
#
8+
#
9+
# 示例 1:
10+
#
11+
#
12+
# 输入: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
13+
# 输出: 3
14+
# 解释:
15+
# 从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
16+
# 开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
17+
# 开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
18+
# 开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
19+
# 开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
20+
# 开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
21+
# 因此,3 可为起始索引。
22+
#
23+
# 示例 2:
24+
#
25+
#
26+
# 输入: gas = [2,3,4], cost = [3,4,3]
27+
# 输出: -1
28+
# 解释:
29+
# 你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。
30+
# 我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油
31+
# 开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油
32+
# 开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油
33+
# 你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。
34+
# 因此,无论怎样,你都不可能绕环路行驶一周。
35+
#
36+
#
37+
#
38+
# 提示:
39+
#
40+
#
41+
# gas.length == n
42+
# cost.length == n
43+
# 1 <= n <= 105
44+
# 0 <= gas[i], cost[i] <= 104
45+
#
46+
# Related Topics 贪心 数组
47+
# 👍 991 👎 0
48+
49+
50+
# leetcode submit region begin(Prohibit modification and deletion)
51+
classSolution:
52+
defcanCompleteCircuit(self,gas:List[int],cost:List[int])->int:
53+
ifsum(gas)<sum(cost):
54+
return-1
55+
delta=0
56+
pos=0
57+
foriinrange(len(gas)):
58+
delta+=gas[i]-cost[i]
59+
ifdelta<0:
60+
pos=i+1
61+
delta=0
62+
returnpos
63+
# leetcode submit region end(Prohibit modification and deletion)

‎script/[376]摆动序列.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为 摆动序列 。第一个差(如果存在的话)可能是正数或负数。仅有一个元素或者含两个不等元素的序列也
2+
# 视作摆动序列。
3+
#
4+
#
5+
#
6+
# 例如, [1, 7, 4, 9, 2, 5] 是一个 摆动序列 ,因为差值 (6, -3, 5, -7, 3) 是正负交替出现的。
7+
#
8+
# 相反,[1, 4, 7, 2, 5] 和 [1, 7, 4, 5, 5] 不是摆动序列,第一个序列是因为它的前两个差值都是正数,第二个序列是因为它的最后一
9+
# 个差值为零。
10+
#
11+
#
12+
# 子序列 可以通过从原始序列中删除一些(也可以不删除)元素来获得,剩下的元素保持其原始顺序。
13+
#
14+
# 给你一个整数数组 nums ,返回 nums 中作为 摆动序列 的 最长子序列的长度 。
15+
#
16+
#
17+
#
18+
# 示例 1:
19+
#
20+
#
21+
# 输入:nums = [1,7,4,9,2,5]
22+
# 输出:6
23+
# 解释:整个序列均为摆动序列,各元素之间的差值为 (6, -3, 5, -7, 3) 。
24+
#
25+
#
26+
# 示例 2:
27+
#
28+
#
29+
# 输入:nums = [1,17,5,10,13,15,10,5,16,8]
30+
# 输出:7
31+
# 解释:这个序列包含几个长度为 7 摆动序列。
32+
# 其中一个是 [1, 17, 10, 13, 10, 16, 8] ,各元素之间的差值为 (16, -7, 3, -3, 6, -8) 。
33+
#
34+
#
35+
# 示例 3:
36+
#
37+
#
38+
# 输入:nums = [1,2,3,4,5,6,7,8,9]
39+
# 输出:2
40+
#
41+
#
42+
#
43+
#
44+
# 提示:
45+
#
46+
#
47+
# 1 <= nums.length <= 1000
48+
# 0 <= nums[i] <= 1000
49+
#
50+
#
51+
#
52+
#
53+
# 进阶:你能否用 O(n) 时间复杂度完成此题?
54+
# Related Topics 贪心 数组 动态规划
55+
# 👍 737 👎 0
56+
57+
58+
# leetcode submit region begin(Prohibit modification and deletion)
59+
classSolution:
60+
defwiggleMaxLength(self,nums:List[int])->int:
61+
prediff,curdiff,counter=0,0,1
62+
foriinrange(1,len(nums)):
63+
curdiff=nums[i]-nums[i-1]
64+
if (curdiff>0andprediff<=0)or (curdiff<0andprediff>=0):
65+
prediff=curdiff
66+
counter+=1
67+
returncounter
68+
# leetcode submit region end(Prohibit modification and deletion)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp