-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
LeetCode/find-the-index-of-the-first-occurrence-in-a-string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters