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 copyListWithRandomPointer #6

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];
};
60 changes: 60 additions & 0 deletions solutions/linked-lists/copyListWithRandomPointer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const copyRandomList = (head) => {
let copies = new Map();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment explaining why you used a Map instead of an object (this is an object const x = {};).

I believe the reason is because you want to use an instance of the Node class as your key (JS objects only allow strings as the keys).

copies.set(null, null);

let current = head;

while (current) {
const copy = new Node(current.val);
copies.set(current, copy);
current = current.next;
}

current = head;

while (current) {
const copy = copies.get(current);
copy.next = copies.get(current.next);
copy.random = copies.get(current.random);
current = current.next;
}

return copies.get(head);
};

/*
O(1) space solution. Honestly, don't even worry about knowing how to do this.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this seems like something you shouldn't have to know how to do. But, on my Amazon interviews, I got this question and the interviewer definitely seemed to expect me to get the O(1) solution (I finished the O(n) one pretty fast and she spent quite a long time watching me struggled to get O(1)). I think we should still tell them to not worry about knowing how to do this, but what do you think?

*/
const copyRandomList = (head) => {
if (!head) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment or some comments explaining how this works, I think it's not very intuitive.


let current = head;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every variable in this function can be turned into a const instead of let. const means that you cannot reassign variables e.g.
const x = {};
x = {h: 5};

That is bad^

But this is fine:
const x = {};
x.h = 5;

Change this in the O(n) solution too


while (current) {
let copy = new Node(current.val, current.next, null);
copy.next = current.next;
current.next = copy;
current = current.next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do current = current.next.next;, no need for this to be 2 lines.

current = current.next;
}

current = head;

while(current) {
current.next.random = current.random ? current.random.next : null;
current = current.next.next;
}

current = head;
let copy = head.next;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name this better. Maybe like headOfCopy.

let copyPointer = copy;

while(current) {
current.next = current.next.next;
current = current.next;
copyPointer.next = copyPointer.next ? copyPointer.next.next : null;
copyPointer = copyPointer.next;
}

return copy;
};