Skip to content

[khyo] Week 2 #753

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 4 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 3sum/higeuni.js
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
};

20 changes: 20 additions & 0 deletions climbing-stairs/higeuni.js
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;
};

14 changes: 14 additions & 0 deletions valid-anagram/higeuni.js
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('')
};

Loading