Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1177 from aakhtar3/1dp
Browse files Browse the repository at this point in the history
refactor: js - 1dp
  • Loading branch information
aakhtar3 authored Sep 24, 2022
2 parents 7f95db3 + 8377ec3 commit 66fbb23
Show file tree
Hide file tree
Showing 12 changed files with 1,276 additions and 174 deletions.
193 changes: 176 additions & 17 deletions javascript/139-Word-Break.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,179 @@
let wordBreak = function (s, wordDict) {
let dp = new Array(s.length + 1).fill(false);
dp[s.length] = true;

for (let i = s.length - 1; i >= 0; i--) {
for (let w of wordDict) {
if (
i + w.length <= s.length &&
s.substring(i, i + w.length) === w
) {
dp[i] = dp[i + w.length];
}
if (dp[i]) {
break;
}
/**
* Brute Force - DFS
* Hash Set - Distinct Keys
* Time O(2^N) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);/* Time O(N) | Space O(N) */

return canBreak(s, wordSet); /* Time O(2^N) | Space O(N) */
};

var canBreak = (s, wordSet, start = 0) => {
const isBaseCase = (start === s.length);
if (isBaseCase) return true;

return dfs(s, wordSet, start);/* Time O(2^N) | Space O(N) */
}

var dfs = (s, wordSet, start) => {
for (let end = (start + 1); end <= s.length; end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */

const _canBreak = wordSet.has(word)
&& canBreak(s, wordSet, end); /* Time O(2^N) | Space O(N) */
if (_canBreak) return true;
}

return false;
}

/**
* DP - Top Down
* Array - Memoization
* Hash Set - Distinct Keys
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict); /* Time O(N) | Space O(N) */
const memo = new Array(s.length).fill(null); /* | Space O(N) */
const start = 0;

return canBreak(s, wordSet, start, memo); /* Time O(N * N * N) | Space O(N) */
}

var canBreak = (s, wordSet, start, memo) => {
const isBaseCase1 = (s.length === start);
if (isBaseCase1) return true;

const hasSeen = (memo[start] !== null);
if (hasSeen) return memo[start];

return dfs(s, wordSet, start, memo);/* Time O(N * N * N) | Space O(N) */
}

var dfs = (s, wordSet, start, memo) => {
for (let end = (start + 1); (end <= s.length); end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */

const _canBreak = wordSet.has(word)
&& canBreak(s, wordSet, end, memo); /* Time O(N * N) */
if (_canBreak) {
memo[start] = true;
return true;
}
}

return dp[0];
};
memo[start] = false;
return false;
}

/**
* DP - Bottom Up
* Array - Tabulation
* Hash Set - Distinct Keys
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);/* Time O(N) | Space O(N) */
const tabu = initTabu(s); /* | Space O(N) */

canBreak(s, wordSet, tabu); /* Time O(N * N * N) | Space O(N) */

return tabu[s.length];
}

const initTabu = (s) => {
const tabu = new Array((s.length + 1)).fill(false);/* Space O(N) */

tabu[0] = true;

return tabu;
}

var canBreak = (s, wordSet, tabu) => {
for (let end = 1; (end <= s.length); end++) {/* Time O(N) */
checkWord(s, wordSet, end, tabu); /* Time O(N * N) | Space O(N) */
}
}

var checkWord = (s, wordSet, end, tabu) => {
for (let start = 0; (start < end); start++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */

const canBreak = tabu[start] && wordSet.has(word);
if (!canBreak) continue;

tabu[end] = true;

return;
}
}

/**
* Tree Traversal - BFS
* Queue - Level Order Space O(WIDTH)
* Hash Set - Distinct Keys
* Array - Seen
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function(s, wordDict) {
const wordSet = new Set(wordDict); /* Time O(N) | Space O(N) */
const queue = new Queue([ 0 ]); /* | Space O(N) */
const seen = new Array(s.length).fill(false);/* | Space O(N) */

return bfs(queue, s, wordSet, seen); /* Time O(N * N * N) | Space O(N + WIDTH) */
}

const bfs = (queue, s, wordSet, seen) => {
while (!queue.isEmpty()) {
for (let level = (queue.size() - 1); (0 <= level); level--) {/* Time O(N) */
if (canWordBreak(queue, s, wordSet, seen)) return true; /* Time O(N * N) | Space O(N + WIDTH) */
}
}

return false;
}

var canWordBreak = (queue, s, wordSet, seen) => {
const start = queue.dequeue();

const hasSeen = seen[start];
if (hasSeen) return false;

if (canBreak(queue, s, start, wordSet)) return true;/* Time O(N * N) | Space O(N + WIDTH) */

seen[start] = true; /* | Space O(N) */
return false;
}

