From 46b8f2abc55b9ffca0915875b2eb527db3cbca4f Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Wed, 11 Dec 2024 11:32:31 +0900 Subject: [PATCH 1/5] Add solutions : Week 1 --- contains-duplicate/khyo.js | 9 ++++++ house-robber/khyo.js | 25 +++++++++++++++ longest-common-subsequence/khyo.js | 49 ++++++++++++++++++++++++++++++ top-k-frequent-elements/khyo.js | 25 +++++++++++++++ valid-palindrome/khyo.js | 23 ++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 contains-duplicate/khyo.js create mode 100644 house-robber/khyo.js create mode 100644 longest-common-subsequence/khyo.js create mode 100644 top-k-frequent-elements/khyo.js create mode 100644 valid-palindrome/khyo.js diff --git a/contains-duplicate/khyo.js b/contains-duplicate/khyo.js new file mode 100644 index 000000000..850fb8b72 --- /dev/null +++ b/contains-duplicate/khyo.js @@ -0,0 +1,9 @@ +// 풀이 +// Set으로 중복 제거 후 nums와 길이 비교 + +// TC : O(N) +// SC : O(N) + +var containsDuplicate = function(nums) { + return new Set(nums).size !== nums.length +}; diff --git a/house-robber/khyo.js b/house-robber/khyo.js new file mode 100644 index 000000000..18d882ac9 --- /dev/null +++ b/house-robber/khyo.js @@ -0,0 +1,25 @@ +// 풀이 +// dp를 이용한 풀이 +// 점화식 : dp[index] = Math.max(dp[index-1], dp[index-2] + nums[index]) +// 사용되는 변수가 index-1, index-2 뿐이라 불필요한 배열을 제거하고자 함. + +// TC : O(N) +// SC : O(1) + +var rob = function(nums) { + let dp = new Array(2); + + dp[0] = nums[0] + // nums의 길이가 1인 경우 예외처리 + dp[1] = nums.length > 1 ? Math.max(nums[0], nums[1]) : nums[0] + + nums.forEach((num, index) => { + if(index <= 1) return; + + let temp = Math.max(dp[1], dp[0] + nums[index]) + dp[0] = dp[1] + dp[1] = temp + }) + + return dp[1] +}; diff --git a/longest-common-subsequence/khyo.js b/longest-common-subsequence/khyo.js new file mode 100644 index 000000000..a2543d1a6 --- /dev/null +++ b/longest-common-subsequence/khyo.js @@ -0,0 +1,49 @@ +// 정렬을 이용한 풀이 +// TC : O(NlogN) +// SC : O(N) + +var longestConsecutiveUsingSort = function (nums) { + if (nums.length === 0) return 0; + + const uniqueNums = [...new Set(nums)].sort((a, b) => a - b); + + let max_cnt = 0; + let cnt = 1; + for (let i = 1; i < uniqueNums.length; ++i) { + if (uniqueNums[i - 1] + 1 == uniqueNums[i]) { + cnt += 1; + } else { + max_cnt = cnt > max_cnt ? cnt : max_cnt; + cnt = 1; + } + } + max_cnt = cnt > max_cnt ? cnt : max_cnt; + return max_cnt; +}; + +// 집합을 이용한 풀이 +// TC : O(N) +// SC : O(N) + +var longestConsecutive = function (nums) { + if (nums.length === 0) return 0; + + const numSet = new Set(nums); + let maxLength = 0; + + for (const num of numSet) { + if (!numSet.has(num - 1)) { + let currentNum = num; + let currentLength = 1; + + while (numSet.has(currentNum + 1)) { + currentNum++; + currentLength++; + } + + maxLength = Math.max(maxLength, currentLength); + } + } + + return maxLength; +}; diff --git a/top-k-frequent-elements/khyo.js b/top-k-frequent-elements/khyo.js new file mode 100644 index 000000000..023716b0a --- /dev/null +++ b/top-k-frequent-elements/khyo.js @@ -0,0 +1,25 @@ +// 풀이 +// 1. 각 요소의 빈도를 계산하여 countObject 생성 +// 2. countObject를 정렬하여 상위 k개의 요소 추출 +// 3. 추출된 요소를 배열로 반환 + +// TC : O(NlogN) +// SC : O(N) + +var topKFrequent = function(nums, k) { + const countObject = {} + nums.map((num) => { + if(countObject[num]) { + countObject[num] += 1 + }else { + countObject[num] = 1 + } + }) + + const sortedObject = Object.entries(countObject) + .sort((a,b) => b[1] - a[1]) + .slice(0, k) + .map(item => Number(item[0])) + + return sortedObject +}; diff --git a/valid-palindrome/khyo.js b/valid-palindrome/khyo.js new file mode 100644 index 000000000..568f7f486 --- /dev/null +++ b/valid-palindrome/khyo.js @@ -0,0 +1,23 @@ +// 풀이 +// 1. 영어, 숫자만 남기고 소문자로 변환한 newStr 생성 +// 2. 투포인터를 이용해 문자열 팰린드롬 여부 확인 (초기값 : true) +// 3. 중간에 팰린드롬이 아닌 지점을 발견하면 flag를 false로 변경 후 return + +// TC : O(N) +// SC : O(N) + +var isPalindrome = function(s) { + const newStr = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase(); + let flag = true + let left = 0, right = newStr.length - 1 + while(left < right){ + if(newStr[left] === newStr[right]) { + left += 1; + right -= 1; + }else { + flag = false; + break; + } + } + return flag +}; From e43d056dd8da59179f37a583b39a4cd06dd672ce Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Wed, 11 Dec 2024 11:46:19 +0900 Subject: [PATCH 2/5] fix : add '\n' --- contains-duplicate/khyo.js | 1 + house-robber/khyo.js | 1 + longest-common-subsequence/khyo.js | 1 + top-k-frequent-elements/khyo.js | 1 + valid-palindrome/khyo.js | 1 + 5 files changed, 5 insertions(+) diff --git a/contains-duplicate/khyo.js b/contains-duplicate/khyo.js index 850fb8b72..e5bd164dc 100644 --- a/contains-duplicate/khyo.js +++ b/contains-duplicate/khyo.js @@ -7,3 +7,4 @@ var containsDuplicate = function(nums) { return new Set(nums).size !== nums.length }; + diff --git a/house-robber/khyo.js b/house-robber/khyo.js index 18d882ac9..2ddaafeb4 100644 --- a/house-robber/khyo.js +++ b/house-robber/khyo.js @@ -23,3 +23,4 @@ var rob = function(nums) { return dp[1] }; + diff --git a/longest-common-subsequence/khyo.js b/longest-common-subsequence/khyo.js index a2543d1a6..1d56fd8ba 100644 --- a/longest-common-subsequence/khyo.js +++ b/longest-common-subsequence/khyo.js @@ -47,3 +47,4 @@ var longestConsecutive = function (nums) { return maxLength; }; + diff --git a/top-k-frequent-elements/khyo.js b/top-k-frequent-elements/khyo.js index 023716b0a..19d4d56e7 100644 --- a/top-k-frequent-elements/khyo.js +++ b/top-k-frequent-elements/khyo.js @@ -23,3 +23,4 @@ var topKFrequent = function(nums, k) { return sortedObject }; + diff --git a/valid-palindrome/khyo.js b/valid-palindrome/khyo.js index 568f7f486..857350721 100644 --- a/valid-palindrome/khyo.js +++ b/valid-palindrome/khyo.js @@ -21,3 +21,4 @@ var isPalindrome = function(s) { } return flag }; + From 3e4997093f2ba0c807d6dbb750941a5cba80224f Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Wed, 11 Dec 2024 11:57:24 +0900 Subject: [PATCH 3/5] change : file name --- contains-duplicate/{khyo.js => higeuni.js} | 0 house-robber/{khyo.js => higeuni.js} | 0 longest-common-subsequence/{khyo.js => higeuni.js} | 0 top-k-frequent-elements/{khyo.js => higeuni.js} | 0 valid-palindrome/{khyo.js => higeuni.js} | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename contains-duplicate/{khyo.js => higeuni.js} (100%) rename house-robber/{khyo.js => higeuni.js} (100%) rename longest-common-subsequence/{khyo.js => higeuni.js} (100%) rename top-k-frequent-elements/{khyo.js => higeuni.js} (100%) rename valid-palindrome/{khyo.js => higeuni.js} (100%) diff --git a/contains-duplicate/khyo.js b/contains-duplicate/higeuni.js similarity index 100% rename from contains-duplicate/khyo.js rename to contains-duplicate/higeuni.js diff --git a/house-robber/khyo.js b/house-robber/higeuni.js similarity index 100% rename from house-robber/khyo.js rename to house-robber/higeuni.js diff --git a/longest-common-subsequence/khyo.js b/longest-common-subsequence/higeuni.js similarity index 100% rename from longest-common-subsequence/khyo.js rename to longest-common-subsequence/higeuni.js diff --git a/top-k-frequent-elements/khyo.js b/top-k-frequent-elements/higeuni.js similarity index 100% rename from top-k-frequent-elements/khyo.js rename to top-k-frequent-elements/higeuni.js diff --git a/valid-palindrome/khyo.js b/valid-palindrome/higeuni.js similarity index 100% rename from valid-palindrome/khyo.js rename to valid-palindrome/higeuni.js From 70c59be8287cdb824c80280744cb00bc7d8ab8ac Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Wed, 11 Dec 2024 16:16:05 +0900 Subject: [PATCH 4/5] fix: move file to longest-consecutive-sequence --- .../higeuni.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {longest-common-subsequence => longest-consecutive-sequence}/higeuni.js (100%) diff --git a/longest-common-subsequence/higeuni.js b/longest-consecutive-sequence/higeuni.js similarity index 100% rename from longest-common-subsequence/higeuni.js rename to longest-consecutive-sequence/higeuni.js From 2db6f2c438000744bce81748b869c48aefee94d7 Mon Sep 17 00:00:00 2001 From: HiGeuni Date: Wed, 11 Dec 2024 16:17:37 +0900 Subject: [PATCH 5/5] fix: map to forEach --- top-k-frequent-elements/higeuni.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top-k-frequent-elements/higeuni.js b/top-k-frequent-elements/higeuni.js index 19d4d56e7..a88642d6f 100644 --- a/top-k-frequent-elements/higeuni.js +++ b/top-k-frequent-elements/higeuni.js @@ -8,7 +8,7 @@ var topKFrequent = function(nums, k) { const countObject = {} - nums.map((num) => { + nums.forEach((num) => { if(countObject[num]) { countObject[num] += 1 }else {