Skip to content

Commit 18627c3

Browse files
committed
2024-05-17 v. 5.6.9: added "2418. Sort the People"
1 parent 4b972cc commit 18627c3

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
429429
| 2399. Check Distances Between Same Letters | [Link](https://leetcode.com/problems/check-distances-between-same-letters/) | [Link](./lib/easy/2399_check_distances_between_same_letters.rb) |
430430
| 2404. Most Frequent Even Element | [Link](https://leetcode.com/problems/most-frequent-even-element/) | [Link](./lib/easy/2404_most_frequent_even_element.rb) |
431431
| 2413. Smallest Even Multiple | [Link](https://leetcode.com/problems/smallest-even-multiple/) | [Link](./lib/easy/2413_smallest_even_multiple.rb) |
432+
| 2418. Sort the People | [Link](https://leetcode.com/problems/sort-the-people/) | [Link](./lib/easy/2418_sort_the_people.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 = '5.6.8'
8+
s.version = '5.6.9'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/easy/2418_sort_the_people.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/sort-the-people/
4+
# @param {String[]} names
5+
# @param {Integer[]} heights
6+
# @return {String[]}
7+
def sort_people(names, heights)
8+
values = {}
9+
(0...heights.length).each { |i| values[heights[i]] = i }
10+
heights.sort!
11+
12+
heights.map { |height| names[values[height]] }.reverse
13+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/2418_sort_the_people'
5+
require 'minitest/autorun'
6+
7+
class SortThePeopleTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(%w[Mary Emma John], sort_people(%w[Mary John Emma], [180, 165, 170]))
10+
assert_equal(%w[Bob Alice Bob], sort_people(%w[Alice Bob Bob], [155, 185, 150]))
11+
end
12+
end

0 commit comments

Comments
 (0)