From 408f184e520b713b97c15ada3aa5fc20b7be1dfb Mon Sep 17 00:00:00 2001 From: TotschKa Date: Mon, 9 Dec 2024 22:07:56 +0900 Subject: [PATCH 1/5] contains duplicate --- contains-duplicate/Totschka.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 contains-duplicate/Totschka.ts diff --git a/contains-duplicate/Totschka.ts b/contains-duplicate/Totschka.ts new file mode 100644 index 000000000..7c179787b --- /dev/null +++ b/contains-duplicate/Totschka.ts @@ -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; +} From 6e44a93775c998532ebcd81f8de23cd8a0851992 Mon Sep 17 00:00:00 2001 From: TotschKa Date: Tue, 10 Dec 2024 01:22:44 +0900 Subject: [PATCH 2/5] valid palindrome --- valid-palindrome/Totschka.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 valid-palindrome/Totschka.ts diff --git a/valid-palindrome/Totschka.ts b/valid-palindrome/Totschka.ts new file mode 100644 index 000000000..1cbd6b48e --- /dev/null +++ b/valid-palindrome/Totschka.ts @@ -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; +} From 8541dec58c410e8ef8644a0a8872aa150c7d98e8 Mon Sep 17 00:00:00 2001 From: TotschKa Date: Tue, 10 Dec 2024 23:32:53 +0900 Subject: [PATCH 3/5] top k frequent elements --- top-k-frequent-elements/Totschka.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 top-k-frequent-elements/Totschka.ts diff --git a/top-k-frequent-elements/Totschka.ts b/top-k-frequent-elements/Totschka.ts new file mode 100644 index 000000000..ecd54770e --- /dev/null +++ b/top-k-frequent-elements/Totschka.ts @@ -0,0 +1,10 @@ +// https://leetcode.com/problems/top-k-frequent-elements/ +function topKFrequent(nums: number[], k: number): number[] { + const counter = new Map(); + 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); +} From 5406dafd5a3c578b034d049c0f8fb12cb9e7068e Mon Sep 17 00:00:00 2001 From: TotschKa Date: Thu, 12 Dec 2024 00:01:15 +0900 Subject: [PATCH 4/5] longest consecutive sequence --- longest-consecutive-sequence/Totschka.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 longest-consecutive-sequence/Totschka.ts diff --git a/longest-consecutive-sequence/Totschka.ts b/longest-consecutive-sequence/Totschka.ts new file mode 100644 index 000000000..0061ed72a --- /dev/null +++ b/longest-consecutive-sequence/Totschka.ts @@ -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; +} From 47b2de270be556ba7d7e666a751c4f47e224ae00 Mon Sep 17 00:00:00 2001 From: TotschKa Date: Sat, 14 Dec 2024 23:31:01 +0900 Subject: [PATCH 5/5] house robber --- house-robber/Totschka.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 house-robber/Totschka.ts diff --git a/house-robber/Totschka.ts b/house-robber/Totschka.ts new file mode 100644 index 000000000..91e92b254 --- /dev/null +++ b/house-robber/Totschka.ts @@ -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; +}