Skip to content
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
31 changes: 31 additions & 0 deletions 3sum/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);

const result: number[][] = [];
for (let i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;
const target = -nums[i];

let j = i + 1;
let k = nums.length - 1;
while (j < k) {
const currentSum = nums[j] + nums[k];

if (target < currentSum) {
k--;
} else if (target > currentSum) {
j++;
} else {
result.push([nums[i], nums[j], nums[k]]);

j++;
k--;

while (j < k && nums[j] === nums[j - 1]) j++;
while (j < k && nums[k] === nums[k + 1]) k--;
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

내부에 반복문 사용으로 최적화 시킨 부분이 너무 좋네요

}
}
}

return result;
}
11 changes: 11 additions & 0 deletions climbing-stairs/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function climbStairs(n: number): number {
const dp: number[] = Array.from({ length: n + 1 }, () => 0);
dp[0] = 1;
dp[1] = 1;

for (let i = 2; i < n + 1; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

return dp[n];
}
16 changes: 16 additions & 0 deletions product-of-array-except-self/hyer0705.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function productExceptSelf(nums: number[]): number[] {
const n = nums.length;
const result = Array(n).fill(1);

for (let i = 1; i < n; i++) {
result[i] = nums[i - 1] * result[i - 1];
}

let suffixProduct = 1;
for (let i = n - 1; i >= 0; i--) {
result[i] = result[i] * suffixProduct;
suffixProduct *= nums[i];
}

return result;
}
20 changes: 20 additions & 0 deletions valid-anagram/hyer0705.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Map을 하나만 설정해서 s는 +, t는 -를 진행하고 마지막에 value가 0이 아닌게 하나라도 있으면 false를 리턴하면 공간 복잡도를 더 줄일 수 있지 않을까요? :)
Map 대신 길이 28자리 Array로 알파벳의 인덱스에 +,- 를 진행하면 공간을 더 효율화 할 수도 있을것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Map을 한 개 사용하거나 Array를 사용하는 법은 생각지 못했는데 감사합니다! 다음에 한 번 더 풀어볼 때는 말해주신 방법으로 공간 복잡도를 줄여볼게요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;

const sCountMap = new Map<string, number>();
const tCountMap = new Map<string, number>();

for (let i = 0; i < s.length; i++) {
const currentS = s[i];
const currentT = t[i];

sCountMap.set(currentS, (sCountMap.get(currentS) || 0) + 1);
tCountMap.set(currentT, (tCountMap.get(currentT) || 0) + 1);
}

for (const [k, v] of sCountMap) {
if (!tCountMap.has(k) || tCountMap.get(k) !== v) return false;
}

return true;
}
32 changes: 32 additions & 0 deletions validate-binary-search-tree/hyer0705.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

BFS사용으로 풀이해 주셨네요! DFS 풀이도 도전해 보시면 좋을것 같습니다 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

다음에는 DFS로 풀이해보겠습니다~

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* 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)
* }
* }
*/

function isValidBST(root: TreeNode | null): boolean {
const queue: [TreeNode, number, number][] = [[root, -Infinity, +Infinity]];

while (queue.length > 0) {
const [currentNode, minVal, maxVal] = queue.shift();

if (minVal >= currentNode.val || currentNode.val >= maxVal) return false;

if (currentNode.left) {
queue.push([currentNode.left, minVal, currentNode.val]);
}
if (currentNode.right) {
queue.push([currentNode.right, currentNode.val, maxVal]);
}
}

return true;
}