Skip to content

Commit 0d59d62

Browse files
authored
2025-02-28 v. 8.7.7: added "1679. Max Number of K-Sum Pairs"
2 parents b74eacd + 094eafc commit 0d59d62

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
@@ -710,6 +710,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
710710
| 1641. Count Sorted Vowel Strings | [Link](https://leetcode.com/problems/count-sorted-vowel-strings/) | [Link](./lib/medium/1641_count_sorted_vowel_strings.rb) | [Link](./test/medium/test_1641_count_sorted_vowel_strings.rb) |
711711
| 1669. Merge In Between Linked Lists | [Link](https://leetcode.com/problems/merge-in-between-linked-lists/) | [Link](./lib/medium/1669_merge_in_between_linked_lists.rb) | [Link](./test/medium/test_1669_merge_in_between_linked_lists.rb) |
712712
| 1670. Design Front Middle Back Queue | [Link](https://leetcode.com/problems/design-front-middle-back-queue/) | [Link](./lib/medium/1670_design_front_middle_back_queue.rb) | [Link](./test/medium/test_1670_design_front_middle_back_queue.rb) |
713+
| 1679. Max Number of K-Sum Pairs | [Link](https://leetcode.com/problems/max-number-of-k-sum-pairs/) | [Link](./lib/medium/1679_max_number_of_k_sum_pairs.rb) | [Link](./test/medium/test_1679_max_number_of_k_sum_pairs.rb) |
713714
| 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) |
714715
| 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) |
715716
| 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.7.6'
8+
s.version = '8.7.7'
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/max-number-of-k-sum-pairs/
4+
# @param {Integer[]} nums
5+
# @param {Integer} k
6+
# @return {Integer}
7+
def max_operations(nums, k)
8+
count = {}
9+
result = 0
10+
11+
nums.each do |num|
12+
s = k - num
13+
s_count = count.fetch(s, 0)
14+
15+
if s_count.positive?
16+
result += 1
17+
count[s] = s_count - 1
18+
else
19+
count[num] = count.fetch(num, 0) + 1
20+
end
21+
end
22+
23+
result
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/1679_max_number_of_k_sum_pairs'
5+
require 'minitest/autorun'
6+
7+
class MaxNumberOfKSumPairsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
2,
11+
max_operations(
12+
[1, 2, 3, 4],
13+
5
14+
)
15+
)
16+
end
17+
18+
def test_default_two
19+
assert_equal(
20+
1,
21+
max_operations(
22+
[3, 1, 3, 4, 3],
23+
6
24+
)
25+
)
26+
end
27+
end

0 commit comments

Comments
 (0)