-
-
Notifications
You must be signed in to change notification settings - Fork 248
[soobing] WEEK 02 Solutions #1761
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
68e598d
feat(soobing): week2 > product-of-array-except-self
soobing 7882a55
feat(soobing): week2 > 3sum
soobing 4e82462
feat(soobing): week2 > validate-binary-search-tree
soobing cc68a6d
feat(soobing): week2 > climbing-stairs
soobing 1f116af
feat(soobing): week2 > valid-anagram
soobing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 문제 유형 | ||
* - Array (정렬 + 투포인터) | ||
* | ||
* 문제 설명 | ||
* - 3개의 수를 더해서 0이 되는 경우를 찾아서 배열로 반환하기 | ||
* | ||
* 아이디어 | ||
* 1) 정렬 후 투포인터 사용, 중복 제거 | ||
|
||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
const result: number[][] = []; | ||
|
||
// sorting | ||
nums.sort((a, b) => a - b); | ||
|
||
for (let i = 0; i < nums.length - 2; i++) { | ||
// 중복 제거 | ||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
||
let left = i + 1; | ||
let right = nums.length - 1; | ||
while (left < right) { | ||
const sum = nums[i] + nums[left] + nums[right]; | ||
if (sum === 0) { | ||
result.push([nums[i], nums[left], nums[right]]); | ||
left++; | ||
right--; | ||
|
||
// 중복 제거 | ||
while (left < right && nums[left] === nums[left - 1]) left++; | ||
while (left < right && nums[right] === nums[right + 1]) right--; | ||
} else if (sum < 0) { | ||
left++; | ||
} else { | ||
right--; | ||
} | ||
} | ||
} | ||
return result; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 문제 유형 | ||
* - DP (피보나치) | ||
* | ||
* 문제 설명 | ||
* - 계단을 올라가는 방법의 수를 구하기 | ||
* | ||
* 아이디어 | ||
* 1) 피보나치 수열 활용 | ||
* - climbStairs(n) = climbStairs(n-1) + climbStairs(n-2) | ||
*/ | ||
function climbStairsBottomUp(n: number): number { | ||
function fibonacci(n: number, memo = new Map<number, number>()) { | ||
if (n === 1) return 1; | ||
if (n === 2) return 2; | ||
|
||
if (memo.has(n)) return memo.get(n); | ||
const result = fibonacci(n - 1, memo) + fibonacci(n - 2, memo); | ||
memo.set(n, result); | ||
return result; | ||
} | ||
return fibonacci(n); | ||
} | ||
|
||
function climbStairsTopDown(n: number): number { | ||
const dp = new Array(n + 1).fill(0); | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
|
||
for (let i = 3; i <= n; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 2]; | ||
} | ||
return dp[n]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 문제 유형 | ||
* - Array | ||
* | ||
* 문제 설명 | ||
* - 자기 자신을 제외한 나머지의 곱 구하기 | ||
* | ||
* 아이디어 | ||
* 1) Array - 자기 자신보다 이전, 이후 의 누적곱(left, right) 구하고, 최종적으로 곱하기 | ||
* 2) 0읠 제외한 나머지의 곱, 0의 갯수 카운트를 이용하여 조건에 따라 계산하기 | ||
*/ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const answer = Array(nums.length).fill(0); | ||
let zeroCount = 0; | ||
const productWithoutZero = nums.reduce((acc, cur) => { | ||
if (cur === 0) { | ||
zeroCount++; | ||
return acc; | ||
} | ||
return cur * acc; | ||
}, 1); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (zeroCount > 0) { | ||
if (nums[i]) answer[i] = 0; | ||
else answer[i] = zeroCount > 1 ? 0 : productWithoutZero; | ||
} else { | ||
answer[i] = productWithoutZero / nums[i]; | ||
soobing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
return answer; | ||
} | ||
|
||
function productExceptSelfArrayVersion(nums: number[]): number[] { | ||
const answer = Array(nums.length); | ||
const left = Array(nums.length).fill(1); | ||
const right = Array(nums.length).fill(1); | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
left[i] = left[i - 1] * nums[i - 1]; | ||
} | ||
|
||
for (let i = nums.length - 2; i >= 0; i--) { | ||
right[i] = right[i + 1] * nums[i + 1]; | ||
} | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
answer[i] = left[i] * right[i]; | ||
} | ||
|
||
return answer; | ||
} |
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. 최적화 까지 진행 하신 부분이 너무 좋네요 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 문제 유형 | ||
* - String | ||
* | ||
* 문제 설명 | ||
* - 두 문자열이 애너그램인지 확인하기 | ||
* | ||
* 아이디어 | ||
* 1) 문자열을 맵으로 변환하고, 정렬 후 비교하기 | ||
* 2) 문자열 정렬 없이 하나의 map으로 더하고 빼기하여 0인지 확인하기 | ||
*/ | ||
function mapString(str: string) { | ||
const map = new Map<string, number>(); | ||
for (let i = 0; i < str.length; i++) { | ||
map.set(str[i], (map.get(str[i]) || 0) + 1); | ||
} | ||
return map; | ||
} | ||
function isAnagram(s: string, t: string): boolean { | ||
const sMap = mapString(s); | ||
const tMap = mapString(t); | ||
|
||
const sKeys = [...sMap.keys()].sort().join(""); | ||
const tKeys = [...tMap.keys()].sort().join(""); | ||
|
||
if (sKeys !== tKeys) return false; | ||
|
||
for (let i = 0; i < sKeys.length; i++) { | ||
const key = sKeys[i]; | ||
if (sMap.get(key) !== tMap.get(key)) return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// 아이디어 2 | ||
function isAnagramDeveloped(s: string, t: string): boolean { | ||
if (s.length !== t.length) return false; | ||
|
||
const count = new Map<string, number>(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
count.set(s[i], (count.get(s[i]) || 0) + 1); | ||
count.set(t[i], (count.get(t[i]) || 0) - 1); | ||
} | ||
|
||
for (const val of count.values()) { | ||
if (val !== 0) 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. DFS 로 풀이 하려면 어떤 풀이가 나올지 한번 도전해보시면 좋을것 같습니다! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 문제 유형 | ||
* - Tree | ||
* | ||
* 문제 설명 | ||
* - 이진 탐색 트리가 맞는지 확인하기 | ||
* | ||
* 아이디어 | ||
* 1) 중위 순회 후 정렬된 배열인지 확인 | ||
* | ||
*/ | ||
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 isSorted(arr: number[]) { | ||
for (let i = 1; i < arr.length; i++) { | ||
if (arr[i - 1] >= arr[i]) return false; | ||
} | ||
return true; | ||
} | ||
function inorder(node: TreeNode | null, arr: number[]) { | ||
if (node === null) return; | ||
inorder(node.left, arr); | ||
arr.push(node.val); | ||
inorder(node.right, arr); | ||
} | ||
function isValidBST(root: TreeNode | null): boolean { | ||
const sortedArray: number[] = []; | ||
inorder(root, sortedArray); | ||
return isSorted(sortedArray); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.