Skip to content

[khyo] Week 1 #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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-consecutive-sequence/higeuni.js
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.forEach((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
};

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