Skip to content

Commit 49715e8

Browse files
authored
Merge pull request #705 from nakjun12/main
[nakjun12] Week1
2 parents f6faac3 + 88e8975 commit 49715e8

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

contains-duplicate/nakjun12.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function (nums) {
6+
return new Set(nums).size !== nums.length;
7+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* TC: O(n)
3+
* SC: O(n)
4+
* */
5+
function longestConsecutive(nums: number[]): number {
6+
if (nums.length === 0) return 0;
7+
8+
const numSet = new Set(nums);
9+
let longest = 0;
10+
11+
for (const num of nums) {
12+
if (numSet.has(num - 1)) {
13+
continue;
14+
}
15+
16+
let length = 1;
17+
while (numSet.has(num + length)) {
18+
length++;
19+
}
20+
21+
longest = Math.max(length, longest);
22+
}
23+
24+
return longest;
25+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function topKFrequent(nums: number[], k: number): number[] {
2+
const frequency = nums.reduce(
3+
(acc, n) => acc.set(n, (acc.get(n) ?? 0) + 1),
4+
new Map<number, number>()
5+
);
6+
7+
return Array.from(frequency)
8+
.sort((a, b) => b[1] - a[1])
9+
.slice(0, k)
10+
.map((item) => item[0]);
11+
}

valid-palindrome/nakjun12.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function isPalindrome(s: string): boolean {
2+
const convertWord = s.toLowerCase().match(/[a-z0-9]/g) ?? [];
3+
const length = convertWord.length;
4+
for (let i = 0; i < length / 2; i++) {
5+
if (convertWord[i] !== convertWord[length - 1 - i]) {
6+
return false;
7+
}
8+
}
9+
10+
return true;
11+
}

0 commit comments

Comments
 (0)