diff --git a/apps/docs/content/array/1365-how-many-numbers-are-smaller-than-the-current-number.mdx b/apps/docs/content/array/1365-how-many-numbers-are-smaller-than-the-current-number.mdx deleted file mode 100644 index 7d83301..0000000 --- a/apps/docs/content/array/1365-how-many-numbers-are-smaller-than-the-current-number.mdx +++ /dev/null @@ -1,77 +0,0 @@ ---- -title: '1365. How Many Numbers Are Smaller Than the Current Number' -description: Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i] -sidebar: - label: 'How Many Numbers Are Smaller Than the Current Number' - badge: 'Easy' ---- - -Array - -### Example 1: -- Input: `nums = [8,1,2,2,3]` -- Output: `[4,0,1,1,3]` -- Explanation: For `nums[0]=8` there exist four smaller numbers than it (`1`, `2`, `2` and `3`). For `nums[1]=1` does not exist any smaller number than it. For `nums[2]=2` there exist one smaller number than it (`1`). For `nums[3]=2` there exist one smaller number than it (`1`). For `nums[4]=3` there exist three smaller numbers than it (`1`, `2` and `2`). - -### Example 2: -- Input: `nums = [6,5,4,8]` -- Output: `[2,1,0,3]` - -### Example 3: -- Input: `nums = [7,7,7,7]` -- Output: `[0,0,0,0]` - -### Constraints: - -- `2 <= nums.length <= 500` -- `0 <= nums[i] <= 100` - -## Approach - -```mermaid -flowchart TD - S(["smallerNumbersThanCurrent(nums)"]) --> F["freq: count every value in nums"] - F --> O["sorted = keys of freq, ascending"] - O --> I["count = 0, smaller = {}"] - I --> L{"more num in sorted?"} - L -- yes --> A["smaller[num] = count — everything already passed is smaller"] - A --> B["count += freq[num]"] - B --> L - L -- no --> M(["return nums.map(n => smaller[n])"]) -``` - -## Solution - -```js -/** - * @param {number[]} nums - * @return {number[]} - */ - -var smallerNumbersThanCurrent = function (nums) { - // Step 1: Begin by initializing a [Frequency Map]() - const freq = {}; - for (let n of nums) freq[n] = (freq[n] || 0) + 1; - - // Step 2: Sort the numbers by ascending order - const sorted = Object.keys(freq).sort((a, b) => a - b); - - // Step 3: Init a count of numbers smaller than the active number - let count = 0; - - // Step 4: Init a map to track number of values smaller for each number - const smaller = {}; - - // Step 5: Iterate over the sorted list - for (let num of sorted) { - // Set count for active number - smaller[num] = count; - - // Update the count by frequency - count += freq[num]; - } - - // Step 6: Use original list and find number of smaller values than it - return nums.map((n) => smaller[n]); -}; -``` diff --git a/apps/docs/content/array/1413-minimum-value-to-get-positive-step-by-step-sum.mdx b/apps/docs/content/array/1413-minimum-value-to-get-positive-step-by-step-sum.mdx deleted file mode 100644 index fde0a7a..0000000 --- a/apps/docs/content/array/1413-minimum-value-to-get-positive-step-by-step-sum.mdx +++ /dev/null @@ -1,69 +0,0 @@ ---- -title: '1413. Minimum Value to Get Positive Step by Step Sum' -description: Given an array of integers nums, you start with an initial positive value startValue -sidebar: - label: 'Minimum Value to Get Positive Step by Step Sum' - badge: 'Easy' ---- - -Array - -### Example 1: -- Input: `nums = [-3,2,-3,4,2]` -- Output: `5` -- Explanation: If you choose `startValue = 4`, in the third iteration your step by step sum is less than `1`. - -``` -step by step sum -startValue = 4 | startValue = 5 | nums - (4 -3 ) = 1 | (5 -3 ) = 2 | -3 - (1 +2 ) = 3 | (2 +2 ) = 4 | 2 - (3 -3 ) = 0 | (4 -3 ) = 1 | -3 - (0 +4 ) = 4 | (1 +4 ) = 5 | 4 - (4 +2 ) = 6 | (5 +2 ) = 7 | 2 -``` - -### Example 2: -- Input: `nums = [1,2]` -- Output: `1` -- Explanation: Minimum start value should be positive. - -### Example 3: -- Input: `nums = [1,-2,-3]` -- Output: `5` - -### Constraints: - -- `1 <= nums.length <= 100` -- `-100 <= nums[i] <= 100` - -## Approach - -```mermaid -flowchart TD - S(["minStartValue(nums)"]) --> I["prefix = [nums[0]]"] - I --> L{"more i in 1..n-1?"} - L -- yes --> P["prefix.push(prefix[i-1] + nums[i])"] - P --> L - L -- no --> M["min = smallest running total"] - M --> R(["return max(1, 1 - min) — lift the dip to 1"]) -``` - -## Solution - -```js -/** - * @param {number[]} nums - * @return {number} - */ -var minStartValue = function(nums) { - let prefix = [nums[0]]; - // Make prefix sum start at 1 after initializing seed value - for (let i = 1; i < nums.length; i++){ - prefix.push(prefix[i - 1] + nums[i]); - } - - let min = Math.min(...prefix); - return Math.max(1, 1 - min); -}; -``` diff --git a/apps/docs/content/array/1426-counting-elements.mdx b/apps/docs/content/array/1426-counting-elements.mdx deleted file mode 100644 index 1377109..0000000 --- a/apps/docs/content/array/1426-counting-elements.mdx +++ /dev/null @@ -1,58 +0,0 @@ ---- -title: '1426. Counting Elements' -description: Given an integer array arr, count how many elements x there are, such that x + 1 is also in arr. If there are duplicates in arr, count them separately -sidebar: - label: 'Counting Elements' - badge: 'Easy' ---- - -Array - -### Example 1: -- Input: `arr = [1,2,3]` -- Output: `2` -- Explanation: `1` and `2` are counted cause `2` and `3` are in `arr`. - -### Example 2: -- Input: `arr = [1,1,3,3,5,5,7,7]` -- Output: `0` -- Explanation: No numbers are counted, cause there is no `2`, `4`, `6`, or `8` in `arr`. - -### Constraints: - -- `1 <= arr.length <= 1000` -- `0 <= arr[i] <= 1000` - -## Approach - -```mermaid -flowchart TD - S(["countElements(arr)"]) --> I["arrSet = Set(arr), count = 0"] - I --> L{"more n in arr?"} - L -- no --> E(["return count"]) - L -- yes --> H{"arrSet has n + 1?"} - H -- yes --> C["count++"] - C --> L - H -- no --> L -``` - -## Solution - -```js -/** - * @param {number[]} arr - * @return {number} - */ -var countElements = function(arr) { - let arrSet = new Set(arr); - let count = 0; - - for (let n of arr) { - let sum = n + 1; - if (arrSet.has(sum)) { - count++; - } - } - return count; -}; -``` diff --git a/apps/docs/content/array/1480-running-sum-of-1d-array.mdx b/apps/docs/content/array/1480-running-sum-of-1d-array.mdx deleted file mode 100644 index 3906a0d..0000000 --- a/apps/docs/content/array/1480-running-sum-of-1d-array.mdx +++ /dev/null @@ -1,55 +0,0 @@ ---- -title: '1480. Running Sum of 1d Array' -description: Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]) -sidebar: - label: 'Running Sum of 1d Array' - badge: 'Easy' ---- - -Array - -### Example 1: -- Input: `nums = [1,2,3,4]` -- Output: `[1,3,6,10]` -- Explanation: Running sum is obtained as follows: `[1, 1+2, 1+2+3, 1+2+3+4]`. - -### Example 2: -- Input: `nums = [1,1,1,1,1]` -- Output: `[1,2,3,4,5]` -- Explanation: Running sum is obtained as follows: `[1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]`. - -### Example 3: -- Input: `nums = [3,1,2,10,1]` -- Output: `[3,4,6,16,17]` - -### Constraints: - -- `1 <= nums.length <= 1000` -- `-10^6 <= nums[i] <= 10^6` - -## Approach - -```mermaid -flowchart TD - S(["runningSum(nums)"]) --> I["prefix = [nums[0]]"] - I --> L{"more i in 1..n-1?"} - L -- yes --> P["prefix.push(prefix[i-1] + nums[i])"] - P --> L - L -- no --> E(["return prefix"]) -``` - -## Solution - -```js -/** - * @param {number[]} nums - * @return {number[]} - */ -var runningSum = function(nums) { - let prefix = [nums[0]]; - for (let i = 1; i < nums.length; i++){ - prefix.push(prefix[i - 1] + nums[i]); - } - return prefix; -}; -``` diff --git a/apps/docs/content/array/1636-sort-array-by-increasing-frequency.mdx b/apps/docs/content/array/1636-sort-array-by-increasing-frequency.mdx deleted file mode 100644 index 20d1edc..0000000 --- a/apps/docs/content/array/1636-sort-array-by-increasing-frequency.mdx +++ /dev/null @@ -1,58 +0,0 @@ ---- -title: '1636. Sort Array by Increasing Frequency' -description: Given an array of integers nums, sort the array in increasing order based on the frequency of the values. If multiple values have the same frequency, sort them in decreasing order -sidebar: - label: 'Sort Array by Increasing Frequency' - badge: 'Easy' ---- - -Array - -### Example 1: -- Input: `nums = [1,1,2,2,2,3]` -- Output: `[3,1,1,2,2,2]` -- Explanation: `'3'` has a frequency of `1`, `'1'` has a frequency of `2`, and `'2'` has a frequency of `3`. - -### Example 2: -- Input: `nums = [2,3,1,3,2]` -- Output: `[1,3,3,2,2]` -- Explanation: `'2'` and `'3'` both have a frequency of `2`, so they are sorted in decreasing order. - -### Example 3: -- Input: `nums = [-1,1,-6,4,5,-6,1,4,1]` -- Output: `[5,-1,4,4,-6,-6,1,1,1]` - -### Constraints: - -- `1 <= nums.length <= 100` -- `-100 <= nums[i] <= 100` - -## Approach - -```mermaid -flowchart TD - S(["frequencySort(nums)"]) --> F["freq: count every value in nums"] - F --> C["sort nums with a two-key comparator"] - C --> K1["primary: freq[a] - freq[b] — rarer first"] - C --> K2["tie-break: b - a — larger value first"] - K1 --> R(["return nums"]) - K2 --> R -``` - -## Solution - -```js -/** - * @param {number[]} nums - * @return {number[]} - */ -var frequencySort = function (nums) { - const freq = {}; - for (let n of nums) freq[n] = (freq[n] || 0) + 1; - return nums.sort((a, b) => freq[a] - freq[b] || b - a); -}; -``` - -## Explanation - - diff --git a/apps/docs/content/array/268-missing-number.mdx b/apps/docs/content/array/268-missing-number.mdx deleted file mode 100644 index 7309442..0000000 --- a/apps/docs/content/array/268-missing-number.mdx +++ /dev/null @@ -1,71 +0,0 @@ ---- -title: '268. Missing Number' -description: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array -sidebar: - label: 'Missing Number' - badge: 'Easy' ---- - -Array - -::::warning -Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity? -:::: - -### Example 1: -- Input: `nums = [3,0,1]` -- Output: `2` -- Explanation: `n = 3` since there are `3` numbers, so all numbers are in the range `[0,3]`. `2` is the missing number in the range since it does not appear in `nums`. - -### Example 2: -- Input: `nums = [0,1]` -- Output: `2` -- Explanation: `n = 2` since there are `2` numbers, so all numbers are in the range `[0,2]`. `2` is the missing number in the range since it does not appear in `nums`. - -### Example 3: -- Input: `nums = [9,6,4,2,3,5,7,0,1]` -- Output: `8` -- Explanation: `n = 9` since there are `9` numbers, so all numbers are in the range `[0,9]`. `8` is the missing number in the range since it does not appear in `nums`. - -### Constraints: - -- `n == nums.length` -- `1 <= n <= 10^4` -- `0 <= nums[i] <= n` -- All the numbers of `nums` are unique. - -## Approach - -```mermaid -flowchart TD - S(["missingNumber(nums)"]) --> I["numSet = Set(nums), expectedCount = n + 1"] - I --> L{"more i in 0..expectedCount-1?"} - L -- yes --> Q{"numSet has i?"} - Q -- no --> R(["return i"]) - Q -- yes --> L - L -- no --> E(["return -1"]) -``` - -## Solution - -```js -/** - * @param {number[]} nums - * @return {number} - */ -var missingNumber = function(nums) { - const numSet = new Set(nums); - const expectedCount = nums.length + 1; - - for (let i = 0; i < expectedCount; i++){ - if(!numSet.has(i)){ - return i; - } - } - return -1; -}; -``` - -## Explanation - - diff --git a/apps/docs/content/array/271-encode-and-decode-strings.mdx b/apps/docs/content/array/271-encode-and-decode-strings.mdx deleted file mode 100644 index 86259c2..0000000 --- a/apps/docs/content/array/271-encode-and-decode-strings.mdx +++ /dev/null @@ -1,73 +0,0 @@ ---- -title: '271. Encode and Decode Strings' -description: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings -sidebar: - label: 'Encode and Decode Strings' - badge: 'Medium' ---- - -Array - -::::warning -Could you write a generalized algorithm to work on any possible set of characters? -:::: - -### Example 1: -- Input: `dummy_input = ["Hello","World"]` -- Output: `["Hello","World"]` -- Explanation: Machine `1`: Codec encoder = new Codec(); String msg = encoder.encode(strs); Machine `1` ---msg---> Machine `2` Machine `2`: Codec decoder = new Codec(); `String[] strs` = decoder.decode(msg); - -### Example 2: -- Input: `dummy_input = [""]` -- Output: `[""]` - -### Constraints: - -- `1 <= strs.length <= 200` -- `0 <= strs[i].length <= 200` -- `strs[i]` contains any possible characters out of 256 valid ASCII characters. - -## Approach - -::::warning -The diagram traces the code below literally. That code does not solve the problem: both `encode` and `decode` have only a docstring for a body, so each returns `None` and `codec.decode(codec.encode(strs))` yields `None` instead of `strs`. -:::: - -```mermaid -flowchart TD - subgraph state["state — Codec holds no fields"] - X["Codec() — nothing is stored between encode and decode"] - end - subgraph encode["encode(strs)"] - E1["docstring only — no statement runs"] --> E2(["return None"]) - end - subgraph decode["decode(s)"] - D1["docstring only — no statement runs"] --> D2(["return None"]) - end - state --> encode - state --> decode -``` - -## Solution - -```py -class Codec: - def encode(self, strs: List[str]) -> str: - """Encodes a list of strings to a single string. - """ - - - def decode(self, s: str) -> List[str]: - """Decodes a single string to a list of strings. - """ - - - -# Your Codec object will be instantiated and called as such: -# codec = Codec() -# codec.decode(codec.encode(strs)) -``` - -## Explanation - - diff --git a/apps/docs/content/array/meta.ts b/apps/docs/content/array/meta.ts deleted file mode 100644 index 5546306..0000000 --- a/apps/docs/content/array/meta.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { defineMeta } from "blume"; - -export default defineMeta({ title: "Array", order: 0, icon: "brackets" }); diff --git a/apps/docs/content/hash-table/217-contains-duplicate.mdx b/apps/docs/content/hash-table/217-contains-duplicate.mdx index 15d9e0b..5a19588 100644 --- a/apps/docs/content/hash-table/217-contains-duplicate.mdx +++ b/apps/docs/content/hash-table/217-contains-duplicate.mdx @@ -42,32 +42,12 @@ flowchart TD ## Solution - - -```py Python +```py class Solution: def containsDuplicate(self, nums: List[int]) -> bool: + return len(set(nums)) != len(nums) ``` -```js JavaScript -/** - * @param {number[]} nums - * @return {boolean} - */ -var containsDuplicate = function(nums) { - const freq = {}; - for (let n of nums) { - freq[n] = (freq[n] || 0) + 1; - if (freq[n] >= 2) { - return true; - } - } - return false; -}; -``` - - - ## Explanation diff --git a/work/1/Easy/Array/1365.how-many-numbers-are-smaller-than-the-current-number.js b/work/1/Easy/Array/1365.how-many-numbers-are-smaller-than-the-current-number.js deleted file mode 100644 index 3ed3996..0000000 --- a/work/1/Easy/Array/1365.how-many-numbers-are-smaller-than-the-current-number.js +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 1365. How Many Numbers Are Smaller Than the Current Number - * Difficulty: Easy - * https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/ - * - * ────────────────────────────────────────────────── - * - * Given the array nums, for each nums[i] find out how many numbers in - * the array are smaller than it. That is, for each nums[i] you have to - * count the number of valid j's such that j != i and nums[j] < nums[i]. - * - * Return the answer in an array. - * - * - * - * Example 1: - * - * Input: nums = [8,1,2,2,3] - * Output: [4,0,1,1,3] - * Explanation: - * For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and - * 3). - * For nums[1]=1 does not exist any smaller number than it. - * For nums[2]=2 there exist one smaller number than it (1). - * For nums[3]=2 there exist one smaller number than it (1). - * For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2). - * - * Example 2: - * - * Input: nums = [6,5,4,8] - * Output: [2,1,0,3] - * - * Example 3: - * - * Input: nums = [7,7,7,7] - * Output: [0,0,0,0] - * - * - * - * Constraints: - * - * • 2 <= nums.length <= 500 - * - * • 0 <= nums[i] <= 100 - */ - -/** - * @param {number[]} nums - * @return {number[]} - */ - -var smallerNumbersThanCurrent = function (nums) { - // Step 1: Begin by initializing a [Frequency Map]() - const freq = {}; - for (let n of nums) freq[n] = (freq[n] || 0) + 1; - - // Step 2: Sort the numbers by ascending order - const sorted = Object.keys(freq).sort((a, b) => a - b); - - // Step 3: Init a count of numbers smaller than the active number - let count = 0; - - // Step 4: Init a map to track number of values smaller for each number - const smaller = {}; - - // Step 5: Iterate over the sorted list - for (let num of sorted) { - // Set count for active number - smaller[num] = count; - - // Update the count by frequency - count += freq[num]; - } - - // Step 6: Use original list and find number of smaller values than it - return nums.map((n) => smaller[n]); -}; diff --git a/work/1/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js b/work/1/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js deleted file mode 100644 index 76c504a..0000000 --- a/work/1/Easy/Array/1413.minimum-value-to-get-positive-step-by-step-sum.js +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 1413. Minimum Value to Get Positive Step by Step Sum - * Difficulty: Easy - * https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/ - * - * ────────────────────────────────────────────────── - * - * Given an array of integers nums, you start with an initial positive - * value startValue. - * - * In each iteration, you calculate the step by step sum of startValue - * plus elements in nums (from left to right). - * - * Return the minimum positive value of startValue such that the step by - * step sum is never less than 1. - * - * - * - * Example 1: - * - * Input: nums = [-3,2,-3,4,2] - * Output: 5 - * Explanation: If you choose startValue = 4, in the third iteration - * your step by step sum is less than 1. - * step by step sum - * startValue = 4 | startValue = 5 | nums - * (4 -3 ) = 1 | (5 -3 ) = 2 | -3 - * (1 +2 ) = 3 | (2 +2 ) = 4 | 2 - * (3 -3 ) = 0 | (4 -3 ) = 1 | -3 - * (0 +4 ) = 4 | (1 +4 ) = 5 | 4 - * (4 +2 ) = 6 | (5 +2 ) = 7 | 2 - * - * Example 2: - * - * Input: nums = [1,2] - * Output: 1 - * Explanation: Minimum start value should be positive. - * - * Example 3: - * - * Input: nums = [1,-2,-3] - * Output: 5 - * - * - * - * Constraints: - * - * • 1 <= nums.length <= 100 - * - * • -100 <= nums[i] <= 100 -*/ - -/** - * @param {number[]} nums - * @return {number} - */ -var minStartValue = function(nums) { - let prefix = [nums[0]]; - // Make prefix sum start at 1 after initializing seed value - for (let i = 1; i < nums.length; i++){ - prefix.push(prefix[i - 1] + nums[i]); - } - - let min = Math.min(...prefix); - return Math.max(1, 1 - min); -}; diff --git a/work/1/Easy/Array/1426.counting-elements.js b/work/1/Easy/Array/1426.counting-elements.js deleted file mode 100644 index ff8259b..0000000 --- a/work/1/Easy/Array/1426.counting-elements.js +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 1426. Counting Elements - * Difficulty: Easy - * https://leetcode.com/problems/counting-elements/ - * - * ────────────────────────────────────────────────── - * - * Given an integer array arr, count how many elements x there are, such - * that x + 1 is also in arr. If there are duplicates in arr, count them - * separately. - * - * - * - * Example 1: - * - * Input: arr = [1,2,3] - * Output: 2 - * Explanation: 1 and 2 are counted cause 2 and 3 are in arr. - * - * Example 2: - * - * Input: arr = [1,1,3,3,5,5,7,7] - * Output: 0 - * Explanation: No numbers are counted, cause there is no 2, 4, 6, or 8 - * in arr. - * - * - * - * Constraints: - * - * • 1 <= arr.length <= 1000 - * - * • 0 <= arr[i] <= 1000 -*/ - -/** - * @param {number[]} arr - * @return {number} - */ -var countElements = function(arr) { - let arrSet = new Set(arr); - let count = 0; - - for (let n of arr) { - let sum = n + 1; - if (arrSet.has(sum)) { - count++; - } - } - return count; -}; diff --git a/work/1/Easy/Array/1480.running-sum-of-1d-array.js b/work/1/Easy/Array/1480.running-sum-of-1d-array.js deleted file mode 100644 index 18f80c1..0000000 --- a/work/1/Easy/Array/1480.running-sum-of-1d-array.js +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 1480. Running Sum of 1d Array - * Difficulty: Easy - * https://leetcode.com/problems/running-sum-of-1d-array/ - * - * ────────────────────────────────────────────────── - * - * Given an array nums. We define a running sum of an array as - * runningSum[i] = sum(nums[0]…nums[i]). - * - * Return the running sum of nums. - * - * - * - * Example 1: - * - * Input: nums = [1,2,3,4] - * Output: [1,3,6,10] - * Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, - * 1+2+3+4]. - * - * Example 2: - * - * Input: nums = [1,1,1,1,1] - * Output: [1,2,3,4,5] - * Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, - * 1+1+1+1, 1+1+1+1+1]. - * - * Example 3: - * - * Input: nums = [3,1,2,10,1] - * Output: [3,4,6,16,17] - * - * - * - * Constraints: - * - * • 1 <= nums.length <= 1000 - * - * • -10^6 <= nums[i] <= 10^6 -*/ - -/** - * @param {number[]} nums - * @return {number[]} - */ -var runningSum = function(nums) { - let prefix = [nums[0]]; - for (let i = 1; i < nums.length; i++){ - prefix.push(prefix[i - 1] + nums[i]); - } - return prefix; -}; diff --git a/work/1/Easy/Array/1636.sort-array-by-increasing-frequency.js b/work/1/Easy/Array/1636.sort-array-by-increasing-frequency.js deleted file mode 100644 index 2ace9f9..0000000 --- a/work/1/Easy/Array/1636.sort-array-by-increasing-frequency.js +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 1636. Sort Array by Increasing Frequency - * Difficulty: Easy - * https://leetcode.com/problems/sort-array-by-increasing-frequency/ - * - * ────────────────────────────────────────────────── - * - * Given an array of integers nums, sort the array in increasing order - * based on the frequency of the values. If multiple values have the same - * frequency, sort them in decreasing order. - * - * Return the sorted array. - * - * - * - * Example 1: - * - * Input: nums = [1,1,2,2,2,3] - * Output: [3,1,1,2,2,2] - * Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and - * '2' has a frequency of 3. - * - * Example 2: - * - * Input: nums = [2,3,1,3,2] - * Output: [1,3,3,2,2] - * Explanation: '2' and '3' both have a frequency of 2, so they are - * sorted in decreasing order. - * - * Example 3: - * - * Input: nums = [-1,1,-6,4,5,-6,1,4,1] - * Output: [5,-1,4,4,-6,-6,1,1,1] - * - * - * - * Constraints: - * - * • 1 <= nums.length <= 100 - * - * • -100 <= nums[i] <= 100 - */ - -/** - * @param {number[]} nums - * @return {number[]} - */ -var frequencySort = function (nums) { - const freq = {}; - for (let n of nums) freq[n] = (freq[n] || 0) + 1; - return nums.sort((a, b) => freq[a] - freq[b] || b - a); -}; diff --git a/work/1/Easy/Array/217.contains-duplicate.js b/work/1/Easy/Array/217.contains-duplicate.js deleted file mode 100644 index a9c6c9b..0000000 --- a/work/1/Easy/Array/217.contains-duplicate.js +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 217. Contains Duplicate - * Difficulty: Easy - * https://leetcode.com/problems/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. - * - * - * - * Example 1: - * - * Input: nums = [1,2,3,1] - * - * Output: true - * - * Explanation: - * - * The element 1 occurs at the indices 0 and 3. - * - * Example 2: - * - * Input: nums = [1,2,3,4] - * - * Output: false - * - * Explanation: - * - * All elements are distinct. - * - * Example 3: - * - * Input: nums = [1,1,1,3,3,4,3,2,4,2] - * - * Output: true - * - * - * - * Constraints: - * - * • 1 <= nums.length <= 10^5 - * - * • -10^9 <= nums[i] <= 10^9 -*/ - -/** - * @param {number[]} nums - * @return {boolean} - */ -var containsDuplicate = function(nums) { - const freq = {}; - for (let n of nums) { - freq[n] = (freq[n] || 0) + 1; - if (freq[n] >= 2) { - return true; - } - } - return false; -}; diff --git a/work/1/Easy/Array/217.contains-duplicate.py b/work/1/Easy/Array/217.contains-duplicate.py index f9abb7f..9b5a25c 100644 --- a/work/1/Easy/Array/217.contains-duplicate.py +++ b/work/1/Easy/Array/217.contains-duplicate.py @@ -9,7 +9,7 @@ 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. - + Example 1: @@ -37,15 +37,16 @@ Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true - + Constraints: - • 1 <= nums.length <= 10^5 + • 1 <= nums.length <= 10^5 - • -10^9 <= nums[i] <= 10^9 + • -10^9 <= nums[i] <= 10^9 """ + class Solution: def containsDuplicate(self, nums: List[int]) -> bool: - + return len(set(nums)) != len(nums) diff --git a/work/1/Easy/Array/268.missing-number.js b/work/1/Easy/Array/268.missing-number.js deleted file mode 100644 index ef657c2..0000000 --- a/work/1/Easy/Array/268.missing-number.js +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 268. Missing Number - * Difficulty: Easy - * https://leetcode.com/problems/missing-number/ - * - * ────────────────────────────────────────────────── - * - * Given an array nums containing n distinct numbers in the range [0, - * n], return the only number in the range that is missing from the - * array. - * - * - * - * Example 1: - * - * Input: nums = [3,0,1] - * - * Output: 2 - * - * Explanation: - * - * n = 3 since there are 3 numbers, so all numbers are in the range - * [0,3]. 2 is the missing number in the range since it does not appear - * in nums. - * - * Example 2: - * - * Input: nums = [0,1] - * - * Output: 2 - * - * Explanation: - * - * n = 2 since there are 2 numbers, so all numbers are in the range - * [0,2]. 2 is the missing number in the range since it does not appear - * in nums. - * - * Example 3: - * - * Input: nums = [9,6,4,2,3,5,7,0,1] - * - * Output: 8 - * - * Explanation: - * - * n = 9 since there are 9 numbers, so all numbers are in the range - * [0,9]. 8 is the missing number in the range since it does not appear - * in nums. - * - * - * - * - * - * - * - * - * - * - * - * Constraints: - * - * • n == nums.length - * - * • 1 <= n <= 10^4 - * - * • 0 <= nums[i] <= n - * - * • All the numbers of nums are unique. - * - * - * - * Follow up: Could you implement a solution using only O(1) extra space - * complexity and O(n) runtime complexity? -*/ - -/** - * @param {number[]} nums - * @return {number} - */ -var missingNumber = function(nums) { - const numSet = new Set(nums); - const expectedCount = nums.length + 1; - - for (let i = 0; i < expectedCount; i++){ - if(!numSet.has(i)){ - return i; - } - } - return -1; -}; diff --git a/work/1/Medium/Array/271.encode-and-decode-strings.py b/work/1/Medium/Array/271.encode-and-decode-strings.py deleted file mode 100644 index cd82096..0000000 --- a/work/1/Medium/Array/271.encode-and-decode-strings.py +++ /dev/null @@ -1,92 +0,0 @@ -""" -271. Encode and Decode Strings -Difficulty: Medium -https://leetcode.com/problems/encode-and-decode-strings/ - -────────────────────────────────────────────────── - -Design an algorithm to encode a list of strings to a string. The -encoded string is then sent over the network and is decoded back to -the original list of strings. - -Machine 1 (sender) has the function: - -string encode(vector strs) { - // ... your code - return encoded_string; -} -Machine 2 (receiver) has the function: - -vector decode(string s) { - //... your code - return strs; -} - -So Machine 1 does: - -string encoded_string = encode(strs); - -and Machine 2 does: - -vector strs2 = decode(encoded_string); - -strs2 in Machine 2 should be the same as strs in Machine 1. - -Implement the encode and decode methods. - -You are not allowed to solve the problem using any serialize methods -(such as eval). - - - -Example 1: - -Input: dummy_input = ["Hello","World"] -Output: ["Hello","World"] -Explanation: -Machine 1: -Codec encoder = new Codec(); -String msg = encoder.encode(strs); -Machine 1 ---msg---> Machine 2 - -Machine 2: -Codec decoder = new Codec(); -String[] strs = decoder.decode(msg); - -Example 2: - -Input: dummy_input = [""] -Output: [""] - - - -Constraints: - - • 1 <= strs.length <= 200 - - • 0 <= strs[i].length <= 200 - -• strs[i] contains any possible characters out of 256 valid ASCII -characters. - - - -Follow up: Could you write a generalized algorithm to work on any -possible set of characters? -""" - -class Codec: - def encode(self, strs: List[str]) -> str: - """Encodes a list of strings to a single string. - """ - - - def decode(self, s: str) -> List[str]: - """Decodes a single string to a list of strings. - """ - - - -# Your Codec object will be instantiated and called as such: -# codec = Codec() -# codec.decode(codec.encode(strs))