-
Notifications
You must be signed in to change notification settings - Fork 1
Add solutions and explanations for problems 3583, 3772, 3771, 3770, 3766, 3768, 3767, 3765, 3761, 3760 #112
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,6 +86,68 @@ def __init__(self, val=0, next=None): | |||||||||||||||||||||||||||
| self.next = next | ||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## 1431. Kids With the Greatest Number of Candies [Easy] | ||||||||||||||||||||||||||||
| https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ### Explanation | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## 1431. Kids With the Greatest Number of Candies [Easy] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| https://leetcode.com/problems/kids-with-the-greatest-number-of-candies | ||||||||||||||||||||||||||||
|
Comment on lines
+89
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove duplicate heading structure. The problem section has a duplicated heading: ## 1431. Kids With the Greatest Number of Candies [Easy]
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
### Explanation
-## 1431. Kids With the Greatest Number of Candies [Easy]
-
-https://leetcode.com/problems/kids-with-the-greatest-number-of-candies
-
## Description📝 Committable suggestion
Suggested change
🧰 Tools🪛 LanguageTool[style] ~89-~89: The word ‘Greatest’ tends to be overused in this context. Consider an alternative. (A_GREAT_NUMBER) [style] ~94-~94: The word ‘Greatest’ tends to be overused in this context. Consider an alternative. (A_GREAT_NUMBER) 🪛 markdownlint-cli2 (0.18.1)90-90: Bare URL used (MD034, no-bare-urls) 94-94: Multiple headings with the same content (MD024, no-duplicate-heading) 96-96: Bare URL used (MD034, no-bare-urls) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## Description | ||||||||||||||||||||||||||||
| There are `n` kids with candies. You are given an integer array `candies`, where each `candies[i]` represents the number of candies the `i`th kid has, and an integer `extraCandies`, denoting the number of extra candies that you have. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Return a boolean array `result` of length `n`, where `result[i]` is `true` if, after giving the `i`th kid all the `extraCandies`, they will have the greatest number of candies among all the kids, or false otherwise. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Note that multiple kids can have the greatest number of candies. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| **Examples** | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ```text | ||||||||||||||||||||||||||||
| Input: candies = [2,3,5,1,3], extraCandies = 3 | ||||||||||||||||||||||||||||
| Output: [true,true,true,false,true] | ||||||||||||||||||||||||||||
| Explanation: If you give all extraCandies to: | ||||||||||||||||||||||||||||
| - Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids. | ||||||||||||||||||||||||||||
| - Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids. | ||||||||||||||||||||||||||||
| - Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids. | ||||||||||||||||||||||||||||
| - Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids. | ||||||||||||||||||||||||||||
| - Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Input: candies = [4,2,1,1,2], extraCandies = 1 | ||||||||||||||||||||||||||||
| Output: [true,false,false,false,false] | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Input: candies = [12,1,12], extraCandies = 10 | ||||||||||||||||||||||||||||
| Output: [true,false,true] | ||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| **Constraints** | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ```text | ||||||||||||||||||||||||||||
| - n == candies.length | ||||||||||||||||||||||||||||
| - 2 <= n <= 100 | ||||||||||||||||||||||||||||
| - 1 <= candies[i] <= 100 | ||||||||||||||||||||||||||||
| - 1 <= extraCandies <= 50 | ||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## Hint | ||||||||||||||||||||||||||||
| For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## Explanation | ||||||||||||||||||||||||||||
|
Comment on lines
+133
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove duplicate These headings duplicate earlier structure in the same problem section. The Hint content at line 134 should use -## Hint
+### Hint
For each kid, check if their candies plus `extraCandies` is at least as much as the current maximum.
-## Explanation
First, you want to know the highest number of candies any kid currently has...🧰 Tools🪛 markdownlint-cli2 (0.18.1)133-133: Multiple headings with the same content (MD024, no-duplicate-heading) 136-136: Multiple headings with the same content (MD024, no-duplicate-heading) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||
| First, you want to know the highest number of candies any kid currently has. This is important because you need a reference point to see if giving extra candies to a kid will make them "the greatest." | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| For each kid, you add the `extraCandies` to their current amount. You do this because you want to see if, after the bonus, they can reach or beat the current maximum. If they do, you mark them as `True` in our answer list; otherwise, False. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| You only need to find the maximum once, and then just compare each kid's total to it. Don't need to recalculate the maximum for every kid. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ### Solution | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ```python | ||||||||||||||||||||||||||||
| def kidsWithCandies(candies, extraCandies): | ||||||||||||||||||||||||||||
| max_candies = max(candies) # Find the current maximum | ||||||||||||||||||||||||||||
| return [(c + extraCandies) >= max_candies for c in candies] | ||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| ## 1798. Maximum Number of Consecutive Values You Can Make [Medium] | ||||||||||||||||||||||||||||
| https://leetcode.com/problems/maximum-number-of-consecutive-values-you-can-make/ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| ## Explanation | ||
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We need to count special triplets (i, j, k) where i < j < k, nums[i] == nums[j] * 2, and nums[k] == nums[j] * 2. In other words, for each middle element j, we need to count how many i < j have nums[i] == target and how many k > j have nums[k] == target, where target = nums[j] * 2. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `3 <= n <= 10^5`, `0 <= nums[i] <= 10^5` | ||
| - **Time Complexity:** O(n) - single pass through array with hash map operations | ||
| - **Space Complexity:** O(n) for hash maps storing frequencies | ||
| - **Edge Case:** If no triplets exist, return 0 | ||
|
|
||
| **1.2 High-level approach:** | ||
| We process each position j as the middle element. We maintain two frequency maps: one for elements before j (freq_before) and one for elements after j (freq_after). For each j, we count how many i < j have nums[i] == target and how many k > j have nums[k] == target, then multiply these counts. | ||
|
|
||
|  | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Check if the external image URL is accessible
curl -I -s -o /dev/null -w "%{http_code}" "https://assets.leetcode.com/static_assets/others/frequency-counting-triplets.png"Repository: romankurnovskii/leetcode-apps Length of output: 76 🏁 Script executed: # Search for project guidelines on asset hosting in documentation
git ls-files | grep -E "(README|CONTRIBUTING|GUIDELINES|docs)" | head -20Repository: romankurnovskii/leetcode-apps Length of output: 98 🏁 Script executed: # Look for patterns of image references in other markdown files
rg "!\[.*\]\(https?://" --type md | head -20Repository: romankurnovskii/leetcode-apps Length of output: 2286 🏁 Script executed: # Check if there are any local image assets in the repository
fd -e png -e jpg -e jpeg -e gif -e svg | head -20Repository: romankurnovskii/leetcode-apps Length of output: 55 Fix broken image URL. The image URL returns a 404 error. Other documentation assets in the repository use 🤖 Prompt for AI Agents |
||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** Check all triplets (i, j, k) where i < j < k, which is O(n³) | ||
| - **Optimized Strategy:** For each j, use hash maps to count valid i and k positions in O(1), achieving O(n) time | ||
| - **Emphasize the optimization:** Hash maps allow us to count frequencies before and after each position efficiently without nested loops | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Initialize freq_before (empty) and freq_after (contains all elements initially) | ||
| 2. Process each position j from left to right | ||
| 3. Remove nums[j] from freq_after (since j is no longer "after") | ||
| 4. Calculate target = nums[j] * 2 | ||
| 5. Count valid i positions (freq_before[target]) and valid k positions (freq_after[target]) | ||
| 6. Multiply counts and add to result (modulo 10^9 + 7) | ||
| 7. Add nums[j] to freq_before for future j positions | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `nums = [6, 3, 6]` | ||
| - Initialize: `freq_before = {}`, `freq_after = {6: 2, 3: 1}`, `res = 0`, `MOD = 10^9 + 7` | ||
|
|
||
| **2.2 Start Processing:** | ||
| We iterate through each position j from 0 to n-1. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
|
|
||
| | j | nums[j] | Remove from freq_after | target | freq_before[target] | freq_after[target] | Count | res | | ||
| |---|---------|------------------------|--------|---------------------|-------------------|-------|-----| | ||
| | 0 | 6 | freq_after = {6: 1, 3: 1} | 12 | 0 | 0 | 0 * 0 = 0 | 0 | | ||
| | 0 | - | Add to freq_before | - | freq_before = {6: 1} | - | - | 0 | | ||
| | 1 | 3 | freq_after = {6: 1} | 6 | 1 (from freq_before) | 1 (from freq_after) | 1 * 1 = 1 | 1 | | ||
| | 1 | - | Add to freq_before | - | freq_before = {6: 1, 3: 1} | - | - | 1 | | ||
| | 2 | 6 | freq_after = {} | 12 | 0 | 0 | 0 * 0 = 0 | 1 | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| For each j, we update frequency maps and count triplets where j is the middle element. | ||
|
|
||
| **2.5 Return Result:** | ||
| After processing all positions, `res = 1`, which is the number of special triplets. The triplet is (0, 1, 2) where nums[0] = 6 = 3 * 2 and nums[2] = 6 = 3 * 2. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,55 +2,50 @@ | |
|
|
||
| ### Strategy (The "Why") | ||
|
|
||
| **Restate the problem:** We need to convert string `source` to `target` with minimum cost using substring conversions. Operations must be disjoint or identical (can't overlap partially). | ||
| **Restate the problem:** We need to find the maximum number of substrings we can split a string into such that each substring starts with a distinct character. No two substrings can start with the same character. | ||
|
|
||
| **1.1 Constraints & Complexity:** | ||
| - Input size: `1 <= source.length == target.length <= 1000`, `1 <= original.length <= 100` | ||
| - **Time Complexity:** O(m³ + n² * m) where m is number of unique strings and n is string length, due to Floyd-Warshall and DP | ||
| - **Space Complexity:** O(m² + n) for distance matrix and DP array | ||
| - **Edge Case:** If source == target, return 0 | ||
| - Input size: `1 <= s.length <= 10^5` | ||
| - **Time Complexity:** O(n) - single pass to count distinct characters | ||
| - **Space Complexity:** O(1) - using a set of at most 26 characters (lowercase English letters) | ||
| - **Edge Case:** If all characters are the same (e.g., "aaaa"), we can only have 1 substring | ||
|
|
||
| **1.2 High-level approach:** | ||
| We model substring conversions as a graph, use Floyd-Warshall to find shortest paths, then use dynamic programming to find the minimum cost to convert prefixes of the source string. | ||
| The key insight is that each substring must start with a distinct character. Since we can only use each starting character once, the maximum number of substrings equals the number of distinct characters in the string. | ||
|
|
||
|  | ||
|  | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Check if the external image URL is accessible
curl -I -s -o /dev/null -w "%{http_code}" "https://assets.leetcode.com/static_assets/others/distinct-characters.png"Repository: romankurnovskii/leetcode-apps Length of output: 76 🏁 Script executed: # Search for project guidelines about external assets or documentation standards
find . -type f \( -name "README*" -o -name "CONTRIBUTING*" -o -name "GUIDELINES*" -o -name "*.md" \) | head -20 | xargs grep -l "asset\|image\|external\|documentation" 2>/dev/null | head -10Repository: romankurnovskii/leetcode-apps Length of output: 299 🏁 Script executed: # Check the structure of the explanations directory to understand the asset handling pattern
ls -la explanations/ | head -20Repository: romankurnovskii/leetcode-apps Length of output: 1125 🏁 Script executed: # Look for any other image references in markdown files to see the pattern
rg "!\[.*\]\(https?://" explanations/ | head -20Repository: romankurnovskii/leetcode-apps Length of output: 2216 🏁 Script executed: # Check CONTRIBUTING.md for guidelines about assets
cat CONTRIBUTING.md | grep -A 5 -B 5 -i "asset\|image\|external"Repository: romankurnovskii/leetcode-apps Length of output: 629 🏁 Script executed: # Check README.md for asset guidelines
cat README.md | grep -A 5 -B 5 -i "asset\|image\|external"Repository: romankurnovskii/leetcode-apps Length of output: 809 🏁 Script executed: # Let's examine the file in question to understand context
cat -n explanations/3760/en.md | head -25Repository: romankurnovskii/leetcode-apps Length of output: 1855 🏁 Script executed: # Test a few other LeetCode image URLs from the repo to see if they're accessible
echo "Testing other LeetCode CDN images..."
curl -I -s -o /dev/null -w "distinct-characters.png: %{http_code}\n" "https://assets.leetcode.com/static_assets/others/distinct-characters.png"
curl -I -s -o /dev/null -w "frequency-counting-triplets.png: %{http_code}\n" "https://assets.leetcode.com/static_assets/others/frequency-counting-triplets.png"
curl -I -s -o /dev/null -w "oranges.png: %{http_code}\n" "https://assets.leetcode.com/uploads/2019/02/16/oranges.png"
curl -I -s -o /dev/null -w "interleave.jpg: %{http_code}\n" "https://assets.leetcode.com/uploads/2020/09/02/interleave.jpg"Repository: romankurnovskii/leetcode-apps Length of output: 212 Fix broken image URL. The image at line 16 returns a 404 error. The LeetCode CDN path 🤖 Prompt for AI Agents |
||
|
|
||
| **1.3 Brute force vs. optimized strategy:** | ||
| - **Brute Force:** Try all possible sequences of substring conversions, which is exponential. | ||
| - **Optimized Strategy:** Precompute shortest paths between all string pairs using Floyd-Warshall, then use DP where dp[i] = minimum cost to convert first i characters. | ||
| - **Brute Force:** Try all possible ways to split the string, which is exponential | ||
| - **Optimized Strategy:** Simply count distinct characters - each distinct character can start exactly one substring, so the answer is the count of distinct characters | ||
| - **Emphasize the optimization:** The constraint that each substring must start with a distinct character directly limits the answer to the number of distinct characters | ||
|
|
||
| **1.4 Decomposition:** | ||
| 1. Assign unique IDs to all unique strings in original and changed arrays | ||
| 2. Build graph and run Floyd-Warshall to find shortest conversion costs | ||
| 3. Use DP: dp[i] = min cost to convert source[0:i] to target[0:i] | ||
| 4. For each position, try all possible substring conversions ending there | ||
| 5. Return dp[n] or -1 if impossible | ||
| 1. Count the number of distinct characters in the string | ||
| 2. Since each substring must start with a distinct character, and we can only use each character once as a start | ||
| 3. The maximum number of substrings equals the number of distinct characters | ||
| 4. Return the count | ||
|
|
||
| ### Steps (The "How") | ||
|
|
||
| **2.1 Initialization & Example Setup:** | ||
| Let's use the example: `source = "abcd"`, `target = "acbe"`, `original = ["a","b","c","c","e","d"]`, `changed = ["b","c","b","e","b","e"]`, `cost = [2,5,5,1,2,20]` | ||
| - Map strings to IDs: a→0, b→1, c→2, e→3, d→4 | ||
| - Build graph and run Floyd-Warshall | ||
| Let's use the example: `s = "abab"` | ||
| - Distinct characters: 'a' and 'b' (2 distinct characters) | ||
| - We can split into: "a" and "bab" (starts with 'a' and 'b') | ||
|
|
||
| **2.2 Start Processing:** | ||
| We initialize dp[0] = 0, then process each position. | ||
| **2.2 Start Checking:** | ||
| We count distinct characters in the string. | ||
|
|
||
| **2.3 Trace Walkthrough:** | ||
| Processing `source = "abcd"` to `target = "acbe"`: | ||
|
|
||
| | i | source[0:i] | target[0:i] | Options | dp[i] | | ||
| |---|-------------|-------------|---------|-------| | ||
| | 0 | "" | "" | Base case | 0 | | ||
| | 1 | "a" | "a" | No change (dp[0] + 0) | 0 | | ||
| | 2 | "ab" | "ac" | Try "b"→"c": dp[1] + cost(b→c) = 0 + 5 = 5 | 5 | | ||
| | 3 | "abc" | "acb" | Try "c"→"b": dp[2] + cost(c→b) = 5 + 5 = 10<br>Or "bc"→"cb": need to check if exists | 10 | | ||
| | 4 | "abcd" | "acbe" | Try "d"→"e": dp[3] + cost(d→e) = 10 + 20 = 30 | 30 | | ||
|
|
||
| Actually, we need to check all substring conversions. For position 2, we can convert "b" to "c" at cost 5. For position 3, we convert "c" to "b" at cost 5 (or find better path). For position 4, we convert "d" to "e" at cost 20. Total: 5 + 5 + 20 = 30, but the example says 28, so there must be a better path. | ||
| | String | Distinct Characters | Count | Maximum Substrings | | ||
| |--------|-------------------|-------|-------------------| | ||
| | "abab" | {'a', 'b'} | 2 | 2 | | ||
| | "abcd" | {'a', 'b', 'c', 'd'} | 4 | 4 | | ||
| | "aaaa" | {'a'} | 1 | 1 | | ||
|
|
||
| **2.4 Increment and Loop:** | ||
| For each position i, we try all possible substring conversions ending at i and take the minimum cost. | ||
| We iterate through the string once to collect all distinct characters in a set. | ||
|
|
||
| **2.5 Return Result:** | ||
| After processing, `dp[4] = 28` (using optimal conversions), which is the minimum cost to convert "abcd" to "acbe". | ||
| For "abab", we have 2 distinct characters, so the maximum number of substrings is 2. We can split as "a" + "bab" where "a" starts with 'a' and "bab" starts with 'b'. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the duplicate 1431 header block (keep the fuller section that follows).
You have two consecutive "1431" headings; the first is a stub. Drop the first to avoid duplication and resolve MD024.
Apply this diff:
📝 Committable suggestion
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
8926-8926: Bare URL used
(MD034, no-bare-urls)
8930-8930: Multiple headings with the same content
(MD024, no-duplicate-heading)
🤖 Prompt for AI Agents