Skip to content

2025-03-21 v. 9.0.7: added "2161. Partition Array According to Given Pivot" #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2131. Longest Palindrome by Concatenating Two Letter Words | [Link](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/) | [Link](./lib/medium/2131_longest_palindrome_by_concatenating_two_letter_words.rb) | [Link](./test/medium/test_2131_longest_palindrome_by_concatenating_two_letter_words.rb) |
| 2149. Rearrange Array Elements by Sign | [Link](https://leetcode.com/problems/rearrange-array-elements-by-sign/) | [Link](./lib/medium/2149_rearrange_array_elements_by_sign.rb) | [Link](./test/medium/test_2149_rearrange_array_elements_by_sign.rb) |
| 2150. Find All Lonely Numbers in the Array | [Link](https://leetcode.com/problems/find-all-lonely-numbers-in-the-array/) | [Link](./lib/medium/2150_find_all_lonely_numbers_in_the_array.rb) | [Link](./test/medium/test_2150_find_all_lonely_numbers_in_the_array.rb) |
| 2161. Partition Array According to Given Pivot | [Link](https://leetcode.com/problems/partition-array-according-to-given-pivot/) | [Link](./lib/medium/2161_partition_array_according_to_given_pivot.rb) | [Link](./test/medium/test_2161_partition_array_according_to_given_pivot.rb) |
| 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) |
| 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) |
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '9.0.6'
s.version = '9.0.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
33 changes: 33 additions & 0 deletions lib/medium/2161_partition_array_according_to_given_pivot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# https://leetcode.com/problems/partition-array-according-to-given-pivot/
# @param {Integer[]} nums
# @param {Integer} pivot
# @return {Integer[]}
def pivot_array(nums, pivot)
result = ::Array.new(nums.size, 0)
left = 0
right = nums.size - 1
i = 0

while left < right && i < nums.size
if nums[i] < pivot
result[left] = nums[i]
left += 1
end

if nums[nums.size - i - 1] > pivot
result[right] = nums[nums.length - i - 1]
right -= 1
end

i += 1
end

while left <= right
result[left] = pivot
left += 1
end

result
end
27 changes: 27 additions & 0 deletions test/medium/test_2161_partition_array_according_to_given_pivot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/2161_partition_array_according_to_given_pivot'
require 'minitest/autorun'

class PartitionArrayAccordingToGivenPivotTest < ::Minitest::Test
def test_default_one
assert_equal(
[9, 5, 3, 10, 10, 12, 14],
pivot_array(
[9, 12, 5, 10, 14, 3, 10],
10
)
)
end

def test_default_two
assert_equal(
[-3, 2, 4, 3],
pivot_array(
[-3, 4, 3, 2],
2
)
)
end
end