diff --git a/study-plan/data-structure/day-1/contains-duplicate.rb b/study-plan/data-structure/day-01/contains-duplicate.rb similarity index 100% rename from study-plan/data-structure/day-1/contains-duplicate.rb rename to study-plan/data-structure/day-01/contains-duplicate.rb diff --git a/study-plan/data-structure/day-1/contains-duplicate.swift b/study-plan/data-structure/day-01/contains-duplicate.swift similarity index 100% rename from study-plan/data-structure/day-1/contains-duplicate.swift rename to study-plan/data-structure/day-01/contains-duplicate.swift diff --git a/study-plan/data-structure/day-1/maximum-subarray.rb b/study-plan/data-structure/day-01/maximum-subarray.rb similarity index 100% rename from study-plan/data-structure/day-1/maximum-subarray.rb rename to study-plan/data-structure/day-01/maximum-subarray.rb diff --git a/study-plan/data-structure/day-02/merge-sorted-array.rb b/study-plan/data-structure/day-02/merge-sorted-array.rb new file mode 100644 index 0000000..5ab150e --- /dev/null +++ b/study-plan/data-structure/day-02/merge-sorted-array.rb @@ -0,0 +1,59 @@ +# @param {Integer[]} nums1 +# @param {Integer} m +# @param {Integer[]} nums2 +# @param {Integer} n +# @return {Void} Do not return anything, modify nums1 in-place instead. +def merge(nums1, m, nums2, n) + # shift numbers to right + i, k = m - 1, nums1.size - 1 + while i >= 0 do + nums1[k] = nums1[i] + i -= 1 + k -= 1 + end + + # merge them + i, j, k = nums1.size - m, 0, 0 + while i < nums1.size && j < n do + if nums1[i] < nums2[j] then + nums1[k] = nums1[i] + i += 1 + else + nums1[k] = nums2[j] + j += 1 + end + k += 1 + end + + while j < n do + nums1[k] = nums2[j] + j += 1 + k += 1 + end +end + +RSpec.describe "merge" do + it "nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 ~> [1,2,2,3,5,6]" do + nums1, m = [1,2,3,0,0,0], 3 + nums2, n = [2,5,6], 3 + + merge(nums1, m, nums2, n) + expect(nums1).to eq([1,2,2,3,5,6]) + end + + it "nums1 = [0], m = 0, nums2 = [1], n = 1 ~> [1]" do + nums1, m = [1], 1 + nums2, n = [], 0 + + merge(nums1, m, nums2, n) + expect(nums1).to eq([1]) + end + + it "[3, 3] for 6 is [0, 1]" do + nums1, m = [0], 0 + nums2, n = [1], 1 + + merge(nums1, m, nums2, n) + expect(nums1).to eq([1]) + end +end diff --git a/study-plan/data-structure/day-02/two-sum.rb b/study-plan/data-structure/day-02/two-sum.rb new file mode 100644 index 0000000..0ea7604 --- /dev/null +++ b/study-plan/data-structure/day-02/two-sum.rb @@ -0,0 +1,30 @@ +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer[]} +def two_sum(nums, target) + encountered = Hash.new + + nums.each_index { |i| + complement = encountered.assoc(target - nums[i]) + + if complement != nil then + return [complement[1], i] + else + encountered[nums[i]] = i + end + } +end + +RSpec.describe "two_sum from" do + it "[2, 7, 11, 15] for 9 is [0, 1]" do + expect(two_sum([2, 7, 11, 15], 9)).to eq([0, 1]) + end + + it "[3, 2, 4] for 6 is [1, 2]" do + expect(two_sum([3, 2, 4], 6)).to eq([1, 2]) + end + + it "[3, 3] for 6 is [0, 1]" do + expect(two_sum([3, 3], 6)).to eq([0, 1]) + end +end