Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions longest-consecutive-sequence/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 시간 복잡도 O(n log n)
// 공간 복잡도 O(n)

/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
if (nums.length === 0) return []

let maxSequenceLength = -Infinity


const setNums = [...new Set(nums)].toSorted((a,b) => a - b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그런데 문제 조건에서 O(n)에 동작하는 코드를 작성해야 한다고 했는데, 해당 코드는 시간복잡도가 nlogn이 되는 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그냥 생각나는데로 풀어버렸네요 ㅜ
정렬을 안쓰고 풀어봐야겠어요 ~


let count = 0;
for (let i = 0 ; i < setNums.length; i++) {
if (setNums[i]+1 === setNums[i+1]) {
count += 1
} else {
count += 1
maxSequenceLength = Math.max(maxSequenceLength, count)
count = 0;
}
}

return maxSequenceLength
};


console.log(longestConsecutive([100,4,200,1,3,2]))
console.log(longestConsecutive([0,3,7,2,5,8,4,6,0,1]))
19 changes: 19 additions & 0 deletions missing-number/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 시간복잡도 O(n log n)
// 공간복잡도 O(1)

/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function(nums) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js에서 sort는 내부적으로 추가적인 메모리를 사용해서 공간복잡도가 O(1)은 아닐 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그렇네요 정렬할 때 공간복잡도가 On 사용됩니다!
감사합니다 : )

nums.sort((a,b) => a - b);

for (let i = 0 ; i <= nums.length; i++) {
if (i !== nums[i]) return i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

숫자가 0부터 시작하니까 이렇게 비교해서 풀어도 좋겠네요! 배워갑니다 :)

}

};

console.log(missingNumber([3, 0, 1]))
console.log(missingNumber([0, 1]))
console.log(missingNumber([9,6,4,2,3,5,7,0,1]))
29 changes: 29 additions & 0 deletions valid-palindrome/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
const strs = s.replace(/[^a-z0-9]/gi, '').toLowerCase();

let leftIdx = 0;
let rightIdx = strs.length - 1

while (leftIdx <= rightIdx) {
if (strs[leftIdx] !== strs[rightIdx]) return false

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 양쪽에서 인덱스를 증가시키거나 감소시키면서 하나씩 비교하니 풀이가 굉장히 깔끔하네요!

leftIdx++
rightIdx--
}


return true
};

const s = "A man, a plan, a canal: Panama"


console.log(isPalindrome(s))