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
6 changes: 6 additions & 0 deletions contains-duplicate/junzero741.ts
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,6 @@
// TC: O(n)
// SC: O(n)
function containsDuplicate(nums: number[]): boolean {
const uniqueNums = new Set<number>(nums);
return uniqueNums.size < nums.length
};
17 changes: 17 additions & 0 deletions house-robber/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TC: O(N)
// SC: O(1)
function rob(nums: number[]): number {
if (nums.length === 0) return 0;

let prevMax: number = 0;
let currMax: number = 0;

for (const amount of nums) {
const nextMax: number = Math.max(currMax, prevMax + amount);

prevMax = currMax;
currMax = nextMax;
}

return currMax;
}
23 changes: 23 additions & 0 deletions longest-consecutive-sequence/junzero741.ts
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,23 @@
// TC: O(N)
// SC: O(N)
function longestConsecutive(nums: number[]): number {
let finalMaxSequence = 0;

// O(n)
const numsSet = new Set(nums);

// O(n)
for(const num of numsSet) {
if(numsSet.has(num - 1)) {
continue;
}
let currentMaxSequence = 1;
while(numsSet.has(num + currentMaxSequence)) {
currentMaxSequence++;
}
finalMaxSequence = Math.max(finalMaxSequence, currentMaxSequence);
}

return finalMaxSequence;

};
37 changes: 37 additions & 0 deletions top-k-frequent-elements/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

// TC: O(NlogN)
// SC: O(N)
function topKFrequent(nums: number[], k: number): number[] {

/**
Map ( O(N) )
1 : 4
4 : 1
3 : 3
2 : 2

-> convert it to array ( O(N) ) : [[1,4], [4,1], [3,3], [2,2]]
-> sort by value ( O(NlogN) ): [[1,4], [3,3], [2,2], [4,1]]
-> slice(0,k) ( (O(N) ) : [[1,4], [3,3]]
-> make array from keys ( (O(N) ) : [1,4]
*/

const numMap = new Map<number, number>();
const n = nums.length;

for(let i = 0; i < n; i++) {
const num = nums[i];
const frequency = (numMap.get(num) || 0)
numMap.set(num, frequency + 1);
}


const numFrequencyArr = Array.from(numMap);
const sortedNumFrequencyKeysArr =
numFrequencyArr
.sort((a,b) => b[1]-a[1])
.map((entry) => entry[0]);
Copy link
Contributor

Choose a reason for hiding this comment

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

작은 부분이긴한데, map전에 slice를 하면 실행시간면에서 약간이라도 줄일 수 있을 것 같습니다

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 sortedNumFrequencyKeysArr.slice(0, k)
};
18 changes: 18 additions & 0 deletions two-sum/junzero741.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// TC: O(n)
// SC: O(n)
function twoSum(nums: number[], target: number): number[] {
const n = nums.length;
const complementMap = new Map<number, number>();

for(let aIndex = 0; aIndex < n; aIndex++) {
const a = nums[aIndex];
const b = target - a;

if(complementMap.has(b)) {
return [complementMap.get(b) || -1, aIndex];
Copy link
Contributor

Choose a reason for hiding this comment

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

해당부분은 위에서 has로 존재가 먼저 평가되어서 or로 null 체크를 안해도 될 것 같습니다!

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 문 안에서 아래와 같이 풀면 더 효율적이겠네요!

    const bIndex = complementMap.get(b);
        if(bIndex !== undefined) {
            return [bIndex, aIndex];
        }

}
complementMap.set(a, aIndex);
}

return [-1, -1];
};