-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jdy8739] WEEK 02 #704
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
[jdy8739] WEEK 02 #704
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5dc1809
climbing-stairs solution
jdy8739 cc36bac
fix: 개행 추가
jdy8739 efa4da1
climbing-stairs solution
jdy8739 dc2264d
fix: 파일 마지막 개행 추가
jdy8739 1008b49
Valid Anagram solution
jdy8739 73e71b4
주차에 맞지않는 문제풀이 삭제
jdy8739 7cf288b
3sum solution
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,45 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function(nums) { | ||
const answer = []; | ||
|
||
const sorted = nums.sort((a, b) => a - b); | ||
|
||
for (let i=0; i<sorted.length; i++) { | ||
if (i > 0 && sorted[i] === sorted[i - 1]) { | ||
// 위 조건으로 중복 숫자 필터 | ||
continue; | ||
} | ||
|
||
let l = i + 1; | ||
let h = sorted.length - 1; | ||
|
||
while (l < h) { | ||
const sum = sorted[i] + sorted[l] + sorted[h]; | ||
|
||
if (sum === 0) { | ||
const arr = [sorted[i], sorted[l], sorted[h]]; | ||
answer.push(arr); | ||
|
||
// 아래 반복문으로 중복 숫자 필터 | ||
while (l < h && sorted[l] === sorted[l + 1]) l++; | ||
while (l < h && sorted[h] === sorted[h - 1]) h--; | ||
|
||
h--; | ||
l++; | ||
} | ||
|
||
if (sum > 0) h--; | ||
if (sum < 0) l++; | ||
} | ||
} | ||
|
||
return answer; | ||
}; | ||
|
||
// TC: O(n2) | ||
// SC: O(1) | ||
jdy8739 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
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,22 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function(n) { | ||
let first = 1; | ||
let second = 2; | ||
|
||
if (n <= 2) { | ||
return n; | ||
} | ||
|
||
for (let i=2; i<n; i++) { | ||
let tmp = second; | ||
second = first + second; | ||
first = tmp; | ||
} | ||
|
||
return second; | ||
}; | ||
|
||
|
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 @@ | ||
const getDictionary = (s) => { | ||
const arr = s.split(''); | ||
|
||
const dict = {}; | ||
|
||
for (let i=0; i<arr.length; i++) { | ||
const key = arr[i]; | ||
const value = dict[key]; | ||
|
||
if (value === undefined) { | ||
dict[key] = 1; | ||
} else { | ||
dict[key] = dict[key] + 1; | ||
} | ||
} | ||
|
||
return dict; | ||
} | ||
|
||
const checkSameLength = (dictA, dictB) => { | ||
return Object.keys(dictA).length === Object.keys(dictB).length | ||
} | ||
|
||
const checkSameDict = (s, t) => { | ||
for (const key in s) { | ||
if (s[key] !== t[key]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
const dictA = getDictionary(s); | ||
|
||
const dictB = getDictionary(t); | ||
|
||
return checkSameLength(dictA, dictB) && checkSameDict(dictA, dictB); | ||
jdy8739 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
// 공간복잡도: 해시 테이블에 모든 문자를 저장하게 되므로 O(n) | ||
// 시간복잡도: 두 개의 문자열을 한 번씩 루프를 돌고 있기 때문에 0(n) | ||
|
Oops, something went wrong.
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.
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.
중복을 제거하기 위해 set을 사용하지 않고 while로 사용하신 이유가 공간복잡도 때문이실까요 ?