From 739971da1ef3aa33bf872b469c7b0c50458ee136 Mon Sep 17 00:00:00 2001 From: fartem Date: Tue, 11 Mar 2025 08:34:06 +0300 Subject: [PATCH] 2025-03-11 v. 8.9.1: added "1996. The Number of Weak Characters in the Game" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...e_number_of_weak_characters_in_the_game.rb | 24 ++++++++++ ...e_number_of_weak_characters_in_the_game.rb | 45 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1996_the_number_of_weak_characters_in_the_game.rb create mode 100644 test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb diff --git a/README.md b/README.md index 9da62d7b..84cfc5fc 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index d78165dd..d9c451b0 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/medium/1996_the_number_of_weak_characters_in_the_game.rb b/lib/medium/1996_the_number_of_weak_characters_in_the_game.rb new file mode 100644 index 00000000..ddd425ee --- /dev/null +++ b/lib/medium/1996_the_number_of_weak_characters_in_the_game.rb @@ -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 diff --git a/test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb b/test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb new file mode 100644 index 00000000..96582650 --- /dev/null +++ b/test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb @@ -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