diff --git a/study-plan/algorithm/day-1/binary-search.rb b/study-plan/algorithm/day-01/binary-search.rb similarity index 100% rename from study-plan/algorithm/day-1/binary-search.rb rename to study-plan/algorithm/day-01/binary-search.rb diff --git a/study-plan/algorithm/day-1/first-bad-version.rb b/study-plan/algorithm/day-01/first-bad-version.rb similarity index 100% rename from study-plan/algorithm/day-1/first-bad-version.rb rename to study-plan/algorithm/day-01/first-bad-version.rb diff --git a/study-plan/algorithm/day-1/search-insert-position.rb b/study-plan/algorithm/day-01/search-insert-position.rb similarity index 100% rename from study-plan/algorithm/day-1/search-insert-position.rb rename to study-plan/algorithm/day-01/search-insert-position.rb diff --git a/study-plan/algorithm/day-02/rotate-array.rb b/study-plan/algorithm/day-02/rotate-array.rb new file mode 100644 index 0000000..213fc16 --- /dev/null +++ b/study-plan/algorithm/day-02/rotate-array.rb @@ -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 diff --git a/study-plan/algorithm/day-02/squares-of-a-sorted-array.rb b/study-plan/algorithm/day-02/squares-of-a-sorted-array.rb new file mode 100644 index 0000000..649c2a3 --- /dev/null +++ b/study-plan/algorithm/day-02/squares-of-a-sorted-array.rb @@ -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