Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions contains-duplicate/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// 풀이
// Set으로 중복 제거 후 nums와 길이 비교

// TC : O(N)
// SC : O(N)

var containsDuplicate = function(nums) {
return new Set(nums).size !== nums.length
};

26 changes: 26 additions & 0 deletions house-robber/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 풀이
// 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]
};

50 changes: 50 additions & 0 deletions longest-common-subsequence/higeuni.js
Copy link
Contributor

@obzva obzva Dec 11, 2024

Choose a reason for hiding this comment

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

안녕하세요 파일을 다른 폴더에 생성하신 것 같아요
longest-consecutive-sequence 폴더 아래에 생성해주세요~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵! 반영했습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 정렬을 이용한 풀이
// 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;
};

26 changes: 26 additions & 0 deletions top-k-frequent-elements/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 풀이
// 1. 각 요소의 빈도를 계산하여 countObject 생성
// 2. countObject를 정렬하여 상위 k개의 요소 추출
// 3. 추출된 요소를 배열로 반환

// TC : O(NlogN)
// SC : O(N)

var topKFrequent = function(nums, k) {
const countObject = {}
nums.map((num) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

결과는 비슷할 것 같은데 .map 메소드 대신에 .forEach메소드를 사용하시는게 더 적절하지 않을까 싶어요 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

덕분에 차이를 공부해보면서 .map.forEach의 반환값 차이가 있다는 것을 알게되었습니다 😊

감사합니다!!

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
};

24 changes: 24 additions & 0 deletions valid-palindrome/higeuni.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 풀이
// 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
};

Loading