Skip to content

[hanseulhyi] - Week 1 #641

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 6 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
23 changes: 23 additions & 0 deletions contains-duplicate/hanseulhee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {boolean}
*
* 접근: 중첩 반복문을 통해 중복이 있으면 true 반환, 중복이 없으면 false를 반환하도록 설계하였습니다.
* 그러나 배열의 요소를 두 번 돌기 때문에 더 효율적인 방법으로 설계하고자 하였습니다.
*
* 해결: 더 효율적인 방법으로 중복 여부를 검사하는 Set을 사용하게 되었습니다.
* Set에 해당 요소가 있는 지 확인하고 있다면 true를 없다면 false를 반환하도록 하였습니다.
*/

var containsDuplicate = function(nums) {
const duplicate = new Set();

for (let i = 0; i < nums.length; i++) {
if (duplicate.has(nums[i])) {
return true;
}
duplicate.add(nums[i]);
}

return false;
};
20 changes: 20 additions & 0 deletions house-robber/hanseulhee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} nums
* @return {number}
*/

var rob = function (nums) {
// 집이 하나인 경우
if (nums.length === 1) return nums[0]

let prevMax = 0 // 두 집 전까지의 최대 금액
let currMax = 0 // 이전 집까지의 최대 금액

for (let num of nums) {
let temp = currMax // 현재 최대 금액
currMax = Math.max(currMax, prevMax + num) // 현재 집을 털거나 털지 않을 경우 중 큰 값
prevMax = temp // 이전 집으로 이동
}

return currMax // 최대 금액 반환
}
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/hanseulhee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number[]} nums
* @return {number}
*/

var longestConsecutive = function (nums) {
// Set으로 배열에서 중복된 요소 제거
const numSet = new Set(nums)

// 최장 길이
let longest = 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 // 연속된 길이 증가
}

longest = Math.max(longest, currentStreak)
}
}

return longest
}
22 changes: 22 additions & 0 deletions top-k-frequent-elements/hanseulhee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/

function topKFrequent(nums, k) {
// 숫자의 빈도를 저장할 Map 생성
const frequency = new Map();

// 빈도 계산
for (const num of nums) {
// Map에 이미 숫자가 있으면 +1, 없으면 1로 reset
frequency.set(num, (frequency.get(num) || 0) + 1);
}

// 빈도 순으로 숫자 정렬 후 가장 빈도가 높은 k개의 숫자를 반환
return [...frequency.entries()] // entries를 이용해 Map을 배열로 변환
.sort((a, b) => b[1] - a[1])
.slice(0, k)
.map(entry => entry[0]);
}
24 changes: 24 additions & 0 deletions valid-palindrome/hanseulhee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {string} s
* @return {boolean}
*
* 해결: 먼저 문자열의 정방향, 역방향 비교를 위해 문자열을 소문자로 변환하였으며 특수기호는 제외하였습니다.
* 정방향과 역방향을 비교하여 문자가 모두 일치하면 true 아니라면 false를 반환하였습니다.
*/
var isPalindrome = function (s) {
const filterS = s.toLowerCase().replace(/[^a-z0-9]/g, "");

let left = 0;
let right = filterS.length - 1;

while (left < right) {
if (filterS[left] !== filterS[right]) {
return false;
} else {
left++;
right--;
}
}

return true;
};
Loading