Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[이병현] Week 4 #412

Merged
merged 12 commits into from
Sep 4, 2024
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] = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 방문 체크를 격자판에 직접 체크하면 방문 배열이 필요없군요 ㅎㅎ


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