Skip to content

Commit f058a8b

Browse files
committed
Min Cost Climbing Stairs & Number of Steps to Reduce a Number to Zero & Fizz Buzz & Ransom Note
1 parent 8adf833 commit f058a8b

15 files changed

+258
-32
lines changed

README.MD

Lines changed: 33 additions & 29 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55
},
66
"scripts": {
77
"test": "jest"
8-
},
9-
"jest": {
10-
"verbose": true
118
}
129
}

solutions/canConstruct/README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [383. Ransom Note](https://leetcode.com/problems/ransom-note/)
2+
3+
Given two strings `ransomNote` and `magazine`, return `true` if `ransomNote` can be constructed by using the letters from `magazine` and `false` otherwise.
4+
5+
Each letter in `magazine` can only be used once in `ransomNote`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} ransomNote
3+
* @param {string} magazine
4+
* @return {boolean}
5+
*/
6+
function canConstruct(ransomNote, magazine) {
7+
if (ransomNote.length > magazine.length) return false;
8+
9+
const result = {};
10+
11+
for (const letter of magazine) {
12+
result[letter] = result[letter]
13+
? result[letter] + 1
14+
: 1;
15+
}
16+
17+
for (const letter of ransomNote) {
18+
if (!result[letter]) return false;
19+
result[letter]--;
20+
}
21+
22+
return true;
23+
}
24+
25+
module.exports = canConstruct;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const canConstruct = require('./canConstruct');
2+
3+
describe('Ransom Note', () => {
4+
it('returns true if ransomNote can be constructed from magazine', () => {
5+
expect(canConstruct('a', 'a')).toBe(true);
6+
expect(canConstruct('aa', 'aab')).toBe(true);
7+
expect(canConstruct('aab', 'baa')).toBe(true);
8+
expect(canConstruct('ab', 'baa')).toBe(true);
9+
expect(canConstruct('abc', 'abdefc')).toBe(true);
10+
expect(canConstruct('abcde', 'aabbccddeeffg')).toBe(true);
11+
});
12+
13+
it('returns false if ransomNote cannot be constructed from magazine', () => {
14+
expect(canConstruct('b', 'a')).toBe(false);
15+
expect(canConstruct('aa', 'ab')).toBe(false);
16+
expect(canConstruct('abccdef', 'abdeffc')).toBe(false);
17+
expect(canConstruct('aabbccddeeffg', 'abcdef')).toBe(false);
18+
});
19+
20+
it('returns true if ransomNote is an empty string', () => {
21+
expect(canConstruct('', 'a')).toBe(true);
22+
expect(canConstruct('', '')).toBe(true);
23+
});
24+
25+
it('returns false if ransomNote is longer than magazine', () => {
26+
expect(canConstruct('abc', 'ab')).toBe(false);
27+
});
28+
});

solutions/fib/fib.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fib = require('./fib.js');
2+
3+
describe('Fibonacci Number', () => {
4+
it('returns the correct Fibonacci number for small inputs', () => {
5+
expect(fib(0)).toBe(0);
6+
expect(fib(1)).toBe(1);
7+
expect(fib(2)).toBe(1);
8+
expect(fib(3)).toBe(2);
9+
expect(fib(4)).toBe(3);
10+
expect(fib(5)).toBe(5);
11+
});
12+
13+
it('returns the correct Fibonacci number for larger inputs', () => {
14+
expect(fib(10)).toBe(55);
15+
expect(fib(20)).toBe(6765);
16+
expect(fib(30)).toBe(832040);
17+
});
18+
});

solutions/fizzBuzz/README.MD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## [412. Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)
2+
3+
Given an integer `n`, return a string array `answer` (1-indexed) where:
4+
5+
- `answer[i] == "FizzBuzz"` if i is divisible by 3 and 5.
6+
- `answer[i] == "Fizz"` if i is divisible by 3.
7+
- `answer[i] == "Buzz"` if i is divisible by 5.
8+
- `answer[i] == i` (as a string) if none of the above conditions are true.

solutions/fizzBuzz/fizzBuzz.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
function fizzBuzz(n) {
6+
const result = [];
7+
8+
for (let i = 1; i <= n; i++) {
9+
let value = '';
10+
11+
if (i % 3 === 0) value += 'Fizz';
12+
if (i % 5 === 0) value += 'Buzz';
13+
14+
result.push(value ? value : i.toString())
15+
}
16+
17+
return result;
18+
}
19+
20+
module.exports = fizzBuzz;