var canBreak = (queue, s, start, wordSet) => {
for (let end = start + 1; end <= s.length; end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */

if (!wordSet.has(word)) continue;

queue.enqueue(end); /* | Space O(WIDTH) */

const _canBreak = end === s.length;
if (_canBreak) return true;
}

return false
}
66 changes: 53 additions & 13 deletions javascript/152-Maximum-Product-Subarray.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
/**
* Brute Force - Linear Search
* Time O(N^2) | Space O(1)
* https://leetcode.com/problems/maximum-product-subarray/
* @param {number[]} nums
* @return {number}
*/
var maxProduct = function (nums) {
let result = nums[0];
let prevMax = nums[0];
let prevMin = nums[0];
for (let i = 1; i < nums.length; i++) {
currMax = Math.max(nums[i], prevMax * nums[i], prevMin * nums[i]);
currMin = Math.min(nums[i], prevMax * nums[i], prevMin * nums[i]);

prevMax = currMax;
prevMin = currMin;

result = Math.max(currMax, result);
var maxProduct = (nums) => {
const isEmpty = nums.length === 0;
if (isEmpty) return 0;

return linearSearch(nums);/* Time O(N * N) */
}

const linearSearch = (nums, max = nums[0]) => {
for (let index = 0; index < nums.length; index++) {/* Time O(N) */
max = getMax(nums, index, max); /* Time O(N) */
}

return max;
}

const getMax = (nums, index, max, product = 1) => {
for (let num = index; num < nums.length; num++) {/* Time O(N) */
product *= nums[num];
max = Math.max(max, product);
}
return result;

return max;
}

/**
* Greedy - product
* Time O(N) | Space O(1)
* https://leetcode.com/problems/maximum-product-subarray/
* @param {number[]} nums
* @return {number}
*/
var maxProduct = (nums) => {
const isEmpty = nums.length === 0;
if (isEmpty) return 0;

return greedySearch(nums);/* Time O(N) */
};

const greedySearch = (nums) => {
let min = max = product = nums[0];

for (let num = 1; num < nums.length; num++) {/* Time O(N) */
const [ minProduct, maxProduct ] = [ (min * nums[num]), (max * nums[num]) ];

min = Math.min(maxProduct, minProduct, nums[num]);
max = Math.max(maxProduct, minProduct, nums[num]);

product = Math.max(product, max);
}

return product;
}
109 changes: 100 additions & 9 deletions javascript/198-House-Robber.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
function rob(nums) {
let rob1 = 0;
let rob2 = 0;

for (const n of nums) {
let temp = Math.max(n + rob1, rob2);
rob1 = rob2;
rob2 = temp;
/**
* Brute Force - DFS
* Time O(2^N) | Space O(N)
* https://leetcode.com/problems/house-robber/
* @param {number[]} nums
* @return {number}
*/
var rob = (nums, i = 0) => {
const isBaseCase = nums <= i;
if (isBaseCase) return 0;

const [ next, nextNext ] = [ (i + 1), (i + 2) ];
const right = nums[i];
const mid = rob(nums, next); /* Time O(2^N) | Space O(N) */
const left = rob(nums, nextNext);/* Time O(2^N) | Space O(N) */
const house = left + right;

return Math.max(house, mid);
};

/**
* DP - Top Down
* Array - Memoization
* Time O(N) | Space O(N)
* https://leetcode.com/problems/house-robber/
* @param {number[]} nums
* @return {number}
*/
var rob = (nums, i = 0, memo = initMemo(nums)) => {
const isBaseCase = nums.length <= i;
if (isBaseCase) return 0;

const hasSeen = 0 <= memo[i];
if (hasSeen) return memo[i];

const [ next, nextNext ] = [ (i + 1), (i + 2) ];
const right = nums[i];
const mid = rob(nums, next, memo); /* Time O(N) | Space O(N) */
const left = rob(nums, nextNext, memo);/* Time O(N) | Space O(N) */
const house = left + right;

memo[i] = Math.max(mid, house); /* | Space O(N) */

return memo[i];
};

const initMemo = (nums) => Array(nums.length + 1).fill(-1);

/**
* DP - Bottom Up
* Array - Tabulation
* Time O(N) | Space O(N)
* https://leetcode.com/problems/house-robber/
* @param {number[]} nums
* @return {number}
*/
var rob = (nums) => {
if (!nums.length) return 0;

const tabu = initTabu(nums);

for (let i = 1; i < nums.length; i++) {/* Time O(N) */
const right = nums[i];
const mid = tabu[i];
const left = tabu[i - 1];
const house = left + right;

tabu[i + 1] = Math.max(mid, house); /* Space O(N) */
}

return rob2;
return tabu[nums.length]
};

const initTabu = (nums) => {
const tabu = Array(nums.length + 1).fill(0);

tabu[1] = nums[0];

return tabu;
}

/**
* DP - Bottom Up
* Time O(N) | Space O(1)
* https://leetcode.com/problems/house-robber/
* @param {number[]} nums
* @return {number}
*/
var rob = (nums) => {
if (!nums.length) return 0;

let [ left, mid ] = [ 0, 0 ];

for (const right of nums) {/* Time O(N) */
const temp = mid;
const house = left + right;

mid = Math.max(mid, house);
left = temp;
}

return mid;
};
Loading

0 comments on commit 66fbb23

Please sign in to comment.