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
10 changes: 10 additions & 0 deletions contains-duplicate/prgmr99.js
Copy link
Contributor

Choose a reason for hiding this comment

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

중복을 제거한 Set 객체의 길이와 기존 배열의 길이를 비교하셨네요!!
한 눈에 봤을 때 이해가 잘 되어서 좋았습니다 👍

Copy link
Member 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,10 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
const numArrLength = nums.length;
const numSetSize = new Set(nums).size;

return numArrLength !== numSetSize;
};
20 changes: 20 additions & 0 deletions house-robber/prgmr99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {number[]} nums
* @return {number}
*/
var rob = function (nums) {
let maxMoneyAtPrevHouse = 0;
let maxMoneyAtTwoHousesBack = 0;

for (let i = 0; i < nums.length; i++) {
let currentMax = Math.max(
nums[i] + maxMoneyAtTwoHousesBack,
maxMoneyAtPrevHouse
);

maxMoneyAtTwoHousesBack = maxMoneyAtPrevHouse;
maxMoneyAtPrevHouse = currentMax;
}

return maxMoneyAtPrevHouse;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

간결하게 잘 푸신 것 같아요!
전 집까지의 누적합과 현재 집 + 전전집의 누적합을 비교해서 더 큰 값을 선택하도록 구현해주신걸로 이해했는데 맞을까요?
그리고 제가 이 문제는 제대로 이해하지 못해서 더 공부해봤는데

다음과 같은 식을 알게됐어요.

currentMax = max(
  nums[i] + DP[i-2],   // 이번 집을 털면 → i-1 집은 못 턴다
  DP[i-1]              // 이번 집을 안 털면 → i-1 집까지의 최대값 그대로
)

이런 방식으로 구현하신게 맞을까요?

추가로 변수명을 직관적으로 잘 적어주셔서 이해하기 더 편했습니다👍

Copy link
Member Author

Choose a reason for hiding this comment

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

넵! 맞습니다!
말씀해주신 currentMax가 i번째 집까지 털었을 때의 최댓값을 의미한다고 이해해주시면 될 것 같습니다..!

33 changes: 33 additions & 0 deletions longest-consecutive-sequence/prgmr99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
let result = 1;
let maxLength = 1;

if (nums.length === 0) return 0;

const sortedNums = 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.

저는 이 부분에서 별도로 변수에 할당을 하진 않았는데 배열이 어떤 상태인지 생각할 때 덜 헷갈리게하려면 이렇게 작성하는 습관을 들이는게 좋을거같네요..!

Copy link
Member Author

Choose a reason for hiding this comment

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

바로 하셔도 사실 큰 문제는 없어서,
두 방식 다 좋은 것 같아요!!


for (let i = 1; i < sortedNums.length; i++) {
const prevNum = sortedNums[i - 1];
const currentNum = sortedNums[i];

const diff = currentNum - prevNum;

if (diff === 1) {
result += 1;

if (maxLength < result) {
maxLength = result;
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분이 최대 길이를 갱신해주는 부분이군요!
저는 길이를 누적시키다 문제 조건에 맞지 않을 때 갱신해줬는데
좋은 방법 같습니다 ㅎㅎ

}
} else if (diff === 0) {
continue;
} else {
result = 1;
}
}

return maxLength;
};
33 changes: 33 additions & 0 deletions top-k-frequent-elements/prgmr99.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function (nums, k) {
const result = [];
const numMap = new Map();

for (let i = 0; i < nums.length; i++) {
const currentValue = numMap.get(nums[i]);

if (currentValue) {
numMap.set(nums[i], currentValue + 1);
} else {
numMap.set(nums[i], 1);
}
}

const mapToArr = [...numMap.entries()];
Copy link
Contributor

Choose a reason for hiding this comment

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

이터러블의 entries 함수와 스프레드 연산자로 배열을 만드셨네요.
좋은 것 같아요 ㅎㅎ 나머지는 저와 동일한 방법으로 푸셔서 이견 없습니다!


mapToArr.sort((a, b) => {
if (a[1] < b[1]) return 1;
if (a[1] > b[1]) return -1;
return 0;
});

for (let i = 0; i < k; i++) {
result.push(mapToArr[i][0]);
}

return result;
};
19 changes: 19 additions & 0 deletions two-sum/prgmr99.js
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
Member Author

Choose a reason for hiding this comment

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

감사합니다~~
1주차 고생하셨습니다~~

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const numMap = new Map();

for (let i = 0; i < nums.length; i++) {
const firstNum = nums[i];
const secondNum = target - nums[i];

if (numMap.has(secondNum)) {
return [numMap.get(secondNum), i];
}

numMap.set(firstNum, i);
}
};