Skip to content

Commit

Permalink
Merge pull request #412 from tolluset/main
Browse files Browse the repository at this point in the history
[μ΄λ³‘ν˜„] Week 4
  • Loading branch information
tolluset authored Sep 4, 2024
2 parents d561e58 + 378093f commit 7c3afab
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
42 changes: 42 additions & 0 deletions longest-consecutive-sequence/tolluset.ts
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions maximum-subarray/tolluset.ts
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions missing-number/tolluset.ts
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions valid-palindrome/tolluset.ts
Original file line number Diff line number Diff line change
@@ -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
83 changes: 83 additions & 0 deletions word-search/tolluset.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7c3afab

Please sign in to comment.