Skip to content

Commit b90d3cd

Browse files
authored
Solution to 1046 and 67 (#177)
1 parent b60c754 commit b90d3cd

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/unresolved/1046.last-stone-weight.jl renamed to src/problems/1046.last-stone-weight.jl

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 1046. Last Stone Weight
33
# id: problem1046
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Queensley E
5+
# date: 2024-12-11
66
# difficulty: Easy
77
# categories: Heap, Greedy
88
# link: <https://leetcode.com/problems/last-stone-weight/description/>
@@ -46,5 +46,31 @@
4646
## @lc code=start
4747
using LeetCode
4848

49+
using DataStructures
50+
51+
function last_stone_weight(stones::Vector{Int64})
52+
# Create a PriorityQueue with unique identifiers for each stone
53+
heap = PriorityQueue{Tuple{Int64, Int64}, Int64}()
54+
for (i, stone) in enumerate(stones)
55+
enqueue!(heap, (stone, i), -stone) # Use negative stone value for max-heap
56+
end
57+
58+
while length(heap) > 1
59+
# Extract the two largest stones
60+
largest, _ = dequeue!(heap)
61+
second_largest, _ = dequeue!(heap)
62+
63+
# If they are not the same, calculate the difference and enqueue it
64+
if largest != second_largest
65+
enqueue!(heap, (largest - second_largest, length(heap) + 1), -(largest - second_largest))
66+
end
67+
end
68+
69+
# Return the last stone or 0 if the heap is empty
70+
return isempty(heap) ? 0 : first(first(collect(keys(heap))))
71+
end
72+
73+
last_stone_weight([2, 7, 4, 1, 8, 2])
74+
4975
## add your code here:
5076
## @lc code=end

src/unresolved/67.add-binary.jl renamed to src/problems/67.add-binary.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,17 @@
4141
## @lc code=start
4242
using LeetCode
4343

44+
function add_binary(a::String, b::String)
45+
46+
num1 = parse(Int64, a; base=2)
47+
num2 = parse(Int64, b; base=2)
48+
49+
sum = num1 + num2
50+
51+
return string(sum, base=2)
52+
53+
end
54+
55+
4456
## add your code here:
4557
## @lc code=end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Test
2+
3+
@testset "1046.last-stone-weight.jl" begin
4+
5+
@test last_stone_weight([2, 7, 4, 1, 8, 1]) == 1 # Problem example
6+
@test last_stone_weight([1, 1]) == 0 # All stones cancel out
7+
@test last_stone_weight([5]) == 5 # Single stone
8+
@test last_stone_weight([10, 4, 3]) == 3 # Large differences
9+
@test last_stone_weight([8, 8, 2, 2, 3]) == 1 # Mixed case
10+
@test last_stone_weight([7, 6, 7, 6]) == 0 # Multiple canceling pairs
11+
12+
end

test/problems/67.add-binary.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Test
2+
@testset "67.add-binary.jl" begin
3+
4+
@test add_binary("0", "0") == "0"
5+
@test add_binary("1", "1") == "10"
6+
@test add_binary("1010", "1011") == "10101"
7+
@test add_binary("110", "11") == "1001"
8+
@test add_binary("1111", "1111") == "11110"
9+
@test add_binary("0", "101") == "101"
10+
@test add_binary("100000", "1") == "100001"
11+
@test add_binary("111111", "1") == "1000000"
12+
@test add_binary("101010", "110110") == "1100000"
13+
@test add_binary("1000000000", "1000000000") == "10000000000"
14+
15+
16+
end

0 commit comments

Comments
 (0)