From e3c25bca3bd8f16dfe99dfea915fbce3c8ff77b7 Mon Sep 17 00:00:00 2001 From: Zai Santillan Date: Fri, 31 Mar 2023 13:31:04 +0800 Subject: [PATCH] feat(LeetCode): add solutions --- LeetCode/best-time-to-buy-and-sell-stock.ts | 32 ++++++++++++++++ ...dex-of-the-first-occurrence-in-a-string.ts | 16 ++++++++ ...ngest-continuous-increasing-subsequence.ts | 3 +- LeetCode/move-zeroes.ts | 34 +++++++++++++++++ LeetCode/pascals-triangle.ts | 37 +++++++++++++++++++ LeetCode/ransom-note.ts | 18 +++++++++ ...product-and-sum-of-digits-of-an-integer.ts | 2 + LeetCode/valid-palindrome.ts | 4 +- 8 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 LeetCode/best-time-to-buy-and-sell-stock.ts create mode 100644 LeetCode/move-zeroes.ts create mode 100644 LeetCode/pascals-triangle.ts create mode 100644 LeetCode/ransom-note.ts diff --git a/LeetCode/best-time-to-buy-and-sell-stock.ts b/LeetCode/best-time-to-buy-and-sell-stock.ts new file mode 100644 index 0000000..c5fa441 --- /dev/null +++ b/LeetCode/best-time-to-buy-and-sell-stock.ts @@ -0,0 +1,32 @@ +// Solution 1 +function maxProfit(prices: number[]): number { + let l = 0; + let r = 0; + let maxProfit = 0; + + while (l < prices.length) { + if (prices[l] < prices[r]) { + maxProfit = Math.max(maxProfit, prices[r] - prices[l]); + } else { + l = r; + } + r++; + } + + return maxProfit; +} + +// Solution 2 +function maxProfit2(prices: number[]): number { + let mn = prices[0]; + let mx = 0; + + for (let i = 0; i < prices.length; i++) { + mn = Math.min(mn, prices[i]); + mx = Math.max(mx, prices[i] - mn); + } + + return mx; +} + +console.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5 diff --git a/LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts b/LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts index cd23930..6dbdcc2 100644 --- a/LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts +++ b/LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts @@ -1,3 +1,19 @@ +// Solution 1 function strStr(haystack: string, needle: string): number { return haystack.indexOf(needle); } + +// Solution 2: using sliding window +function strStr2(haystack: string, needle: string): number { + let n = haystack.length; + let m = needle.length; + + for (let windowStart = 0; windowStart <= n - m; windowStart++) { + for (let i = 0; i < m; i++) { + if (needle[i] !== haystack[windowStart + i]) break; + if (i === m - 1) return windowStart; + } + } + + return -1; +} diff --git a/LeetCode/longest-continuous-increasing-subsequence.ts b/LeetCode/longest-continuous-increasing-subsequence.ts index fab9a6b..3febdeb 100644 --- a/LeetCode/longest-continuous-increasing-subsequence.ts +++ b/LeetCode/longest-continuous-increasing-subsequence.ts @@ -1,5 +1,6 @@ // https://leetcode.com/problems/longest-continuous-increasing-subsequence/solutions/3359434/javascript-typescript-solution/ +// Solution 1 function findLengthOfLCIS(nums: number[]): number { let cur = 1; let ans = 1; @@ -42,7 +43,7 @@ function findLengthOfLCIS(nums: number[]): number { // cur=2, ans=3 -// using DP +// Solution 2: using DP function findLengthOfLCIS2(nums: number[]): number { const dp = new Array(nums.length).fill(1); diff --git a/LeetCode/move-zeroes.ts b/LeetCode/move-zeroes.ts new file mode 100644 index 0000000..71b1d55 --- /dev/null +++ b/LeetCode/move-zeroes.ts @@ -0,0 +1,34 @@ +/** + Do not return anything, modify nums in-place instead. + */ + +// Solution 1 +function moveZeroes(nums: number[]): void { + let pos = 0; // last non zero index + + for (let i = 0; i < nums.length; i++) { + if (nums[i] !== 0) nums[pos++] = nums[i]; + } + + for (let i = pos; i < nums.length; i++) nums[i] = 0; + + console.log(nums); +} + +// Solution 2 +function moveZeroes2(nums: number[]): void { + let pos = 0; + + for (let i = 0; i < nums.length; i++) { + if (nums[i]) { + [nums[i], nums[pos]] = [nums[pos], nums[i]]; + pos++; + } + } + + console.log(nums); +} + +moveZeroes([0, 1, 0, 3, 12]); // [1,3,12,0,0] +moveZeroes([0]); // [0] +moveZeroes([0, 0, 1]); // [1, 0, 0] diff --git a/LeetCode/pascals-triangle.ts b/LeetCode/pascals-triangle.ts new file mode 100644 index 0000000..8dfb15f --- /dev/null +++ b/LeetCode/pascals-triangle.ts @@ -0,0 +1,37 @@ +// Solution 1 +function generate(numRows: number): number[][] { + let triangle: number[][] = []; + + for (let i = 0; i < numRows; i++) { + let row: number[] = []; + + for (let j = 0; j <= i; j++) { + if (j === 0 || j === i) { + row.push(1); + } else { + row.push(triangle[i - 1][j - 1] + triangle[i - 1][j]); + } + } + + triangle.push(row); + } + + return triangle; +} + +// Solution 2 +function generate2(numRows: number): number[][] { + let ans: number[][] = []; + + for (let i = 0; i < numRows; i++) { + ans[i] = [1]; + + for (let j = 1; j < i; j++) { + ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j]; + } + + ans[i][i] = 1; + } + + return ans; +} diff --git a/LeetCode/ransom-note.ts b/LeetCode/ransom-note.ts new file mode 100644 index 0000000..16522b6 --- /dev/null +++ b/LeetCode/ransom-note.ts @@ -0,0 +1,18 @@ +function canConstruct(ransomNote: string, magazine: string): boolean { + const map: Record = {}; + + for (let letter of magazine) { + map[letter] = ++map[letter] || 1; + } + + for (let letter of ransomNote) { + if (!map[letter]) return false; + map[letter]--; + } + + return true; +} + +console.log(canConstruct('a', 'b')); // false +console.log(canConstruct('aa', 'ab')); // false +console.log(canConstruct('aa', 'aab')); // true diff --git a/LeetCode/subtract-the-product-and-sum-of-digits-of-an-integer.ts b/LeetCode/subtract-the-product-and-sum-of-digits-of-an-integer.ts index 2c7ce38..b941136 100644 --- a/LeetCode/subtract-the-product-and-sum-of-digits-of-an-integer.ts +++ b/LeetCode/subtract-the-product-and-sum-of-digits-of-an-integer.ts @@ -1,3 +1,4 @@ +// Solution 1 function subtractProductAndSum(n: number): number { let sum = 0, product = 1; @@ -11,6 +12,7 @@ function subtractProductAndSum(n: number): number { return product - sum; } +// Solution 2 function subtractProductAndSum2(n: number): number { // let x = Array.from(String(n), Number); let x = String(n).split('').map(Number); // same above diff --git a/LeetCode/valid-palindrome.ts b/LeetCode/valid-palindrome.ts index 0fb296f..d0fcde8 100644 --- a/LeetCode/valid-palindrome.ts +++ b/LeetCode/valid-palindrome.ts @@ -1,10 +1,11 @@ +// Solution 1 function isPalindrome(s: string): boolean { let str = s.replace(/[^a-z0-9]/gi, '').toLowerCase(); return str === str.split('').reverse().join(''); } -// using two pointers +// Solution 2: using two pointers function isPalindrome2(s: string): boolean { let str = s.replace(/[^a-z0-9]/gi, '').toLowerCase(); @@ -13,6 +14,7 @@ function isPalindrome2(s: string): boolean { while (l < r) { if (str[l] !== str[r]) return false; + l++, r--; } return true; }