-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from HiGeuni/main
[khyo] Week 2
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,43 @@ | ||
// complexity | ||
// time: O(n^2) | ||
// - sort: O(n log n) | ||
// - for loop: O(n) | ||
// - while loop: O(n) | ||
// space: O(n) | ||
// - sortedNums: O(n) | ||
// - else : O(1) | ||
|
||
var threeSum = function(nums) { | ||
const sortedNums = nums.sort((a, b) => a - b) | ||
const lengthOfArray = nums.length; | ||
const answer = []; | ||
|
||
for(let i = 0; i < lengthOfArray; i++ ) { | ||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
const target = (-1) * sortedNums[i]; | ||
|
||
let left = i + 1; | ||
let right = lengthOfArray - 1; | ||
|
||
while(left < right) { | ||
const sumOfLeftAndRight = sortedNums[left] + sortedNums[right]; | ||
const diff = sumOfLeftAndRight - target; | ||
|
||
if( diff > 0 ){ | ||
right -= 1; | ||
} else if ( diff < 0 ){ | ||
left += 1 | ||
} else { | ||
answer.push([sortedNums[i], sortedNums[left], sortedNums[right]]); | ||
// ์ค๋ณต๋๋ ๋ถ๋ถ์ ์ฒ๋ฆฌํ๋ ๊ณผ์ ์์ ๊ณ์ fail๋์ด ์ฐพ์๋ณด๋ ์ด๋ ๊ฒ ํด์ผ ํต๊ณผ๋์๋ค. | ||
while(left < right && sortedNums[left] === sortedNums[left + 1]) left ++; | ||
while(left < right && sortedNums[right] === sortedNums[right - 1]) right --; | ||
|
||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
return answer | ||
}; | ||
|
This file contains 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,20 @@ | ||
// complexity | ||
// time: O(n) | ||
// - for loop: O(n) | ||
// space: O(1) | ||
// - n์ ๊ด๊ณ์์ด ์์๊ฐ์ ๋ณ์๋ง ์ฌ์ฉ๋๋ฏ๋ก O(1) | ||
|
||
var climbStairs = function(n) { | ||
let num1 = 1; | ||
let num2 = 1; | ||
let temp = 0; | ||
|
||
for(let i = 2; i<=n; ++i ) { | ||
temp = num2; | ||
num2 = num1 + num2; | ||
num1 = temp; | ||
} | ||
|
||
return num2; | ||
}; | ||
|
This file contains 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,14 @@ | ||
// complexity | ||
// time: O(n log n) | ||
// - sort: O(n log n) | ||
// - split: O(n) | ||
// - join: O(n) | ||
// space: O(n) | ||
// - sortedS: O(n) | ||
// - sortedT: O(n) | ||
// - else : O(1) | ||
|
||
var isAnagram = function(s, t) { | ||
return s.split('').sort().join('') === t.split('').sort().join('') | ||
}; | ||
|