-
-
Notifications
You must be signed in to change notification settings - Fork 304
[yuhyeon99] WEEK 02 solutions #2049
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,26 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[][]} | ||
| */ | ||
| var threeSum = function(nums) { | ||
| var answer = new Set(); | ||
| 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 usedNumSet = new Set(); | ||
| for(let j = i + 1; j < nums.length; j ++) { | ||
| let target = -(nums[i] + nums[j]); | ||
|
|
||
| if(usedNumSet.has(target)) { | ||
| let triplets = [nums[i], nums[j], target].sort((a, b) => a - b); | ||
| answer.add(triplets.join(',')); | ||
| } | ||
|
|
||
| usedNumSet.add(nums[j]); | ||
| } | ||
| } | ||
|
|
||
| return [...answer].map(e => e.split(',').map(Number)); | ||
| }; |
|
Contributor
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. 계단 오르기 문제는 저는 개인적으로 몇 번 풀어 봤는데, 하나씩 구하다 보니 결국 피보나치 수열의 모양이 되더라구요. 이거는 다른 책에서 본 부분인데, 계단을 0개 오르는 경우의 수도 1개로 친다는 말이 있더군요. 그렇게 생각하면 완벽한 피보나치 수열의 모양이 되지 않나 합니다.
Contributor
Author
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. 오..그렇군요 왜인지 좀 더 생각해봐야겠네요 감사합니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var climbStairs = function(n) { | ||
| // 피보나치 수열을 활용해서 정답을 풀어야할까? | ||
| // 피보나치 수열이란: F(n) = F(n - 1) + F(n - 2); | ||
| // 반환 조건은 n이 1또는 0일 때 해당 수를 반환할 수 있음 | ||
| // 이 문제에서는 2 계단 오르는 상황을 포함시켜야해서 n 이 2일 때 2를 반환하도록 설정 | ||
| // 근데 이제 시간초과가 발생해서 캐싱 해줘야함. | ||
| var cache = { '0': 0, '1': 1, '2': 2 }; | ||
|
|
||
| var fibo = (n) => { | ||
| let result = 0; | ||
|
|
||
| if (typeof(cache[n]) === 'number') { | ||
| result = cache[n]; | ||
| } else { | ||
| result = cache[n] = fibo(n - 1) + fibo(n - 2); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| return fibo(n); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
| */ | ||
| var productExceptSelf = function(nums) { | ||
| const n = nums.length; | ||
| const result = new Array(n); | ||
|
|
||
| result[0] = 1; | ||
| for (let i = 1; i < n; i++) { | ||
| result[i] = result[i - 1] * nums[i - 1]; | ||
| } | ||
|
|
||
| let rightProduct = 1; | ||
| for (let i = n - 1; i >= 0; i--) { | ||
| result[i] *= rightProduct; | ||
| rightProduct *= nums[i]; | ||
| } | ||
|
|
||
| return result; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * 2 개의 문자열 s와 t가 주어진다. 만약 t가 s의 anagram일 경우 true를 반환하고 그렇지 않은 경우 false를 반환하는 함수 | ||
| * @param {string} s | ||
| * @param {string} t | ||
| * @return {boolean} | ||
| */ | ||
| var isAnagram = function(s, t) { | ||
| var sMap = new Map(); | ||
| var tMap = new Map(); | ||
|
|
||
| for(let e of [...s]) { | ||
| if(sMap.has(e)) { | ||
| sMap.set(e, sMap.get(e) + 1); | ||
| } else { | ||
| sMap.set(e, 1); | ||
| } | ||
| } | ||
|
|
||
| for(let e of [...t]) { | ||
| if(tMap.has(e)) { | ||
| tMap.set(e, tMap.get(e) + 1); | ||
| } else { | ||
| tMap.set(e, 1); | ||
| } | ||
| } | ||
|
|
||
| if(sMap.size > tMap.size) { | ||
| for(let s of sMap) { | ||
| if(tMap.get(s[0]) && tMap.get(s[0]) === s[1]) continue; | ||
| return false; | ||
| } | ||
| } else { | ||
| for(let t of tMap) { | ||
| if(sMap.get(t[0]) && sMap.get(t[0]) === t[1]) continue; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * function TreeNode(val, left, right) { | ||
| * this.val = (val===undefined ? 0 : val) | ||
| * this.left = (left===undefined ? null : left) | ||
| * this.right = (right===undefined ? null : right) | ||
| * } | ||
| */ | ||
| /** | ||
| * @param {TreeNode} root | ||
| * @return {boolean} | ||
| */ | ||
| var isValidBST = function(root) { | ||
| function dfs(node, low, high) { | ||
| if(!node) return true; | ||
|
|
||
| if(!(low < node.val && node.val < high)) return false; | ||
|
|
||
| return dfs(node.left, low, node.val) && dfs(node.right, node.val, high); | ||
| } | ||
|
|
||
| return dfs(root, -Infinity, Infinity); | ||
| }; |
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.
정렬 기준을 뺄셈을 통한 값의 차이로 두는 것이 제 입장에선 흥미로운 지점이었습니다.