Skip to content

Commit 547daa6

Browse files
authored
2025-01-17 v. 8.1.4: added "973. K Closest Points to Origin"
2 parents 38385ac + c46c9ab commit 547daa6

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
648648
| 951. Flip Equivalent Binary Trees | [Link](https://leetcode.com/problems/flip-equivalent-binary-trees/) | [Link](./lib/medium/951_flip_equivalent_binary_trees.rb) | [Link](./test/medium/test_951_flip_equivalent_binary_trees.rb) |
649649
| 958. Check Completeness of a Binary Tree | [Link](https://leetcode.com/problems/check-completeness-of-a-binary-tree/) | [Link](./lib/medium/958_check_completeness_of_a_binary_tree.rb) | [Link](./test/medium/test_958_check_completeness_of_a_binary_tree.rb) |
650650
| 967. Numbers With Same Consecutive Differences | [Link](https://leetcode.com/problems/numbers-with-same-consecutive-differences/) | [Link](./lib/medium/967_numbers_with_same_consecutive_differences.rb) | [Link](./test/medium/test_967_numbers_with_same_consecutive_differences.rb) |
651+
| 973. K Closest Points to Origin | [Link](https://leetcode.com/problems/k-closest-points-to-origin/) | [Link](./lib/medium/973_k_closest_points_to_origin.rb) | [Link](./test/medium/test_973_k_closest_points_to_origin.rb) |
651652
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
652653
| 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) |
653654
| 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) |

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.1.3'
8+
s.version = '8.1.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/k-closest-points-to-origin/
4+
# @param {Integer[][]} points
5+
# @param {Integer} k
6+
# @return {Integer[][]}
7+
def k_closest(points, k)
8+
points.sort_by { |point| point[0] * point[0] + point[1] * point[1] }[0...k]
9+
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/973_k_closest_points_to_origin'
5+
require 'minitest/autorun'
6+
7+
class KClosestPointsToOriginTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[
11+
[-2, 2]
12+
],
13+
k_closest(
14+
[
15+
[1, 3],
16+
[-2, 2]
17+
],
18+
1
19+
)
20+
)
21+
end
22+
23+
def test_default_two
24+
assert_equal(
25+
[
26+
[3, 3],
27+
[-2, 4]
28+
],
29+
k_closest(
30+
[
31+
[3, 3],
32+
[5, -1],
33+
[-2, 4]
34+
],
35+
2
36+
)
37+
)
38+
end
39+
end

0 commit comments

Comments
 (0)