Skip to content

Commit d03b960

Browse files
authored
Merge pull request #630 from fartem/2441_Largest_Positive_Integer_That_Exists_With_Its_Negative
2024-05-23 v. 5.7.3: added "2441. Largest Positive Integer That Exists With Its Negative"
2 parents 802373b + fe1aaf7 commit d03b960

4 files changed

+34
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
433433
| 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) |
434434
| 2427. Number of Common Factors | [Link](https://leetcode.com/problems/number-of-common-factors/) | [Link](./lib/easy/2427_number_of_common_factors.rb) |
435435
| 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) |
436+
| 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) |

leetcode-ruby.gemspec

+1-1
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 = '5.7.2'
8+
s.version = '5.7.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require 'set'
4+
5+
# https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/
6+
# @param {Integer[]} nums
7+
# @return {Integer}
8+
def find_max_k(nums)
9+
nums.sort!
10+
set = nums.to_set
11+
12+
nums.reverse_each do |num|
13+
break unless num.positive?
14+
15+
return num if set.include?(-num)
16+
end
17+
18+
-1
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2441_largest_positive_integer_that_exists_with_its_negative'
5+
require 'minitest/autorun'
6+
7+
class LargestPositiveIntegerThatExistsWithItsNegativeTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(3, find_max_k([-1, 2, -3, 3]))
10+
assert_equal(7, find_max_k([-1, 10, 6, 7, -7, 1]))
11+
assert_equal(-1, find_max_k([-10, 8, 6, 7, -2, -3]))
12+
end
13+
end

0 commit comments

Comments
 (0)