1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

chore: reorganize files

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-07-23 12:53:31 +02:00
parent 229e74a381
commit 6c3cfcd876
Signed by: mfocko
GPG key ID: 7C47D46246790496
36 changed files with 745 additions and 727 deletions

View file

@ -1,23 +0,0 @@
#include <string>
#include <vector>
using std::string;
using std::vector;
class Solution {
public:
vector<string> divideString(string s, int k, char fill) {
vector<string> 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;
}
};

View file

@ -0,0 +1 @@
../../problems/divide-a-string-into-groups-of-size-k.cpp

View file

@ -1,89 +0,0 @@
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
using std::vector;
class Solution {
public:
long long maxRunTime(int n, vector<int> &batteries) {
vector<int> 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<int> 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;
}

View file

@ -0,0 +1 @@
../../problems/maximum-running-time-of-n-computers.cpp

View file

@ -1,33 +0,0 @@
#include <cassert>
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;
}

View file

@ -0,0 +1 @@
../../problems/minimum-moves-to-reach-target-score.cpp

View file

@ -1,51 +0,0 @@
#include <cassert>
#include <functional>
#include <vector>
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<Question> &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<vector<int>> &questions) {
vector<Question> 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<vector<int>> 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;
}

View file

@ -0,0 +1 @@
../../problems/solving-questions-with-brainpower.cpp

31
problems/binary-search.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,12 @@
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var encountered = Set<Int>()
for x in nums {
if encountered.contains(x) {
return true
}
encountered.insert(x)
}
return false
}
}

View file

@ -0,0 +1,23 @@
#include <string>
#include <vector>
using std::string;
using std::vector;
class Solution {
public:
vector<string> divideString(string s, int k, char fill) {
vector<string> 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;
}
};

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,89 @@
#include <algorithm>
#include <cassert>
#include <numeric>
#include <vector>
using std::vector;
class Solution {
public:
long long maxRunTime(int n, vector<int> &batteries) {
vector<int> 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<int> 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;
}

View file

@ -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

View 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

View file

@ -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

View file

@ -0,0 +1,33 @@
#include <cassert>
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;
}

68
problems/rotate-array.rb Normal file
View 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

View file

@ -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

View file

@ -0,0 +1,51 @@
#include <cassert>
#include <functional>
#include <vector>
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<Question> &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<vector<int>> &questions) {
vector<Question> 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<vector<int>> 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;
}

View 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

43
problems/tribonacci.rb Normal file
View file

@ -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

30
problems/two-sum.rb Normal file
View 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

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/binary-search.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/first-bad-version.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/search-insert-position.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/rotate-array.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/squares-of-a-sorted-array.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/contains-duplicate.rb

View file

@ -1,12 +0,0 @@
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
var encountered = Set<Int>()
for x in nums {
if encountered.contains(x) {
return true
}
encountered.insert(x)
}
return false
}
}

View file

@ -0,0 +1 @@
../../../problems/contains-duplicate.swift

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/maximum-subarray.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/merge-sorted-array.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/two-sum.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/fibonacci-number.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/tribonacci.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/climbing-stairs.rb

View file

@ -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

View file

@ -0,0 +1 @@
../../../problems/min-cost-climbing-stairs.rb