Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added reverseLinkedList #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
19 changes: 19 additions & 0 deletions solutions/dp/climbingStairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,22 @@ const climbStairs = (n) => {

return cache[n];
};

/*
BETTER BOTTOM-UP DP SOLUTION
*/
const climbStairs = (n) => {
if (n <= 2) return n;

let oneStepBack = 2;
let twoStepsBack = 1;
let totalWaysToReachI = 0;

for (let i = 2; i < n; i++) {
totalWaysToReachI = oneStepBack + twoStepsBack;
twoStepsBack = oneStepBack;
oneStepBack = totalWaysToReachI;
}

return totalWaysToReachI;
};
19 changes: 19 additions & 0 deletions solutions/dp/fibonacciNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ const fib = (n) => {

return cache[n];
};

/*
BETTER BOTTOM-UP DP SOLUTION
*/
const fib = (n) => {
if (n <= 1) return n;

let curFib = 0;
let lastFib = 1;
let lastLastFib = 0;

for (let i = 2; i <= n; i++) {
curFib = lastFib + lastLastFib;
lastLastFib = lastFib;
lastFib = curFib;
}

return curFib;
};
15 changes: 15 additions & 0 deletions solutions/dp/houseRobber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
BOTTOM-UP DP SOLUTION
*/
const rob = (nums) => {
let previousTotalRobbed = 0;
let currentTotalRobbed = 0;

nums.forEach((money) => {
const temp = currentTotalRobbed;
currentTotalRobbed = Math.max(previousTotalRobbed + money, currentTotalRobbed);
previousTotalRobbed = temp;
});

return currentTotalRobbed;
};
34 changes: 34 additions & 0 deletions solutions/dp/houseRobberII.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const robHelper = (nums, startIdx) => {
let previousTotalRobbed = 0;
let currentTotalRobbed = 0;
let temp;

const numHousesAccountingForPotentialSkips = nums.length - 1 + startIdx;
for (let i = startIdx; i < numHousesAccountingForPotentialSkips; i++) {
const money = nums[i];

temp = currentTotalRobbed;
currentTotalRobbed = Math.max(previousTotalRobbed + money, currentTotalRobbed);
previousTotalRobbed = temp;
}

return currentTotalRobbed;
};

/*
BOTTOM-UP DP SOLUTION
*/
const rob = (nums) => {
const firstHouseMoney = nums[0];
if (nums.length === 1) {
return firstHouseMoney;
}
if (nums.length === 2) {
const secondHouseMoney = nums[1];
return Math.max(firstHouseMoney, secondHouseMoney);
}

const maxMoneyIfSkipFirstHouse = robHelper(nums, 1);
const maxMoneyIfDontSkipFirstHouse = robHelper(nums, 0);
return Math.max(maxMoneyIfSkipFirstHouse, maxMoneyIfDontSkipFirstHouse);
};
16 changes: 16 additions & 0 deletions solutions/dp/maximumSubarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
BOTTOM-UP DP SOLUTION
*/
const maxSubArray = (nums) => {
let currentSubarraySum = nums[0];
let maxSubarraySum = nums[0];

for (let i = 1; i < nums.length; i++) {
let number = nums[i];

currentSubarraySum = Math.max(number, currentSubarraySum + number);
maxSubarraySum = Math.max(maxSubarraySum, currentSubarraySum);
}

return maxSubarraySum;
};
27 changes: 27 additions & 0 deletions solutions/dp/perfectSquares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
BOTTOM-UP DP SOLUTION
*/
const numSquares = (n) => {
const cache = new Array(n + 1);
cache.fill(Infinity);
cache[0] = 0;

for (let target = 1; target <= n; target++) {
let integer = 1;
let perfectSquare = 1;
while (perfectSquare <= target) {
cache[target] = Math.min(
cache[target],
cache[target - perfectSquare] + 1,
);
integer++;
perfectSquare = integer * integer;
}
}

return cache[n];
};

/*
TOP-DOWN DP SOLUTION
*/
53 changes: 53 additions & 0 deletions solutions/dp/wordBreak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
TOP-DOWN DP SOLUTION
*/

/**
* Reduces a sequence of names to initials.
* @param {String} s a string containing potential words
* @param {Set} wordSet a set containing all valid words
* @param {Number} start an index in s indicating where to start
* @param {Object} cache cache Object
* @return {Boolean} whether or not the string can be broken up into words
*/
const wordBreakHelper = (s, wordSet, start, cache) => {
if (cache.has(start)) return false;
if (start === s.length) return true;

for (let end = start + 1; end < s.length + 1; end++) {
const potentialWord = s.slice(start, end);

if (wordSet.has(potentialWord)) {
if (wordBreakHelper(s, wordSet, end, cache)) return true;
}
}

cache.add(start);
return false;
};

const wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);
return wordBreakHelper(s, wordSet, 0, new Set());
};

/*
BOTTOM-UP DP SOLUTION
*/
const wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);
const cache = new Array(s.length + 1);
cache.fill(false);
cache[0] = true;

for (let end = 1; end <= s.length; end++) {
for (let start = 0; start < end; start++) {
const w = s.slice(start, end);
if (cache[start] === true && wordSet.has(w)) {
cache[end] = true;
}
}
}

return cache[s.length];
};
13 changes: 13 additions & 0 deletions solutions/linked-lists/reverseLinkedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const reverseList = (head) => {
let current = head;
let previous = null;

while (current) {
const temporary = current.next;
current.next = previous;
previous = current
current = temporary;
}

return previous;
};