Skip to content

[jangwonyoon] WEEK 01 solutions #1712

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
Jul 26, 2025
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/jangwonyoon.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 sortedNums = nums.sort((a, b) => a - b);
const length = sortedNums.length - 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

length 변수가 재사용되지 않는다면 아래 for loop 구문에 바로 넣으면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

보기 좋게 쓸 변수로 사용했는데, 생각해보니 재사용 되지 않는다면, 메모리만 사용하는 상황이네요 리뷰 감사합니다.


for (let i = 0; i < length; i++) {
const left = nums[i];
const right = nums[i + 1];

if (left === right) {
return true;
}
}

return false;
};
60 changes: 60 additions & 0 deletions longest-consecutive-sequence/jangwonyoon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* solve 1
*
* 시간 복잡도: O(n log n)
* 공간 복잡도: O(n)
*
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
// 예외
if (nums.length === 0) {
return 0;
}

const sortedArr = [...new Set(nums)].sort((a, b) => a - b);

let result = 1;
let consecutive = 1;

for (let i = 0; i < sortedArr.length - 1; i++) {
const curr = sortedArr[i];
const next = sortedArr[i + 1];

if (next === curr + 1) {
consecutive += 1;
} else {
consecutive = 1;
}

result = Math.max(result, consecutive);
}

return result;
};

/**
* solve 2
*
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function(nums) {
let longest = 0;
Copy link
Contributor

@nancyel nancyel Jul 26, 2025

Choose a reason for hiding this comment

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

변수 이름이 명확해서 코드의 흐름을 파악하기 쉽네요!

const numSet = new Set(nums);

for (const num of numSet) {
if (numSet.has(num - 1)) continue;
let length = 1;

while (numSet.has(num + length)) length++;

longest = Math.max(length, longest);
}

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

/*
* 시간 복잡도: O(n log n)
* 공간 복잡도: O(n)
*
* 1. map 구조를 사용해서 몇번 나왔는지 카운트한다.
* 2. map 구조에서 가장 큰 수를 구한다.
*/
var topKFrequent = function(nums, k) {
const map = new Map();

for (let i = 0; i < nums.length; i++) {
if (!map.has(nums[i])) {
map.set(nums[i], 1);
} else {
map.set(nums[i], map.get(nums[i]) + 1);
}
}

const result = [...map.entries()].sort((a, b) => b[1] - a[1]);

return result.slice(0, k).map((val) => val[0]);
};
50 changes: 50 additions & 0 deletions two-sum/jangwonyoon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* solve 1
*
* 시간 복잡도: O(n^2)
* 공간 복잡도: O(1)
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const length = nums.length - 1;

for (let i = 0; i < length; i++) {
for(let j = length; j > 0; j--) {
const sum = nums[i] + nums[j];

if (i !== j) {
if (sum === target) {
return [i , j];
}
}
}
}
};


/**
* solve 2
*
* 시간 복잡도: O(n)
* 공간 복잡도: O(n)
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
const map = new Map();

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

if (map.has(temp)) {
return [map.get(temp), i];
}

map.set(nums[i], i);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

twoSum 함수의 리턴값의 타입이 number[] 이면 for loop 을 모두 순회한 후 리턴값으로 빈 배열을 반환하면 어떨까요?

};