-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
}; |
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); | ||
}; |
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; | ||
}; |
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 | ||
*/ |
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]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const copyRandomList = (head) => { | ||
let copies = new Map(); | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every variable in this function can be turned into a That is bad^ But this is fine: 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do |
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name this better. Maybe like |
||
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; | ||
}; |
There was a problem hiding this comment.
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).