Skip to content

Commit

Permalink
feat(LeetCode): add solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
plskz committed Mar 31, 2023
1 parent f887ce3 commit e3c25bc
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 2 deletions.
32 changes: 32 additions & 0 deletions LeetCode/best-time-to-buy-and-sell-stock.ts
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion LeetCode/longest-continuous-increasing-subsequence.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
34 changes: 34 additions & 0 deletions LeetCode/move-zeroes.ts
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions LeetCode/pascals-triangle.ts
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions LeetCode/ransom-note.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function canConstruct(ransomNote: string, magazine: string): boolean {
const map: Record<string, number> = {};

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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Solution 1
function subtractProductAndSum(n: number): number {
let sum = 0, product = 1;

Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion LeetCode/valid-palindrome.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -13,6 +14,7 @@ function isPalindrome2(s: string): boolean {

while (l < r) {
if (str[l] !== str[r]) return false;
l++, r--;
}
return true;
}
Expand Down

0 comments on commit e3c25bc

Please sign in to comment.