diff --git a/README.md b/README.md index 4e20e6ae..ca4318ab 100644 --- a/README.md +++ b/README.md @@ -2683,6 +2683,7 @@ 3205|[Maximum Array Hopping Score I](./solutions/3205-maximum-array-hopping-score-i.js)|Medium| 3208|[Alternating Groups II](./solutions/3208-alternating-groups-ii.js)|Medium| 3215|[Count Triplets with Even XOR Set Bits II](./solutions/3215-count-triplets-with-even-xor-set-bits-ii.js)|Medium| +3217|[Delete Nodes From Linked List Present in Array](./solutions/3217-delete-nodes-from-linked-list-present-in-array.js)|Medium| 3221|[Maximum Array Hopping Score II](./solutions/3221-maximum-array-hopping-score-ii.js)|Medium| 3223|[Minimum Length of String After Operations](./solutions/3223-minimum-length-of-string-after-operations.js)|Medium| 3227|[Vowels Game in a String](./solutions/3227-vowels-game-in-a-string.js)|Medium| @@ -2695,12 +2696,14 @@ 3272|[Find the Count of Good Integers](./solutions/3272-find-the-count-of-good-integers.js)|Hard| 3279|[Maximum Total Area Occupied by Pistons](./solutions/3279-maximum-total-area-occupied-by-pistons.js)|Hard| 3284|[Sum of Consecutive Subarrays](./solutions/3284-sum-of-consecutive-subarrays.js)|Medium| +3289|[The Two Sneaky Numbers of Digitville](./solutions/3289-the-two-sneaky-numbers-of-digitville.js)|Easy| 3294|[Convert Doubly Linked List to Array II](./solutions/3294-convert-doubly-linked-list-to-array-ii.js)|Medium| 3299|[Sum of Consecutive Subsequences](./solutions/3299-sum-of-consecutive-subsequences.js)|Hard| 3304|[Find the K-th Character in String Game I](./solutions/3304-find-the-k-th-character-in-string-game-i.js)|Easy| 3306|[Count of Substrings Containing Every Vowel and K Consonants II](./solutions/3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium| 3307|[Find the K-th Character in String Game II](./solutions/3307-find-the-k-th-character-in-string-game-ii.js)|Hard| 3313|[Find the Last Marked Nodes in Tree](./solutions/3313-find-the-last-marked-nodes-in-tree.js)|Hard| +3318|[Find X-Sum of All K-Long Subarrays I](./solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js)|Easy| 3323|[Minimize Connected Groups by Inserting Interval](./solutions/3323-minimize-connected-groups-by-inserting-interval.js)|Medium| 3329|[Count Substrings With K-Frequency Characters II](./solutions/3329-count-substrings-with-k-frequency-characters-ii.js)|Hard| 3330|[Find the Original Typed String I](./solutions/3330-find-the-original-typed-string-i.js)|Easy| @@ -2717,12 +2720,14 @@ 3349|[Adjacent Increasing Subarrays Detection I](./solutions/3349-adjacent-increasing-subarrays-detection-i.js)|Easy| 3350|[Adjacent Increasing Subarrays Detection II](./solutions/3350-adjacent-increasing-subarrays-detection-ii.js)|Medium| 3353|[Minimum Total Operations](./solutions/3353-minimum-total-operations.js)|Easy| +3354|[Make Array Elements Equal to Zero](./solutions/3354-make-array-elements-equal-to-zero.js)|Easy| 3355|[Zero Array Transformation I](./solutions/3355-zero-array-transformation-i.js)|Medium| 3356|[Zero Array Transformation II](./solutions/3356-zero-array-transformation-ii.js)|Medium| 3359|[Find Sorted Submatrices With Maximum Element at Most K](./solutions/3359-find-sorted-submatrices-with-maximum-element-at-most-k.js)|Hard| 3362|[Zero Array Transformation III](./solutions/3362-zero-array-transformation-iii.js)|Medium| 3363|[Find the Maximum Number of Fruits Collected](./solutions/3363-find-the-maximum-number-of-fruits-collected.js)|Hard| 3369|[Design an Array Statistics Tracker](./solutions/3369-design-an-array-statistics-tracker.js)|Hard| +3370|[Smallest Number With All Set Bits](./solutions/3370-smallest-number-with-all-set-bits.js)|Easy| 3372|[Maximize the Number of Target Nodes After Connecting Trees I](./solutions/3372-maximize-the-number-of-target-nodes-after-connecting-trees-i.js)|Medium| 3373|[Maximize the Number of Target Nodes After Connecting Trees II](./solutions/3373-maximize-the-number-of-target-nodes-after-connecting-trees-ii.js)|Hard| 3375|[Minimum Operations to Make Array Values Equal to K](./solutions/3375-minimum-operations-to-make-array-values-equal-to-k.js)|Easy| diff --git a/solutions/3217-delete-nodes-from-linked-list-present-in-array.js b/solutions/3217-delete-nodes-from-linked-list-present-in-array.js new file mode 100644 index 00000000..62329285 --- /dev/null +++ b/solutions/3217-delete-nodes-from-linked-list-present-in-array.js @@ -0,0 +1,37 @@ +/** + * 3217. Delete Nodes From Linked List Present in Array + * https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/ + * Difficulty: Medium + * + * You are given an array of integers nums and the head of a linked list. Return the head + * of the modified linked list after removing all nodes from the linked list that have + * a value that exists in nums. + */ + +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {number[]} nums + * @param {ListNode} head + * @return {ListNode} + */ +var modifiedList = function(nums, head) { + const set = new Set(nums); + const temp = new ListNode(0, head); + let current = temp; + + while (current.next) { + if (set.has(current.next.val)) { + current.next = current.next.next; + } else { + current = current.next; + } + } + + return temp.next; +}; diff --git a/solutions/3289-the-two-sneaky-numbers-of-digitville.js b/solutions/3289-the-two-sneaky-numbers-of-digitville.js new file mode 100644 index 00000000..00f41559 --- /dev/null +++ b/solutions/3289-the-two-sneaky-numbers-of-digitville.js @@ -0,0 +1,31 @@ +/** + * 3289. The Two Sneaky Numbers of Digitville + * https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/ + * Difficulty: Easy + * + * In the town of Digitville, there was a list of numbers called nums containing integers + * from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, + * two mischievous numbers sneaked in an additional time, making the list longer than usual. + * + * As the town detective, your task is to find these two sneaky numbers. Return an array of + * size two containing the two numbers (in any order), so peace can return to Digitville. + */ + +/** + * @param {number[]} nums + * @return {number[]} + */ +var getSneakyNumbers = function(nums) { + const set = new Set(); + const result = []; + + for (const n of nums) { + if (set.has(n)) { + result.push(n); + } else { + set.add(n); + } + } + + return result; +}; diff --git a/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js b/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js new file mode 100644 index 00000000..2a030036 --- /dev/null +++ b/solutions/3318-find-x-sum-of-all-k-long-subarrays-i.js @@ -0,0 +1,60 @@ +/** + * 3318. Find X-Sum of All K-Long Subarrays I + * https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i/ + * Difficulty: Easy + * + * You are given an array nums of n integers and two integers k and x. + * + * The x-sum of an array is calculated by the following procedure: + * - Count the occurrences of all elements in the array. + * - Keep only the occurrences of the top x most frequent elements. If two elements + * have the same number of occurrences, the element with the bigger value is + * considered more frequent. + * - Calculate the sum of the resulting array. + * + * Note that if an array has less than x distinct elements, its x-sum is the sum of the array. + * + Return an integer array answer of length n - k + 1 where answer[i] is the x-sum of the + subarray nums[i..i + k - 1]. + */ + +/** + * @param {number[]} nums + * @param {number} k + * @param {number} x + * @return {number[]} + */ +var findXSum = function(nums, k, x) { + const n = nums.length; + const result = []; + + for (let i = 0; i <= n - k; i++) { + const subarray = nums.slice(i, i + k); + result.push(helper(subarray)); + } + + return result; + + function helper(subarray) { + const frequency = new Map(); + + for (const num of subarray) { + frequency.set(num, (frequency.get(num) || 0) + 1); + } + + const elements = Array.from(frequency.entries()); + elements.sort((a, b) => { + if (a[1] !== b[1]) return b[1] - a[1]; + return b[0] - a[0]; + }); + + const topX = elements.slice(0, x); + let sum = 0; + + for (const [value, count] of topX) { + sum += value * count; + } + + return sum; + } +}; diff --git a/solutions/3354-make-array-elements-equal-to-zero.js b/solutions/3354-make-array-elements-equal-to-zero.js new file mode 100644 index 00000000..865cff41 --- /dev/null +++ b/solutions/3354-make-array-elements-equal-to-zero.js @@ -0,0 +1,60 @@ +/** + * 3354. Make Array Elements Equal to Zero + * https://leetcode.com/problems/make-array-elements-equal-to-zero/ + * Difficulty: Easy + * + * You are given an integer array nums. + * + * Start by selecting a starting position curr such that nums[curr] == 0, and choose a movement + * direction of either left or right. + * + * After that, you repeat the following process: + * - If curr is out of the range [0, n - 1], this process ends. + * - If nums[curr] == 0, move in the current direction by incrementing curr if you are moving + * right, or decrementing curr if you are moving left. + * - Else if nums[curr] > 0: + * - Decrement nums[curr] by 1. + * - Reverse your movement direction (left becomes right and vice versa). + * - Take a step in your new direction. + * + * A selection of the initial position curr and movement direction is considered valid if + * every element in nums becomes 0 by the end of the process. + * + * Return the number of possible valid selections. + */ + +/** + * @param {number[]} nums + * @return {number} + */ +var countValidSelections = function(nums) { + const n = nums.length; + let result = 0; + + for (let i = 0; i < n; i++) { + if (nums[i] === 0) { + if (helper(i, -1)) result++; + if (helper(i, 1)) result++; + } + } + + return result; + + function helper(startIndex, direction) { + const arr = [...nums]; + let current = startIndex; + let dir = direction; + + while (current >= 0 && current < n) { + if (arr[current] === 0) { + current += dir; + } else { + arr[current]--; + dir = -dir; + current += dir; + } + } + + return arr.every(val => val === 0); + } +}; diff --git a/solutions/3370-smallest-number-with-all-set-bits.js b/solutions/3370-smallest-number-with-all-set-bits.js new file mode 100644 index 00000000..ea6b33ca --- /dev/null +++ b/solutions/3370-smallest-number-with-all-set-bits.js @@ -0,0 +1,20 @@ +/** + * 3370. Smallest Number With All Set Bits + * https://leetcode.com/problems/smallest-number-with-all-set-bits/ + * Difficulty: Easy + * + * You are given a positive number n. + * + * Return the smallest number x greater than or equal to n, such that the binary + * representation of x contains only set bits + */ + +/** + * @param {number} n + * @return {number} + */ +var smallestNumber = function(n) { + const bits = n.toString(2).length; + const result = (1 << bits) - 1; + return result >= n ? result : (1 << (bits + 1)) - 1; +};