-
-
Notifications
You must be signed in to change notification settings - Fork 247
[youngduck] WEEK 02 solutions #1739
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
9 commits
Select commit
Hold shift + click to select a range
1fe7d80
valid anagram 풀이
youngduck ce3d49d
climbing stairs 풀이
youngduck 87fc3f9
refactor: 린트오류 수정
youngduck d45338c
product-of-array-except-self풀이
youngduck fa2826c
product-of-array-except-self풀이
youngduck 9eb1636
누적곱 풀이 주석수정
youngduck fe593e3
feat: 3sum풀이
youngduck 1204565
feat: 시간복잡도공간복잡도주석추가
youngduck 0e431df
fix: 줄바꿈수정
youngduck 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,49 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function(nums) { | ||
|
||
// 최악의 경우 3중 for문이므로 투포인터 기법을써서 최적화 해도 for문 하나는 필요함 | ||
|
||
// 결과 배열 | ||
const result = []; | ||
|
||
// 투포인터 기법 사용을 위한 정렬 | ||
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){ | ||
left++; | ||
} | ||
else if (sum > 0){ | ||
right--; | ||
} | ||
else{ | ||
// 합이 0인 경우 결과에 추가 | ||
result.push([nums[i], nums[left], nums[right]]); | ||
|
||
// 두 번째, 세 번째 요소의 중복 건너뛰기 | ||
while(left < right && nums[left] === nums[left + 1]) left++; | ||
while(left < right && nums[right] === nums[right - 1]) right--; | ||
|
||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
// 시간복잡도: O(n^2) | ||
// 공간복잡도: O(1) | ||
|
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 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
// 문제를 보자마자 든 생각: dfs처럼 하나씩,두개씩 가지뻗어가면서? -> 점화식,피보나치처럼 최적화된 형태겠는데? 여기까지는 생각. 근데 2칸전, 1칸전의 덧셈이라는 결론까지 내지는 못함 | ||
|
||
// 그래서 default값을 n=1,2,3까지 세팅해놓고 생각했었다. 다른사람의 점화식 풀이 참고 후 역순으로 출발해서 2칸전, 1칸전에 대한 상황에 집중하는 방식 이해 완료. 구현은 내가 직접. | ||
if (n === 1) { | ||
return 1; | ||
} | ||
|
||
if (n === 2) { | ||
return 2; | ||
} | ||
|
||
return climbStairs(n - 1) + climbStairs(n - 2); | ||
}; | ||
|
||
// 시간복잡도: O(2^n) | ||
// 공간복잡도: O(n) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairsOptimized = function (n) { | ||
const dp = Array(n).fill(0); | ||
dp[1] = 1; | ||
dp[2] = 2; | ||
|
||
if (n >= 3) { | ||
for (let i = 3; i <= n; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 2]; | ||
} | ||
} | ||
|
||
return dp[n]; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) !! dp라는 배열대신 변수 두개로 O(1)로 줄일 수 있음 |
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,28 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function (nums) { | ||
const numsLength = nums.length; | ||
|
||
const result = new Array(numsLength).fill(1); | ||
|
||
// 일반적으로 for문 하나로 자기자신 제외하면서 곱셈을하면서 처리할경우 O(n^2). | ||
// for문 두개로 나눠서 처리할경우 O(n). 누적곱 개념을 활용해줘야함 | ||
// for문 하나로 누적곱 개념을 활용해서 처리할경우 O(n). | ||
|
||
let left = 1; | ||
let right = 1; | ||
|
||
for (let i = 0; i < numsLength; i++) { | ||
result[i] *= left; | ||
left *= nums[i]; | ||
|
||
result[numsLength - i - 1] *= right; | ||
right *= nums[numsLength - i - 1]; | ||
} | ||
|
||
// 시간복잡도: O(n), 공간복잡도: O(1) | ||
|
||
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,59 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function (s, t) { | ||
const mapS = new Map(); | ||
const mapT = new Map(); | ||
|
||
[...s].map((item) => { | ||
if (mapS.has(item)) { | ||
const itemCount = mapS.get(item); | ||
mapS.set(item, itemCount + 1); | ||
} else { | ||
mapS.set(item, 1); | ||
} | ||
}); | ||
|
||
[...t].map((item) => { | ||
if (mapT.has(item)) { | ||
const itemCount = mapT.get(item); | ||
mapT.set(item, itemCount + 1); | ||
} else { | ||
mapT.set(item, 1); | ||
} | ||
}); | ||
|
||
// NOTE - t가 s의 anagram이라는 뜻을 갯수가 같지않아도 된다고 이해했으나 anagram정의는 s구성원을 모자람,남김없이 t를만들 수 있는 상태 | ||
if (mapS.size !== mapT.size) { | ||
return false; | ||
} | ||
|
||
for (const [key, value] of mapS) { | ||
if (mapT.get(key) !== value) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// - 문자열 s와 t를 각각 한 번씩 순회: O(n) + O(n) = O(2n) = O(n) | ||
// - Map 비교를 위한 순회: O(k), 여기서 k는 고유 문자 개수 | ||
// - 따라서 전체 시간복잡도는 O(n) | ||
// 공간복잡도: O(1) | ||
// - 두 개의 Map 객체 생성: mapS와 mapT | ||
// - 각 Map은 최대 k개의 고유 문자를 저장 (k는 고유 문자 개수) | ||
// - 소문자 영문자만 사용하므로 k ≤ 26 (a-z) | ||
// - 따라서 전체 공간복잡도는 O(1) (상수 시간) | ||
|
||
// follow up: 유니코드 테스트 케이스. 큰 의미는 없음 | ||
// console.log(isAnagram("😀😀", "😀😀😀")); // false | ||
// console.log(isAnagram("한글글", "글한글")); // true | ||
// console.log(isAnagram("café", "éfac")); // true | ||
// console.log(isAnagram("Hello世界", "世界Hello")); // true | ||
// console.log(isAnagram("안녕 하세요", "하세요 안녕")); // true | ||
// console.log(isAnagram("Café", "éfac")); // false | ||
// console.log(isAnagram("Café", "Éfac")); // false |
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.