Skip to content

[Wan] Week 1 #683

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

Merged
merged 5 commits into from
Dec 13, 2024
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
21 changes: 21 additions & 0 deletions contains-duplicate/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* 217. Contains Duplicate
* 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.
* https://leetcode.com/problems/contains-duplicate/description/
*/
function containsDuplicate(nums: number[]): boolean {
const set = new Set<number>();

for (let i = 0; i < nums.length; i++) {
if (set.has(nums[i])) {
return true;
}
set.add(nums[i]);
}

return false;
Comment on lines +7 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

아래와 같이 한줄로 풀이하는 방법도 있을것 같습니다 :)
new Set(nums).size !== nums.length;

}

// TC: O(n)
// https://262.ecma-international.org/15.0/index.html#sec-get-set.prototype.size
// 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.
33 changes: 33 additions & 0 deletions longest-consecutive-sequence/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 128. Longest Consecutive Sequence
* Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
*
* You must write an algorithm that runs in O(n) time.
* https://leetcode.com/problems/longest-consecutive-sequence/description/
*/
function longestConsecutive(nums: number[]): number {
const set = new Set(nums);
const sorted = [...set].sort((a, b) => a - b);
Copy link
Contributor

Choose a reason for hiding this comment

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

set을 사용하신김에 has로 원하는 수가 set안에 있는지 확인하는 방법을 사용할 수도 있을것같습니다!


if (sorted.length === 0) {
return 0;
}

let longestSequence = 1;
let currentSequence = 1;
for (let i = 0; i - 1 < sorted.length; i++) {
if (Math.abs(sorted[i + 1] - sorted[i]) === 1) {
currentSequence++;
} else {
if (currentSequence > longestSequence) {
longestSequence = currentSequence;
}
currentSequence = 1;
}
}

return longestSequence;
}

// O(n) time
// O(n) space
31 changes: 31 additions & 0 deletions top-k-frequent-elements/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 347. Top K Frequent Elements
* Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in
* any order.
* https://leetcode.com/problems/top-k-frequent-elements/description/
*/
function topKFrequent(nums: number[], k: number): number[] {
const map = new Map<number, number>();

nums.forEach((n) => {
const count = map.get(n);
if (count) {
map.set(n, count + 1);
return;
}
map.set(n, 1);
});

const kvArray = [...map];
const sorted = kvArray.sort(([, v1], [, v2]) => v2 - v1);

const result: number[] = [];
for (let i = 0; i < k; i++) {
result.push(sorted[i][0]);
}

return result;
}

// O(n) time
// O(n) space
16 changes: 16 additions & 0 deletions valid-palindrome/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 125. Valid Palindrome
* A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all
* non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters
* and numbers.
* Given a string s, return true if it is a palindrome, or false otherwise.
* https://leetcode.com/problems/valid-palindrome/description/
*/
function isPalindrome(s: string): boolean {
const chars = s.replace(/[^a-z0-9]/gi, "").toLowerCase();

return chars === [...chars].reverse().join("");
Comment on lines +10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

정규식과 리스트 관련 함수를 잘 활용하신것 같아요 :)

}

// O(n) time
// O(n) space
Loading