Skip to content

2025-01-24 v. 8.2.6: added "1038. Binary Search Tree to Greater Sum Tree" #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1019. Next Greater Node In Linked List | [Link](https://leetcode.com/problems/next-greater-node-in-linked-list/) | [Link](./lib/medium/1019_next_greater_node_in_linked_list.rb) | [Link](./test/medium/test_1019_next_greater_node_in_linked_list.rb) |
| 1023. Camelcase Matching | [Link](https://leetcode.com/problems/camelcase-matching/) | [Link](./lib/medium/1023_camelcase_matching.rb) | [Link](./test/medium/test_1023_camelcase_matching.rb) |
| 1026. Maximum Difference Between Node and Ancestor | [Link](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/) | [Link](./lib/medium/1026_maximum_difference_between_node_and_ancestor.rb) | [Link](./test/medium/test_1026_maximum_difference_between_node_and_ancestor.rb) |
| 1038. Binary Search Tree to Greater Sum Tree | [Link](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/) | [Link](./lib/medium/1038_binary_search_tree_to_greater_sum_tree.rb) | [Link](./test/medium/test_1038_binary_search_tree_to_greater_sum_tree.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.2.5.9'
s.version = '8.2.6'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
23 changes: 23 additions & 0 deletions lib/medium/1038_binary_search_tree_to_greater_sum_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
# @param {Integer[]} arr
# @param {Integer} k
# @return {Integer}
def max_sum_after_partitioning(arr, k)
partitioned = ::Array.new(arr.size + 1, 0)

(1..arr.size).each do |i|
max = 0
val = 0

[i, k].min.times do |j|
max = [max, arr[i - j - 1]].max
val = [partitioned[i - j - 1] + max * (j + 1), val].max
end

partitioned[i] = val
end

partitioned[arr.size]
end
37 changes: 37 additions & 0 deletions test/medium/test_1038_binary_search_tree_to_greater_sum_tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/1038_binary_search_tree_to_greater_sum_tree'
require 'minitest/autorun'

class BinarySearchTreeToGreaterSumTreeTest < ::Minitest::Test
def test_default_one
assert_equal(
84,
max_sum_after_partitioning(
[1, 15, 7, 9, 2, 5, 10],
3
)
)
end

def test_default_two
assert_equal(
83,
max_sum_after_partitioning(
[1, 4, 1, 5, 7, 3, 6, 1, 9, 9, 3],
4
)
)
end

def test_default_three
assert_equal(
1,
max_sum_after_partitioning(
[1],
1
)
)
end
end
Loading