Skip to content

Commit 4c96f24

Browse files
committed
Solution to 1024
1 parent ce6025a commit 4c96f24

4 files changed

+49
-7
lines changed

src/problems/1008.construct-binary-search-tree-from-preorder-traversal.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function bst_from_preorder(preorder::AbstractVector{Int})
5353
end
5454
i -= 1
5555
root.left = bst_from_preorder(@view preorder[2:i])
56-
root.right = bst_from_preorder(@view preorder[i+1:end])
57-
root
56+
root.right = bst_from_preorder(@view preorder[(i + 1):end])
57+
return root
5858
end
5959
## @lc code=end

src/unresolved/1024.video-stitching.jl renamed to src/problems/1024.video-stitching.jl

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1024. Video Stitching
33
# id: problem1024
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Indigo
5+
# date: 2022-04-11
66
# difficulty: Medium
77
# categories: Dynamic Programming
88
# link: <https://leetcode.com/problems/video-stitching/description/>
@@ -78,5 +78,20 @@
7878
## @lc code=start
7979
using LeetCode
8080

81-
## add your code here:
81+
function video_stitching(clips::Vector{Vector{Int}}, time::Int)
82+
max_next = fill(0, time)
83+
last, prev, res = 1, 1, 0
84+
for it in clips
85+
max_next[it[1] + 1] = max(max_next[it[1] + 1], it[2] + 1)
86+
end
87+
for i in 1:time
88+
last = max(last, max_next[i])
89+
i == last && return -1
90+
if i == prev
91+
res += 1
92+
prev = last
93+
end
94+
end
95+
return res
96+
end
8297
## @lc code=end

src/problems/91.decode-ways.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ function num_decodings(s)
7979
dp[1] = 1
8080
for i in 1:len
8181
s[i] != '0' && (dp[i + 1] += dp[i])
82-
(i > 1 && s[i - 1] != '0' && parse(Int, s[i-1:i]) <= 26) && (dp[i + 1] += dp[i - 1])
82+
(i > 1 && s[i - 1] != '0' && parse(Int, s[(i - 1):i]) <= 26) &&
83+
(dp[i + 1] += dp[i - 1])
8384
end
84-
dp[end]
85+
return dp[end]
8586
end
8687
## @lc code=end

test/problems/1024.video-stitching.jl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@testset "1024.video-stitching.jl" begin
2+
@test video_stitching([[0, 4], [2, 8]], 5) == 2
3+
@test video_stitching(
4+
[
5+
[0, 1],
6+
[6, 8],
7+
[0, 2],
8+
[5, 6],
9+
[0, 4],
10+
[0, 3],
11+
[6, 7],
12+
[1, 3],
13+
[4, 7],
14+
[1, 4],
15+
[2, 5],
16+
[2, 6],
17+
[3, 4],
18+
[4, 5],
19+
[5, 7],
20+
[6, 9],
21+
],
22+
9,
23+
) == 3
24+
@test video_stitching([[0, 1], [1, 2]], 5) == -1
25+
@test video_stitching([[0, 2], [4, 6], [8, 10], [1, 9], [1, 5], [5, 9]], 10) == 3
26+
end

0 commit comments

Comments
 (0)