Skip to content

Commit b901b4e

Browse files
authored
Merge pull request #683 from taewanseoul/main
2 parents 8f1e53e + 054e552 commit b901b4e

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

contains-duplicate/taewanseoul.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 217. Contains Duplicate
3+
* Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
4+
* https://leetcode.com/problems/contains-duplicate/description/
5+
*/
6+
function containsDuplicate(nums: number[]): boolean {
7+
const set = new Set<number>();
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (set.has(nums[i])) {
11+
return true;
12+
}
13+
set.add(nums[i]);
14+
}
15+
16+
return false;
17+
}
18+
19+
// TC: O(n)
20+
// https://262.ecma-international.org/15.0/index.html#sec-get-set.prototype.size
21+
// Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable implementation model.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 128. Longest Consecutive Sequence
3+
* Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
4+
*
5+
* You must write an algorithm that runs in O(n) time.
6+
* https://leetcode.com/problems/longest-consecutive-sequence/description/
7+
*/
8+
function longestConsecutive(nums: number[]): number {
9+
const set = new Set(nums);
10+
const sorted = [...set].sort((a, b) => a - b);
11+
12+
if (sorted.length === 0) {
13+
return 0;
14+
}
15+
16+
let longestSequence = 1;
17+
let currentSequence = 1;
18+
for (let i = 0; i - 1 < sorted.length; i++) {
19+
if (Math.abs(sorted[i + 1] - sorted[i]) === 1) {
20+
currentSequence++;
21+
} else {
22+
if (currentSequence > longestSequence) {
23+
longestSequence = currentSequence;
24+
}
25+
currentSequence = 1;
26+
}
27+
}
28+
29+
return longestSequence;
30+
}
31+
32+
// O(n) time
33+
// O(n) space
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 347. Top K Frequent Elements
3+
* Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in
4+
* any order.
5+
* https://leetcode.com/problems/top-k-frequent-elements/description/
6+
*/
7+
function topKFrequent(nums: number[], k: number): number[] {
8+
const map = new Map<number, number>();
9+
10+
nums.forEach((n) => {
11+
const count = map.get(n);
12+
if (count) {
13+
map.set(n, count + 1);
14+
return;
15+
}
16+
map.set(n, 1);
17+
});
18+
19+
const kvArray = [...map];
20+
const sorted = kvArray.sort(([, v1], [, v2]) => v2 - v1);
21+
22+
const result: number[] = [];
23+
for (let i = 0; i < k; i++) {
24+
result.push(sorted[i][0]);
25+
}
26+
27+
return result;
28+
}
29+
30+
// O(n) time
31+
// O(n) space

valid-palindrome/taewanseoul.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* 125. Valid Palindrome
3+
* A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all
4+
* non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters
5+
* and numbers.
6+
* Given a string s, return true if it is a palindrome, or false otherwise.
7+
* https://leetcode.com/problems/valid-palindrome/description/
8+
*/
9+
function isPalindrome(s: string): boolean {
10+
const chars = s.replace(/[^a-z0-9]/gi, "").toLowerCase();
11+
12+
return chars === [...chars].reverse().join("");
13+
}
14+
15+
// O(n) time
16+
// O(n) space

0 commit comments

Comments
 (0)