Skip to content

Commit db2c835

Browse files
authored
2025-02-20 v. 8.6.5: added "1481. Least Number of Unique Integers after K Removals"
2 parents 5da2693 + 122c90c commit db2c835

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
698698
| 1457. Pseudo-Palindromic Paths in a Binary Tree | [Link](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | [Link](./lib/medium/1457_pseudo_palindromic_paths_in_a_binary_tree.rb) | [Link](./test/medium/test_1457_pseudo_palindromic_paths_in_a_binary_tree.rb) |
699699
| 1461. Check If a String Contains All Binary Codes of Size K | [Link](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Link](./lib/medium/1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) | [Link](./test/medium/test_1461_check_if_a_string_contains_all_binary_codes_of_size_k.rb) |
700700
| 1472. Design Browser History | [Link](https://leetcode.com/problems/design-browser-history/) | [Link](./lib/medium/1472_design_browser_history.rb) | [Link](./test/medium/test_1472_design_browser_history.rb) |
701+
| 1481. Least Number of Unique Integers after K Removals | [Link](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/) | [Link](./lib/medium/1481_least_number_of_unique_integers_after_k_removals.rb) | [Link](./test/medium/test_1481_least_number_of_unique_integers_after_k_removals.rb) |
701702
| 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) |
702703
| 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) |
703704
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.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.6.4'
8+
s.version = '8.6.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/
4+
# @param {Integer[]} arr
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def find_least_num_of_unique_ints(arr, k)
8+
nums_with_count = ::Hash.new(0).tap { |h| arr.each { |num| h[num] += 1 } }
9+
to_sort = nums_with_count.values.sort
10+
11+
count = 0
12+
p = 0
13+
14+
while k.positive?
15+
c = to_sort[p]
16+
17+
count += 1 if k >= c
18+
19+
k -= c
20+
p += 1
21+
end
22+
23+
nums_with_count.size - count
24+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1481_least_number_of_unique_integers_after_k_removals'
5+
require 'minitest/autorun'
6+
7+
class LeastNumberOfUniqueIntegersAfterKRemovalsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
1,
11+
find_least_num_of_unique_ints(
12+
[5, 5, 4],
13+
1
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
2,
21+
find_least_num_of_unique_ints(
22+
[4, 3, 1, 1, 3, 3, 2],
23+
3
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)