mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
data-structure(i): add day 2 and rename day 1
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
5d50c507d8
commit
5d622d4583
5 changed files with 89 additions and 0 deletions
59
study-plan/data-structure/day-02/merge-sorted-array.rb
Normal file
59
study-plan/data-structure/day-02/merge-sorted-array.rb
Normal file
|
@ -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
|
30
study-plan/data-structure/day-02/two-sum.rb
Normal file
30
study-plan/data-structure/day-02/two-sum.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue