algorithm(i): add day 2 and rename day 1
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
c1a688bf2f
commit
2a40331d5d
5 changed files with 125 additions and 0 deletions
68
study-plan/algorithm/day-02/rotate-array.rb
Normal file
68
study-plan/algorithm/day-02/rotate-array.rb
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
# @param {Integer[]} nums
|
||||||
|
# @param {Integer} k
|
||||||
|
# @return {Void} Do not return anything, modify nums in-place instead.
|
||||||
|
def rotate(nums, k)
|
||||||
|
def reverse(nums, from, to)
|
||||||
|
while from < to do
|
||||||
|
nums[from], nums[to] = nums[to], nums[from]
|
||||||
|
|
||||||
|
from += 1
|
||||||
|
to -= 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
k = k % nums.size
|
||||||
|
reverse(nums, 0, nums.size - 1)
|
||||||
|
reverse(nums, 0, k - 1)
|
||||||
|
reverse(nums, k, nums.size - 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec.describe "rotate" do
|
||||||
|
it "nums = [1,2,3,4,5,6,7], k = 0" do
|
||||||
|
nums = [1,2,3,4,5,6,7]
|
||||||
|
k = 0
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([1,2,3,4,5,6,7])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [1,2,3,4,5,6,7], k = 1" do
|
||||||
|
nums = [1,2,3,4,5,6,7]
|
||||||
|
k = 1
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([7,1,2,3,4,5,6])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [1,2,3,4,5,6,7], k = 2" do
|
||||||
|
nums = [1,2,3,4,5,6,7]
|
||||||
|
k = 2
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([6,7,1,2,3,4,5])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [1,2,3,4,5,6,7], k = 3" do
|
||||||
|
nums = [1,2,3,4,5,6,7]
|
||||||
|
k = 3
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([5,6,7,1,2,3,4])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [-1,-100,3,99], k = 1" do
|
||||||
|
nums = [-1,-100,3,99]
|
||||||
|
k = 1
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([99,-1,-100,3])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [-1,-100,3,99], k = 2" do
|
||||||
|
nums = [-1,-100,3,99]
|
||||||
|
k = 2
|
||||||
|
|
||||||
|
rotate(nums, k)
|
||||||
|
expect(nums).to eq([3,99,-1,-100])
|
||||||
|
end
|
||||||
|
end
|
57
study-plan/algorithm/day-02/squares-of-a-sorted-array.rb
Normal file
57
study-plan/algorithm/day-02/squares-of-a-sorted-array.rb
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
# @param {Integer[]} nums
|
||||||
|
# @return {Integer[]}
|
||||||
|
def sorted_squares(nums)
|
||||||
|
result = []
|
||||||
|
|
||||||
|
split = nums.find_index { |x| x >= 0 }
|
||||||
|
|
||||||
|
i, j = nil, nil
|
||||||
|
if split == nil then
|
||||||
|
i, j = nums.size - 1, nums.size
|
||||||
|
else
|
||||||
|
i, j = split - 1, split
|
||||||
|
end
|
||||||
|
|
||||||
|
while i >= 0 and j < nums.size do
|
||||||
|
i_s = nums[i] * nums[i]
|
||||||
|
j_s = nums[j] * nums[j]
|
||||||
|
|
||||||
|
if i_s < j_s then
|
||||||
|
result.push(i_s)
|
||||||
|
i -= 1
|
||||||
|
else
|
||||||
|
result.push(j_s)
|
||||||
|
j += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
while i >= 0 do
|
||||||
|
result.push(nums[i] * nums[i])
|
||||||
|
i -= 1
|
||||||
|
end
|
||||||
|
|
||||||
|
while j < nums.size do
|
||||||
|
result.push(nums[j] * nums[j])
|
||||||
|
j += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
RSpec.describe "rotate" do
|
||||||
|
it "nums = [-4,-1,0,3,10]" do
|
||||||
|
expect(sorted_squares([-4,-1,0,3,10])).to eq([0,1,9,16,100])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [-7,-3,2,3,11]" do
|
||||||
|
expect(sorted_squares([-7,-3,2,3,11])).to eq([4,9,9,49,121])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [-7,-3,0,0,0,0,2,3,11]" do
|
||||||
|
expect(sorted_squares([-7,-3,0,0,0,0,2,3,11])).to eq([0,0,0,0,4,9,9,49,121])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "nums = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]" do
|
||||||
|
expect(sorted_squares([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])).to eq([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1])
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue