Skip to content

Commit 4a4544c

Browse files
authored
add probelm 1245 (#171)
1 parent 4dd18a9 commit 4a4544c

8 files changed

+95
-11
lines changed

src/problems/1051.height-checker.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,6 @@ using LeetCode
6464

6565
function height_checker(heights::Vector{Int})
6666
sorted = sort(heights)
67-
count(i -> heights[i] != sorted[i], 1:length(heights))
67+
return count(i -> heights[i] != sorted[i], 1:length(heights))
6868
end
6969
## @lc code=end

src/problems/1054.distant-barcodes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ function rearrange_barcodes(barcodes::Vector{Int})
5858
idx > len && (idx = 2)
5959
end
6060
end
61-
res
61+
return res
6262
end
6363
## @lc code=end

src/problems/1245.tree-diameter.jl

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# ---
2+
# title: 1245. Tree Diameter
3+
# id: problem1245
4+
# author: Jerry Ling
5+
# date: 2023-06-08
6+
# difficulty: Medium
7+
# categories: Tree
8+
# link: <https://leetcode.com/problems/tree-diameter/>
9+
# hidden: true
10+
# ---
11+
#
12+
# Given an undirected tree, return its diameter: the number of edges in a longest path in that tree.
13+
#
14+
# The tree is given as an array of edges where edges[i] = [u, v] is a bidirectional edge between nodes u and v. Each node has labels in the set {0, 1, ..., edges.length}.
15+
#
16+
#
17+
# **Example 1:**
18+
#
19+
# Input: edges = [[0,1],[0,2]]
20+
# Output: 2
21+
# Explanation:
22+
# A longest path of the tree is the path 1 - 0 - 2.
23+
#
24+
#
25+
# **Example 2:**
26+
#
27+
# Input: edges = [[0,1],[1,2],[2,3],[1,4],[4,5]]
28+
# Output: 4
29+
# Explanation:
30+
# A longest path of the tree is the path 3 - 2 - 1 - 4 - 5.
31+
#
32+
#
33+
#
34+
# **Constraints:**
35+
#
36+
# * 0 <= edges.length < 10^4
37+
# * edges[i][0] != edges[i][1]
38+
# * 0 <= edges[i][j] <= edges.length
39+
#
40+
#
41+
## @lc code=start
42+
using LeetCode
43+
44+
function tree_diameter(edges::Vector{Vector{Int}})
45+
n = length(edges)
46+
neighbors = Dict(i => Int[] for i in 0:n)
47+
for edge in edges
48+
u, v = edge
49+
push!(neighbors[u], v)
50+
push!(neighbors[v], u)
51+
end
52+
53+
function depth_first(node, neighbors)
54+
# (current length, current node)
55+
longest = (0, node)
56+
57+
# each tuple is (current length, current node, previous node)
58+
todo = [(longest..., -1)]
59+
while !isempty(todo)
60+
len, this, prev = pop!(todo)
61+
if len > longest[1]
62+
longest = (len, this)
63+
end
64+
65+
for next in neighbors[this]
66+
next == prev && continue
67+
push!(todo, (len + 1, next, this))
68+
end
69+
end
70+
71+
return longest
72+
end
73+
74+
# we find one "end" of the eventual chain by doing depth first once
75+
# and a second time starting from the known end
76+
_, one_end = depth_first(0, neighbors)
77+
diameter, _ = depth_first(one_end, neighbors)
78+
79+
return diameter
80+
end
81+
## @lc code=end

src/problems/1513.number-of-substrings-with-only-1s.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,6 @@ using LeetCode
6767

6868
function num_sub(s::String)
6969
ss = split(s, '0')
70-
sum(binomial(length(subs) + 1, 2) for subs in ss)
70+
return sum(binomial(length(subs) + 1, 2) for subs in ss)
7171
end
7272
## @lc code=end

src/problems/904.fruit-into-baskets.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,6 @@ function total_fruit(fruits::Vector{Int})
9797
end
9898
res = max(res, i - j + 1)
9999
end
100-
res
100+
return res
101101
end
102102
## @lc code=end

test/problems/1245.tree-diameter.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@testset "1245.tree-diameter" begin
2+
@test tree_diameter([[0,1],[1,2],[2,3],[1,4],[4,5]]) == 4
3+
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@testset "886.possible-bipartition.jl" begin
2-
@test possible_bipartition(4, [[1,2],[1,3],[2,4]])
3-
@test !possible_bipartition(3, [[1,2],[1,3],[2,3]])
4-
@test !possible_bipartition(5, [[1,2],[2,3],[3,4],[4,5],[1,5]])
2+
@test possible_bipartition(4, [[1, 2], [1, 3], [2, 4]])
3+
@test !possible_bipartition(3, [[1, 2], [1, 3], [2, 3]])
4+
@test !possible_bipartition(5, [[1, 2], [2, 3], [3, 4], [4, 5], [1, 5]])
55
end
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@testset "904.fruit-into-baskets.jl" begin
2-
@test total_fruit([1,2,1]) == 3
3-
@test total_fruit([0,1,2,2]) == 3
4-
@test total_fruit([1,2,3,2,2]) == 4
5-
@test total_fruit([3,3,3,1,2,1,1,2,3,3,4]) == 5
2+
@test total_fruit([1, 2, 1]) == 3
3+
@test total_fruit([0, 1, 2, 2]) == 3
4+
@test total_fruit([1, 2, 3, 2, 2]) == 4
5+
@test total_fruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4]) == 5
66
end

0 commit comments

Comments
 (0)