solutions/fizzBuzz/fizzBuzz.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fizzBuzz = require('./fizzBuzz');
2+
3+
describe('Fizz Buzz', () => {
4+
it('returns an array of the correct length', () => {
5+
expect(fizzBuzz(15)).toHaveLength(15);
6+
expect(fizzBuzz(0)).toHaveLength(0);
7+
});
8+
9+
it('returns the correct output for each number', () => {
10+
expect(fizzBuzz(15)).toEqual([
11+
'1',
12+
'2',
13+
'Fizz',
14+
'4',
15+
'Buzz',
16+
'Fizz',
17+
'7',
18+
'8',
19+
'Fizz',
20+
'Buzz',
21+
'11',
22+
'Fizz',
23+
'13',
24+
'14',
25+
'FizzBuzz',
26+
]);
27+
});
28+
29+
it('returns an empty array when given 0 as input', () => {
30+
expect(fizzBuzz(0)).toEqual([]);
31+
});
32+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [746. Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)
2+
3+
You are given an integer array `cost` where `cost[i]` is the cost of `i` step on a staircase. Once you pay the cost, you can either climb one or two steps.
4+
5+
You can either start from the step with index `0`, or the step with index `1`.
6+
7+
Return the minimum cost to reach the top of the floor.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} cost
3+
* @return {number}
4+
*/
5+
function minCostClimbingStairs(cost) {
6+
if (cost.length === 1) return 0;
7+
if (cost.length === 2) return Math.min(cost[0], cost[1]);
8+
9+
let first = cost[0], second = cost[1];
10+
11+
for (let i = 2; i < cost.length; i++) {
12+
const current = cost[i] + Math.min(first, second);
13+
14+
first = second;
15+
second = current;
16+
}
17+
18+
return Math.min(first, second);
19+
}
20+
21+
module.exports = minCostClimbingStairs;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const minCostClimbingStairs = require('./minCostClimbingStairs');
2+
3+
describe('Min Cost Climbing Stairs', () => {
4+
it('returns the correct minimum cost to climb the stairs', () => {
5+
expect(minCostClimbingStairs([10, 15, 20])).toBe(15);
6+
expect(minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1])).toBe(6);
7+
expect(minCostClimbingStairs([10, 2, 5, 4, 1, 7])).toBe(7);
8+
expect(minCostClimbingStairs([5, 10])).toBe(5);
9+
});
10+
11+
it('returns 0 if the cost array has only one element', () => {
12+
expect(minCostClimbingStairs([10])).toBe(0);
13+
});
14+
15+
it('returns the minimum of the first two costs if the cost array has only two elements', () => {
16+
expect(minCostClimbingStairs([10, 20])).toBe(10);
17+
expect(minCostClimbingStairs([20, 10])).toBe(10);
18+
});
19+
});

solutions/numberOfSteps/README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [1342. Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)
2+
3+
Given an integer `num`, return the number of steps to reduce it to zero.
4+
5+
In one step, if the current number is even, you have to divide it by `2`, otherwise, you have to subtract `1` from it.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
function numberOfSteps(num) {
6+
let stepsCount = 0;
7+
8+
while (num) {
9+
num = num % 2 === 0 ? num / 2 : num - 1;
10+
stepsCount++;
11+
}
12+
13+
return stepsCount;
14+
}
15+
16+
module.exports = numberOfSteps;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const numberOfSteps = require('./numberOfSteps');
2+
3+
describe('Number of Steps to Reduce a Number to Zero', () => {
4+
it('returns the correct number of steps for positive even numbers', () => {
5+
expect(numberOfSteps(4)).toBe(3);
6+
expect(numberOfSteps(8)).toBe(4);
7+
expect(numberOfSteps(12)).toBe(5);
8+
expect(numberOfSteps(100)).toBe(9);
9+
});
10+
11+
it('returns the correct number of steps for positive odd numbers', () => {
12+
expect(numberOfSteps(3)).toBe(3);
13+
expect(numberOfSteps(7)).toBe(5);
14+
expect(numberOfSteps(11)).toBe(6);
15+
expect(numberOfSteps(101)).toBe(10);
16+
});
17+
18+
it('returns 0 for 0 input', () => {
19+
expect(numberOfSteps(0)).toBe(0);
20+
});
21+
});

0 commit comments

Comments
 (0)