From b72ba0d28be22b988f79e5461f25cd5b750e4e2b Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Fri, 20 Dec 2024 13:44:51 +0900 Subject: [PATCH 1/4] Add solutions : Week 2(1,2) --- climbing-stairs/higeuni.js | 17 +++++++++++++++++ valid-anagram/higeuni.js | 7 +++++++ 2 files changed, 24 insertions(+) create mode 100644 climbing-stairs/higeuni.js create mode 100644 valid-anagram/higeuni.js diff --git a/climbing-stairs/higeuni.js b/climbing-stairs/higeuni.js new file mode 100644 index 000000000..a7c9fbd1b --- /dev/null +++ b/climbing-stairs/higeuni.js @@ -0,0 +1,17 @@ +// complexity +// time: O(n) +// space: 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; +}; \ No newline at end of file diff --git a/valid-anagram/higeuni.js b/valid-anagram/higeuni.js new file mode 100644 index 000000000..ba06a45b2 --- /dev/null +++ b/valid-anagram/higeuni.js @@ -0,0 +1,7 @@ +// complexity +// time: O(n log n) +// space: O(n) + +var isAnagram = function(s, t) { + return s.split('').sort().join('') === t.split('').sort().join('') +}; \ No newline at end of file From a8c058990cd2dfc506e65ffefbdae35d1222c179 Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Fri, 20 Dec 2024 23:11:17 +0900 Subject: [PATCH 2/4] Add solutions : 3sum and add blank line --- 3sum/higeuni.js | 36 ++++++++++++++++++++++++++++++++++++ climbing-stairs/higeuni.js | 2 +- valid-anagram/higeuni.js | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 3sum/higeuni.js diff --git a/3sum/higeuni.js b/3sum/higeuni.js new file mode 100644 index 000000000..0d87fd6f4 --- /dev/null +++ b/3sum/higeuni.js @@ -0,0 +1,36 @@ +// complexity +// time: O(n^2) +// space: O(n) + +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]]); + while(left < right && sortedNums[left] === sortedNums[left + 1]) left ++; + while(left < right && sortedNums[right] === sortedNums[right - 1]) right --; + + left++; + right--; + } + } + } + return answer +}; diff --git a/climbing-stairs/higeuni.js b/climbing-stairs/higeuni.js index a7c9fbd1b..44fe31fb8 100644 --- a/climbing-stairs/higeuni.js +++ b/climbing-stairs/higeuni.js @@ -14,4 +14,4 @@ var climbStairs = function(n) { } return num2; -}; \ No newline at end of file +}; diff --git a/valid-anagram/higeuni.js b/valid-anagram/higeuni.js index ba06a45b2..d7e0d2800 100644 --- a/valid-anagram/higeuni.js +++ b/valid-anagram/higeuni.js @@ -4,4 +4,4 @@ var isAnagram = function(s, t) { return s.split('').sort().join('') === t.split('').sort().join('') -}; \ No newline at end of file +}; From 7d632668afdda3b099efb1581b6a42d9f1fbb765 Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Fri, 20 Dec 2024 23:12:17 +0900 Subject: [PATCH 3/4] Add new line --- 3sum/higeuni.js | 2 ++ climbing-stairs/higeuni.js | 1 + valid-anagram/higeuni.js | 1 + 3 files changed, 4 insertions(+) diff --git a/3sum/higeuni.js b/3sum/higeuni.js index 0d87fd6f4..07a265f3d 100644 --- a/3sum/higeuni.js +++ b/3sum/higeuni.js @@ -24,6 +24,7 @@ var threeSum = function(nums) { 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 --; @@ -34,3 +35,4 @@ var threeSum = function(nums) { } return answer }; + diff --git a/climbing-stairs/higeuni.js b/climbing-stairs/higeuni.js index 44fe31fb8..9bce31079 100644 --- a/climbing-stairs/higeuni.js +++ b/climbing-stairs/higeuni.js @@ -15,3 +15,4 @@ var climbStairs = function(n) { return num2; }; + diff --git a/valid-anagram/higeuni.js b/valid-anagram/higeuni.js index d7e0d2800..a1aef97f5 100644 --- a/valid-anagram/higeuni.js +++ b/valid-anagram/higeuni.js @@ -5,3 +5,4 @@ var isAnagram = function(s, t) { return s.split('').sort().join('') === t.split('').sort().join('') }; + From 736ee8483b8d6da7ae7af84613e75e1c0c00b914 Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Sat, 21 Dec 2024 10:41:23 +0900 Subject: [PATCH 4/4] docs: add complexity analysis --- 3sum/higeuni.js | 5 +++++ climbing-stairs/higeuni.js | 2 ++ valid-anagram/higeuni.js | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/3sum/higeuni.js b/3sum/higeuni.js index 07a265f3d..d08950171 100644 --- a/3sum/higeuni.js +++ b/3sum/higeuni.js @@ -1,6 +1,11 @@ // 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) diff --git a/climbing-stairs/higeuni.js b/climbing-stairs/higeuni.js index 9bce31079..ea9b321e0 100644 --- a/climbing-stairs/higeuni.js +++ b/climbing-stairs/higeuni.js @@ -1,6 +1,8 @@ // complexity // time: O(n) +// - for loop: O(n) // space: O(1) +// - n에 관계없이 상수개의 변수만 사용되므로 O(1) var climbStairs = function(n) { let num1 = 1; diff --git a/valid-anagram/higeuni.js b/valid-anagram/higeuni.js index a1aef97f5..72e662abc 100644 --- a/valid-anagram/higeuni.js +++ b/valid-anagram/higeuni.js @@ -1,6 +1,12 @@ // 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('')