Skip to content

[gitsunmin] WEEK 01 Solutions #323

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

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions contains-duplicate/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* https://leetcode.com/problems/contains-duplicate/
* time complexity : O(n)
* space complexity : O(n)
*/
function containsDuplicate(nums: number[]): boolean {
return new Set(nums).size !== nums.length
};
73 changes: 73 additions & 0 deletions kth-smallest-element-in-a-bst/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* https://leetcode.com/problems/kth-smallest-element-in-a-bst/
* time complexity : O(n)
* space complexity : O(n)
*/

/**
* * 문제에서 정의된 타입
*/
class TreeNode {
val: number
left: TreeNode | null
right: TreeNode | null
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val === undefined ? 0 : val)
this.left = (left === undefined ? null : left)
this.right = (right === undefined ? null : right)
}
}

/**
* ! 문제에서의 Input과 실제 정의된, 사용되는 input이 다르기 때문에, 한 번 변환 작업을 거처야함. (실제 제출 시 제외한 함수 입니다.)
*/
const arrayToTreeNode = (arr: Array<number | null>) => (k: number): [TreeNode | null, number] => {
let root = new TreeNode(arr[0]!);
let queue: (TreeNode | null)[] = [root];
let i = 1;

while (i < arr.length) {
let current = queue.shift();
if (current !== null && current !== undefined) {
if (arr[i] !== null) {
current.left = new TreeNode(arr[i]!);
queue.push(current.left);
}
i++;
if (i < arr.length && arr[i] !== null) {
current.right = new TreeNode(arr[i]!);
queue.push(current.right);
}
i++;
}
}

return [root, k];
}

// const input1 = arrayToTreeNode([3, 1, 4, null, 2]);
// const input2 = arrayToTreeNode([5, 3, 6, 2, 4, null, null, 1]);

// const output1 = kthSmallest(...input1(1))
// const output2 = kthSmallest(...input2(3))

// console.log('output1:', output1);
// console.log('output2:', output2);


function inOrderTraversal(node: TreeNode | null): number[] {
if (node === null) {
return [];
}

return [
...inOrderTraversal(node.left),
node.val,
...inOrderTraversal(node.right)
];
}

function kthSmallest(root: TreeNode | null, k: number): number {
const result = inOrderTraversal(root);
return result[k - 1]
}
8 changes: 8 additions & 0 deletions number-of-1-bits/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* https://leetcode.com/problems/number-of-1-bits/
* time complexity : O(log n)
* space complexity : O(log n)
*/
function hammingWeight(n: number): number {
return n.toString(2).replaceAll('0', '').length
}
Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

이렇게 간단하게 해결하는 방법도 있었네요! 👍👍

21 changes: 21 additions & 0 deletions palindromic-substrings/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* https://leetcode.com/problems/palindromic-substrings/
* time complexity : O(n^2)
* space complexity : O(n)
*/

const expandAroundCenter = (s: string, left: number, right: number): number => {
if (left < 0 || right >= s.length || s[left] !== s[right]) {
return 0;
}
return 1 + expandAroundCenter(s, left - 1, right + 1);
};
Comment on lines +7 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요! expandAroundCenter 함수를 countSubstrings 외부로 뺀 이유가 있으실까요?
countSubstrings 안에서 함수를 정의하면 파라매터로 s를 굳이 넘겨주지 않아도 될 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nhistory
안녕하세요
아 넵 함수 내에서 다른 스코프의 변수를 참조하는게 안 좋다고 생각해서 파라미터로 받도록 했어요

Copy link
Contributor

Choose a reason for hiding this comment

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

side effect를 고려하는 좋은 사고라고 생각합니다 어떤 분은 그런 pure function은 네이밍 컨벤션을 calc로 시작하도록 맞추기도 하시더라고요


function countSubstrings(s: string): number {

return [...Array(s.length).keys()].reduce((totalCount, i) => {
return totalCount +
expandAroundCenter(s, i, i) +
expandAroundCenter(s, i, i + 1);
}, 0);
};
22 changes: 22 additions & 0 deletions top-k-frequent-elements/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* https://leetcode.com/problems/top-k-frequent-elements/
* time complexity : O(n)
* space complexity : O(n)
*/
function topKFrequent(nums: number[], k: number): number[] {
const record = nums.reduce((acc, curr) => {
acc[curr] = (acc[curr] ?? 0) + 1;
return acc;
}, {});

const array: Array<number[]> = new Array(nums.length);
for (const num in record) {
const v = record[num];
if (!array[v]) {
array[v] = [];
}
array[v].push(Number(num));
}

return array.reduce((acc, curr) => [...curr, ...acc], []).slice(0, k);
};