-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jiji-hoon96] WEEK 01 solutions #1131
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
Changes from 6 commits
0e743b7
ffa1083
e042359
e8c67a6
86c8c3c
a682075
def85b8
05e860e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* 배열에 중복된 요소가 있는지 확인하는 함수 | ||
* @param nums - 확인할 정수 배열 | ||
* @returns 중복된 요소가 있으면 true, 모든 요소가 고유하면 false | ||
* | ||
* 기존에 for문을 사용해 filter,indexOf 방식을 사용했지만, 시간복잡도가 O(n^2)이라서 시간초과가 발생했다. | ||
* Set을 사용하면 중복된 요소를 제거하고, size를 통해 중복된 요소가 있는지 확인할 수 있다. | ||
*/ | ||
function containsDuplicate(nums: number[]): boolean { | ||
return new Set(nums).size !== nums.length | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* | ||
* @param {number[]} nums | ||
* @param nums | ||
* | ||
* 풀이 | ||
* dp 배열을 사용해 nums 배열의 길이만큼 초기화한다. | ||
* dp[0]은 nums[0]으로 초기화하고, dp[1]은 nums[0]과 nums[1] 중 큰 값으로 초기화한다. | ||
* dp[2]부터는 dp[i] = Math.max(dp[i-1], dp[i-2] + nums[i])로 초기화한다. | ||
* dp[i-1]은 i번째 집을 털지 않은 경우, dp[i-2] + nums[i]는 i번째 집을 털고 i-1번째 집을 털지 않은 경우이다. | ||
* | ||
*/ | ||
|
||
function rob(nums: number[]): number { | ||
const n = nums.length | ||
if(n===0) return 0 | ||
if(n===1) return nums[0]; | ||
|
||
let dp0 = nums[0] | ||
let dp1 = Math.max(nums[0], nums[1]); | ||
|
||
for(let i=2;i<n;i++){ | ||
const curMaxValue = Math.max(dp1, dp0 + nums[i]); | ||
dp0 = dp1; | ||
dp1 = curMaxValue; | ||
} | ||
|
||
return dp1 | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* | ||
* 연속된 숫자의 최대 길이를 구하는 문제 | ||
* @param {number[]} nums | ||
* @return {number} | ||
* | ||
* 풀이 | ||
* | ||
* nums 배열을 중복을 제거하고 오름차순으로 정렬한다. | ||
* 중복을 제거하고 정렬한 배열을 순회하면서 연속된 숫자의 길이를 구한다. | ||
*/ | ||
|
||
function longestConsecutive(nums: number[]): number { | ||
if (nums.length === 0) return 0; | ||
const sortNum = Array.from(new Set(nums)).sort((a, b) => a - b); | ||
|
||
if (sortNum.length === 1) return 1; | ||
|
||
const resultArray : number[] = [] | ||
|
||
let count = 1; | ||
|
||
for(let i=0; i<sortNum.length-1; i++){ | ||
const prevNum = sortNum[i]; | ||
const nextNum = sortNum[i+1]; | ||
|
||
if(prevNum +1 === nextNum){ | ||
count ++; | ||
}else{ | ||
resultArray.push(count) | ||
count =1; | ||
} | ||
} | ||
resultArray.push(count); | ||
|
||
return resultArray.length > 0 ? Math.max(...resultArray) : 1; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* nums 배열에서 가장 많이 등장하는 k개의 요소를 반환하는 문제 | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
* | ||
* 풀이 | ||
* countObject 객체를 생성해 nums 배열의 요소를 key로, 등장 횟수를 value로 저장한다. | ||
* 문제는 해결 했지만, O(n log n)의 시간복잡도로 개선할 여지가 있다. | ||
* 개선하기 위해서 Heap 을 사용해볼 수 있다고하는데, Heap을 사용하는 방법을 알아야겠다. | ||
* Heap 을 사용하면 O(n log k)의 시간복잡도로 개선할 수 있다고 한다.. | ||
*/ | ||
|
||
function topKFrequent(nums: number[], k: number): number[] { | ||
let result = [] | ||
const countObject: { [key: number]: number } = {}; | ||
|
||
for(const num of nums){ | ||
countObject[num] = (countObject[num] || 0) +1; | ||
} | ||
|
||
const sortObject= Object.entries(countObject).sort((a,b) => b[1]- a[1]); | ||
|
||
for(const [key] of sortObject){ | ||
if(k>0){ | ||
result.push(Number(key)) | ||
k--; | ||
|
||
} | ||
|
||
} | ||
|
||
return result | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* 두 수의 합이 특정 값(target)이 되게 하는 나머지 하나의 수를 찾는 문제 => 보수를 찾는 문제 | ||
* @param nums - 정수 배열 | ||
* @param target - 두 수의 합 | ||
* @returns 두 수의 인덱스 | ||
* | ||
* 풀이 1 | ||
* 이중 for 문을 사용해 nums의 요소를 더한 값이 target과 같은지 확인한다. | ||
* 이렇게 해결했더니 O(n^2)의 시간복잡도로 시간초과가 발생할 수 있다고 판단. | ||
* | ||
* 풀이 2 | ||
* Map을 사용해 for 문을 한 번만 사용하도록 수정해보았다. | ||
*/ | ||
|
||
function twoSum(nums: number[], target: number): number[] { | ||
const newMap = new Map<number,number>(); | ||
|
||
for(let i=0; i<nums.length; i++){ | ||
const complement = target - nums[i]; | ||
|
||
if(newMap.has(complement)){ | ||
return [newMap.get(complement)!, i] | ||
} | ||
|
||
newMap.set(nums[i], i) | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sortedNum이 더 명확한 변수명일 것 같아요!