Skip to content

[DoDo] Week 1 #655

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 8 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
9 changes: 9 additions & 0 deletions contains-duplicate/y00eunji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
// set에 넣어 중복을 줄이고 길이를 비교한다.

return new Set(nums).size !== nums.length;
};
23 changes: 23 additions & 0 deletions house-robber/y00eunji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function(nums) {
const len = nums.length;
if (len === 0) return 0;
if (len === 1) return nums[0];
if (len === 2) return Math.max(nums[0], nums[1]);

const dp = Array(len).fill(0);
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);

// 현재 집을 터는 경우와 안 터는 경우 중 최대값 선택
// 1. 이전 집까지의 최대 금액 (현재 집 스킵)
// 2. 전전 집까지의 최대 금액 + 현재 집 금액 (현재 집 선택)
for (let i = 2; i < len; i++) {
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

안녕하세요~ 😊
해당 점화식을 잘 살펴보면 dpi, i-1, i-2번 요소만 사용하고 있다는 걸 알 수 있습니다
아직 시간이 좀 남았으니 공간 복잡도 최적화에 도전해보시면 어떨까요?

}

return dp[len - 1];
};
27 changes: 27 additions & 0 deletions longest-consecutive-sequence/y00eunji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
const numSet = new Set(nums);
let longestStreak = 0;

for (const num of numSet) {
// 현재 숫자가 연속 시퀀스의 시작점인지 확인
// 즉, num-1이 set에 없어야 함
if (!numSet.has(num - 1)) {
let currentNum = num;
let currentStreak = 1;

// 현재 숫자의 연속된 다음 숫자들을 찾음
while (numSet.has(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}

longestStreak = Math.max(longestStreak, currentStreak);
}
}

return longestStreak;
};
20 changes: 20 additions & 0 deletions top-k-frequent-elements/y00eunji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function(nums, k) {
// 1. nums 배열을 순회하여 각 숫자의 빈도를 계산하고 obj 객체에 저장
const obj = nums.reduce((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
}, {});

// 2. obj 객체의 키-값 쌍을 배열로 변환하고, 값을 기준으로 내림차순 정렬, k개 추출
const frequentArr = Object.entries(obj)
.sort(([, valueA], [, valueB]) => valueB - valueA)
.slice(0, k)
.map(([key]) => +key);

return frequentArr;
};
10 changes: 10 additions & 0 deletions valid-palindrome/y00eunji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function(s) {
const filtered = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
const reversed = filtered.split('').reverse().join('');

return filtered === reversed;
};
Loading