diff --git a/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp b/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp deleted file mode 100644 index 599bf05..0000000 --- a/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -using std::string; -using std::vector; - -class Solution { - public: - vector divideString(string s, int k, char fill) { - vector result; - - for (int i = 0; i < s.size(); i += k) { - result.push_back(s.substr(i, k)); - } - - auto size_of_last = result.back().size(); - if (size_of_last < k) { - result.back().append(k - size_of_last, fill); - } - - return result; - } -}; diff --git a/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp b/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp new file mode 120000 index 0000000..d2eb76d --- /dev/null +++ b/contest/weekly-contest-276/divide-a-string-into-groups-of-size-k.cpp @@ -0,0 +1 @@ +../../problems/divide-a-string-into-groups-of-size-k.cpp \ No newline at end of file diff --git a/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp b/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp deleted file mode 100644 index 8672af8..0000000 --- a/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include - -using std::vector; - -class Solution { - public: - long long maxRunTime(int n, vector &batteries) { - vector bats{batteries}; - - std::sort(bats.begin(), bats.end()); - long long total = std::accumulate(bats.begin(), bats.end(), 0L); - - long long k; - for (k = 0; bats.back() > total / (n - k); k++) { - total -= bats.back(); - bats.pop_back(); - } - return total / (n - k); - } -}; - -int main() { - Solution s; - vector batteries; - - batteries = {3, 3, 3}; - assert(s.maxRunTime(2, batteries) == 4); - - batteries = {1, 1, 1, 1}; - assert(s.maxRunTime(2, batteries) == 2); - - batteries = {10, 10, 3, 5}; - assert(s.maxRunTime(3, batteries) == 8); - - batteries = { - 6251, 4133, 9898, 1536, 5475, 6340, 7430, 4413, 2558, 3941, 6934, 7326, - 7948, 7736, 1408, 5752, 836, 4511, 7684, 3939, 1376, 2305, 2090, 8419, - 3813, 4430, 890, 4120, 3415, 9706, 879, 158, 2239, 5462, 5773, 5285, - 5540, 305, 2211, 691, 4335, 5912, 3542, 5229, 996, 2609, 2173, 87, - 5683, 2946, 1456, 9590, 3625, 1807, 6909, 1328, 1548, 8182, 1690, 7440, - 8310, 8502, 320, 2654, 2254, 1306, 7696, 7187, 3977, 3270, 2989, 1629, - 5725, 7937, 5317, 9763, 9238, 3712, 1925, 2, 1463, 6309, 4997, 7786, - 1811, 7579, 3290, 8355, 63, 5010, 3574, 5364, 748, 1877, 106, 1735, - 7809, 5060, 9677, 4831, 1524, 9663, 6557, 9399, 5976, 801, 8800, 4297, - 9636, 4828, 3972, 6946, 6170, 9984, 5710, 3318, 4156, 7838, 6856, 2866, - 5900, 4623, 5228, 8063, 2514, 9149, 3509, 4033, 854, 2884, 7160, 8195, - 1936, 8134, 4277, 9442, 5263, 555, 5515, 2341, 2820, 3095, 2974, 7648, - 9116, 6886, 8545, 4055, 2398, 2425, 4861, 2114, 9280, 5045, 3678, 6569, - 7948, 1912, 7856, 6831, 4286, 1645, 9654, 5552, 880, 1864, 6386, 6616, - 3961, 7427, 8649, 2323, 8084, 7334, 8256, 6187, 2244, 9738, 6869, 5888, - 3862, 886, 1419, 2067, 15, 428, 4732, 2098, 7710, 5586, 1972, 5388, - 5171, 9239, 4810, 2461, 3639, 8120, 5296, 9522, 3475, 5703, 2358, 1957, - 9823, 6561, 3690, 1856, 8202, 1343, 1868, 5851, 3177, 7036, 4901, 4891, - 4640, 5927, 7610, 9204, 3538, 7508, 411, 3814, 7545, 7094, 6841, 3125, - 2884, 2205, 8271, 9920, 2322, 20, 8850, 1747, 3317, 2873, 558, 7201, - 6688, 9097, 9399, 2096, 5846, 9502, 5028, 2907, 1667, 4049, 5810, 5850, - 4628, 4511, 2415, 1194, 6477, 9198, 6645, 1281, 3920, 7117, 3301, 1892, - 4402, 8180, 2915, 3548, 211, 5855, 2190, 6552, 5992, 7280, 1291, 4064, - 6139, 9585, 6729, 3481, 5274, 2498, 9484, 3486, 863, 1913, 2921, 4733, - 544, 62, 5139, 4408, 6174, 9175, 1119, 7858, 6215, 2901, 4862, 4020, - 7371, 2778, 3086, 6559, 9187, 7591, 6007, 8762, 3577, 8984, 6653, 4975, - 3939, 1385, 4076, 6350, 3248, 3102, 8331, 194, 9552, 409, 1527, 6387, - 8477, 5410, 3223, 1168, 6578, 4004, 3442, 1767, 5979, 8644, 4893, 8495, - 5152, 5450, 2221, 7171, 8308, 6288, 8536, 5671, 6642, 6999, 4134, 5572, - 3639, 884, 9915, 7104, 8288, 994, 9765, 9467, 627, 2549, 2237, 713, - 9487, 9727, 569, 5755, 2975, 5248, 894, 3083, 7820, 7696, 4780, 952, - 9787, 6791, 1939, 682, 2917, 6001, 1781, 2712, 5098, 2002, 5641, 9202, - 7651, 4817, 7685, 3900, 8720, 7808, 8628, 2196, 4155, 3357, 7890, 1361, - 7537, 6157, 7107, 2972, 5548, 2297, 9888, 6257, 2596, 9319, 850, 2181, - 2284, 6877, 9344, 1177, 2238, 5140, 6476, 6289, 2673, 3266, 5291, 3098, - 541, 81, 8251, 8235, 8118, 8717, 2289, 4159, 2926, 478, 9137, 3921, - 2591, 669, 8164, 6245, 5742, 6076, 3839, 4243, 4656, 2448, 1168, 4481, - 3734, 2909, 4499, 1479, 4204, 3693, 5440, 2921, 4044, 9615, 7430, 2716, - 7520, 3354, 3683, 6058, 4212, 4940, 5645, 5101, 9339, 2422, 3201, 813, - 2753, 8796, 5485, 1080, 4433, 3875, 831, 33, 1689, 6643, 5753, 7352, - 1670, 2525, 3606, 34, 5768, 5256, 531, 427, 4931, 8174, 8413, 6014, - 3464, 3793, 6932, 3943, 7916, 3954, 1452, 4165, 4047, 2844, 9685, 6882, - 9535, 4995, 7836, 5296, 1140, 2403, 7723, 4388, 4791, 4967, 3788, 3694, - 9258, 370, 7174, 8340, 9182, 7522, 7852, 242, 1429, 5904, 7794, 7736, - 2421, 3885, 819, 3136, 8815, 5737, 2043, 6398, 9528, 9011, 5056, 7761, - 6120, 9561, 6053, 8264, 1648, 4598, 3448, 2563, 4717, 9078, 5032, 1163, - 7788, 2227, 4478, 4172}; - assert(s.maxRunTime(13, batteries) == 207113); - - return 0; -} diff --git a/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp b/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp new file mode 120000 index 0000000..b568f78 --- /dev/null +++ b/contest/weekly-contest-276/maximum-running-time-of-n-computers.cpp @@ -0,0 +1 @@ +../../problems/maximum-running-time-of-n-computers.cpp \ No newline at end of file diff --git a/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp b/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp deleted file mode 100644 index cd79bc3..0000000 --- a/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -class Solution { - public: - int minMoves(int target, int maxDoubles) { - int moves; - for (moves = 0; target > 1 && maxDoubles > 0; moves++) { - if (target % 2 == 0) { - maxDoubles--; - target /= 2; - } else { - target--; - } - } - - // remaining increments - moves += target - 1; - - return moves; - } -}; - -int main() { - Solution s; - - assert(s.minMoves(5, 0) == 4); - assert(s.minMoves(19, 2) == 7); - assert(s.minMoves(10, 4) == 4); - assert(s.minMoves(766972377, 92)); - assert(s.minMoves(1000000000, 5)); - - return 0; -} diff --git a/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp b/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp new file mode 120000 index 0000000..2c1fbf3 --- /dev/null +++ b/contest/weekly-contest-276/minimum-moves-to-reach-target-score.cpp @@ -0,0 +1 @@ +../../problems/minimum-moves-to-reach-target-score.cpp \ No newline at end of file diff --git a/contest/weekly-contest-276/solving-questions-with-brainpower.cpp b/contest/weekly-contest-276/solving-questions-with-brainpower.cpp deleted file mode 100644 index 565c38e..0000000 --- a/contest/weekly-contest-276/solving-questions-with-brainpower.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include - -using std::vector; - -class Solution { - struct Question { - long long answered; - long long skipped; - - long long get() const { return std::max(answered, skipped); } - }; - - long long getWithOffset(vector &questions, int offset) { - auto i = questions.size() - offset - 1; - if (i < 0 || i >= questions.size()) { - return 0; - } - - return questions[i].get(); - } - - public: - long long mostPoints(vector> &questions) { - vector stack; - - std::for_each(questions.rbegin(), questions.rend(), [&](auto &q_in) { - Question q; - q.answered = q_in[0] + getWithOffset(stack, q_in[1]); - q.skipped = getWithOffset(stack, 0); - - stack.push_back(q); - }); - - return stack.back().get(); - } -}; - -int main() { - Solution s; - vector> questions; - - questions = {{3, 2}, {4, 3}, {4, 4}, {2, 5}}; - assert(s.mostPoints(questions) == 5); - - questions = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; - assert(s.mostPoints(questions) == 7); - - return 0; -} diff --git a/contest/weekly-contest-276/solving-questions-with-brainpower.cpp b/contest/weekly-contest-276/solving-questions-with-brainpower.cpp new file mode 120000 index 0000000..3e02b58 --- /dev/null +++ b/contest/weekly-contest-276/solving-questions-with-brainpower.cpp @@ -0,0 +1 @@ +../../problems/solving-questions-with-brainpower.cpp \ No newline at end of file diff --git a/problems/binary-search.rb b/problems/binary-search.rb new file mode 100644 index 0000000..37103ef --- /dev/null +++ b/problems/binary-search.rb @@ -0,0 +1,31 @@ +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer} +def search(nums, target) + left = 0 + right = nums.size + + while left < right do + mid = (left + right).div(2) + + if nums[mid] == target then + return mid + elsif nums[mid] < target then + left = mid + 1 + else + right = mid + end + end + + return -1 +end + +RSpec.describe "search" do + it "nums = [-1,0,3,5,9,12], target = 9" do + expect(search([-1,0,3,5,9,12], 9)).to eq(4) + end + + it "nums = [-1,0,3,5,9,12], target = 2" do + expect(search([-1,0,3,5,9,12], 2)).to eq(-1) + end +end diff --git a/problems/climbing-stairs.rb b/problems/climbing-stairs.rb new file mode 100644 index 0000000..a52586e --- /dev/null +++ b/problems/climbing-stairs.rb @@ -0,0 +1,27 @@ +# @param {Integer} n +# @return {Integer} +def climb_stairs(n) + possible_ways = [0, 1, 2] + + k = 3 + while possible_ways.size <= n do + possible_ways.push(possible_ways[k - 1] + possible_ways[k - 2]) + k += 1 + end + + return possible_ways[n] +end + +RSpec.describe "climb_stairs" do + it "1 is 1" do + expect(climb_stairs(1)).to eq(1) + end + + it "2 is 2" do + expect(climb_stairs(2)).to eq(2) + end + + it "3 is 3" do + expect(climb_stairs(3)).to eq(3) + end +end diff --git a/problems/contains-duplicate.rb b/problems/contains-duplicate.rb new file mode 100644 index 0000000..6a44b8e --- /dev/null +++ b/problems/contains-duplicate.rb @@ -0,0 +1,27 @@ +# @param {Integer[]} nums +# @return {Boolean} +def contains_duplicate(nums) + encountered = Set.new + + nums.each { |x| + if encountered.add?(x) == nil then + return true + end + } + + return false +end + +RSpec.describe "contains_duplicate" do + it "nums = [1,2,3,1] contains" do + expect(contains_duplicate([1,2,3,1])).to be true + end + + it "nums = [1,2,3,4] doesn't contain" do + expect(contains_duplicate([1,2,3,4])).to be false + end + + it "nums = [1,1,1,3,3,4,3,2,4,2] contains" do + expect(contains_duplicate([1,1,1,3,3,4,3,2,4,2])).to be true + end +end diff --git a/problems/contains-duplicate.swift b/problems/contains-duplicate.swift new file mode 100644 index 0000000..5d9a997 --- /dev/null +++ b/problems/contains-duplicate.swift @@ -0,0 +1,12 @@ +class Solution { + func containsDuplicate(_ nums: [Int]) -> Bool { + var encountered = Set() + for x in nums { + if encountered.contains(x) { + return true + } + encountered.insert(x) + } + return false + } +} diff --git a/problems/divide-a-string-into-groups-of-size-k.cpp b/problems/divide-a-string-into-groups-of-size-k.cpp new file mode 100644 index 0000000..599bf05 --- /dev/null +++ b/problems/divide-a-string-into-groups-of-size-k.cpp @@ -0,0 +1,23 @@ +#include +#include + +using std::string; +using std::vector; + +class Solution { + public: + vector divideString(string s, int k, char fill) { + vector result; + + for (int i = 0; i < s.size(); i += k) { + result.push_back(s.substr(i, k)); + } + + auto size_of_last = result.back().size(); + if (size_of_last < k) { + result.back().append(k - size_of_last, fill); + } + + return result; + } +}; diff --git a/problems/fibonacci-number.rb b/problems/fibonacci-number.rb new file mode 100644 index 0000000..de4202f --- /dev/null +++ b/problems/fibonacci-number.rb @@ -0,0 +1,39 @@ +# @param {Integer} n +# @return {Integer} +def fib(n) + if n == 0 then + return 0 + end + + prev, current = 0, 1 + (2..n).each { + prev, current = current, prev + current + } + return current +end + +RSpec.describe "fib of " do + it "0 is 0" do + expect(fib(0)).to eq(0) + end + + it "1 is 1" do + expect(fib(1)).to eq(1) + end + + it "2 is 1" do + expect(fib(2)).to eq(1) + end + + it "3 is 2" do + expect(fib(3)).to eq(2) + end + + it "4 is 3" do + expect(fib(4)).to eq(3) + end + + it "5 is 5" do + expect(fib(5)).to eq(5) + end +end diff --git a/problems/first-bad-version.rb b/problems/first-bad-version.rb new file mode 100644 index 0000000..60c33cf --- /dev/null +++ b/problems/first-bad-version.rb @@ -0,0 +1,46 @@ +# The is_bad_version API is already defined for you. +# @param {Integer} version +# @return {boolean} whether the version is bad +# def is_bad_version(version): + +# @param {Integer} n +# @return {Integer} +def first_bad_version(n) + left = 1 + right = n + + while left < right do + mid = (left + right).div(2) + + if is_bad_version(mid) then + right = mid + else + left = mid + 1 + end + end + + return left +end + +RSpec.describe "first_bad_version" do + it "n = 5, bad = 4" do + def is_bad_version(n) + return n >= 4 + end + expect(first_bad_version(5)).to eq(4) + end + + it "n = 1, bad = 1" do + def is_bad_version(n) + return n >= 1 + end + expect(first_bad_version(1)).to eq(1) + end + + it "n = 1000000, bad = 76192" do + def is_bad_version(n) + return n >= 76192 + end + expect(first_bad_version(1000000)).to eq(76192) + end +end diff --git a/problems/maximum-running-time-of-n-computers.cpp b/problems/maximum-running-time-of-n-computers.cpp new file mode 100644 index 0000000..8672af8 --- /dev/null +++ b/problems/maximum-running-time-of-n-computers.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include + +using std::vector; + +class Solution { + public: + long long maxRunTime(int n, vector &batteries) { + vector bats{batteries}; + + std::sort(bats.begin(), bats.end()); + long long total = std::accumulate(bats.begin(), bats.end(), 0L); + + long long k; + for (k = 0; bats.back() > total / (n - k); k++) { + total -= bats.back(); + bats.pop_back(); + } + return total / (n - k); + } +}; + +int main() { + Solution s; + vector batteries; + + batteries = {3, 3, 3}; + assert(s.maxRunTime(2, batteries) == 4); + + batteries = {1, 1, 1, 1}; + assert(s.maxRunTime(2, batteries) == 2); + + batteries = {10, 10, 3, 5}; + assert(s.maxRunTime(3, batteries) == 8); + + batteries = { + 6251, 4133, 9898, 1536, 5475, 6340, 7430, 4413, 2558, 3941, 6934, 7326, + 7948, 7736, 1408, 5752, 836, 4511, 7684, 3939, 1376, 2305, 2090, 8419, + 3813, 4430, 890, 4120, 3415, 9706, 879, 158, 2239, 5462, 5773, 5285, + 5540, 305, 2211, 691, 4335, 5912, 3542, 5229, 996, 2609, 2173, 87, + 5683, 2946, 1456, 9590, 3625, 1807, 6909, 1328, 1548, 8182, 1690, 7440, + 8310, 8502, 320, 2654, 2254, 1306, 7696, 7187, 3977, 3270, 2989, 1629, + 5725, 7937, 5317, 9763, 9238, 3712, 1925, 2, 1463, 6309, 4997, 7786, + 1811, 7579, 3290, 8355, 63, 5010, 3574, 5364, 748, 1877, 106, 1735, + 7809, 5060, 9677, 4831, 1524, 9663, 6557, 9399, 5976, 801, 8800, 4297, + 9636, 4828, 3972, 6946, 6170, 9984, 5710, 3318, 4156, 7838, 6856, 2866, + 5900, 4623, 5228, 8063, 2514, 9149, 3509, 4033, 854, 2884, 7160, 8195, + 1936, 8134, 4277, 9442, 5263, 555, 5515, 2341, 2820, 3095, 2974, 7648, + 9116, 6886, 8545, 4055, 2398, 2425, 4861, 2114, 9280, 5045, 3678, 6569, + 7948, 1912, 7856, 6831, 4286, 1645, 9654, 5552, 880, 1864, 6386, 6616, + 3961, 7427, 8649, 2323, 8084, 7334, 8256, 6187, 2244, 9738, 6869, 5888, + 3862, 886, 1419, 2067, 15, 428, 4732, 2098, 7710, 5586, 1972, 5388, + 5171, 9239, 4810, 2461, 3639, 8120, 5296, 9522, 3475, 5703, 2358, 1957, + 9823, 6561, 3690, 1856, 8202, 1343, 1868, 5851, 3177, 7036, 4901, 4891, + 4640, 5927, 7610, 9204, 3538, 7508, 411, 3814, 7545, 7094, 6841, 3125, + 2884, 2205, 8271, 9920, 2322, 20, 8850, 1747, 3317, 2873, 558, 7201, + 6688, 9097, 9399, 2096, 5846, 9502, 5028, 2907, 1667, 4049, 5810, 5850, + 4628, 4511, 2415, 1194, 6477, 9198, 6645, 1281, 3920, 7117, 3301, 1892, + 4402, 8180, 2915, 3548, 211, 5855, 2190, 6552, 5992, 7280, 1291, 4064, + 6139, 9585, 6729, 3481, 5274, 2498, 9484, 3486, 863, 1913, 2921, 4733, + 544, 62, 5139, 4408, 6174, 9175, 1119, 7858, 6215, 2901, 4862, 4020, + 7371, 2778, 3086, 6559, 9187, 7591, 6007, 8762, 3577, 8984, 6653, 4975, + 3939, 1385, 4076, 6350, 3248, 3102, 8331, 194, 9552, 409, 1527, 6387, + 8477, 5410, 3223, 1168, 6578, 4004, 3442, 1767, 5979, 8644, 4893, 8495, + 5152, 5450, 2221, 7171, 8308, 6288, 8536, 5671, 6642, 6999, 4134, 5572, + 3639, 884, 9915, 7104, 8288, 994, 9765, 9467, 627, 2549, 2237, 713, + 9487, 9727, 569, 5755, 2975, 5248, 894, 3083, 7820, 7696, 4780, 952, + 9787, 6791, 1939, 682, 2917, 6001, 1781, 2712, 5098, 2002, 5641, 9202, + 7651, 4817, 7685, 3900, 8720, 7808, 8628, 2196, 4155, 3357, 7890, 1361, + 7537, 6157, 7107, 2972, 5548, 2297, 9888, 6257, 2596, 9319, 850, 2181, + 2284, 6877, 9344, 1177, 2238, 5140, 6476, 6289, 2673, 3266, 5291, 3098, + 541, 81, 8251, 8235, 8118, 8717, 2289, 4159, 2926, 478, 9137, 3921, + 2591, 669, 8164, 6245, 5742, 6076, 3839, 4243, 4656, 2448, 1168, 4481, + 3734, 2909, 4499, 1479, 4204, 3693, 5440, 2921, 4044, 9615, 7430, 2716, + 7520, 3354, 3683, 6058, 4212, 4940, 5645, 5101, 9339, 2422, 3201, 813, + 2753, 8796, 5485, 1080, 4433, 3875, 831, 33, 1689, 6643, 5753, 7352, + 1670, 2525, 3606, 34, 5768, 5256, 531, 427, 4931, 8174, 8413, 6014, + 3464, 3793, 6932, 3943, 7916, 3954, 1452, 4165, 4047, 2844, 9685, 6882, + 9535, 4995, 7836, 5296, 1140, 2403, 7723, 4388, 4791, 4967, 3788, 3694, + 9258, 370, 7174, 8340, 9182, 7522, 7852, 242, 1429, 5904, 7794, 7736, + 2421, 3885, 819, 3136, 8815, 5737, 2043, 6398, 9528, 9011, 5056, 7761, + 6120, 9561, 6053, 8264, 1648, 4598, 3448, 2563, 4717, 9078, 5032, 1163, + 7788, 2227, 4478, 4172}; + assert(s.maxRunTime(13, batteries) == 207113); + + return 0; +} diff --git a/problems/maximum-subarray.rb b/problems/maximum-subarray.rb new file mode 100644 index 0000000..99124db --- /dev/null +++ b/problems/maximum-subarray.rb @@ -0,0 +1,29 @@ +# @param {Integer[]} nums +# @return {Integer} +def max_sub_array(nums) + if nums.empty? then + return nil + end + + sum, running_sum = nums.first, nums.first + nums.drop(1).each { |x| + running_sum = [running_sum + x, x].max + sum = [sum, running_sum].max + } + + return sum +end + +RSpec.describe "max_sub_array of " do + it "[-2,1,-3,4,-1,2,1,-5,4] is 6" do + expect(max_sub_array([-2,1,-3,4,-1,2,1,-5,4])).to eq(6) + end + + it "[1] is 1" do + expect(max_sub_array([1])).to eq(1) + end + + it "[5,4,-1,7,8] is 23" do + expect(max_sub_array([5,4,-1,7,8])).to eq(23) + end +end diff --git a/problems/merge-sorted-array.rb b/problems/merge-sorted-array.rb new file mode 100644 index 0000000..5ab150e --- /dev/null +++ b/problems/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/problems/min-cost-climbing-stairs.rb b/problems/min-cost-climbing-stairs.rb new file mode 100644 index 0000000..01ac4f2 --- /dev/null +++ b/problems/min-cost-climbing-stairs.rb @@ -0,0 +1,28 @@ +# @param {Integer[]} cost +# @return {Integer} +def min_cost_climbing_stairs(cost) + def get(cost, k) + if k < 0 || k >= cost.size then + return 0 + end + return cost[k] + end + + i = cost.size - 1 + while i >= 0 do + cost[i] += [get(cost, i + 1), get(cost, i + 2)].min + i -= 1 + end + + return cost.take(2).min +end + +RSpec.describe "min_cost_climbing_stairs" do + it "of [10,15,20] is 15" do + expect(min_cost_climbing_stairs([10,15,20])).to eq(15) + end + + it "of [1,100,1,1,1,100,1,1,100,1] is 6" do + expect(min_cost_climbing_stairs([1,100,1,1,1,100,1,1,100,1])).to eq(6) + end +end diff --git a/problems/minimum-moves-to-reach-target-score.cpp b/problems/minimum-moves-to-reach-target-score.cpp new file mode 100644 index 0000000..cd79bc3 --- /dev/null +++ b/problems/minimum-moves-to-reach-target-score.cpp @@ -0,0 +1,33 @@ +#include + +class Solution { + public: + int minMoves(int target, int maxDoubles) { + int moves; + for (moves = 0; target > 1 && maxDoubles > 0; moves++) { + if (target % 2 == 0) { + maxDoubles--; + target /= 2; + } else { + target--; + } + } + + // remaining increments + moves += target - 1; + + return moves; + } +}; + +int main() { + Solution s; + + assert(s.minMoves(5, 0) == 4); + assert(s.minMoves(19, 2) == 7); + assert(s.minMoves(10, 4) == 4); + assert(s.minMoves(766972377, 92)); + assert(s.minMoves(1000000000, 5)); + + return 0; +} diff --git a/problems/rotate-array.rb b/problems/rotate-array.rb new file mode 100644 index 0000000..213fc16 --- /dev/null +++ b/problems/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/problems/search-insert-position.rb b/problems/search-insert-position.rb new file mode 100644 index 0000000..b2c2bf7 --- /dev/null +++ b/problems/search-insert-position.rb @@ -0,0 +1,35 @@ +# @param {Integer[]} nums +# @param {Integer} target +# @return {Integer} +def search_insert(nums, target) + left = 0 + right = nums.size + + while left < right do + mid = (left + right).div(2) + + if nums[mid] == target then + return mid + elsif nums[mid] < target then + left = mid + 1 + else + right = mid + end + end + + return left +end + +RSpec.describe "search_insert" do + it "nums = [1,3,5,6], target = 5" do + expect(search_insert([1,3,5,6], 5)).to eq(2) + end + + it "nums = [1,3,5,6], target = 2" do + expect(search_insert([1,3,5,6], 2)).to eq(1) + end + + it "nums = [1,3,5,6], target = 7" do + expect(search_insert([1,3,5,6], 7)).to eq(4) + end +end diff --git a/problems/solving-questions-with-brainpower.cpp b/problems/solving-questions-with-brainpower.cpp new file mode 100644 index 0000000..565c38e --- /dev/null +++ b/problems/solving-questions-with-brainpower.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +using std::vector; + +class Solution { + struct Question { + long long answered; + long long skipped; + + long long get() const { return std::max(answered, skipped); } + }; + + long long getWithOffset(vector &questions, int offset) { + auto i = questions.size() - offset - 1; + if (i < 0 || i >= questions.size()) { + return 0; + } + + return questions[i].get(); + } + + public: + long long mostPoints(vector> &questions) { + vector stack; + + std::for_each(questions.rbegin(), questions.rend(), [&](auto &q_in) { + Question q; + q.answered = q_in[0] + getWithOffset(stack, q_in[1]); + q.skipped = getWithOffset(stack, 0); + + stack.push_back(q); + }); + + return stack.back().get(); + } +}; + +int main() { + Solution s; + vector> questions; + + questions = {{3, 2}, {4, 3}, {4, 4}, {2, 5}}; + assert(s.mostPoints(questions) == 5); + + questions = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}}; + assert(s.mostPoints(questions) == 7); + + return 0; +} diff --git a/problems/squares-of-a-sorted-array.rb b/problems/squares-of-a-sorted-array.rb new file mode 100644 index 0000000..649c2a3 --- /dev/null +++ b/problems/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 diff --git a/problems/tribonacci.rb b/problems/tribonacci.rb new file mode 100644 index 0000000..bfb45c3 --- /dev/null +++ b/problems/tribonacci.rb @@ -0,0 +1,43 @@ +# @param {Integer} n +# @return {Integer} +def tribonacci(n) + sequence = [0, 1, 1] + if n < 3 then + return sequence[n] + end + + (3..n).each { + sequence = sequence.drop(1) + [sequence.sum] + } + return sequence.last +end + +RSpec.describe "tribonacci of " do + it "0 is 0" do + expect(tribonacci(0)).to eq(0) + end + + it "1 is 1" do + expect(tribonacci(1)).to eq(1) + end + + it "2 is 1" do + expect(tribonacci(2)).to eq(1) + end + + it "3 is 2" do + expect(tribonacci(3)).to eq(2) + end + + it "4 is 4" do + expect(tribonacci(4)).to eq(4) + end + + it "5 is 7" do + expect(tribonacci(5)).to eq(7) + end + + it "25 is 1389537" do + expect(tribonacci(25)).to eq(1389537) + end +end diff --git a/problems/two-sum.rb b/problems/two-sum.rb new file mode 100644 index 0000000..0ea7604 --- /dev/null +++ b/problems/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 diff --git a/study-plan/algorithm/day-01/binary-search.rb b/study-plan/algorithm/day-01/binary-search.rb deleted file mode 100644 index 37103ef..0000000 --- a/study-plan/algorithm/day-01/binary-search.rb +++ /dev/null @@ -1,31 +0,0 @@ -# @param {Integer[]} nums -# @param {Integer} target -# @return {Integer} -def search(nums, target) - left = 0 - right = nums.size - - while left < right do - mid = (left + right).div(2) - - if nums[mid] == target then - return mid - elsif nums[mid] < target then - left = mid + 1 - else - right = mid - end - end - - return -1 -end - -RSpec.describe "search" do - it "nums = [-1,0,3,5,9,12], target = 9" do - expect(search([-1,0,3,5,9,12], 9)).to eq(4) - end - - it "nums = [-1,0,3,5,9,12], target = 2" do - expect(search([-1,0,3,5,9,12], 2)).to eq(-1) - end -end diff --git a/study-plan/algorithm/day-01/binary-search.rb b/study-plan/algorithm/day-01/binary-search.rb new file mode 120000 index 0000000..37b7d7a --- /dev/null +++ b/study-plan/algorithm/day-01/binary-search.rb @@ -0,0 +1 @@ +../../../problems/binary-search.rb \ No newline at end of file diff --git a/study-plan/algorithm/day-01/first-bad-version.rb b/study-plan/algorithm/day-01/first-bad-version.rb deleted file mode 100644 index 60c33cf..0000000 --- a/study-plan/algorithm/day-01/first-bad-version.rb +++ /dev/null @@ -1,46 +0,0 @@ -# The is_bad_version API is already defined for you. -# @param {Integer} version -# @return {boolean} whether the version is bad -# def is_bad_version(version): - -# @param {Integer} n -# @return {Integer} -def first_bad_version(n) - left = 1 - right = n - - while left < right do - mid = (left + right).div(2) - - if is_bad_version(mid) then - right = mid - else - left = mid + 1 - end - end - - return left -end - -RSpec.describe "first_bad_version" do - it "n = 5, bad = 4" do - def is_bad_version(n) - return n >= 4 - end - expect(first_bad_version(5)).to eq(4) - end - - it "n = 1, bad = 1" do - def is_bad_version(n) - return n >= 1 - end - expect(first_bad_version(1)).to eq(1) - end - - it "n = 1000000, bad = 76192" do - def is_bad_version(n) - return n >= 76192 - end - expect(first_bad_version(1000000)).to eq(76192) - end -end diff --git a/study-plan/algorithm/day-01/first-bad-version.rb b/study-plan/algorithm/day-01/first-bad-version.rb new file mode 120000 index 0000000..eb8d7ea --- /dev/null +++ b/study-plan/algorithm/day-01/first-bad-version.rb @@ -0,0 +1 @@ +../../../problems/first-bad-version.rb \ No newline at end of file diff --git a/study-plan/algorithm/day-01/search-insert-position.rb b/study-plan/algorithm/day-01/search-insert-position.rb deleted file mode 100644 index b2c2bf7..0000000 --- a/study-plan/algorithm/day-01/search-insert-position.rb +++ /dev/null @@ -1,35 +0,0 @@ -# @param {Integer[]} nums -# @param {Integer} target -# @return {Integer} -def search_insert(nums, target) - left = 0 - right = nums.size - - while left < right do - mid = (left + right).div(2) - - if nums[mid] == target then - return mid - elsif nums[mid] < target then - left = mid + 1 - else - right = mid - end - end - - return left -end - -RSpec.describe "search_insert" do - it "nums = [1,3,5,6], target = 5" do - expect(search_insert([1,3,5,6], 5)).to eq(2) - end - - it "nums = [1,3,5,6], target = 2" do - expect(search_insert([1,3,5,6], 2)).to eq(1) - end - - it "nums = [1,3,5,6], target = 7" do - expect(search_insert([1,3,5,6], 7)).to eq(4) - end -end diff --git a/study-plan/algorithm/day-01/search-insert-position.rb b/study-plan/algorithm/day-01/search-insert-position.rb new file mode 120000 index 0000000..457a93f --- /dev/null +++ b/study-plan/algorithm/day-01/search-insert-position.rb @@ -0,0 +1 @@ +../../../problems/search-insert-position.rb \ No newline at end of file diff --git a/study-plan/algorithm/day-02/rotate-array.rb b/study-plan/algorithm/day-02/rotate-array.rb deleted file mode 100644 index 213fc16..0000000 --- a/study-plan/algorithm/day-02/rotate-array.rb +++ /dev/null @@ -1,68 +0,0 @@ -# @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/rotate-array.rb b/study-plan/algorithm/day-02/rotate-array.rb new file mode 120000 index 0000000..fba6b06 --- /dev/null +++ b/study-plan/algorithm/day-02/rotate-array.rb @@ -0,0 +1 @@ +../../../problems/rotate-array.rb \ No newline at end of file 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 deleted file mode 100644 index 649c2a3..0000000 --- a/study-plan/algorithm/day-02/squares-of-a-sorted-array.rb +++ /dev/null @@ -1,57 +0,0 @@ -# @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 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 120000 index 0000000..c13b9f9 --- /dev/null +++ b/study-plan/algorithm/day-02/squares-of-a-sorted-array.rb @@ -0,0 +1 @@ +../../../problems/squares-of-a-sorted-array.rb \ No newline at end of file diff --git a/study-plan/data-structure/day-01/contains-duplicate.rb b/study-plan/data-structure/day-01/contains-duplicate.rb deleted file mode 100644 index 6a44b8e..0000000 --- a/study-plan/data-structure/day-01/contains-duplicate.rb +++ /dev/null @@ -1,27 +0,0 @@ -# @param {Integer[]} nums -# @return {Boolean} -def contains_duplicate(nums) - encountered = Set.new - - nums.each { |x| - if encountered.add?(x) == nil then - return true - end - } - - return false -end - -RSpec.describe "contains_duplicate" do - it "nums = [1,2,3,1] contains" do - expect(contains_duplicate([1,2,3,1])).to be true - end - - it "nums = [1,2,3,4] doesn't contain" do - expect(contains_duplicate([1,2,3,4])).to be false - end - - it "nums = [1,1,1,3,3,4,3,2,4,2] contains" do - expect(contains_duplicate([1,1,1,3,3,4,3,2,4,2])).to be true - end -end diff --git a/study-plan/data-structure/day-01/contains-duplicate.rb b/study-plan/data-structure/day-01/contains-duplicate.rb new file mode 120000 index 0000000..b518777 --- /dev/null +++ b/study-plan/data-structure/day-01/contains-duplicate.rb @@ -0,0 +1 @@ +../../../problems/contains-duplicate.rb \ No newline at end of file diff --git a/study-plan/data-structure/day-01/contains-duplicate.swift b/study-plan/data-structure/day-01/contains-duplicate.swift deleted file mode 100644 index 5d9a997..0000000 --- a/study-plan/data-structure/day-01/contains-duplicate.swift +++ /dev/null @@ -1,12 +0,0 @@ -class Solution { - func containsDuplicate(_ nums: [Int]) -> Bool { - var encountered = Set() - for x in nums { - if encountered.contains(x) { - return true - } - encountered.insert(x) - } - return false - } -} diff --git a/study-plan/data-structure/day-01/contains-duplicate.swift b/study-plan/data-structure/day-01/contains-duplicate.swift new file mode 120000 index 0000000..ffdf992 --- /dev/null +++ b/study-plan/data-structure/day-01/contains-duplicate.swift @@ -0,0 +1 @@ +../../../problems/contains-duplicate.swift \ No newline at end of file diff --git a/study-plan/data-structure/day-01/maximum-subarray.rb b/study-plan/data-structure/day-01/maximum-subarray.rb deleted file mode 100644 index 99124db..0000000 --- a/study-plan/data-structure/day-01/maximum-subarray.rb +++ /dev/null @@ -1,29 +0,0 @@ -# @param {Integer[]} nums -# @return {Integer} -def max_sub_array(nums) - if nums.empty? then - return nil - end - - sum, running_sum = nums.first, nums.first - nums.drop(1).each { |x| - running_sum = [running_sum + x, x].max - sum = [sum, running_sum].max - } - - return sum -end - -RSpec.describe "max_sub_array of " do - it "[-2,1,-3,4,-1,2,1,-5,4] is 6" do - expect(max_sub_array([-2,1,-3,4,-1,2,1,-5,4])).to eq(6) - end - - it "[1] is 1" do - expect(max_sub_array([1])).to eq(1) - end - - it "[5,4,-1,7,8] is 23" do - expect(max_sub_array([5,4,-1,7,8])).to eq(23) - end -end diff --git a/study-plan/data-structure/day-01/maximum-subarray.rb b/study-plan/data-structure/day-01/maximum-subarray.rb new file mode 120000 index 0000000..cb8679f --- /dev/null +++ b/study-plan/data-structure/day-01/maximum-subarray.rb @@ -0,0 +1 @@ +../../../problems/maximum-subarray.rb \ No newline at end of file diff --git a/study-plan/data-structure/day-02/merge-sorted-array.rb b/study-plan/data-structure/day-02/merge-sorted-array.rb deleted file mode 100644 index 5ab150e..0000000 --- a/study-plan/data-structure/day-02/merge-sorted-array.rb +++ /dev/null @@ -1,59 +0,0 @@ -# @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/merge-sorted-array.rb b/study-plan/data-structure/day-02/merge-sorted-array.rb new file mode 120000 index 0000000..668222d --- /dev/null +++ b/study-plan/data-structure/day-02/merge-sorted-array.rb @@ -0,0 +1 @@ +../../../problems/merge-sorted-array.rb \ No newline at end of file diff --git a/study-plan/data-structure/day-02/two-sum.rb b/study-plan/data-structure/day-02/two-sum.rb deleted file mode 100644 index 0ea7604..0000000 --- a/study-plan/data-structure/day-02/two-sum.rb +++ /dev/null @@ -1,30 +0,0 @@ -# @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 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 120000 index 0000000..8424170 --- /dev/null +++ b/study-plan/data-structure/day-02/two-sum.rb @@ -0,0 +1 @@ +../../../problems/two-sum.rb \ No newline at end of file diff --git a/study-plan/dynamic-programming/day-01/fibonacci-number.rb b/study-plan/dynamic-programming/day-01/fibonacci-number.rb deleted file mode 100644 index de4202f..0000000 --- a/study-plan/dynamic-programming/day-01/fibonacci-number.rb +++ /dev/null @@ -1,39 +0,0 @@ -# @param {Integer} n -# @return {Integer} -def fib(n) - if n == 0 then - return 0 - end - - prev, current = 0, 1 - (2..n).each { - prev, current = current, prev + current - } - return current -end - -RSpec.describe "fib of " do - it "0 is 0" do - expect(fib(0)).to eq(0) - end - - it "1 is 1" do - expect(fib(1)).to eq(1) - end - - it "2 is 1" do - expect(fib(2)).to eq(1) - end - - it "3 is 2" do - expect(fib(3)).to eq(2) - end - - it "4 is 3" do - expect(fib(4)).to eq(3) - end - - it "5 is 5" do - expect(fib(5)).to eq(5) - end -end diff --git a/study-plan/dynamic-programming/day-01/fibonacci-number.rb b/study-plan/dynamic-programming/day-01/fibonacci-number.rb new file mode 120000 index 0000000..44665de --- /dev/null +++ b/study-plan/dynamic-programming/day-01/fibonacci-number.rb @@ -0,0 +1 @@ +../../../problems/fibonacci-number.rb \ No newline at end of file diff --git a/study-plan/dynamic-programming/day-01/tribonacci.rb b/study-plan/dynamic-programming/day-01/tribonacci.rb deleted file mode 100644 index bfb45c3..0000000 --- a/study-plan/dynamic-programming/day-01/tribonacci.rb +++ /dev/null @@ -1,43 +0,0 @@ -# @param {Integer} n -# @return {Integer} -def tribonacci(n) - sequence = [0, 1, 1] - if n < 3 then - return sequence[n] - end - - (3..n).each { - sequence = sequence.drop(1) + [sequence.sum] - } - return sequence.last -end - -RSpec.describe "tribonacci of " do - it "0 is 0" do - expect(tribonacci(0)).to eq(0) - end - - it "1 is 1" do - expect(tribonacci(1)).to eq(1) - end - - it "2 is 1" do - expect(tribonacci(2)).to eq(1) - end - - it "3 is 2" do - expect(tribonacci(3)).to eq(2) - end - - it "4 is 4" do - expect(tribonacci(4)).to eq(4) - end - - it "5 is 7" do - expect(tribonacci(5)).to eq(7) - end - - it "25 is 1389537" do - expect(tribonacci(25)).to eq(1389537) - end -end diff --git a/study-plan/dynamic-programming/day-01/tribonacci.rb b/study-plan/dynamic-programming/day-01/tribonacci.rb new file mode 120000 index 0000000..c495ca2 --- /dev/null +++ b/study-plan/dynamic-programming/day-01/tribonacci.rb @@ -0,0 +1 @@ +../../../problems/tribonacci.rb \ No newline at end of file diff --git a/study-plan/dynamic-programming/day-02/climbing-stairs.rb b/study-plan/dynamic-programming/day-02/climbing-stairs.rb deleted file mode 100644 index a52586e..0000000 --- a/study-plan/dynamic-programming/day-02/climbing-stairs.rb +++ /dev/null @@ -1,27 +0,0 @@ -# @param {Integer} n -# @return {Integer} -def climb_stairs(n) - possible_ways = [0, 1, 2] - - k = 3 - while possible_ways.size <= n do - possible_ways.push(possible_ways[k - 1] + possible_ways[k - 2]) - k += 1 - end - - return possible_ways[n] -end - -RSpec.describe "climb_stairs" do - it "1 is 1" do - expect(climb_stairs(1)).to eq(1) - end - - it "2 is 2" do - expect(climb_stairs(2)).to eq(2) - end - - it "3 is 3" do - expect(climb_stairs(3)).to eq(3) - end -end diff --git a/study-plan/dynamic-programming/day-02/climbing-stairs.rb b/study-plan/dynamic-programming/day-02/climbing-stairs.rb new file mode 120000 index 0000000..929dcac --- /dev/null +++ b/study-plan/dynamic-programming/day-02/climbing-stairs.rb @@ -0,0 +1 @@ +../../../problems/climbing-stairs.rb \ No newline at end of file diff --git a/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb b/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb deleted file mode 100644 index 01ac4f2..0000000 --- a/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb +++ /dev/null @@ -1,28 +0,0 @@ -# @param {Integer[]} cost -# @return {Integer} -def min_cost_climbing_stairs(cost) - def get(cost, k) - if k < 0 || k >= cost.size then - return 0 - end - return cost[k] - end - - i = cost.size - 1 - while i >= 0 do - cost[i] += [get(cost, i + 1), get(cost, i + 2)].min - i -= 1 - end - - return cost.take(2).min -end - -RSpec.describe "min_cost_climbing_stairs" do - it "of [10,15,20] is 15" do - expect(min_cost_climbing_stairs([10,15,20])).to eq(15) - end - - it "of [1,100,1,1,1,100,1,1,100,1] is 6" do - expect(min_cost_climbing_stairs([1,100,1,1,1,100,1,1,100,1])).to eq(6) - end -end diff --git a/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb b/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb new file mode 120000 index 0000000..7ef66fd --- /dev/null +++ b/study-plan/dynamic-programming/day-02/min-cost-climbing-stairs.rb @@ -0,0 +1 @@ +../../../problems/min-cost-climbing-stairs.rb \ No newline at end of file