diff --git a/longest-consecutive-sequence/tolluset.ts b/longest-consecutive-sequence/tolluset.ts new file mode 100644 index 000000000..7e469271f --- /dev/null +++ b/longest-consecutive-sequence/tolluset.ts @@ -0,0 +1,42 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function longestConsecutive(nums: number[]): number { + const n = nums.length; + + if (n <= 1) { + return n; + } + + const numsSet = new Set(nums); + let longestLen = 0; + + for (let i = 0; i < n; i++) { + let current = nums[i]; + + if (!numsSet.has(current - 1)) { + let currentLen = 1; + + while (numsSet.has(current + 1)) { + current++; + currentLen++; + } + + longestLen = Math.max(longestLen, currentLen); + } + } + return longestLen; +} + +const t1 = longestConsecutive([100, 4, 200, 1, 3, 2]); +console.info("🚀 : tolluset.ts:5: t1=", t1); // 4 + +const t2 = longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1]); +console.info("🚀 : tolluset.ts:8: t2=", t2); // 9 + +const t3 = longestConsecutive([9, 1, 4, 7, 3, -1, 0, 5, 8, -1, 6]); +console.info("🚀 : tolluset.ts:40: t3=", t3); // 7 + +const t4 = longestConsecutive([-6, -1, -1, 9, -8, -6, -6, 4, 4, -3, -8, -1]); +console.info("🚀 : tolluset.ts:59: t4=", t4); // 1 diff --git a/maximum-subarray/tolluset.ts b/maximum-subarray/tolluset.ts new file mode 100644 index 000000000..361f37672 --- /dev/null +++ b/maximum-subarray/tolluset.ts @@ -0,0 +1,39 @@ +/* + * TC: O(n) + * SC: O(1) + * */ +function maxProduct(nums: number[]): number { + const n = nums.length; + + if (n === 1) { + return nums[0]; + } + + let max = 0, + min = 0, + res = 0; + + for (let i = 0; i < n; i++) { + const cur = nums[i]; + + if (cur < 0) { + [max, min] = [min, max]; + } + + max = Math.max(cur, max * cur); + min = Math.min(cur, min * cur); + + res = Math.max(res, max); + } + + return res; +} + +const t1 = maxProduct([2, 3, -2, 4]); +console.info("🚀 : tolluset.ts:3: t1=", t1); // 6 + +const t2 = maxProduct([-2, 0, -1]); +console.info("🚀 : tolluset.ts:6: t2=", t2); // 0 + +const t3 = maxProduct([-2]); +console.info("🚀 : tolluset.ts:34: t3=", t3); // -2 diff --git a/missing-number/tolluset.ts b/missing-number/tolluset.ts new file mode 100644 index 000000000..c8456434e --- /dev/null +++ b/missing-number/tolluset.ts @@ -0,0 +1,21 @@ +/* + * TC: O(n) + * SC: O(1) + * */ +function missingNumber(nums: number[]): number { + const n = nums.length; + + const sum = (n * (n + 1)) / 2; + const actualSum = nums.reduce((acc, curr) => acc + curr, 0); + + return sum - actualSum; +} + +const t1 = missingNumber([3, 0, 1]); +console.info("🚀 : tolluset.ts:3: t1=", t1); // 2 + +const t2 = missingNumber([0, 1]); +console.info("🚀 : tolluset.ts:6: t2=", t2); // 2 + +const t3 = missingNumber([9, 6, 4, 2, 3, 5, 7, 0, 1]); +console.info("🚀 : tolluset.ts:9: t3=", t3); // 8 diff --git a/valid-palindrome/tolluset.ts b/valid-palindrome/tolluset.ts new file mode 100644 index 000000000..c980841b6 --- /dev/null +++ b/valid-palindrome/tolluset.ts @@ -0,0 +1,29 @@ +/* + * TC: O(n) + * SC: O(n) + * */ +function isPalindrome(s: string): boolean { + const parsedString = s.replace(/[^A-Za-z0-9]/g, "").toLowerCase(); + const n = parsedString.length; + + if (n === 0) { + return true; + } + + for (let i = 0; i < n / 2; i++) { + if (parsedString[i] !== parsedString[n - i - 1]) { + return false; + } + } + + return true; +} + +const t1 = isPalindrome("A man, a plan, a canal: Panama"); +console.info("🚀 : tolluset.ts:18: t1=", t1); // true + +const t2 = isPalindrome("race a car"); +console.info("🚀 : tolluset.ts:21: t2=", t2); // false + +const t3 = isPalindrome(" "); +console.info("🚀 : tolluset.ts:24: t3=", t3); // ture diff --git a/word-search/tolluset.ts b/word-search/tolluset.ts new file mode 100644 index 000000000..6cd3bc66d --- /dev/null +++ b/word-search/tolluset.ts @@ -0,0 +1,83 @@ +/* + * TC: O(row * column * 4^words.length) + * SC: O(n) + * */ +function exist(board: string[][], word: string): boolean { + const words = word.split(""); + const n = words.length; + + const rowLen = board.length; + const columnLen = board[0].length; + + const dfs = (row: number, column: number, cursor: number) => { + if (cursor === n) { + return true; + } + + if ( + row < 0 || + row >= rowLen || + column < 0 || + column >= columnLen || + board[row][column] !== words[cursor] + ) { + return false; + } + + const current = board[row][column]; + board[row][column] = ""; + + if ( + dfs(row - 1, column, cursor + 1) || + dfs(row + 1, column, cursor + 1) || + dfs(row, column - 1, cursor + 1) || + dfs(row, column + 1, cursor + 1) + ) { + return true; + } + + board[row][column] = current; + + return false; + }; + + for (let row = 0; row < rowLen; row++) { + for (let column = 0; column < columnLen; column++) { + if (dfs(row, column, 0)) { + return true; + } + } + } + + return false; +} + +const t1 = exist( + [ + ["A", "B", "C", "E"], + ["S", "F", "C", "S"], + ["A", "D", "E", "E"], + ], + "ABCCED", +); +console.info("🚀 : tolluset.ts:5: t1=", t1); // true + +const t2 = exist( + [ + ["A", "B", "C", "E"], + ["S", "F", "C", "S"], + ["A", "D", "E", "E"], + ], + "SEE", +); +console.info("🚀 : tolluset.ts:15: t2=", t2); // true + +const t3 = exist( + [ + ["A", "B", "C", "E"], + ["S", "F", "C", "S"], + ["A", "D", "E", "E"], + ], + "ABCB", +); +console.info("🚀 : tolluset.ts:24: t3=", t3); // false