Skip to content

Commit 27013bd

Browse files
authored
Merge pull request #400 from fartem/1636_Sort_Array_by_Increasing_Frequency
2023-11-01 v. 3.4.3: added "1636. Sort Array by Increasing Frequency"
2 parents d4963bb + b871056 commit 27013bd

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
289289
| 1619. Mean of Array After Removing Some Elements | [Link](https://leetcode.com/problems/mean-of-array-after-removing-some-elements/) | [Link](./lib/easy/1619_mean_of_array_after_removing_some_elements.rb) |
290290
| 1624. Largest Substring Between Two Equal Characters | [Link](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [Link](./lib/easy/1624_largest_substring_between_two_equal_characters.rb) |
291291
| 1629. Slowest Key | [Link](https://leetcode.com/problems/slowest-key/) | [Link](./lib/easy/1629_slowest_key.rb) |
292+
| 1636. Sort Array by Increasing Frequency | [Link](https://leetcode.com/problems/sort-array-by-increasing-frequency/) | [Link](./lib/easy/1636_sort_array_by_increasing_frequency.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 = '3.4.2'
8+
s.version = '3.4.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/sort-array-by-increasing-frequency/
4+
# @param {Integer[]} nums
5+
# @return {Integer[]}
6+
def frequency_sort(nums)
7+
nums
8+
.tally
9+
.sort { |a, b| a[1] == b[1] ? b[0] <=> a[0] : a[1] <=> b[1] }
10+
.sum([]) { |num, freq| [num] * freq }
11+
end
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+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/1636_sort_array_by_increasing_frequency'
5+
require 'minitest/autorun'
6+
7+
class SortArrayByIncreasingFrequencyTest < ::Minitest::Test
8+
def test_default
9+
assert_equal([3, 1, 1, 2, 2, 2], frequency_sort([1, 1, 2, 2, 2, 3]))
10+
assert_equal([1, 3, 3, 2, 2], frequency_sort([2, 3, 1, 3, 2]))
11+
assert_equal([5, -1, 4, 4, -6, -6, 1, 1, 1], frequency_sort([-1, 1, -6, 4, 5, -6, 1, 4, 1]))
12+
end
13+
end

0 commit comments

Comments
 (0)