From e2a514394a2ca4aee648b7067f5b64a2a2c9290d Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:49:19 -0500 Subject: [PATCH 01/14] Add solution #3442 --- README.md | 1 + ...erence-between-even-and-odd-frequency-i.js | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js diff --git a/README.md b/README.md index 0c92dcf0..c0a41c70 100644 --- a/README.md +++ b/README.md @@ -2222,6 +2222,7 @@ 3397|[Maximum Number of Distinct Elements After Operations](./solutions/3397-maximum-number-of-distinct-elements-after-operations.js)|Medium| 3402|[Minimum Operations to Make Columns Strictly Increasing](./solutions/3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy| 3403|[Find the Lexicographically Largest String From the Box I](./solutions/3403-find-the-lexicographically-largest-string-from-the-box-i.js)|Medium| +3442|[Maximum Difference Between Even and Odd Frequency I](./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js)|Easy| 3452|[Sum of Good Numbers](./solutions/3452-sum-of-good-numbers.js)|Easy| 3461|[Check If Digits Are Equal in String After Operations I](./solutions/3461-check-if-digits-are-equal-in-string-after-operations-i.js)|Easy| 3462|[Maximum Sum With at Most K Elements](./solutions/3462-maximum-sum-with-at-most-k-elements.js)|Medium| diff --git a/solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js b/solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js new file mode 100644 index 00000000..313d3fb5 --- /dev/null +++ b/solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js @@ -0,0 +1,39 @@ +/** + * 3442. Maximum Difference Between Even and Odd Frequency I + * https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i/ + * Difficulty: Easy + * + * You are given a string s consisting of lowercase English letters. + * + * Your task is to find the maximum difference diff = freq(a1) - freq(a2) between the frequency + * of characters a1 and a2 in the string such that: + * - a1 has an odd frequency in the string. + * - a2 has an even frequency in the string. + * + * Return this maximum difference. + */ + +/** + * @param {string} s + * @return {number} + */ +var maxDifference = function(s) { + const map = new Map(); + + for (const char of s) { + map.set(char, (map.get(char) || 0) + 1); + } + + let maxOdd = 0; + let minEven = Infinity; + + for (const freq of map.values()) { + if (freq % 2 === 1) { + maxOdd = Math.max(maxOdd, freq); + } else { + minEven = Math.min(minEven, freq); + } + } + + return maxOdd - minEven; +}; From 64ed1dbdf57c5421d6b13d2e86aab9413296a962 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:50:48 -0500 Subject: [PATCH 02/14] Add solution #251 --- README.md | 1 + solutions/0251-flatten-2d-vector.js | 44 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 solutions/0251-flatten-2d-vector.js diff --git a/README.md b/README.md index c0a41c70..d66153bb 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ 248|[Strobogrammatic Number III](./solutions/0248-strobogrammatic-number-iii.js)|Hard| 249|[Group Shifted Strings](./solutions/0249-group-shifted-strings.js)|Medium| 250|[Count Univalue Subtrees](./solutions/0250-count-univalue-subtrees.js)|Medium| +251|[Flatten 2D Vector](./solutions/0251-flatten-2d-vector.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0251-flatten-2d-vector.js b/solutions/0251-flatten-2d-vector.js new file mode 100644 index 00000000..612cc921 --- /dev/null +++ b/solutions/0251-flatten-2d-vector.js @@ -0,0 +1,44 @@ +/** + * 251. Flatten 2D Vector + * https://leetcode.com/problems/flatten-2d-vector/ + * Difficulty: Medium + * + * Design an iterator to flatten a 2D vector. It should support the next and hasNext operations. + * + * Implement the Vector2D class: + * - Vector2D(int[][] vec) initializes the object with the 2D vector vec. + * - next() returns the next element from the 2D vector and moves the pointer one step forward. + * You may assume that all the calls to next are valid. + * - hasNext() returns true if there are still some elements in the vector, and false otherwise. + */ + +/** + * @param {number[][]} vec + */ +var Vector2D = function(vec) { + this.vector = vec; + this.row = 0; + this.col = 0; +}; + +/** + * @return {number} + */ +Vector2D.prototype.next = function() { + while (this.row < this.vector.length && this.col >= this.vector[this.row].length) { + this.row++; + this.col = 0; + } + return this.vector[this.row][this.col++]; +}; + +/** + * @return {boolean} + */ +Vector2D.prototype.hasNext = function() { + while (this.row < this.vector.length && this.col >= this.vector[this.row].length) { + this.row++; + this.col = 0; + } + return this.row < this.vector.length; +}; From 281d6bd6776644b6d571b731e91dab6b9c9c082d Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:52:03 -0500 Subject: [PATCH 03/14] Add solution #252 --- README.md | 1 + solutions/0252-meeting-rooms.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 solutions/0252-meeting-rooms.js diff --git a/README.md b/README.md index d66153bb..1129452e 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,7 @@ 249|[Group Shifted Strings](./solutions/0249-group-shifted-strings.js)|Medium| 250|[Count Univalue Subtrees](./solutions/0250-count-univalue-subtrees.js)|Medium| 251|[Flatten 2D Vector](./solutions/0251-flatten-2d-vector.js)|Medium| +252|[Meeting Rooms](./solutions/0252-meeting-rooms.js)|Easy| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0252-meeting-rooms.js b/solutions/0252-meeting-rooms.js new file mode 100644 index 00000000..09045654 --- /dev/null +++ b/solutions/0252-meeting-rooms.js @@ -0,0 +1,24 @@ +/** + * 252. Meeting Rooms + * https://leetcode.com/problems/meeting-rooms/ + * Difficulty: Easy + * + * Given an array of meeting time intervals where intervals[i] = [starti, endi], determine + * if a person could attend all meetings. + */ + +/** + * @param {number[][]} intervals + * @return {boolean} + */ +var canAttendMeetings = function(intervals) { + intervals.sort((a, b) => a[0] - b[0]); + + for (let i = 1; i < intervals.length; i++) { + if (intervals[i][0] < intervals[i - 1][1]) { + return false; + } + } + + return true; +}; From dd567cc40756eddd0a419d3322358e5ee71886ad Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:53:02 -0500 Subject: [PATCH 04/14] Add solution #253 --- README.md | 1 + solutions/0253-meeting-rooms-ii.js | 34 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 solutions/0253-meeting-rooms-ii.js diff --git a/README.md b/README.md index 1129452e..00485098 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,7 @@ 250|[Count Univalue Subtrees](./solutions/0250-count-univalue-subtrees.js)|Medium| 251|[Flatten 2D Vector](./solutions/0251-flatten-2d-vector.js)|Medium| 252|[Meeting Rooms](./solutions/0252-meeting-rooms.js)|Easy| +253|[Meeting Rooms II](./solutions/0253-meeting-rooms-ii.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0253-meeting-rooms-ii.js b/solutions/0253-meeting-rooms-ii.js new file mode 100644 index 00000000..2c08e7e3 --- /dev/null +++ b/solutions/0253-meeting-rooms-ii.js @@ -0,0 +1,34 @@ +/** + * 253. Meeting Rooms II + * https://leetcode.com/problems/meeting-rooms-ii/ + * Difficulty: Medium + * + * Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], + * return the minimum number of conference rooms required. + */ + +/** + * @param {number[][]} intervals + * @return {number} + */ +var minMeetingRooms = function(intervals) { + const starts = intervals.map(([start]) => start).sort((a, b) => a - b); + const ends = intervals.map(([, end]) => end).sort((a, b) => a - b); + let rooms = 0; + let startIndex = 0; + let endIndex = 0; + let result = 0; + + while (startIndex < intervals.length) { + if (starts[startIndex] < ends[endIndex]) { + rooms++; + result = Math.max(result, rooms); + startIndex++; + } else { + rooms--; + endIndex++; + } + } + + return result; +}; From 1781b76797d1649ca9617b2eb80df8fa7a64f146 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:03:02 -0500 Subject: [PATCH 05/14] Add solution #3445 --- README.md | 1 + ...rence-between-even-and-odd-frequency-ii.js | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js diff --git a/README.md b/README.md index 00485098..c2bca6de 100644 --- a/README.md +++ b/README.md @@ -2226,6 +2226,7 @@ 3402|[Minimum Operations to Make Columns Strictly Increasing](./solutions/3402-minimum-operations-to-make-columns-strictly-increasing.js)|Easy| 3403|[Find the Lexicographically Largest String From the Box I](./solutions/3403-find-the-lexicographically-largest-string-from-the-box-i.js)|Medium| 3442|[Maximum Difference Between Even and Odd Frequency I](./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js)|Easy| +3445|[Maximum Difference Between Even and Odd Frequency II](./solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js)|Hard| 3452|[Sum of Good Numbers](./solutions/3452-sum-of-good-numbers.js)|Easy| 3461|[Check If Digits Are Equal in String After Operations I](./solutions/3461-check-if-digits-are-equal-in-string-after-operations-i.js)|Easy| 3462|[Maximum Sum With at Most K Elements](./solutions/3462-maximum-sum-with-at-most-k-elements.js)|Medium| diff --git a/solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js b/solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js new file mode 100644 index 00000000..3648091e --- /dev/null +++ b/solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js @@ -0,0 +1,70 @@ +/** + * 3445. Maximum Difference Between Even and Odd Frequency II + * https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-ii/ + * Difficulty: Hard + * + * You are given a string s and an integer k. Your task is to find the maximum difference between + * the frequency of two characters, freq[a] - freq[b], in a substring subs of s, such that: + * - subs has a size of at least k. + * - Character a has an odd frequency in subs. + * - Character b has an even frequency in subs. + * + * Return the maximum difference. + * + * Note that subs can contain more than 2 distinct characters. + */ + +/** + * @param {string} s + * @param {number} k + * @return {number} + */ +var maxDifference = function(s, k) { + const n = s.length; + let result = -Infinity; + + const calculateParityStatus = (freqA, freqB) => { + return ((freqA & 1) << 1) | (freqB & 1); + }; + + for (const charA of ['0', '1', '2', '3', '4']) { + for (const charB of ['0', '1', '2', '3', '4']) { + if (charA === charB) continue; + + const minDifferences = [Infinity, Infinity, Infinity, Infinity]; + let currentFreqA = 0; + let currentFreqB = 0; + let prefixFreqA = 0; + let prefixFreqB = 0; + let leftBoundary = -1; + + for (let rightIndex = 0; rightIndex < n; rightIndex++) { + currentFreqA += s[rightIndex] === charA ? 1 : 0; + currentFreqB += s[rightIndex] === charB ? 1 : 0; + + while (rightIndex - leftBoundary >= k && currentFreqB - prefixFreqB >= 2) { + const prefixStatus = calculateParityStatus(prefixFreqA, prefixFreqB); + minDifferences[prefixStatus] = Math.min( + minDifferences[prefixStatus], + prefixFreqA - prefixFreqB + ); + leftBoundary++; + prefixFreqA += s[leftBoundary] === charA ? 1 : 0; + prefixFreqB += s[leftBoundary] === charB ? 1 : 0; + } + + const currentStatus = calculateParityStatus(currentFreqA, currentFreqB); + const targetStatus = currentStatus ^ 0b10; + + if (minDifferences[targetStatus] !== Infinity) { + result = Math.max( + result, + currentFreqA - currentFreqB - minDifferences[targetStatus] + ); + } + } + } + } + + return result; +}; From b6b62b7af6bdce7a89b62f14c1f047c65e886d24 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:29:20 -0500 Subject: [PATCH 06/14] Add solution #254 --- README.md | 1 + solutions/0254-factor-combinations.js | 36 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 solutions/0254-factor-combinations.js diff --git a/README.md b/README.md index c2bca6de..7e653c64 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,7 @@ 251|[Flatten 2D Vector](./solutions/0251-flatten-2d-vector.js)|Medium| 252|[Meeting Rooms](./solutions/0252-meeting-rooms.js)|Easy| 253|[Meeting Rooms II](./solutions/0253-meeting-rooms-ii.js)|Medium| +254|[Factor Combinations](./solutions/0254-factor-combinations.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0254-factor-combinations.js b/solutions/0254-factor-combinations.js new file mode 100644 index 00000000..ab66abba --- /dev/null +++ b/solutions/0254-factor-combinations.js @@ -0,0 +1,36 @@ +/** + * 254. Factor Combinations + * https://leetcode.com/problems/factor-combinations/ + * Difficulty: Medium + * + * Numbers can be regarded as the product of their factors. + * + * For example, 8 = 2 x 2 x 2 = 2 x 4. + * + * Given an integer n, return all possible combinations of its factors. You may return the + * answer in any order. + * + * Note that the factors should be in the range [2, n - 1]. + */ + +/** + * @param {number} n + * @return {number[][]} + */ +var getFactors = function(n) { + const result = []; + findFactors(n, 2, []); + return result; + + function findFactors(currentNum, start, combination) { + for (let i = start; i * i <= currentNum; i++) { + if (currentNum % i === 0) { + const nextNum = currentNum / i; + if (nextNum >= i && nextNum < currentNum) { + result.push([...combination, i, nextNum]); + findFactors(nextNum, i, [...combination, i]); + } + } + } + } +}; From c28a804ed5fc33607bd9910e9002a8a961538059 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:30:17 -0500 Subject: [PATCH 07/14] Add solution #255 --- README.md | 1 + ...preorder-sequence-in-binary-search-tree.js | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 solutions/0255-verify-preorder-sequence-in-binary-search-tree.js diff --git a/README.md b/README.md index 7e653c64..7c5c7d9a 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,7 @@ 252|[Meeting Rooms](./solutions/0252-meeting-rooms.js)|Easy| 253|[Meeting Rooms II](./solutions/0253-meeting-rooms-ii.js)|Medium| 254|[Factor Combinations](./solutions/0254-factor-combinations.js)|Medium| +255|[Verify Preorder Sequence in Binary Search Tree](./solutions/0255-verify-preorder-sequence-in-binary-search-tree.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0255-verify-preorder-sequence-in-binary-search-tree.js b/solutions/0255-verify-preorder-sequence-in-binary-search-tree.js new file mode 100644 index 00000000..2e502211 --- /dev/null +++ b/solutions/0255-verify-preorder-sequence-in-binary-search-tree.js @@ -0,0 +1,27 @@ +/** + * 255. Verify Preorder Sequence in Binary Search Tree + * https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ + * Difficulty: Medium + * + * Given an array of unique integers preorder, return true if it is the correct preorder + * traversal sequence of a binary search tree. + */ + +/** + * @param {number[]} preorder + * @return {boolean} + */ +var verifyPreorder = function(preorder) { + let minLimit = -Infinity; + const stack = []; + + for (const value of preorder) { + while (stack.length && stack[stack.length - 1] < value) { + minLimit = stack.pop(); + } + if (value <= minLimit) return false; + stack.push(value); + } + + return true; +}; From 8514d2214466e223b3466c413ac4cbc43d897407 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:32:06 -0500 Subject: [PATCH 08/14] Add solution #256 --- README.md | 1 + solutions/0256-paint-house.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 solutions/0256-paint-house.js diff --git a/README.md b/README.md index 7c5c7d9a..f46e3c67 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ 253|[Meeting Rooms II](./solutions/0253-meeting-rooms-ii.js)|Medium| 254|[Factor Combinations](./solutions/0254-factor-combinations.js)|Medium| 255|[Verify Preorder Sequence in Binary Search Tree](./solutions/0255-verify-preorder-sequence-in-binary-search-tree.js)|Medium| +256|[Paint House](./solutions/0256-paint-house.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| diff --git a/solutions/0256-paint-house.js b/solutions/0256-paint-house.js new file mode 100644 index 00000000..c37cc991 --- /dev/null +++ b/solutions/0256-paint-house.js @@ -0,0 +1,34 @@ +/** + * 256. Paint House + * https://leetcode.com/problems/paint-house/ + * Difficulty: Medium + * + * There is a row of n houses, where each house can be painted one of three colors: red, blue, + * or green. The cost of painting each house with a certain color is different. You have to + * paint all the houses such that no two adjacent houses have the same color. + * + * The cost of painting each house with a certain color is represented by an n x 3 cost matrix + * costs. + * - For example, costs[0][0] is the cost of painting house 0 with the color red; costs[1][2] + * is the cost of painting house 1 with color green, and so on... + * + * Return the minimum cost to paint all houses. + */ + +/** + * @param {number[][]} costs + * @return {number} + */ +var minCost = function(costs) { + let prev = [...costs[0]]; + + for (let i = 1; i < costs.length; i++) { + prev = [ + costs[i][0] + Math.min(prev[1], prev[2]), + costs[i][1] + Math.min(prev[0], prev[2]), + costs[i][2] + Math.min(prev[0], prev[1]) + ]; + } + + return Math.min(...prev); +}; From 34bce6d304df5bfc3b7fc6430f19cac95d531b46 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:33:01 -0500 Subject: [PATCH 09/14] Add solution #259 --- README.md | 1 + solutions/0259-3sum-smaller.js | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 solutions/0259-3sum-smaller.js diff --git a/README.md b/README.md index f46e3c67..8868f4a2 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,7 @@ 256|[Paint House](./solutions/0256-paint-house.js)|Medium| 257|[Binary Tree Paths](./solutions/0257-binary-tree-paths.js)|Easy| 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| +259|[3Sum Smaller](./solutions/0259-3sum-smaller.js)|Medium| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| 263|[Ugly Number](./solutions/0263-ugly-number.js)|Easy| 264|[Ugly Number II](./solutions/0264-ugly-number-ii.js)|Medium| diff --git a/solutions/0259-3sum-smaller.js b/solutions/0259-3sum-smaller.js new file mode 100644 index 00000000..89e0d4b5 --- /dev/null +++ b/solutions/0259-3sum-smaller.js @@ -0,0 +1,36 @@ +/** + * 259. 3Sum Smaller + * https://leetcode.com/problems/3sum-smaller/ + * Difficulty: Medium + * + * Given an array of n integers nums and an integer target, find the number of index triplets + * i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. + */ + +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var threeSumSmaller = function(nums, target) { + nums.sort((a, b) => a - b); + const n = nums.length; + let result = 0; + + for (let i = 0; i < n - 2; i++) { + let left = i + 1; + let right = n - 1; + + while (left < right) { + const sum = nums[i] + nums[left] + nums[right]; + if (sum < target) { + result += right - left; + left++; + } else { + right--; + } + } + } + + return result; +}; From 204f1e04339fb3bbeb4863d837bbb96ea764486b Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:45:03 -0500 Subject: [PATCH 10/14] Add solution #261 --- README.md | 1 + solutions/0261-graph-valid-tree.js | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 solutions/0261-graph-valid-tree.js diff --git a/README.md b/README.md index 8868f4a2..d6211ff7 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ 258|[Add Digits](./solutions/0258-add-digits.js)|Easy| 259|[3Sum Smaller](./solutions/0259-3sum-smaller.js)|Medium| 260|[Single Number III](./solutions/0260-single-number-iii.js)|Medium| +261|[Graph Valid Tree](./solutions/0261-graph-valid-tree.js)|Medium| 263|[Ugly Number](./solutions/0263-ugly-number.js)|Easy| 264|[Ugly Number II](./solutions/0264-ugly-number-ii.js)|Medium| 268|[Missing Number](./solutions/0268-missing-number.js)|Easy| diff --git a/solutions/0261-graph-valid-tree.js b/solutions/0261-graph-valid-tree.js new file mode 100644 index 00000000..483b2345 --- /dev/null +++ b/solutions/0261-graph-valid-tree.js @@ -0,0 +1,45 @@ +/** + * 261. Graph Valid Tree + * https://leetcode.com/problems/graph-valid-tree/ + * Difficulty: Medium + * + * You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of + * edges where edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai + * and bi in the graph. + * + * Return true if the edges of the given graph make up a valid tree, and false otherwise. + */ + +/** + * @param {number} n + * @param {number[][]} edges + * @return {boolean} + */ +var validTree = function(n, edges) { + const parent = new Array(n).fill(-1); + + for (const [u, v] of edges) { + if (!union(u, v)) return false; + } + + let components = 0; + for (let i = 0; i < n; i++) { + if (parent[i] === -1) components++; + if (components > 1) return false; + } + + return edges.length === n - 1; + + function find(x) { + if (parent[x] === -1) return x; + return parent[x] = find(parent[x]); + } + + function union(x, y) { + const rootX = find(x); + const rootY = find(y); + if (rootX === rootY) return false; + parent[rootX] = rootY; + return true; + } +}; From 4b4ed1c5202bcfbae1aff86ad0afb92ea7b5cef0 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:46:39 -0500 Subject: [PATCH 11/14] Add solution #265 --- README.md | 1 + solutions/0265-paint-house-ii.js | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 solutions/0265-paint-house-ii.js diff --git a/README.md b/README.md index d6211ff7..7d565dbd 100644 --- a/README.md +++ b/README.md @@ -253,6 +253,7 @@ 261|[Graph Valid Tree](./solutions/0261-graph-valid-tree.js)|Medium| 263|[Ugly Number](./solutions/0263-ugly-number.js)|Easy| 264|[Ugly Number II](./solutions/0264-ugly-number-ii.js)|Medium| +265|[Paint House II](./solutions/0265-paint-house-ii.js)|Hard| 268|[Missing Number](./solutions/0268-missing-number.js)|Easy| 273|[Integer to English Words](./solutions/0273-integer-to-english-words.js)|Hard| 274|[H-Index](./solutions/0274-h-index.js)|Medium| diff --git a/solutions/0265-paint-house-ii.js b/solutions/0265-paint-house-ii.js new file mode 100644 index 00000000..6a883f10 --- /dev/null +++ b/solutions/0265-paint-house-ii.js @@ -0,0 +1,51 @@ +/** + * 265. Paint House II + * https://leetcode.com/problems/paint-house-ii/ + * Difficulty: Hard + * + * There are a row of n houses, each house can be painted with one of the k colors. The cost of + * painting each house with a certain color is different. You have to paint all the houses such + * that no two adjacent houses have the same color. + * + * The cost of painting each house with a certain color is represented by an n x k cost matrix + * costs. + * - For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the + * cost of painting house 1 with color 2, and so on... + * + * Return the minimum cost to paint all houses. + */ + +/** + * @param {number[][]} costs + * @return {number} + */ +var minCostII = function(costs) { + const n = costs.length; + const k = costs[0].length; + let prev = [...costs[0]]; + + for (let i = 1; i < n; i++) { + const curr = new Array(k); + let min1 = Infinity; + let min2 = Infinity; + let index = -1; + + for (let j = 0; j < k; j++) { + if (prev[j] < min1) { + min2 = min1; + min1 = prev[j]; + index = j; + } else if (prev[j] < min2) { + min2 = prev[j]; + } + } + + for (let j = 0; j < k; j++) { + curr[j] = costs[i][j] + (j === index ? min2 : min1); + } + + prev = curr; + } + + return Math.min(...prev); +}; From 109acf98bfb64977061cc7cb56b64742ebe85888 Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:47:52 -0500 Subject: [PATCH 12/14] Add solution #266 --- README.md | 1 + solutions/0266-palindrome-permutation.js | 28 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 solutions/0266-palindrome-permutation.js diff --git a/README.md b/README.md index 7d565dbd..435bbd25 100644 --- a/README.md +++ b/README.md @@ -254,6 +254,7 @@ 263|[Ugly Number](./solutions/0263-ugly-number.js)|Easy| 264|[Ugly Number II](./solutions/0264-ugly-number-ii.js)|Medium| 265|[Paint House II](./solutions/0265-paint-house-ii.js)|Hard| +266|[Palindrome Permutation](./solutions/0266-palindrome-permutation.js)|Easy| 268|[Missing Number](./solutions/0268-missing-number.js)|Easy| 273|[Integer to English Words](./solutions/0273-integer-to-english-words.js)|Hard| 274|[H-Index](./solutions/0274-h-index.js)|Medium| diff --git a/solutions/0266-palindrome-permutation.js b/solutions/0266-palindrome-permutation.js new file mode 100644 index 00000000..9f710d28 --- /dev/null +++ b/solutions/0266-palindrome-permutation.js @@ -0,0 +1,28 @@ +/** + * 266. Palindrome Permutation + * https://leetcode.com/problems/palindrome-permutation/ + * Difficulty: Easy + * + * Given a string s, return true if a permutation of the string could form a palindrome + * and false otherwise. + */ + +/** + * @param {string} s + * @return {boolean} + */ +var canPermutePalindrome = function(s) { + const map = new Map(); + + for (const char of s) { + map.set(char, (map.get(char) || 0) + 1); + } + + let oddCount = 0; + for (const count of map.values()) { + if (count % 2 !== 0) oddCount++; + if (oddCount > 1) return false; + } + + return true; +}; From 83b8a8385bf6e5589f4befeeff428310fbfe0e0a Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:49:49 -0500 Subject: [PATCH 13/14] Add solution #267 --- README.md | 1 + solutions/0267-palindrome-permutation-ii.js | 62 +++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 solutions/0267-palindrome-permutation-ii.js diff --git a/README.md b/README.md index 435bbd25..e7053d3f 100644 --- a/README.md +++ b/README.md @@ -255,6 +255,7 @@ 264|[Ugly Number II](./solutions/0264-ugly-number-ii.js)|Medium| 265|[Paint House II](./solutions/0265-paint-house-ii.js)|Hard| 266|[Palindrome Permutation](./solutions/0266-palindrome-permutation.js)|Easy| +267|[Palindrome Permutation II](./solutions/0267-palindrome-permutation-ii.js)|Medium| 268|[Missing Number](./solutions/0268-missing-number.js)|Easy| 273|[Integer to English Words](./solutions/0273-integer-to-english-words.js)|Hard| 274|[H-Index](./solutions/0274-h-index.js)|Medium| diff --git a/solutions/0267-palindrome-permutation-ii.js b/solutions/0267-palindrome-permutation-ii.js new file mode 100644 index 00000000..6f1127c0 --- /dev/null +++ b/solutions/0267-palindrome-permutation-ii.js @@ -0,0 +1,62 @@ +/** + * 267. Palindrome Permutation II + * https://leetcode.com/problems/palindrome-permutation-ii/ + * Difficulty: Medium + * + * Given a string s, return all the palindromic permutations (without duplicates) of it. + * + * You may return the answer in any order. If s has no palindromic permutation, return an + * empty list. + */ + +/** + * @param {string} s + * @return {string[]} + */ +var generatePalindromes = function(s) { + const map = new Map(); + for (const char of s) { + map.set(char, (map.get(char) || 0) + 1); + } + + let oddChar = ''; + const half = []; + let oddCount = 0; + + for (const [char, count] of map) { + if (count % 2) { + oddCount++; + oddChar = char; + } + for (let i = 0; i < Math.floor(count / 2); i++) { + half.push(char); + } + } + + if (oddCount > 1) return []; + + const result = new Set(); + + half.sort(); + permute(half, [], new Array(half.length).fill(false)); + return Array.from(result); + + function permute(chars, current, used) { + if (current.length === chars.length) { + const palindrome = current.join('') + oddChar + current.reverse().join(''); + result.add(palindrome); + current.reverse(); + return; + } + + for (let i = 0; i < chars.length; i++) { + if (!used[i] && (i === 0 || chars[i] !== chars[i - 1] || used[i - 1])) { + used[i] = true; + current.push(chars[i]); + permute(chars, current, used); + current.pop(); + used[i] = false; + } + } + } +}; From 5b3ba9844209f81e1f0e190173fbd63c656ae66d Mon Sep 17 00:00:00 2001 From: Josh Crozier <5490537+JoshCrozier@users.noreply.github.com> Date: Tue, 10 Jun 2025 22:55:46 -0500 Subject: [PATCH 14/14] Add solution #269 --- README.md | 1 + solutions/0269-alien-dictionary.js | 73 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 solutions/0269-alien-dictionary.js diff --git a/README.md b/README.md index e7053d3f..80fb4078 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,7 @@ 266|[Palindrome Permutation](./solutions/0266-palindrome-permutation.js)|Easy| 267|[Palindrome Permutation II](./solutions/0267-palindrome-permutation-ii.js)|Medium| 268|[Missing Number](./solutions/0268-missing-number.js)|Easy| +269|[Alien Dictionary](./solutions/0269-alien-dictionary.js)|Hard| 273|[Integer to English Words](./solutions/0273-integer-to-english-words.js)|Hard| 274|[H-Index](./solutions/0274-h-index.js)|Medium| 275|[H-Index II](./solutions/0275-h-index-ii.js)|Medium| diff --git a/solutions/0269-alien-dictionary.js b/solutions/0269-alien-dictionary.js new file mode 100644 index 00000000..00e19ca3 --- /dev/null +++ b/solutions/0269-alien-dictionary.js @@ -0,0 +1,73 @@ +/** + * 269. Alien Dictionary + * https://leetcode.com/problems/alien-dictionary/ + * Difficulty: Hard + * + * There is a new alien language that uses the English alphabet. However, the order of the + * letters is unknown to you. + * + * You are given a list of strings words from the alien language's dictionary. Now it is + * claimed that the strings in words are sorted lexicographically by the rules of this new + * language. + * + * If this claim is incorrect, and the given arrangement of string in words cannot correspond + * to any order of letters, return "". + * + * Otherwise, return a string of the unique letters in the new alien language sorted in + * lexicographically increasing order by the new language's rules. If there are multiple + * solutions, return any of them. + */ + +/** + * @param {string[]} words + * @return {string} + */ +var alienOrder = function(words) { + const graph = new Map(); + const inDegree = new Map(); + + for (const word of words) { + for (const char of word) { + if (!graph.has(char)) { + graph.set(char, new Set()); + inDegree.set(char, 0); + } + } + } + + for (let i = 1; i < words.length; i++) { + const prev = words[i - 1]; + const curr = words[i]; + const minLen = Math.min(prev.length, curr.length); + + if (prev.length > curr.length && prev.startsWith(curr)) return ''; + + for (let j = 0; j < minLen; j++) { + if (prev[j] !== curr[j]) { + if (!graph.get(prev[j]).has(curr[j])) { + graph.get(prev[j]).add(curr[j]); + inDegree.set(curr[j], inDegree.get(curr[j]) + 1); + } + break; + } + } + } + + const queue = []; + for (const [char, degree] of inDegree) { + if (degree === 0) queue.push(char); + } + + let result = ''; + while (queue.length) { + const char = queue.shift(); + result += char; + + for (const next of graph.get(char)) { + inDegree.set(next, inDegree.get(next) - 1); + if (inDegree.get(next) === 0) queue.push(next); + } + } + + return result.length === graph.size ? result : ''; +};