Skip to content

2024-05-23 v. 5.7.3: added "2441. Largest Positive Integer That Exists With Its Negative" #630

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
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 @@ -433,3 +433,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2423. Remove Letter To Equalize Frequency | [Link](https://leetcode.com/problems/remove-letter-to-equalize-frequency/) | [Link](./lib/easy/2423_remove_letter_to_equalize_frequency.rb) |
| 2427. Number of Common Factors | [Link](https://leetcode.com/problems/number-of-common-factors/) | [Link](./lib/easy/2427_number_of_common_factors.rb) |
| 2432. The Employee That Worked on the Longest Task | [Link](https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/) | [Link](./lib/easy/2432_the_employee_that_worked_on_the_longest_task.rb) |
| 2441. Largest Positive Integer That Exists With Its Negative | [Link](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | [Link](./lib/easy/2441_largest_positive_integer_that_exists_with_its_negative.rb) |
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 = '5.7.2'
s.version = '5.7.3'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'set'

# https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/
# @param {Integer[]} nums
# @return {Integer}
def find_max_k(nums)
nums.sort!
set = nums.to_set

nums.reverse_each do |num|
break unless num.positive?

return num if set.include?(-num)
end

-1
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2441_largest_positive_integer_that_exists_with_its_negative'
require 'minitest/autorun'

class LargestPositiveIntegerThatExistsWithItsNegativeTest < ::Minitest::Test
def test_default
assert_equal(3, find_max_k([-1, 2, -3, 3]))
assert_equal(7, find_max_k([-1, 10, 6, 7, -7, 1]))
assert_equal(-1, find_max_k([-10, 8, 6, 7, -2, -3]))
end
end
Loading