Skip to content

Commit daec02c

Browse files
authored
2025-02-07 v. 8.4.7: added "1338. Reduce Array Size to The Half"
2 parents 6958750 + 374b935 commit daec02c

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
679679
| 1315. Sum of Nodes with Even-Valued Grandparent | [Link](https://leetcode.com/problems/sum-of-nodes-with-even-valued-grandparent/) | [Link](./lib/medium/1315_sum_of_nodes_with_even_valued_grandparent.rb) | [Link](./test/medium/test_1315_sum_of_nodes_with_even_valued_grandparent.rb) |
680680
| 1325. Delete Leaves With a Given Value | [Link](https://leetcode.com/problems/delete-leaves-with-a-given-value/) | [Link](./lib/medium/1325_delete_leaves_with_a_given_value.rb) | [Link](./test/medium/test_1325_delete_leaves_with_a_given_value.rb) |
681681
| 1329. Sort the Matrix Diagonally | [Link](https://leetcode.com/problems/sort-the-matrix-diagonally/) | [Link](./lib/medium/1329_sort_the_matrix_diagonally.rb) | [Link](./test/medium/test_1329_sort_the_matrix_diagonally.rb) |
682+
| 1338. Reduce Array Size to The Half | [Link](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Link](./lib/medium/1338_reduce_array_size_to_the_half.rb) | [Link](./test/medium/test_1338_reduce_array_size_to_the_half.rb) |
682683
| 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) |
683684
| 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) |
684685
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.4.6'
8+
s.version = '8.4.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/reduce-array-size-to-the-half/
4+
# @param {Integer[]} arr
5+
# @return {Integer}
6+
def min_set_size(arr)
7+
return 1 if arr.size < 3
8+
9+
frequency_map = ::Hash.new(0)
10+
arr.each { |num| frequency_map[num] += 1 }
11+
12+
max_heap = frequency_map.values.sort.reverse
13+
14+
r = 0
15+
result = 0
16+
17+
while r < arr.size / 2 && !max_heap.empty?
18+
r += max_heap.shift
19+
result += 1
20+
end
21+
22+
result
23+
end

test/medium/test_1329_sort_the_matrix_diagonally.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require 'minitest/autorun'
66

77
class SortTheMatrixDiagonallyTest < ::Minitest::Test
8-
def test_diagonal_sort_small_matrix
8+
def test_default_one
99
assert_equal(
1010
[
1111
[1, 1, 1, 1],
@@ -22,7 +22,7 @@ def test_diagonal_sort_small_matrix
2222
)
2323
end
2424

25-
def test_diagonal_sort_large_matrix
25+
def test_default_two
2626
assert_equal(
2727
[
2828
[5, 17, 4, 1, 52, 7],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1338_reduce_array_size_to_the_half'
5+
require 'minitest/autorun'
6+
7+
class ReduceArraySizeToTheHalfTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
2,
11+
min_set_size(
12+
[3, 3, 3, 3, 5, 5, 5, 2, 2, 7]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
1,
20+
min_set_size(
21+
[7, 7, 7, 7, 7, 7]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)