Skip to content

[TotschKa] Week 1 #661

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 15, 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
12 changes: 12 additions & 0 deletions contains-duplicate/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// https://leetcode.com/problems/contains-duplicate/
function containsDuplicate(nums: number[]): boolean {
const counter = {};
for (const n of nums) {
if (!counter[n]) {
counter[n] = 1;
} else {
return true;
}
}
return false;
Comment on lines +3 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

다음과 같이 Set을 활용하는 방법도 있을것 같아요 :)
new Set(nums).size !== nums.length;

}
18 changes: 18 additions & 0 deletions house-robber/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://leetcode.com/problems/house-robber/
let dp;
function rob(nums: number[]): number {
dp = Array.from({ length: nums.length + 1 }, () => -1);
return doRobbery(nums, nums.length - 1);
}

function doRobbery(nums: number[], i: number) {
if (i < 0) {
return 0;
}
if (dp[i] >= 0) {
return dp[i];
}
const money = Math.max(doRobbery(nums, i - 2) + nums[i], doRobbery(nums, i - 1));
dp[i] = money;
return money;
}
18 changes: 18 additions & 0 deletions longest-consecutive-sequence/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// https://leetcode.com/problems/longest-consecutive-sequence/
function longestConsecutive(nums: number[]): number {
if (nums.length === 0) {
return 0;
}
nums = [...new Set(nums)].sort((a, b) => a - b);
let ans = 0;
let left = 0, right = 1;
for (let i = 0; i < nums.length; i++) {
if (nums[i] + 1 === nums[i + 1]) {
ans = Math.max(ans, right - left);
} else {
left = right;
}
right++;
}
return ans + 1;
}
10 changes: 10 additions & 0 deletions top-k-frequent-elements/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://leetcode.com/problems/top-k-frequent-elements/
function topKFrequent(nums: number[], k: number): number[] {
const counter = new Map<number, number>();
for (const n of nums) {
counter.set(n, (counter.get(n) ?? 0) + 1);
}
return [...counter.keys()]
.sort((a, b) => counter.get(b)! - counter.get(a)!)
.slice(0, k);
}
10 changes: 10 additions & 0 deletions valid-palindrome/Totschka.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://leetcode.com/problems/valid-palindrome/description/
function isPalindrome(s: string): boolean {
s = s.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
for (let i = 0; i < s.length / 2; i++) {
if (s[i] !== s[s.length - 1 - i]) {
return false;
}
}
return true;
}
Loading