-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hyer0705] WEEK 02 solutions #1765
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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--; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} |
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]; | ||
} |
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; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map을 하나만 설정해서 s는 +, t는 -를 진행하고 마지막에 value가 0이 아닌게 하나라도 있으면 false를 리턴하면 공간 복잡도를 더 줄일 수 있지 않을까요? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BFS사용으로 풀이해 주셨네요! DFS 풀이도 도전해 보시면 좋을것 같습니다 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
내부에 반복문 사용으로 최적화 시킨 부분이 너무 좋네요