Skip to content

Commit

Permalink
Merge pull request #753 from HiGeuni/main
Browse files Browse the repository at this point in the history
[khyo] Week 2
  • Loading branch information
HiGeuni authored Dec 21, 2024
2 parents 8aa48c4 + 736ee84 commit eaf71df
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
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('')
};

0 comments on commit eaf71df

Please sign in to comment.