-
-
Notifications
You must be signed in to change notification settings - Fork 247
[sukyoungshin] WEEK 01 #1178
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
[sukyoungshin] WEEK 01 #1178
Changes from 5 commits
2a04b9d
8ff78df
0b725bb
21d1e5e
44f3cd6
9a6d07f
3d274dd
c2deba0
7c4c547
c32206a
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,26 @@ | ||
// 1번째 풀이 (hashMap) | ||
function containsDuplicate1(nums: number[]): boolean { | ||
const seen: Record<number, boolean> = {}; | ||
|
||
const isDuplicate = nums.some((num) => { | ||
if (seen[num]) return true; | ||
seen[num] = true; | ||
return false; | ||
}); | ||
|
||
return isDuplicate; | ||
} | ||
|
||
// 2번째 풀이(Set) | ||
function containsDuplicate2(nums: number[]): boolean { | ||
const set = new Set(); | ||
|
||
for (const num of nums) { | ||
if (set.has(num)) { | ||
return true; | ||
} | ||
set.add(num); | ||
} | ||
|
||
return false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 1번풀이 | ||
function longestConsecutive2(nums: number[]): number { | ||
if (nums.length === 0) return 0; | ||
|
||
const copiedNums = [...new Set(nums)].sort((a, b) => a - b); | ||
|
||
let max = 1; // 지금까지 나왔던 가장 긴 연속 수열의 길이 | ||
let count = 1; // 현재 연속 수열의 길이 | ||
|
||
for (let i = 1; i < copiedNums.length; i++) { | ||
const prev = copiedNums[i - 1]; | ||
const current = copiedNums[i]; | ||
|
||
if (current === prev + 1) { | ||
count++; | ||
max = Math.max(max, count); | ||
} else { | ||
count = 1; | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
|
||
// 2번풀이 (hashSet) | ||
function longestConsecutive(nums: number[]): number { | ||
const numSet = new Set(nums); | ||
let max = 0; | ||
|
||
for (const num of numSet) { | ||
// 수열의 시작점인 경우만 탐색 | ||
if (!numSet.has(num - 1)) { | ||
let current = num; | ||
let count = 1; | ||
|
||
while (numSet.has(current + 1)) { | ||
current++; | ||
count++; | ||
} | ||
|
||
max = Math.max(max, count); | ||
} | ||
} | ||
|
||
return max; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 1번풀이 (brute force) | ||
type FrequencyMap = Record<number, number>; | ||
|
||
function topKFrequentCountDown(nums: number[], k: number): number[] { | ||
const freqMap = buildFrequencyMap1(nums); | ||
return pickTopKDescending(freqMap, k); | ||
} | ||
|
||
function buildFrequencyMap1(nums: number[]): FrequencyMap { | ||
const map: FrequencyMap = {}; | ||
for (const num of nums) { | ||
map[num] = (map[num] || 0) + 1; | ||
} | ||
return map; | ||
} | ||
|
||
function pickTopKDescending(freqMap: FrequencyMap, k: number): number[] { | ||
const result: number[] = []; | ||
const entries = Object.entries(freqMap).map(([key, value]) => [ | ||
Number(key), | ||
value, | ||
]); | ||
let currentFrequent = Math.max(...entries.map(([_, freq]) => freq)); | ||
|
||
while (result.length < k && currentFrequent > 0) { | ||
for (const [num, freq] of entries) { | ||
if (freq === currentFrequent) { | ||
result.push(num); | ||
if (result.length === k) break; | ||
} | ||
} | ||
currentFrequent--; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
// 2번풀이(Bucket Sort) | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const frequencyMap = buildFrequencyMap2(nums); | ||
const frequencyBuckets = buildFrequencyBuckets(nums.length, frequencyMap); | ||
|
||
return collectTopKFrequent(frequencyBuckets, k); | ||
} | ||
|
||
function buildFrequencyMap2(nums: number[]): FrequencyMap { | ||
const freqMap: FrequencyMap = {}; | ||
for (const num of nums) { | ||
freqMap[num] = (freqMap[num] || 0) + 1; | ||
} | ||
return freqMap; | ||
} | ||
|
||
function buildFrequencyBuckets( | ||
size: number, | ||
freqMap: FrequencyMap | ||
): number[][] { | ||
const buckets: number[][] = Array(size + 1).fill(null).map(() => []); | ||
|
||
for (const [numStr, frequent] of Object.entries(freqMap)) { | ||
const num = Number(numStr); | ||
buckets[frequent].push(num); | ||
} | ||
|
||
return buckets; | ||
} | ||
|
||
function collectTopKFrequent(buckets: number[][], k: number): number[] { | ||
const result: number[] = []; | ||
|
||
for (let i = buckets.length - 1; i >= 0 && result.length < k; i--) { | ||
for (const num of buckets[i]) { | ||
result.push(num); | ||
if (result.length === k) break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
||
// 3번풀이 (MinHeap) | ||
function topKFrequentHeap(nums: number[], k: number): number[] { | ||
const freqMap: Record<number, number> = {}; | ||
|
||
for (const num of nums) { | ||
freqMap[num] = (freqMap[num] || 0) + 1; | ||
} | ||
|
||
const heap: [number, number][] = []; | ||
for (const [numStr, frequent] of Object.entries(freqMap)) { | ||
const num = Number(numStr); | ||
|
||
heap.push([num, frequent]); | ||
heap.sort((a, b) => a[1] - b[1]); | ||
|
||
if (heap.length > k) { | ||
heap.shift(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서는 |
||
} | ||
|
||
return heap.map(([num]) => num); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 1번째 풀이 (brute force) | ||
function twoSum1(nums: number[], target: number): number[] { | ||
for(let i = 0; i < nums.length; i++) { | ||
for (let j = 1; j < nums.length; j++) { | ||
const a = nums[i]; | ||
const b = nums[j]; | ||
|
||
if (a + b === target && i !== j) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
return []; | ||
}; | ||
|
||
// 2번째 풀이 (indexOf) | ||
function twoSum2(nums: number[], target: number): number[] { | ||
for (let i = 0; i < nums.length; i++) { | ||
const j = target - nums[i]; | ||
const index = nums.indexOf(j); | ||
|
||
if (nums.includes(j) && i !== index) { | ||
return [i, index]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
return []; | ||
}; | ||
|
||
// 3번째 풀이 (HashMap) | ||
function twoSum(nums: number[], target: number): number[] { | ||
const map: Record<number, number> = {}; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const complement = target - nums[i]; | ||
|
||
if (complement in map) { | ||
const j = map[complement]; | ||
return [i, j]; | ||
} else { | ||
map[nums[i]] = i; | ||
} | ||
} | ||
|
||
return []; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 지금 linelint에서 에러가 나고 있는데 답안 제출 가이드를 보니 파일 끝에 개행문자를 추가해야 한다고 해요! |
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.
알고리즘 풀이를 할 때에는 함수를 잘 분리하지 않게 되던데... 함수를 잘 분리해 주셔서 덕분에 긴 흐름을 잘 이해할 수 있었습니다 👍👍