Skip to content
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
19 changes: 19 additions & 0 deletions contains-duplicate/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @return {boolean}
*/

// 첫번째 제출
var containsDuplicate = function (nums) {
const counter = new Map();
for (let e of nums) {
const v = counter.has(e) ? counter.get(e) + 1 : 1;
counter.set(e, v);
}
const answer = [...counter.values()].some((item) => item >= 2);
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

모두 순회하면서 counternums를 넣기 보다는, 조건문을 통해 1보다 커지는 경우를 바로 return 하도록 처리하면 중복이 맨 뒤에 있을 때 좀 더 효율적으로 처리 가능할 것 같습니다!

return answer;
};
// 두번째 제출
var containsDuplicate = function (nums) {
return new Set(nums).size !== nums.length;
Copy link
Contributor

Choose a reason for hiding this comment

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

간단하고 깔끔한 풀이네요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

말씀해주신 것처럼 중복을 발견하는 즉시 return 하면,
특히 중복 값이 배열 뒤쪽에 있을 때 전체 순회를 하지 않아도 되어 더 효율적이고,
시간 단축 + 불필요한 순회 제거라는 큰 이점이 있어 공감합니다!

};
15 changes: 15 additions & 0 deletions house-robber/JangAyeon.js
Copy link
Contributor

Choose a reason for hiding this comment

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

반복문 하나만 사용해도 풀리는 문제였네요.

저는 dp로 풀기 위해 반복문을 하나 더 사용했는데, 하나 알아갑니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
const N = nums.length;
let answer = 0;
let [rob1, rob2] = [0, 0];
for (let num of nums) {
let temp = Math.max(num + rob1, rob2);
rob1 = rob2;
rob2 = temp;
}
Comment on lines +8 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

아래와 같이 로직을 이해했는데, 맞을까요?

  • num + rob1: 위치가 i인 집을 털고, i-2 집까지 털어온 최댓값을 더한 값
  • rob2: 위치 i 집을 포기하고, i-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.

네, 정확하게 이해하신 게 맞습니다!

  • num + rob1 → 현재 집(i)을 털었을 때, 두 칸 전(i-2)까지의 최대 금액인 rob1과 현재 집의 금액을 더한 값
  • rob2 → 현재 집(i)을 털지 않았을 때, 바로 전 집(i-1)까지의 최대 금액

그래서 두 값 중 더 큰 값을 선택해 temp에 저장하고,
rob1, rob2를 각각 i-1, i 시점의 상태로 업데이트하는 구조입니다.

정확하게 로직을 짚어주셔서 감사합니다

return rob2;
};
26 changes: 26 additions & 0 deletions longest-consecutive-sequence/JangAyeon.js
Copy link
Contributor

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,26 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
if (nums.length == 0) return 0;
let answer = 0;
let numsSet = new Set(nums);
const N = nums.length;

for (let num of numsSet) {
if (!numsSet.has(num - 1)) {
let temp = num;
let length = 1;

while (numsSet.has(temp + 1)) {
length += 1;
temp += 1;
}
// console.log(length)
answer = Math.max(length, answer);
}
}

return answer;
};
18 changes: 18 additions & 0 deletions top-k-frequent-elements/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function (nums, k) {
nums = nums.sort((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.

nums를 정렬하신 이유가 궁금합니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

앗 다른 풀이 방법으로 해보다가 지우지 않은 코드가 남아있었네요!!

const counter = new Map();
for (let num of nums) {
const value = counter.has(num) ? counter.get(num) + 1 : 1;
counter.set(num, value);
}
const sorted = [...counter.entries()].sort((a, b) => b[1] - a[1]);
const answer = sorted.slice(0, k).map((item) => item[0]);
// console.log(counter, sorted, answer)

return answer;
};
29 changes: 29 additions & 0 deletions two-sum/JangAyeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/

// 첫번째 통과 풀이 - 브루트포스
var twoSum = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return [i, j];
}
}
}
};
Comment on lines +8 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

브루트포스로도 시간초과가 안 나는 문제였네요!

간단하고 확실한 방법이죠~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

맞아요! 이 문제는 가장 직관적이고 구현도 간단한 풀이로 브루트포스 방식을 사용해보았는데
입력 크기가 커도 이중 반복문으로 시간 초과가 나지 않았습니다. ㅎㅎ
나중에 확장하거나 최적화할 때는 해시맵 풀이로 바꿔서도 풀이해보았습니다 🙂


// 두번째 통과 풀이 - 해시맵 + 효율성 고려
var twoSum = function (nums, target) {
const map = new Map(); // {값: 인덱스}

for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i]; // 필요한 짝 계산
if (map.has(complement)) {
return [map.get(complement), i]; // 이전에 complement가 있었으면 바로 반환
}
map.set(nums[i], i);
}
};
Comment on lines +19 to +29
Copy link
Contributor

Choose a reason for hiding this comment

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

제가 생각하지 못한 접근법이네요!

저는 투포인터로 풀었는데, 해시맵 풀이도 하나 배워갑니다 📝

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오 저는 투포인터 방식은 생각하지 못했는데, 서로 다른 방식으로 풀이 공유할 수 있어서 좋네요.
코멘트 감사합니다! 🙌