From 9ee8d8951cbf3c9d94633012ce72d38c894136cb Mon Sep 17 00:00:00 2001 From: QueensleyC Date: Wed, 11 Dec 2024 20:29:53 +0100 Subject: [PATCH 1/3] Solution to 1046 --- .../1046.last-stone-weight.jl | 32 +++++++++++++++++-- test/problems/1046.last-stone-weight.jl | 12 +++++++ 2 files changed, 41 insertions(+), 3 deletions(-) rename src/{unresolved => problems}/1046.last-stone-weight.jl (58%) create mode 100644 test/problems/1046.last-stone-weight.jl diff --git a/src/unresolved/1046.last-stone-weight.jl b/src/problems/1046.last-stone-weight.jl similarity index 58% rename from src/unresolved/1046.last-stone-weight.jl rename to src/problems/1046.last-stone-weight.jl index 735548009..067b85d34 100644 --- a/src/unresolved/1046.last-stone-weight.jl +++ b/src/problems/1046.last-stone-weight.jl @@ -1,8 +1,8 @@ # --- # title: 1046. Last Stone Weight # id: problem1046 -# author: Tian Jun -# date: 2020-10-31 +# author: Queensley E +# date: 2024-12-11 # difficulty: Easy # categories: Heap, Greedy # link: @@ -44,7 +44,33 @@ # # ## @lc code=start -using LeetCode +# using LeetCode + +using DataStructures + +function last_stone_weight(stones::Vector{Int64}) + # Create a PriorityQueue with unique identifiers for each stone + heap = PriorityQueue{Tuple{Int64, Int64}, Int64}() + for (i, stone) in enumerate(stones) + enqueue!(heap, (stone, i), -stone) # Use negative stone value for max-heap + end + + while length(heap) > 1 + # Extract the two largest stones + largest, _ = dequeue!(heap) + second_largest, _ = dequeue!(heap) + + # If they are not the same, calculate the difference and enqueue it + if largest != second_largest + enqueue!(heap, (largest - second_largest, length(heap) + 1), -(largest - second_largest)) + end + end + + # Return the last stone or 0 if the heap is empty + return isempty(heap) ? 0 : first(first(collect(keys(heap)))) +end + +last_stone_weight([2, 7, 4, 1, 8, 2]) ## add your code here: ## @lc code=end diff --git a/test/problems/1046.last-stone-weight.jl b/test/problems/1046.last-stone-weight.jl new file mode 100644 index 000000000..704111508 --- /dev/null +++ b/test/problems/1046.last-stone-weight.jl @@ -0,0 +1,12 @@ +using Test + +@testset "1046.last-stone-weight.jl" begin + + @test last_stone_weight([2, 7, 4, 1, 8, 1]) == 1 # Problem example + @test last_stone_weight([1, 1]) == 0 # All stones cancel out + @test last_stone_weight([5]) == 5 # Single stone + @test last_stone_weight([10, 4, 3]) == 3 # Large differences + @test last_stone_weight([8, 8, 2, 2, 3]) == 1 # Mixed case + @test last_stone_weight([7, 6, 7, 6]) == 0 # Multiple canceling pairs + +end \ No newline at end of file From a29782d4f5f86093758f7fa42d7dcc1b4a1b9416 Mon Sep 17 00:00:00 2001 From: QueensleyC Date: Wed, 11 Dec 2024 20:34:51 +0100 Subject: [PATCH 2/3] Solution to 1046 --- src/problems/1046.last-stone-weight.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/problems/1046.last-stone-weight.jl b/src/problems/1046.last-stone-weight.jl index 067b85d34..4a6735e84 100644 --- a/src/problems/1046.last-stone-weight.jl +++ b/src/problems/1046.last-stone-weight.jl @@ -44,7 +44,7 @@ # # ## @lc code=start -# using LeetCode +using LeetCode using DataStructures From 5e01a5d2aee4a745123cd5398a27f5e53c5bbefe Mon Sep 17 00:00:00 2001 From: QueensleyC Date: Thu, 12 Dec 2024 18:35:10 +0100 Subject: [PATCH 3/3] Solution to 67 --- src/{unresolved => problems}/67.add-binary.jl | 12 ++++++++++++ test/problems/67.add-binary.jl | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) rename src/{unresolved => problems}/67.add-binary.jl (81%) create mode 100644 test/problems/67.add-binary.jl diff --git a/src/unresolved/67.add-binary.jl b/src/problems/67.add-binary.jl similarity index 81% rename from src/unresolved/67.add-binary.jl rename to src/problems/67.add-binary.jl index 48bf61b87..e418b7f9b 100644 --- a/src/unresolved/67.add-binary.jl +++ b/src/problems/67.add-binary.jl @@ -41,5 +41,17 @@ ## @lc code=start using LeetCode +function add_binary(a::String, b::String) + + num1 = parse(Int64, a; base=2) + num2 = parse(Int64, b; base=2) + + sum = num1 + num2 + + return string(sum, base=2) + +end + + ## add your code here: ## @lc code=end diff --git a/test/problems/67.add-binary.jl b/test/problems/67.add-binary.jl new file mode 100644 index 000000000..74d9ce77b --- /dev/null +++ b/test/problems/67.add-binary.jl @@ -0,0 +1,16 @@ +using Test +@testset "67.add-binary.jl" begin + + @test add_binary("0", "0") == "0" + @test add_binary("1", "1") == "10" + @test add_binary("1010", "1011") == "10101" + @test add_binary("110", "11") == "1001" + @test add_binary("1111", "1111") == "11110" + @test add_binary("0", "101") == "101" + @test add_binary("100000", "1") == "100001" + @test add_binary("111111", "1") == "1000000" + @test add_binary("101010", "110110") == "1100000" + @test add_binary("1000000000", "1000000000") == "10000000000" + + +end \ No newline at end of file