Skip to content

2025-03-11 v. 8.9.1: added "1996. The Number of Weak Characters in the Game" #980

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
Mar 11, 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 @@ -724,6 +724,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1877. Minimize Maximum Pair Sum in Array | [Link](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/) | [Link](./lib/medium/1877_minimize_maximum_pair_sum_in_array.rb) | [Link](./test/medium/test_1877_minimize_maximum_pair_sum_in_array.rb) |
| 1910. Remove All Occurrences of a Substring | [Link](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/) | [Link](./lib/medium/1910_remove_all_occurrences_of_a_substring.rb) | [Link](./test/medium/test_1910_remove_all_occurrences_of_a_substring.rb) |
| 1985. Find the Kth Largest Integer in the Array | [Link](https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/) | [Link](./lib/medium/1985_find_the_kth_largest_integer_in_the_array.rb) | [Link](./test/medium/test_1985_find_the_kth_largest_integer_in_the_array.rb) |
| 1996. The Number of Weak Characters in the Game | [Link](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/) | [Link](./lib/medium/1996_the_number_of_weak_characters_in_the_game.rb) | [Link](./test/medium/test_1996_the_number_of_weak_characters_in_the_game.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) |
| 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) |
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.9.0'
s.version = '8.9.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
24 changes: 24 additions & 0 deletions lib/medium/1996_the_number_of_weak_characters_in_the_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
# @param {Integer[][]} properties
# @return {Integer}
def number_of_weak_characters(properties)
properties.sort_by! { |a| [a[0], -a[1]] }

result = 0
n = properties.length
max = properties[-1][1]

(n - 2).downto(0) do |i|
curr = properties[i][1]

if curr < max
result += 1
else
max = curr
end
end

result
end
45 changes: 45 additions & 0 deletions test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/1996_the_number_of_weak_characters_in_the_game'
require 'minitest/autorun'

class TheNumberOfWeakCharactersInTheGameTest < ::Minitest::Test
def test_default_one
assert_equal(
0,
number_of_weak_characters(
[
[5, 5],
[6, 3],
[3, 6]
]
)
)
end

def test_default_two
assert_equal(
1,
number_of_weak_characters(
[
[2, 2],
[3, 3]
]
)
)
end

def test_default_three
assert_equal(
1,
number_of_weak_characters(
[
[1, 5],
[10, 4],
[4, 3]
]
)
)
end
end