questionFrontendId
int64 1
3.51k
| questionTitle
stringlengths 3
79
| TitleSlug
stringlengths 3
79
| content
stringlengths 431
25.4k
⌀ | difficulty
stringclasses 3
values | totalAccepted
stringlengths 2
6
| totalSubmission
stringlengths 2
6
| totalAcceptedRaw
int64 124
16.8M
| totalSubmissionRaw
int64 285
30.3M
| acRate
stringlengths 4
5
| similarQuestions
stringlengths 2
714
| mysqlSchemas
stringclasses 295
values | category
stringclasses 6
values | codeDefinition
stringlengths 122
24.9k
⌀ | sampleTestCase
stringlengths 1
4.33k
| metaData
stringlengths 37
3.13k
| envInfo
stringclasses 26
values | topicTags
stringlengths 2
153
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2,103 | Rings and Rods | rings-and-rods | <p>There are <code>n</code> rings and each ring is either red, green, or blue. The rings are distributed <strong>across ten rods</strong> labeled from <code>0</code> to <code>9</code>.</p>
<p>You are given a string <code>rings</code> of length <code>2n</code> that describes the <code>n</code> rings that are placed onto the rods. Every two characters in <code>rings</code> forms a <strong>color-position pair</strong> that is used to describe each ring where:</p>
<ul>
<li>The <strong>first</strong> character of the <code>i<sup>th</sup></code> pair denotes the <code>i<sup>th</sup></code> ring's <strong>color</strong> (<code>'R'</code>, <code>'G'</code>, <code>'B'</code>).</li>
<li>The <strong>second</strong> character of the <code>i<sup>th</sup></code> pair denotes the <strong>rod</strong> that the <code>i<sup>th</sup></code> ring is placed on (<code>'0'</code> to <code>'9'</code>).</li>
</ul>
<p>For example, <code>"R3G2B1"</code> describes <code>n == 3</code> rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.</p>
<p>Return <em>the number of rods that have <strong>all three colors</strong> of rings on them.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/23/ex1final.png" style="width: 258px; height: 130px;" />
<pre>
<strong>Input:</strong> rings = "B0B6G0R6R0R6G9"
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- The rod labeled 0 holds 3 rings with all colors: red, green, and blue.
- The rod labeled 6 holds 3 rings, but it only has red and blue.
- The rod labeled 9 holds only a green ring.
Thus, the number of rods with all three colors is 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/23/ex2final.png" style="width: 266px; height: 130px;" />
<pre>
<strong>Input:</strong> rings = "B0R0G0R9R0B0G0"
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- The rod labeled 0 holds 6 rings with all colors: red, green, and blue.
- The rod labeled 9 holds only a red ring.
Thus, the number of rods with all three colors is 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> rings = "G4"
<strong>Output:</strong> 0
<strong>Explanation:</strong>
Only one ring is given. Thus, no rods have all three colors.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>rings.length == 2 * n</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>rings[i]</code> where <code>i</code> is <strong>even</strong> is either <code>'R'</code>, <code>'G'</code>, or <code>'B'</code> (<strong>0-indexed</strong>).</li>
<li><code>rings[i]</code> where <code>i</code> is <strong>odd</strong> is a digit from <code>'0'</code> to <code>'9'</code> (<strong>0-indexed</strong>).</li>
</ul>
| Easy | 85.5K | 105.5K | 85,513 | 105,546 | 81.0% | ['check-if-all-characters-have-equal-number-of-occurrences'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countPoints(string rings) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countPoints(String rings) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countPoints(self, rings):\n \"\"\"\n :type rings: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countPoints(self, rings: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countPoints(char* rings) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountPoints(string rings) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} rings\n * @return {number}\n */\nvar countPoints = function(rings) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countPoints(rings: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $rings\n * @return Integer\n */\n function countPoints($rings) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countPoints(_ rings: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countPoints(rings: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countPoints(String rings) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countPoints(rings string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} rings\n# @return {Integer}\ndef count_points(rings)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countPoints(rings: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_points(rings: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-points rings)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_points(Rings :: unicode:unicode_binary()) -> integer().\ncount_points(Rings) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_points(rings :: String.t) :: integer\n def count_points(rings) do\n \n end\nend"}] | "B0B6G0R6R0R6G9" | {
"name": "countPoints",
"params": [
{
"name": "rings",
"type": "string"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Hash Table', 'String'] |
2,104 | Sum of Subarray Ranges | sum-of-subarray-ranges | <p>You are given an integer array <code>nums</code>. The <strong>range</strong> of a subarray of <code>nums</code> is the difference between the largest and smallest element in the subarray.</p>
<p>Return <em>the <strong>sum of all</strong> subarray ranges of </em><code>nums</code><em>.</em></p>
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[2], range = 2 - 2 = 0
[3], range = 3 - 3 = 0
[1,2], range = 2 - 1 = 1
[2,3], range = 3 - 2 = 1
[1,2,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 1 + 1 + 2 = 4.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 6 subarrays of nums are the following:
[1], range = largest - smallest = 1 - 1 = 0
[3], range = 3 - 3 = 0
[3], range = 3 - 3 = 0
[1,3], range = 3 - 1 = 2
[3,3], range = 3 - 3 = 0
[1,3,3], range = 3 - 1 = 2
So the sum of all ranges is 0 + 0 + 0 + 2 + 0 + 2 = 4.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,-2,-3,4,1]
<strong>Output:</strong> 59
<strong>Explanation:</strong> The sum of all subarray ranges of nums is 59.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
</ul>
<p> </p>
<p><strong>Follow-up:</strong> Could you find a solution with <code>O(n)</code> time complexity?</p>
| Medium | 143.2K | 238.2K | 143,182 | 238,187 | 60.1% | ['next-greater-element-i', 'sum-of-subarray-minimums', 'number-of-visible-people-in-a-queue', 'count-number-of-homogenous-substrings', 'sum-of-total-strength-of-wizards'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long subArrayRanges(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long subArrayRanges(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def subArrayRanges(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def subArrayRanges(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long subArrayRanges(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long SubArrayRanges(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar subArrayRanges = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function subArrayRanges(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function subArrayRanges($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func subArrayRanges(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun subArrayRanges(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int subArrayRanges(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func subArrayRanges(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef sub_array_ranges(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def subArrayRanges(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sub_array_ranges(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sub-array-ranges nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sub_array_ranges(Nums :: [integer()]) -> integer().\nsub_array_ranges(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sub_array_ranges(nums :: [integer]) :: integer\n def sub_array_ranges(nums) do\n \n end\nend"}] | [1,2,3] | {
"name": "subArrayRanges",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Stack', 'Monotonic Stack'] |
2,105 | Watering Plants II | watering-plants-ii | <p>Alice and Bob want to water <code>n</code> plants in their garden. The plants are arranged in a row and are labeled from <code>0</code> to <code>n - 1</code> from left to right where the <code>i<sup>th</sup></code> plant is located at <code>x = i</code>.</p>
<p>Each plant needs a specific amount of water. Alice and Bob have a watering can each, <strong>initially full</strong>. They water the plants in the following way:</p>
<ul>
<li>Alice waters the plants in order from <strong>left to right</strong>, starting from the <code>0<sup>th</sup></code> plant. Bob waters the plants in order from <strong>right to left</strong>, starting from the <code>(n - 1)<sup>th</sup></code> plant. They begin watering the plants <strong>simultaneously</strong>.</li>
<li>It takes the same amount of time to water each plant regardless of how much water it needs.</li>
<li>Alice/Bob <strong>must</strong> water the plant if they have enough in their can to <strong>fully</strong> water it. Otherwise, they <strong>first</strong> refill their can (instantaneously) then water the plant.</li>
<li>In case both Alice and Bob reach the same plant, the one with <strong>more</strong> water currently in his/her watering can should water this plant. If they have the same amount of water, then Alice should water this plant.</li>
</ul>
<p>Given a <strong>0-indexed</strong> integer array <code>plants</code> of <code>n</code> integers, where <code>plants[i]</code> is the amount of water the <code>i<sup>th</sup></code> plant needs, and two integers <code>capacityA</code> and <code>capacityB</code> representing the capacities of Alice's and Bob's watering cans respectively, return <em>the <strong>number of times</strong> they have to refill to water all the plants</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> plants = [2,2,3,3], capacityA = 5, capacityB = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- Initially, Alice and Bob have 5 units of water each in their watering cans.
- Alice waters plant 0, Bob waters plant 3.
- Alice and Bob now have 3 units and 2 units of water respectively.
- Alice has enough water for plant 1, so she waters it. Bob does not have enough water for plant 2, so he refills his can then waters it.
So, the total number of times they have to refill to water all the plants is 0 + 0 + 1 + 0 = 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> plants = [2,2,3,3], capacityA = 3, capacityB = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- Initially, Alice and Bob have 3 units and 4 units of water in their watering cans respectively.
- Alice waters plant 0, Bob waters plant 3.
- Alice and Bob now have 1 unit of water each, and need to water plants 1 and 2 respectively.
- Since neither of them have enough water for their current plants, they refill their cans and then water the plants.
So, the total number of times they have to refill to water all the plants is 0 + 1 + 1 + 0 = 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> plants = [5], capacityA = 10, capacityB = 8
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- There is only one plant.
- Alice's watering can has 10 units of water, whereas Bob's can has 8 units. Since Alice has more water in her can, she waters this plant.
So, the total number of times they have to refill is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == plants.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= plants[i] <= 10<sup>6</sup></code></li>
<li><code>max(plants[i]) <= capacityA, capacityB <= 10<sup>9</sup></code></li>
</ul>
| Medium | 23.8K | 49.6K | 23,834 | 49,551 | 48.1% | ['watering-plants'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumRefill(vector<int>& plants, int capacityA, int capacityB) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumRefill(int[] plants, int capacityA, int capacityB) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumRefill(self, plants, capacityA, capacityB):\n \"\"\"\n :type plants: List[int]\n :type capacityA: int\n :type capacityB: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumRefill(self, plants: List[int], capacityA: int, capacityB: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumRefill(int* plants, int plantsSize, int capacityA, int capacityB) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumRefill(int[] plants, int capacityA, int capacityB) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} plants\n * @param {number} capacityA\n * @param {number} capacityB\n * @return {number}\n */\nvar minimumRefill = function(plants, capacityA, capacityB) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumRefill(plants: number[], capacityA: number, capacityB: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $plants\n * @param Integer $capacityA\n * @param Integer $capacityB\n * @return Integer\n */\n function minimumRefill($plants, $capacityA, $capacityB) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumRefill(_ plants: [Int], _ capacityA: Int, _ capacityB: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumRefill(plants: IntArray, capacityA: Int, capacityB: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumRefill(List<int> plants, int capacityA, int capacityB) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumRefill(plants []int, capacityA int, capacityB int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} plants\n# @param {Integer} capacity_a\n# @param {Integer} capacity_b\n# @return {Integer}\ndef minimum_refill(plants, capacity_a, capacity_b)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumRefill(plants: Array[Int], capacityA: Int, capacityB: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_refill(plants: Vec<i32>, capacity_a: i32, capacity_b: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-refill plants capacityA capacityB)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_refill(Plants :: [integer()], CapacityA :: integer(), CapacityB :: integer()) -> integer().\nminimum_refill(Plants, CapacityA, CapacityB) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_refill(plants :: [integer], capacity_a :: integer, capacity_b :: integer) :: integer\n def minimum_refill(plants, capacity_a, capacity_b) do\n \n end\nend"}] | [2,2,3,3]
5
5 | {
"name": "minimumRefill",
"params": [
{
"name": "plants",
"type": "integer[]"
},
{
"type": "integer",
"name": "capacityA"
},
{
"type": "integer",
"name": "capacityB"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers', 'Simulation'] |
2,106 | Maximum Fruits Harvested After at Most K Steps | maximum-fruits-harvested-after-at-most-k-steps | <p>Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array <code>fruits</code> where <code>fruits[i] = [position<sub>i</sub>, amount<sub>i</sub>]</code> depicts <code>amount<sub>i</sub></code> fruits at the position <code>position<sub>i</sub></code>. <code>fruits</code> is already <strong>sorted</strong> by <code>position<sub>i</sub></code> in <strong>ascending order</strong>, and each <code>position<sub>i</sub></code> is <strong>unique</strong>.</p>
<p>You are also given an integer <code>startPos</code> and an integer <code>k</code>. Initially, you are at the position <code>startPos</code>. From any position, you can either walk to the <strong>left or right</strong>. It takes <strong>one step</strong> to move <strong>one unit</strong> on the x-axis, and you can walk <strong>at most</strong> <code>k</code> steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.</p>
<p>Return <em>the <strong>maximum total number</strong> of fruits you can harvest</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/1.png" style="width: 472px; height: 115px;" />
<pre>
<strong>Input:</strong> fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The optimal way is to:
- Move right to position 6 and harvest 3 fruits
- Move right to position 8 and harvest 6 fruits
You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/2.png" style="width: 512px; height: 129px;" />
<pre>
<strong>Input:</strong> fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
<strong>Output:</strong> 14
<strong>Explanation:</strong>
You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
The optimal way is to:
- Harvest the 7 fruits at the starting position 5
- Move left to position 4 and harvest 1 fruit
- Move right to position 6 and harvest 2 fruits
- Move right to position 7 and harvest 4 fruits
You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/21/3.png" style="width: 476px; height: 100px;" />
<pre>
<strong>Input:</strong> fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong>
You can move at most k = 2 steps and cannot reach any position with fruits.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= fruits.length <= 10<sup>5</sup></code></li>
<li><code>fruits[i].length == 2</code></li>
<li><code>0 <= startPos, position<sub>i</sub> <= 2 * 10<sup>5</sup></code></li>
<li><code>position<sub>i-1</sub> < position<sub>i</sub></code> for any <code>i > 0</code> (<strong>0-indexed</strong>)</li>
<li><code>1 <= amount<sub>i</sub> <= 10<sup>4</sup></code></li>
<li><code>0 <= k <= 2 * 10<sup>5</sup></code></li>
</ul>
| Hard | 12.5K | 34.6K | 12,512 | 34,635 | 36.1% | ['maximum-performance-of-a-team'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxTotalFruits(int[][] fruits, int startPos, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxTotalFruits(self, fruits, startPos, k):\n \"\"\"\n :type fruits: List[List[int]]\n :type startPos: int\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxTotalFruits(self, fruits: List[List[int]], startPos: int, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxTotalFruits(int** fruits, int fruitsSize, int* fruitsColSize, int startPos, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxTotalFruits(int[][] fruits, int startPos, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} fruits\n * @param {number} startPos\n * @param {number} k\n * @return {number}\n */\nvar maxTotalFruits = function(fruits, startPos, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxTotalFruits(fruits: number[][], startPos: number, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $fruits\n * @param Integer $startPos\n * @param Integer $k\n * @return Integer\n */\n function maxTotalFruits($fruits, $startPos, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxTotalFruits(_ fruits: [[Int]], _ startPos: Int, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxTotalFruits(fruits: Array<IntArray>, startPos: Int, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxTotalFruits(List<List<int>> fruits, int startPos, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxTotalFruits(fruits [][]int, startPos int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} fruits\n# @param {Integer} start_pos\n# @param {Integer} k\n# @return {Integer}\ndef max_total_fruits(fruits, start_pos, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxTotalFruits(fruits: Array[Array[Int]], startPos: Int, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_total_fruits(fruits: Vec<Vec<i32>>, start_pos: i32, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-total-fruits fruits startPos k)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_total_fruits(Fruits :: [[integer()]], StartPos :: integer(), K :: integer()) -> integer().\nmax_total_fruits(Fruits, StartPos, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_total_fruits(fruits :: [[integer]], start_pos :: integer, k :: integer) :: integer\n def max_total_fruits(fruits, start_pos, k) do\n \n end\nend"}] | [[2,8],[6,3],[8,6]]
5
4 | {
"name": "maxTotalFruits",
"params": [
{
"name": "fruits",
"type": "integer[][]"
},
{
"type": "integer",
"name": "startPos"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search', 'Sliding Window', 'Prefix Sum'] |
2,107 | Number of Unique Flavors After Sharing K Candies | number-of-unique-flavors-after-sharing-k-candies | null | Medium | 8.4K | 14.1K | 8,412 | 14,081 | 59.7% | ['remove-boxes', 'subarrays-with-k-different-integers'] | [] | Algorithms | null | [1,2,2,3,4,3]
3 | {
"name": "shareCandies",
"params": [
{
"name": "candies",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Sliding Window'] |
2,108 | Find First Palindromic String in the Array | find-first-palindromic-string-in-the-array | <p>Given an array of strings <code>words</code>, return <em>the first <strong>palindromic</strong> string in the array</em>. If there is no such string, return <em>an <strong>empty string</strong> </em><code>""</code>.</p>
<p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["abc","car","ada","racecar","cool"]
<strong>Output:</strong> "ada"
<strong>Explanation:</strong> The first string that is palindromic is "ada".
Note that "racecar" is also palindromic, but it is not the first.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["notapalindrome","racecar"]
<strong>Output:</strong> "racecar"
<strong>Explanation:</strong> The first and only string that is palindromic is "racecar".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> words = ["def","ghi"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There are no palindromic strings, so the empty string is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 100</code></li>
<li><code>1 <= words[i].length <= 100</code></li>
<li><code>words[i]</code> consists only of lowercase English letters.</li>
</ul>
| Easy | 331.4K | 395.2K | 331,420 | 395,230 | 83.9% | ['valid-palindrome'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string firstPalindrome(vector<string>& words) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String firstPalindrome(String[] words) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def firstPalindrome(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def firstPalindrome(self, words: List[str]) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* firstPalindrome(char** words, int wordsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string FirstPalindrome(string[] words) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @return {string}\n */\nvar firstPalindrome = function(words) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function firstPalindrome(words: string[]): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @return String\n */\n function firstPalindrome($words) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func firstPalindrome(_ words: [String]) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun firstPalindrome(words: Array<String>): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String firstPalindrome(List<String> words) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func firstPalindrome(words []string) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @return {String}\ndef first_palindrome(words)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def firstPalindrome(words: Array[String]): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn first_palindrome(words: Vec<String>) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (first-palindrome words)\n (-> (listof string?) string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec first_palindrome(Words :: [unicode:unicode_binary()]) -> unicode:unicode_binary().\nfirst_palindrome(Words) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec first_palindrome(words :: [String.t]) :: String.t\n def first_palindrome(words) do\n \n end\nend"}] | ["abc","car","ada","racecar","cool"] | {
"name": "firstPalindrome",
"params": [
{
"name": "words",
"type": "string[]"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers', 'String'] |
2,109 | Adding Spaces to a String | adding-spaces-to-a-string | <p>You are given a <strong>0-indexed</strong> string <code>s</code> and a <strong>0-indexed</strong> integer array <code>spaces</code> that describes the indices in the original string where spaces will be added. Each space should be inserted <strong>before</strong> the character at the given index.</p>
<ul>
<li>For example, given <code>s = "EnjoyYourCoffee"</code> and <code>spaces = [5, 9]</code>, we place spaces before <code>'Y'</code> and <code>'C'</code>, which are at indices <code>5</code> and <code>9</code> respectively. Thus, we obtain <code>"Enjoy <strong><u>Y</u></strong>our <u><strong>C</strong></u>offee"</code>.</li>
</ul>
<p>Return<strong> </strong><em>the modified string <strong>after</strong> the spaces have been added.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
<strong>Output:</strong> "Leetcode Helps Me Learn"
<strong>Explanation:</strong>
The indices 8, 13, and 15 correspond to the underlined characters in "Leetcode<u><strong>H</strong></u>elps<u><strong>M</strong></u>e<u><strong>L</strong></u>earn".
We then place spaces before those characters.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "icodeinpython", spaces = [1,5,7,9]
<strong>Output:</strong> "i code in py thon"
<strong>Explanation:</strong>
The indices 1, 5, 7, and 9 correspond to the underlined characters in "i<u><strong>c</strong></u>ode<u><strong>i</strong></u>n<u><strong>p</strong></u>y<u><strong>t</strong></u>hon".
We then place spaces before those characters.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "spacing", spaces = [0,1,2,3,4,5,6]
<strong>Output:</strong> " s p a c i n g"
<strong>Explanation:</strong>
We are also able to place spaces before the first character of the string.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
<li><code>s</code> consists only of lowercase and uppercase English letters.</li>
<li><code>1 <= spaces.length <= 3 * 10<sup>5</sup></code></li>
<li><code>0 <= spaces[i] <= s.length - 1</code></li>
<li>All the values of <code>spaces</code> are <strong>strictly increasing</strong>.</li>
</ul>
| Medium | 203.8K | 284.1K | 203,812 | 284,104 | 71.7% | [] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string addSpaces(string s, vector<int>& spaces) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String addSpaces(String s, int[] spaces) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def addSpaces(self, s, spaces):\n \"\"\"\n :type s: str\n :type spaces: List[int]\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def addSpaces(self, s: str, spaces: List[int]) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* addSpaces(char* s, int* spaces, int spacesSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string AddSpaces(string s, int[] spaces) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number[]} spaces\n * @return {string}\n */\nvar addSpaces = function(s, spaces) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function addSpaces(s: string, spaces: number[]): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer[] $spaces\n * @return String\n */\n function addSpaces($s, $spaces) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func addSpaces(_ s: String, _ spaces: [Int]) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun addSpaces(s: String, spaces: IntArray): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String addSpaces(String s, List<int> spaces) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func addSpaces(s string, spaces []int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer[]} spaces\n# @return {String}\ndef add_spaces(s, spaces)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def addSpaces(s: String, spaces: Array[Int]): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn add_spaces(s: String, spaces: Vec<i32>) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (add-spaces s spaces)\n (-> string? (listof exact-integer?) string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec add_spaces(S :: unicode:unicode_binary(), Spaces :: [integer()]) -> unicode:unicode_binary().\nadd_spaces(S, Spaces) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec add_spaces(s :: String.t, spaces :: [integer]) :: String.t\n def add_spaces(s, spaces) do\n \n end\nend"}] | "LeetcodeHelpsMeLearn"
[8,13,15] | {
"name": "addSpaces",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "integer[]",
"name": "spaces"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers', 'String', 'Simulation'] |
2,110 | Number of Smooth Descent Periods of a Stock | number-of-smooth-descent-periods-of-a-stock | <p>You are given an integer array <code>prices</code> representing the daily price history of a stock, where <code>prices[i]</code> is the stock price on the <code>i<sup>th</sup></code> day.</p>
<p>A <strong>smooth descent period</strong> of a stock consists of <strong>one or more contiguous</strong> days such that the price on each day is <strong>lower</strong> than the price on the <strong>preceding day</strong> by <strong>exactly</strong> <code>1</code>. The first day of the period is exempted from this rule.</p>
<p>Return <em>the number of <strong>smooth descent periods</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> prices = [3,2,1,4]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 7 smooth descent periods:
[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]
Note that a period with one day is a smooth descent period by the definition.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> prices = [8,6,7,7]
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are 4 smooth descent periods: [8], [6], [7], and [7]
Note that [8,6] is not a smooth descent period as 8 - 6 ≠ 1.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> prices = [1]
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is 1 smooth descent period: [1]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= prices.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prices[i] <= 10<sup>5</sup></code></li>
</ul>
| Medium | 41.9K | 71K | 41,892 | 70,950 | 59.0% | ['subarray-product-less-than-k', 'number-of-valid-subarrays', 'number-of-zero-filled-subarrays'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long getDescentPeriods(vector<int>& prices) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long getDescentPeriods(int[] prices) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def getDescentPeriods(self, prices):\n \"\"\"\n :type prices: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def getDescentPeriods(self, prices: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long getDescentPeriods(int* prices, int pricesSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long GetDescentPeriods(int[] prices) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} prices\n * @return {number}\n */\nvar getDescentPeriods = function(prices) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function getDescentPeriods(prices: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $prices\n * @return Integer\n */\n function getDescentPeriods($prices) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func getDescentPeriods(_ prices: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun getDescentPeriods(prices: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int getDescentPeriods(List<int> prices) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func getDescentPeriods(prices []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} prices\n# @return {Integer}\ndef get_descent_periods(prices)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def getDescentPeriods(prices: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn get_descent_periods(prices: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (get-descent-periods prices)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec get_descent_periods(Prices :: [integer()]) -> integer().\nget_descent_periods(Prices) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec get_descent_periods(prices :: [integer]) :: integer\n def get_descent_periods(prices) do\n \n end\nend"}] | [3,2,1,4] | {
"name": "getDescentPeriods",
"params": [
{
"name": "prices",
"type": "integer[]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'Dynamic Programming'] |
2,111 | Minimum Operations to Make the Array K-Increasing | minimum-operations-to-make-the-array-k-increasing | <p>You are given a <strong>0-indexed</strong> array <code>arr</code> consisting of <code>n</code> positive integers, and a positive integer <code>k</code>.</p>
<p>The array <code>arr</code> is called <strong>K-increasing</strong> if <code>arr[i-k] <= arr[i]</code> holds for every index <code>i</code>, where <code>k <= i <= n-1</code>.</p>
<ul>
<li>For example, <code>arr = [4, 1, 5, 2, 6, 2]</code> is K-increasing for <code>k = 2</code> because:
<ul>
<li><code>arr[0] <= arr[2] (4 <= 5)</code></li>
<li><code>arr[1] <= arr[3] (1 <= 2)</code></li>
<li><code>arr[2] <= arr[4] (5 <= 6)</code></li>
<li><code>arr[3] <= arr[5] (2 <= 2)</code></li>
</ul>
</li>
<li>However, the same <code>arr</code> is not K-increasing for <code>k = 1</code> (because <code>arr[0] > arr[1]</code>) or <code>k = 3</code> (because <code>arr[0] > arr[3]</code>).</li>
</ul>
<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>arr[i]</code> into <strong>any</strong> positive integer.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array K-increasing for the given </em><code>k</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [5,4,3,2,1], k = 1
<strong>Output:</strong> 4
<strong>Explanation:
</strong>For k = 1, the resultant array has to be non-decreasing.
Some of the K-increasing arrays that can be formed are [5,<u><strong>6</strong></u>,<u><strong>7</strong></u>,<u><strong>8</strong></u>,<u><strong>9</strong></u>], [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,1], [<u><strong>2</strong></u>,<u><strong>2</strong></u>,3,<u><strong>4</strong></u>,<u><strong>4</strong></u>]. All of them require 4 operations.
It is suboptimal to change the array to, for example, [<u><strong>6</strong></u>,<u><strong>7</strong></u>,<u><strong>8</strong></u>,<u><strong>9</strong></u>,<u><strong>10</strong></u>] because it would take 5 operations.
It can be shown that we cannot make the array K-increasing in less than 4 operations.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,1,5,2,6,2], k = 2
<strong>Output:</strong> 0
<strong>Explanation:</strong>
This is the same example as the one in the problem description.
Here, for every index i where 2 <= i <= 5, arr[i-2] <=<b> </b>arr[i].
Since the given array is already K-increasing, we do not need to perform any operations.</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> arr = [4,1,5,2,6,2], k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Indices 3 and 5 are the only ones not satisfying arr[i-3] <= arr[i] for 3 <= i <= 5.
One of the ways we can make the array K-increasing is by changing arr[3] to 4 and arr[5] to 5.
The array will now be [4,1,5,<u><strong>4</strong></u>,6,<u><strong>5</strong></u>].
Note that there can be other ways to make the array K-increasing, but none of them require less than 2 operations.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
<li><code>1 <= arr[i], k <= arr.length</code></li>
</ul>
| Hard | 14.2K | 36.6K | 14,230 | 36,627 | 38.9% | ['longest-increasing-subsequence', 'minimum-swaps-to-make-sequences-increasing'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int kIncreasing(vector<int>& arr, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int kIncreasing(int[] arr, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def kIncreasing(self, arr, k):\n \"\"\"\n :type arr: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def kIncreasing(self, arr: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int kIncreasing(int* arr, int arrSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int KIncreasing(int[] arr, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} arr\n * @param {number} k\n * @return {number}\n */\nvar kIncreasing = function(arr, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function kIncreasing(arr: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $arr\n * @param Integer $k\n * @return Integer\n */\n function kIncreasing($arr, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func kIncreasing(_ arr: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun kIncreasing(arr: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int kIncreasing(List<int> arr, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func kIncreasing(arr []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} arr\n# @param {Integer} k\n# @return {Integer}\ndef k_increasing(arr, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def kIncreasing(arr: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn k_increasing(arr: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (k-increasing arr k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec k_increasing(Arr :: [integer()], K :: integer()) -> integer().\nk_increasing(Arr, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec k_increasing(arr :: [integer], k :: integer) :: integer\n def k_increasing(arr, k) do\n \n end\nend"}] | [5,4,3,2,1]
1 | {
"name": "kIncreasing",
"params": [
{
"name": "arr",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search'] |
2,112 | The Airport With the Most Traffic | the-airport-with-the-most-traffic | null | Medium | 10.2K | 14.3K | 10,227 | 14,329 | 71.4% | [] | ['Create table If Not Exists Flights (departure_airport int, arrival_airport int, flights_count int)', 'Truncate table Flights', "insert into Flights (departure_airport, arrival_airport, flights_count) values ('1', '2', '4')", "insert into Flights (departure_airport, arrival_airport, flights_count) values ('2', '1', '5')", "insert into Flights (departure_airport, arrival_airport, flights_count) values ('2', '4', '5')"] | Database | null | {"headers":{"Flights":["departure_airport","arrival_airport","flights_count"]},"rows":{"Flights":[[1,2,4],[2,1,5],[2,4,5]]}} | {"mysql": ["Create table If Not Exists Flights (departure_airport int, arrival_airport int, flights_count int)"], "mssql": ["Create table Flights (departure_airport int, arrival_airport int, flights_count int)"], "oraclesql": ["Create table Flights (departure_airport int, arrival_airport int, flights_count int)"], "database": true, "name": "airport_with_most_traffic", "pythondata": ["Flights = pd.DataFrame([], columns=['departure_airport', 'arrival_airport', 'flights_count']).astype({'departure_airport':'Int64', 'arrival_airport':'Int64', 'flights_count':'Int64'})"], "postgresql": ["Create table If Not Exists Flights (departure_airport int, arrival_airport int, flights_count int)"], "database_schema": {"Flights": {"departure_airport": "INT", "arrival_airport": "INT", "flights_count": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,113 | Elements in Array After Removing and Replacing Elements | elements-in-array-after-removing-and-replacing-elements | null | Medium | 2.2K | 3.2K | 2,217 | 3,182 | 69.7% | [] | [] | Algorithms | null | [0,1,2]
[[0,2],[2,0],[3,2],[5,0]] | {
"name": "elementInNums",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer[][]",
"name": "queries"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array'] |
2,114 | Maximum Number of Words Found in Sentences | maximum-number-of-words-found-in-sentences | <p>A <strong>sentence</strong> is a list of <strong>words</strong> that are separated by a single space with no leading or trailing spaces.</p>
<p>You are given an array of strings <code>sentences</code>, where each <code>sentences[i]</code> represents a single <strong>sentence</strong>.</p>
<p>Return <em>the <strong>maximum number of words</strong> that appear in a single sentence</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> sentences = ["alice and bob love leetcode", "i think so too", <u>"this is great thanks very much"</u>]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
- The first sentence, "alice and bob love leetcode", has 5 words in total.
- The second sentence, "i think so too", has 4 words in total.
- The third sentence, "this is great thanks very much", has 6 words in total.
Thus, the maximum number of words in a single sentence comes from the third sentence, which has 6 words.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> sentences = ["please wait", <u>"continue to fight"</u>, <u>"continue to win"</u>]
<strong>Output:</strong> 3
<strong>Explanation:</strong> It is possible that multiple sentences contain the same number of words.
In this example, the second and third sentences (underlined) have the same number of words.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= sentences.length <= 100</code></li>
<li><code>1 <= sentences[i].length <= 100</code></li>
<li><code>sentences[i]</code> consists only of lowercase English letters and <code>' '</code> only.</li>
<li><code>sentences[i]</code> does not have leading or trailing spaces.</li>
<li>All the words in <code>sentences[i]</code> are separated by a single space.</li>
</ul>
| Easy | 343.4K | 396.5K | 343,406 | 396,504 | 86.6% | ['number-of-valid-words-in-a-sentence'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int mostWordsFound(vector<string>& sentences) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int mostWordsFound(String[] sentences) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def mostWordsFound(self, sentences):\n \"\"\"\n :type sentences: List[str]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def mostWordsFound(self, sentences: List[str]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int mostWordsFound(char** sentences, int sentencesSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MostWordsFound(string[] sentences) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} sentences\n * @return {number}\n */\nvar mostWordsFound = function(sentences) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function mostWordsFound(sentences: string[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $sentences\n * @return Integer\n */\n function mostWordsFound($sentences) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func mostWordsFound(_ sentences: [String]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun mostWordsFound(sentences: Array<String>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int mostWordsFound(List<String> sentences) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func mostWordsFound(sentences []string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} sentences\n# @return {Integer}\ndef most_words_found(sentences)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def mostWordsFound(sentences: Array[String]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn most_words_found(sentences: Vec<String>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (most-words-found sentences)\n (-> (listof string?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec most_words_found(Sentences :: [unicode:unicode_binary()]) -> integer().\nmost_words_found(Sentences) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec most_words_found(sentences :: [String.t]) :: integer\n def most_words_found(sentences) do\n \n end\nend"}] | ["alice and bob love leetcode","i think so too","this is great thanks very much"] | {
"name": "mostWordsFound",
"params": [
{
"name": "sentences",
"type": "string[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'String'] |
2,115 | Find All Possible Recipes from Given Supplies | find-all-possible-recipes-from-given-supplies | <p>You have information about <code>n</code> different recipes. You are given a string array <code>recipes</code> and a 2D string array <code>ingredients</code>. The <code>i<sup>th</sup></code> recipe has the name <code>recipes[i]</code>, and you can <strong>create</strong> it if you have <strong>all</strong> the needed ingredients from <code>ingredients[i]</code>. A recipe can also be an ingredient for <strong>other </strong>recipes, i.e., <code>ingredients[i]</code> may contain a string that is in <code>recipes</code>.</p>
<p>You are also given a string array <code>supplies</code> containing all the ingredients that you initially have, and you have an infinite supply of all of them.</p>
<p>Return <em>a list of all the recipes that you can create. </em>You may return the answer in <strong>any order</strong>.</p>
<p>Note that two recipes may contain each other in their ingredients.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> recipes = ["bread"], ingredients = [["yeast","flour"]], supplies = ["yeast","flour","corn"]
<strong>Output:</strong> ["bread"]
<strong>Explanation:</strong>
We can create "bread" since we have the ingredients "yeast" and "flour".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> recipes = ["bread","sandwich"], ingredients = [["yeast","flour"],["bread","meat"]], supplies = ["yeast","flour","meat"]
<strong>Output:</strong> ["bread","sandwich"]
<strong>Explanation:</strong>
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> recipes = ["bread","sandwich","burger"], ingredients = [["yeast","flour"],["bread","meat"],["sandwich","meat","bread"]], supplies = ["yeast","flour","meat"]
<strong>Output:</strong> ["bread","sandwich","burger"]
<strong>Explanation:</strong>
We can create "bread" since we have the ingredients "yeast" and "flour".
We can create "sandwich" since we have the ingredient "meat" and can create the ingredient "bread".
We can create "burger" since we have the ingredient "meat" and can create the ingredients "bread" and "sandwich".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == recipes.length == ingredients.length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= ingredients[i].length, supplies.length <= 100</code></li>
<li><code>1 <= recipes[i].length, ingredients[i][j].length, supplies[k].length <= 10</code></li>
<li><code>recipes[i], ingredients[i][j]</code>, and <code>supplies[k]</code> consist only of lowercase English letters.</li>
<li>All the values of <code>recipes</code> and <code>supplies</code> combined are unique.</li>
<li>Each <code>ingredients[i]</code> does not contain any duplicate values.</li>
</ul>
| Medium | 210K | 372.3K | 209,974 | 372,339 | 56.4% | ['course-schedule-ii', 'count-good-meals'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<string> findAllRecipes(vector<string>& recipes, vector<vector<string>>& ingredients, vector<string>& supplies) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<String> findAllRecipes(String[] recipes, List<List<String>> ingredients, String[] supplies) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findAllRecipes(self, recipes, ingredients, supplies):\n \"\"\"\n :type recipes: List[str]\n :type ingredients: List[List[str]]\n :type supplies: List[str]\n :rtype: List[str]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findAllRecipes(self, recipes: List[str], ingredients: List[List[str]], supplies: List[str]) -> List[str]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nchar** findAllRecipes(char** recipes, int recipesSize, char*** ingredients, int ingredientsSize, int* ingredientsColSize, char** supplies, int suppliesSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<string> FindAllRecipes(string[] recipes, IList<IList<string>> ingredients, string[] supplies) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} recipes\n * @param {string[][]} ingredients\n * @param {string[]} supplies\n * @return {string[]}\n */\nvar findAllRecipes = function(recipes, ingredients, supplies) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findAllRecipes(recipes: string[], ingredients: string[][], supplies: string[]): string[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $recipes\n * @param String[][] $ingredients\n * @param String[] $supplies\n * @return String[]\n */\n function findAllRecipes($recipes, $ingredients, $supplies) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findAllRecipes(_ recipes: [String], _ ingredients: [[String]], _ supplies: [String]) -> [String] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findAllRecipes(recipes: Array<String>, ingredients: List<List<String>>, supplies: Array<String>): List<String> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<String> findAllRecipes(List<String> recipes, List<List<String>> ingredients, List<String> supplies) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findAllRecipes(recipes []string, ingredients [][]string, supplies []string) []string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} recipes\n# @param {String[][]} ingredients\n# @param {String[]} supplies\n# @return {String[]}\ndef find_all_recipes(recipes, ingredients, supplies)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findAllRecipes(recipes: Array[String], ingredients: List[List[String]], supplies: Array[String]): List[String] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_all_recipes(recipes: Vec<String>, ingredients: Vec<Vec<String>>, supplies: Vec<String>) -> Vec<String> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-all-recipes recipes ingredients supplies)\n (-> (listof string?) (listof (listof string?)) (listof string?) (listof string?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_all_recipes(Recipes :: [unicode:unicode_binary()], Ingredients :: [[unicode:unicode_binary()]], Supplies :: [unicode:unicode_binary()]) -> [unicode:unicode_binary()].\nfind_all_recipes(Recipes, Ingredients, Supplies) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_all_recipes(recipes :: [String.t], ingredients :: [[String.t]], supplies :: [String.t]) :: [String.t]\n def find_all_recipes(recipes, ingredients, supplies) do\n \n end\nend"}] | ["bread"]
[["yeast","flour"]]
["yeast","flour","corn"] | {
"name": "findAllRecipes",
"params": [
{
"name": "recipes",
"type": "string[]"
},
{
"type": "list<list<string>>",
"name": "ingredients"
},
{
"type": "string[]",
"name": "supplies"
}
],
"return": {
"type": "list<string>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'String', 'Graph', 'Topological Sort'] |
2,116 | Check if a Parentheses String Can Be Valid | check-if-a-parentheses-string-can-be-valid | <p>A parentheses string is a <strong>non-empty</strong> string consisting only of <code>'('</code> and <code>')'</code>. It is valid if <strong>any</strong> of the following conditions is <strong>true</strong>:</p>
<ul>
<li>It is <code>()</code>.</li>
<li>It can be written as <code>AB</code> (<code>A</code> concatenated with <code>B</code>), where <code>A</code> and <code>B</code> are valid parentheses strings.</li>
<li>It can be written as <code>(A)</code>, where <code>A</code> is a valid parentheses string.</li>
</ul>
<p>You are given a parentheses string <code>s</code> and a string <code>locked</code>, both of length <code>n</code>. <code>locked</code> is a binary string consisting only of <code>'0'</code>s and <code>'1'</code>s. For <strong>each</strong> index <code>i</code> of <code>locked</code>,</p>
<ul>
<li>If <code>locked[i]</code> is <code>'1'</code>, you <strong>cannot</strong> change <code>s[i]</code>.</li>
<li>But if <code>locked[i]</code> is <code>'0'</code>, you <strong>can</strong> change <code>s[i]</code> to either <code>'('</code> or <code>')'</code>.</li>
</ul>
<p>Return <code>true</code> <em>if you can make <code>s</code> a valid parentheses string</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/06/eg1.png" style="width: 311px; height: 101px;" />
<pre>
<strong>Input:</strong> s = "))()))", locked = "010100"
<strong>Output:</strong> true
<strong>Explanation:</strong> locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "()()", locked = "0000"
<strong>Output:</strong> true
<strong>Explanation:</strong> We do not need to make any changes because s is already valid.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = ")", locked = "0"
<strong>Output:</strong> false
<strong>Explanation:</strong> locked permits us to change s[0].
Changing s[0] to either '(' or ')' will not make s valid.
</pre>
<p><strong class="example">Example 4:</strong></p>
<pre>
<strong>Input:</strong> s = "(((())(((())", locked = "111111010111"
<strong>Output:</strong> true
<strong>Explanation:</strong> locked permits us to change s[6] and s[8].
We change s[6] and s[8] to ')' to make s valid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == s.length == locked.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'('</code> or <code>')'</code>.</li>
<li><code>locked[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
</ul>
| Medium | 145.7K | 325.6K | 145,704 | 325,564 | 44.8% | ['valid-parentheses', 'generate-parentheses', 'valid-parenthesis-string', 'minimum-remove-to-make-valid-parentheses', 'check-if-there-is-a-valid-parentheses-string-path'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool canBeValid(string s, string locked) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean canBeValid(String s, String locked) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def canBeValid(self, s, locked):\n \"\"\"\n :type s: str\n :type locked: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def canBeValid(self, s: str, locked: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool canBeValid(char* s, char* locked) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool CanBeValid(string s, string locked) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {string} locked\n * @return {boolean}\n */\nvar canBeValid = function(s, locked) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function canBeValid(s: string, locked: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param String $locked\n * @return Boolean\n */\n function canBeValid($s, $locked) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func canBeValid(_ s: String, _ locked: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun canBeValid(s: String, locked: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool canBeValid(String s, String locked) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func canBeValid(s string, locked string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} locked\n# @return {Boolean}\ndef can_be_valid(s, locked)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def canBeValid(s: String, locked: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn can_be_valid(s: String, locked: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (can-be-valid s locked)\n (-> string? string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec can_be_valid(S :: unicode:unicode_binary(), Locked :: unicode:unicode_binary()) -> boolean().\ncan_be_valid(S, Locked) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec can_be_valid(s :: String.t, locked :: String.t) :: boolean\n def can_be_valid(s, locked) do\n \n end\nend"}] | "))()))"
"010100" | {
"name": "canBeValid",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "string",
"name": "locked"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Stack', 'Greedy'] |
2,117 | Abbreviating the Product of a Range | abbreviating-the-product-of-a-range | <p>You are given two positive integers <code>left</code> and <code>right</code> with <code>left <= right</code>. Calculate the <strong>product</strong> of all integers in the <strong>inclusive</strong> range <code>[left, right]</code>.</p>
<p>Since the product may be very large, you will <strong>abbreviate</strong> it following these steps:</p>
<ol>
<li>Count all <strong>trailing</strong> zeros in the product and <strong>remove</strong> them. Let us denote this count as <code>C</code>.
<ul>
<li>For example, there are <code>3</code> trailing zeros in <code>1000</code>, and there are <code>0</code> trailing zeros in <code>546</code>.</li>
</ul>
</li>
<li>Denote the remaining number of digits in the product as <code>d</code>. If <code>d > 10</code>, then express the product as <code><pre>...<suf></code> where <code><pre></code> denotes the <strong>first</strong> <code>5</code> digits of the product, and <code><suf></code> denotes the <strong>last</strong> <code>5</code> digits of the product <strong>after</strong> removing all trailing zeros. If <code>d <= 10</code>, we keep it unchanged.
<ul>
<li>For example, we express <code>1234567654321</code> as <code>12345...54321</code>, but <code>1234567</code> is represented as <code>1234567</code>.</li>
</ul>
</li>
<li>Finally, represent the product as a <strong>string</strong> <code>"<pre>...<suf>eC"</code>.
<ul>
<li>For example, <code>12345678987600000</code> will be represented as <code>"12345...89876e5"</code>.</li>
</ul>
</li>
</ol>
<p>Return <em>a string denoting the <strong>abbreviated product</strong> of all integers in the <strong>inclusive</strong> range</em> <code>[left, right]</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> left = 1, right = 4
<strong>Output:</strong> "24e0"
<strong>Explanation:</strong> The product is 1 × 2 × 3 × 4 = 24.
There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is "24e0".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> left = 2, right = 11
<strong>Output:</strong> "399168e2"
<strong>Explanation:</strong> The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is "399168e2".
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> left = 371, right = 375
<strong>Output:</strong> "7219856259e3"
<strong>Explanation:</strong> The product is 7219856259000.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= left <= right <= 10<sup>4</sup></code></li>
</ul>
| Hard | 4K | 16.5K | 4,001 | 16,455 | 24.3% | ['factorial-trailing-zeroes', 'maximum-trailing-zeros-in-a-cornered-path', 'find-all-good-indices'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string abbreviateProduct(int left, int right) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String abbreviateProduct(int left, int right) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def abbreviateProduct(self, left, right):\n \"\"\"\n :type left: int\n :type right: int\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def abbreviateProduct(self, left: int, right: int) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* abbreviateProduct(int left, int right) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string AbbreviateProduct(int left, int right) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} left\n * @param {number} right\n * @return {string}\n */\nvar abbreviateProduct = function(left, right) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function abbreviateProduct(left: number, right: number): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $left\n * @param Integer $right\n * @return String\n */\n function abbreviateProduct($left, $right) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func abbreviateProduct(_ left: Int, _ right: Int) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun abbreviateProduct(left: Int, right: Int): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String abbreviateProduct(int left, int right) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func abbreviateProduct(left int, right int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} left\n# @param {Integer} right\n# @return {String}\ndef abbreviate_product(left, right)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def abbreviateProduct(left: Int, right: Int): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn abbreviate_product(left: i32, right: i32) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (abbreviate-product left right)\n (-> exact-integer? exact-integer? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec abbreviate_product(Left :: integer(), Right :: integer()) -> unicode:unicode_binary().\nabbreviate_product(Left, Right) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec abbreviate_product(left :: integer, right :: integer) :: String.t\n def abbreviate_product(left, right) do\n \n end\nend"}] | 1
4 | {
"name": "abbreviateProduct",
"params": [
{
"name": "left",
"type": "integer"
},
{
"type": "integer",
"name": "right"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math'] |
2,118 | Build the Equation | build-the-equation | null | Hard | 3.2K | 5.7K | 3,193 | 5,669 | 56.3% | [] | ['Create table If Not Exists Terms (power int, factor int)', 'Truncate table Terms', "insert into Terms (power, factor) values ('2', '1')", "insert into Terms (power, factor) values ('1', '-4')", "insert into Terms (power, factor) values ('0', '2')"] | Database | null | {"headers":{"Terms":["power","factor"]},"rows":{"Terms":[[2,1],[1,-4],[0,2]]}} | {"mysql": ["Create table If Not Exists Terms (power int, factor int)"], "mssql": ["Create table Terms (power int, factor int)"], "oraclesql": ["Create table Terms (power int, factor int)"], "database": true, "name": "build_the_equation", "pythondata": ["Terms = pd.DataFrame([], columns=['power', 'factor']).astype({'power':'Int64', 'factor':'Int64'})"], "postgresql": ["Create table If Not Exists Terms (power int, factor int)"], "database_schema": {"Terms": {"power": "INT", "factor": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,119 | A Number After a Double Reversal | a-number-after-a-double-reversal | <p><strong>Reversing</strong> an integer means to reverse all its digits.</p>
<ul>
<li>For example, reversing <code>2021</code> gives <code>1202</code>. Reversing <code>12300</code> gives <code>321</code> as the <strong>leading zeros are not retained</strong>.</li>
</ul>
<p>Given an integer <code>num</code>, <strong>reverse</strong> <code>num</code> to get <code>reversed1</code>, <strong>then reverse</strong> <code>reversed1</code> to get <code>reversed2</code>. Return <code>true</code> <em>if</em> <code>reversed2</code> <em>equals</em> <code>num</code>. Otherwise return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 526
<strong>Output:</strong> true
<strong>Explanation:</strong> Reverse num to get 625, then reverse 625 to get 526, which equals num.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 1800
<strong>Output:</strong> false
<strong>Explanation:</strong> Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> num = 0
<strong>Output:</strong> true
<strong>Explanation:</strong> Reverse num to get 0, then reverse 0 to get 0, which equals num.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= num <= 10<sup>6</sup></code></li>
</ul>
| Easy | 114.5K | 141.4K | 114,517 | 141,442 | 81.0% | ['reverse-integer', 'reverse-bits'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool isSameAfterReversals(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean isSameAfterReversals(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def isSameAfterReversals(self, num):\n \"\"\"\n :type num: int\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def isSameAfterReversals(self, num: int) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool isSameAfterReversals(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool IsSameAfterReversals(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {boolean}\n */\nvar isSameAfterReversals = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function isSameAfterReversals(num: number): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Boolean\n */\n function isSameAfterReversals($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func isSameAfterReversals(_ num: Int) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun isSameAfterReversals(num: Int): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool isSameAfterReversals(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func isSameAfterReversals(num int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Boolean}\ndef is_same_after_reversals(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def isSameAfterReversals(num: Int): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn is_same_after_reversals(num: i32) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (is-same-after-reversals num)\n (-> exact-integer? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec is_same_after_reversals(Num :: integer()) -> boolean().\nis_same_after_reversals(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec is_same_after_reversals(num :: integer) :: boolean\n def is_same_after_reversals(num) do\n \n end\nend"}] | 526 | {
"name": "isSameAfterReversals",
"params": [
{
"name": "num",
"type": "integer"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math'] |
2,120 | Execution of All Suffix Instructions Staying in a Grid | execution-of-all-suffix-instructions-staying-in-a-grid | <p>There is an <code>n x n</code> grid, with the top-left cell at <code>(0, 0)</code> and the bottom-right cell at <code>(n - 1, n - 1)</code>. You are given the integer <code>n</code> and an integer array <code>startPos</code> where <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> indicates that a robot is initially at cell <code>(start<sub>row</sub>, start<sub>col</sub>)</code>.</p>
<p>You are also given a <strong>0-indexed</strong> string <code>s</code> of length <code>m</code> where <code>s[i]</code> is the <code>i<sup>th</sup></code> instruction for the robot: <code>'L'</code> (move left), <code>'R'</code> (move right), <code>'U'</code> (move up), and <code>'D'</code> (move down).</p>
<p>The robot can begin executing from any <code>i<sup>th</sup></code> instruction in <code>s</code>. It executes the instructions one by one towards the end of <code>s</code> but it stops if either of these conditions is met:</p>
<ul>
<li>The next instruction will move the robot off the grid.</li>
<li>There are no more instructions left to execute.</li>
</ul>
<p>Return <em>an array</em> <code>answer</code> <em>of length</em> <code>m</code> <em>where</em> <code>answer[i]</code> <em>is <strong>the number of instructions</strong> the robot can execute if the robot <strong>begins executing from</strong> the</em> <code>i<sup>th</sup></code> <em>instruction in</em> <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/1.png" style="width: 145px; height: 142px;" />
<pre>
<strong>Input:</strong> n = 3, startPos = [0,1], s = "RRDDLU"
<strong>Output:</strong> [1,5,4,3,1,0]
<strong>Explanation:</strong> Starting from startPos and beginning execution from the i<sup>th</sup> instruction:
- 0<sup>th</sup>: "<u><strong>R</strong></u>RDDLU". Only one instruction "R" can be executed before it moves off the grid.
- 1<sup>st</sup>: "<u><strong>RDDLU</strong></u>". All five instructions can be executed while it stays in the grid and ends at (1, 1).
- 2<sup>nd</sup>: "<u><strong>DDLU</strong></u>". All four instructions can be executed while it stays in the grid and ends at (1, 0).
- 3<sup>rd</sup>: "<u><strong>DLU</strong></u>". All three instructions can be executed while it stays in the grid and ends at (0, 0).
- 4<sup>th</sup>: "<u><strong>L</strong></u>U". Only one instruction "L" can be executed before it moves off the grid.
- 5<sup>th</sup>: "U". If moving up, it would move off the grid.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/2.png" style="width: 106px; height: 103px;" />
<pre>
<strong>Input:</strong> n = 2, startPos = [1,1], s = "LURD"
<strong>Output:</strong> [4,1,0,0]
<strong>Explanation:</strong>
- 0<sup>th</sup>: "<u><strong>LURD</strong></u>".
- 1<sup>st</sup>: "<u><strong>U</strong></u>RD".
- 2<sup>nd</sup>: "RD".
- 3<sup>rd</sup>: "D".
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/3.png" style="width: 67px; height: 64px;" />
<pre>
<strong>Input:</strong> n = 1, startPos = [0,0], s = "LRUD"
<strong>Output:</strong> [0,0,0,0]
<strong>Explanation:</strong> No matter which instruction the robot begins execution from, it would move off the grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == s.length</code></li>
<li><code>1 <= n, m <= 500</code></li>
<li><code>startPos.length == 2</code></li>
<li><code>0 <= start<sub>row</sub>, start<sub>col</sub> < n</code></li>
<li><code>s</code> consists of <code>'L'</code>, <code>'R'</code>, <code>'U'</code>, and <code>'D'</code>.</li>
</ul>
| Medium | 34.7K | 42.5K | 34,653 | 42,539 | 81.5% | ['out-of-boundary-paths', 'robot-return-to-origin'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] executeInstructions(int n, int[] startPos, String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def executeInstructions(self, n, startPos, s):\n \"\"\"\n :type n: int\n :type startPos: List[int]\n :type s: str\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* executeInstructions(int n, int* startPos, int startPosSize, char* s, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] ExecuteInstructions(int n, int[] startPos, string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[]} startPos\n * @param {string} s\n * @return {number[]}\n */\nvar executeInstructions = function(n, startPos, s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function executeInstructions(n: number, startPos: number[], s: string): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[] $startPos\n * @param String $s\n * @return Integer[]\n */\n function executeInstructions($n, $startPos, $s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func executeInstructions(_ n: Int, _ startPos: [Int], _ s: String) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun executeInstructions(n: Int, startPos: IntArray, s: String): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> executeInstructions(int n, List<int> startPos, String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func executeInstructions(n int, startPos []int, s string) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[]} start_pos\n# @param {String} s\n# @return {Integer[]}\ndef execute_instructions(n, start_pos, s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def executeInstructions(n: Int, startPos: Array[Int], s: String): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (execute-instructions n startPos s)\n (-> exact-integer? (listof exact-integer?) string? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec execute_instructions(N :: integer(), StartPos :: [integer()], S :: unicode:unicode_binary()) -> [integer()].\nexecute_instructions(N, StartPos, S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec execute_instructions(n :: integer, start_pos :: [integer], s :: String.t) :: [integer]\n def execute_instructions(n, start_pos, s) do\n \n end\nend"}] | 3
[0,1]
"RRDDLU" | {
"name": "executeInstructions",
"params": [
{
"name": "n",
"type": "integer"
},
{
"type": "integer[]",
"name": "startPos"
},
{
"type": "string",
"name": "s"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Simulation'] |
2,121 | Intervals Between Identical Elements | intervals-between-identical-elements | <p>You are given a <strong>0-indexed</strong> array of <code>n</code> integers <code>arr</code>.</p>
<p>The <strong>interval</strong> between two elements in <code>arr</code> is defined as the <strong>absolute difference</strong> between their indices. More formally, the <strong>interval</strong> between <code>arr[i]</code> and <code>arr[j]</code> is <code>|i - j|</code>.</p>
<p>Return <em>an array</em> <code>intervals</code> <em>of length</em> <code>n</code> <em>where</em> <code>intervals[i]</code> <em>is <strong>the sum of intervals</strong> between </em><code>arr[i]</code><em> and each element in </em><code>arr</code><em> with the same value as </em><code>arr[i]</code><em>.</em></p>
<p><strong>Note:</strong> <code>|x|</code> is the absolute value of <code>x</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> arr = [2,1,3,1,2,3,3]
<strong>Output:</strong> [4,2,7,2,4,4,5]
<strong>Explanation:</strong>
- Index 0: Another 2 is found at index 4. |0 - 4| = 4
- Index 1: Another 1 is found at index 3. |1 - 3| = 2
- Index 2: Two more 3s are found at indices 5 and 6. |2 - 5| + |2 - 6| = 7
- Index 3: Another 1 is found at index 1. |3 - 1| = 2
- Index 4: Another 2 is found at index 0. |4 - 0| = 4
- Index 5: Two more 3s are found at indices 2 and 6. |5 - 2| + |5 - 6| = 4
- Index 6: Two more 3s are found at indices 2 and 5. |6 - 2| + |6 - 5| = 5
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> arr = [10,5,10,10]
<strong>Output:</strong> [5,0,3,4]
<strong>Explanation:</strong>
- Index 0: Two more 10s are found at indices 2 and 3. |0 - 2| + |0 - 3| = 5
- Index 1: There is only one 5 in the array, so its sum of intervals to identical elements is 0.
- Index 2: Two more 10s are found at indices 0 and 3. |2 - 0| + |2 - 3| = 3
- Index 3: Two more 10s are found at indices 0 and 2. |3 - 0| + |3 - 2| = 4
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == arr.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= arr[i] <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/sum-of-distances/description/" target="_blank"> 2615: Sum of Distances.</a></p>
| Medium | 23.3K | 52K | 23,283 | 52,001 | 44.8% | ['continuous-subarray-sum'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> getDistances(vector<int>& arr) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long[] getDistances(int[] arr) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def getDistances(self, arr):\n \"\"\"\n :type arr: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def getDistances(self, arr: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nlong long* getDistances(int* arr, int arrSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long[] GetDistances(int[] arr) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} arr\n * @return {number[]}\n */\nvar getDistances = function(arr) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function getDistances(arr: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $arr\n * @return Integer[]\n */\n function getDistances($arr) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func getDistances(_ arr: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun getDistances(arr: IntArray): LongArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> getDistances(List<int> arr) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func getDistances(arr []int) []int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} arr\n# @return {Integer[]}\ndef get_distances(arr)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def getDistances(arr: Array[Int]): Array[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn get_distances(arr: Vec<i32>) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (get-distances arr)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec get_distances(Arr :: [integer()]) -> [integer()].\nget_distances(Arr) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec get_distances(arr :: [integer]) :: [integer]\n def get_distances(arr) do\n \n end\nend"}] | [2,1,3,1,2,3,3] | {
"name": "getDistances",
"params": [
{
"name": "arr",
"type": "integer[]"
}
],
"return": {
"type": "long[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Prefix Sum'] |
2,122 | Recover the Original Array | recover-the-original-array | <p>Alice had a <strong>0-indexed</strong> array <code>arr</code> consisting of <code>n</code> <strong>positive</strong> integers. She chose an arbitrary <strong>positive integer</strong> <code>k</code> and created two new <strong>0-indexed</strong> integer arrays <code>lower</code> and <code>higher</code> in the following manner:</p>
<ol>
<li><code>lower[i] = arr[i] - k</code>, for every index <code>i</code> where <code>0 <= i < n</code></li>
<li><code>higher[i] = arr[i] + k</code>, for every index <code>i</code> where <code>0 <= i < n</code></li>
</ol>
<p>Unfortunately, Alice lost all three arrays. However, she remembers the integers that were present in the arrays <code>lower</code> and <code>higher</code>, but not the array each integer belonged to. Help Alice and recover the original array.</p>
<p>Given an array <code>nums</code> consisting of <code>2n</code> integers, where <strong>exactly</strong> <code>n</code> of the integers were present in <code>lower</code> and the remaining in <code>higher</code>, return <em>the <strong>original</strong> array</em> <code>arr</code>. In case the answer is not unique, return <em><strong>any</strong> valid array</em>.</p>
<p><strong>Note:</strong> The test cases are generated such that there exists <strong>at least one</strong> valid array <code>arr</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,10,6,4,8,12]
<strong>Output:</strong> [3,7,11]
<strong>Explanation:</strong>
If arr = [3,7,11] and k = 1, we get lower = [2,6,10] and higher = [4,8,12].
Combining lower and higher gives us [2,6,10,4,8,12], which is a permutation of nums.
Another valid possibility is that arr = [5,7,9] and k = 3. In that case, lower = [2,4,6] and higher = [8,10,12].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,3,3]
<strong>Output:</strong> [2,2]
<strong>Explanation:</strong>
If arr = [2,2] and k = 1, we get lower = [1,1] and higher = [3,3].
Combining lower and higher gives us [1,1,3,3], which is equal to nums.
Note that arr cannot be [1,3] because in that case, the only possible way to obtain [1,1,3,3] is with k = 0.
This is invalid since k must be positive.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,435]
<strong>Output:</strong> [220]
<strong>Explanation:</strong>
The only possible combination is arr = [220] and k = 215. Using them, we get lower = [5] and higher = [435].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 * n == nums.length</code></li>
<li><code>1 <= n <= 1000</code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li>The test cases are generated such that there exists <strong>at least one</strong> valid array <code>arr</code>.</li>
</ul>
| Hard | 12.2K | 30.8K | 12,231 | 30,805 | 39.7% | ['find-array-given-subset-sums', 'find-original-array-from-doubled-array'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> recoverArray(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] recoverArray(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def recoverArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def recoverArray(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* recoverArray(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] RecoverArray(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar recoverArray = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function recoverArray(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function recoverArray($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func recoverArray(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun recoverArray(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> recoverArray(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func recoverArray(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef recover_array(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def recoverArray(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn recover_array(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (recover-array nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec recover_array(Nums :: [integer()]) -> [integer()].\nrecover_array(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec recover_array(nums :: [integer]) :: [integer]\n def recover_array(nums) do\n \n end\nend"}] | [2,10,6,4,8,12] | {
"name": "recoverArray",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Two Pointers', 'Sorting', 'Enumeration'] |
2,123 | Minimum Operations to Remove Adjacent Ones in Matrix | minimum-operations-to-remove-adjacent-ones-in-matrix | null | Hard | 1.1K | 2.7K | 1,096 | 2,699 | 40.6% | ['set-matrix-zeroes', '01-matrix', 'minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix', 'remove-all-ones-with-row-and-column-flips'] | [] | Algorithms | null | [[1,1,0],[0,1,1],[1,1,1]] | {
"name": "minimumOperations",
"params": [
{
"name": "grid",
"type": "integer[][]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Graph', 'Matrix'] |
2,124 | Check if All A's Appears Before All B's | check-if-all-as-appears-before-all-bs | <p>Given a string <code>s</code> consisting of <strong>only</strong> the characters <code>'a'</code> and <code>'b'</code>, return <code>true</code> <em>if <strong>every</strong> </em><code>'a'</code> <em>appears before <strong>every</strong> </em><code>'b'</code><em> in the string</em>. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aaabbb"
<strong>Output:</strong> true
<strong>Explanation:</strong>
The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
Hence, every 'a' appears before every 'b' and we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abab"
<strong>Output:</strong> false
<strong>Explanation:</strong>
There is an 'a' at index 2 and a 'b' at index 1.
Hence, not every 'a' appears before every 'b' and we return false.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "bbb"
<strong>Output:</strong> true
<strong>Explanation:</strong>
There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s[i]</code> is either <code>'a'</code> or <code>'b'</code>.</li>
</ul>
| Easy | 102.7K | 142K | 102,701 | 141,963 | 72.3% | ['minimum-deletions-to-make-string-balanced', 'check-if-array-is-sorted-and-rotated', 'check-if-numbers-are-ascending-in-a-sentence'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool checkString(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean checkString(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def checkString(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def checkString(self, s: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool checkString(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool CheckString(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar checkString = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function checkString(s: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Boolean\n */\n function checkString($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func checkString(_ s: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun checkString(s: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool checkString(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func checkString(s string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Boolean}\ndef check_string(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def checkString(s: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn check_string(s: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (check-string s)\n (-> string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec check_string(S :: unicode:unicode_binary()) -> boolean().\ncheck_string(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec check_string(s :: String.t) :: boolean\n def check_string(s) do\n \n end\nend"}] | "aaabbb" | {
"name": "checkString",
"params": [
{
"name": "s",
"type": "string"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String'] |
2,125 | Number of Laser Beams in a Bank | number-of-laser-beams-in-a-bank | <p>Anti-theft security devices are activated inside a bank. You are given a <strong>0-indexed</strong> binary string array <code>bank</code> representing the floor plan of the bank, which is an <code>m x n</code> 2D matrix. <code>bank[i]</code> represents the <code>i<sup>th</sup></code> row, consisting of <code>'0'</code>s and <code>'1'</code>s. <code>'0'</code> means the cell is empty, while<code>'1'</code> means the cell has a security device.</p>
<p>There is <strong>one</strong> laser beam between any <strong>two</strong> security devices <strong>if both</strong> conditions are met:</p>
<ul>
<li>The two devices are located on two <strong>different rows</strong>: <code>r<sub>1</sub></code> and <code>r<sub>2</sub></code>, where <code>r<sub>1</sub> < r<sub>2</sub></code>.</li>
<li>For <strong>each</strong> row <code>i</code> where <code>r<sub>1</sub> < i < r<sub>2</sub></code>, there are <strong>no security devices</strong> in the <code>i<sup>th</sup></code> row.</li>
</ul>
<p>Laser beams are independent, i.e., one beam does not interfere nor join with another.</p>
<p>Return <em>the total number of laser beams in the bank</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/laser1.jpg" style="width: 400px; height: 368px;" />
<pre>
<strong>Input:</strong> bank = ["011001","000000","010100","001000"]
<strong>Output:</strong> 8
<strong>Explanation:</strong> Between each of the following device pairs, there is one beam. In total, there are 8 beams:
* bank[0][1] -- bank[2][1]
* bank[0][1] -- bank[2][3]
* bank[0][2] -- bank[2][1]
* bank[0][2] -- bank[2][3]
* bank[0][5] -- bank[2][1]
* bank[0][5] -- bank[2][3]
* bank[2][1] -- bank[3][2]
* bank[2][3] -- bank[3][2]
Note that there is no beam between any device on the 0<sup>th</sup> row with any on the 3<sup>rd</sup> row.
This is because the 2<sup>nd</sup> row contains security devices, which breaks the second condition.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/laser2.jpg" style="width: 244px; height: 325px;" />
<pre>
<strong>Input:</strong> bank = ["000","111","000"]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There does not exist two devices located on two different rows.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == bank.length</code></li>
<li><code>n == bank[i].length</code></li>
<li><code>1 <= m, n <= 500</code></li>
<li><code>bank[i][j]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
</ul>
| Medium | 222.5K | 260.5K | 222,480 | 260,512 | 85.4% | ['set-matrix-zeroes'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numberOfBeams(vector<string>& bank) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numberOfBeams(String[] bank) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numberOfBeams(self, bank):\n \"\"\"\n :type bank: List[str]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numberOfBeams(self, bank: List[str]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numberOfBeams(char** bank, int bankSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumberOfBeams(string[] bank) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} bank\n * @return {number}\n */\nvar numberOfBeams = function(bank) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numberOfBeams(bank: string[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $bank\n * @return Integer\n */\n function numberOfBeams($bank) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numberOfBeams(_ bank: [String]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numberOfBeams(bank: Array<String>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numberOfBeams(List<String> bank) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numberOfBeams(bank []string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} bank\n# @return {Integer}\ndef number_of_beams(bank)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numberOfBeams(bank: Array[String]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn number_of_beams(bank: Vec<String>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (number-of-beams bank)\n (-> (listof string?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec number_of_beams(Bank :: [unicode:unicode_binary()]) -> integer().\nnumber_of_beams(Bank) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec number_of_beams(bank :: [String.t]) :: integer\n def number_of_beams(bank) do\n \n end\nend"}] | ["011001","000000","010100","001000"] | {
"name": "numberOfBeams",
"params": [
{
"name": "bank",
"type": "string[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'String', 'Matrix'] |
2,126 | Destroying Asteroids | destroying-asteroids | <p>You are given an integer <code>mass</code>, which represents the original mass of a planet. You are further given an integer array <code>asteroids</code>, where <code>asteroids[i]</code> is the mass of the <code>i<sup>th</sup></code> asteroid.</p>
<p>You can arrange for the planet to collide with the asteroids in <strong>any arbitrary order</strong>. If the mass of the planet is <b>greater than or equal to</b> the mass of the asteroid, the asteroid is <strong>destroyed</strong> and the planet <strong>gains</strong> the mass of the asteroid. Otherwise, the planet is destroyed.</p>
<p>Return <code>true</code><em> if <strong>all</strong> asteroids can be destroyed. Otherwise, return </em><code>false</code><em>.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> mass = 10, asteroids = [3,9,19,5,21]
<strong>Output:</strong> true
<strong>Explanation:</strong> One way to order the asteroids is [9,19,5,3,21]:
- The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19
- The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38
- The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43
- The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46
- The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67
All asteroids are destroyed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> mass = 5, asteroids = [4,9,23,4]
<strong>Output:</strong> false
<strong>Explanation:</strong>
The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
This is less than 23, so a collision would not destroy the last asteroid.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= mass <= 10<sup>5</sup></code></li>
<li><code>1 <= asteroids.length <= 10<sup>5</sup></code></li>
<li><code>1 <= asteroids[i] <= 10<sup>5</sup></code></li>
</ul>
| Medium | 55.6K | 106K | 55,576 | 105,955 | 52.5% | ['asteroid-collision'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool asteroidsDestroyed(int mass, vector<int>& asteroids) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean asteroidsDestroyed(int mass, int[] asteroids) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def asteroidsDestroyed(self, mass, asteroids):\n \"\"\"\n :type mass: int\n :type asteroids: List[int]\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool asteroidsDestroyed(int mass, int* asteroids, int asteroidsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool AsteroidsDestroyed(int mass, int[] asteroids) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} mass\n * @param {number[]} asteroids\n * @return {boolean}\n */\nvar asteroidsDestroyed = function(mass, asteroids) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function asteroidsDestroyed(mass: number, asteroids: number[]): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $mass\n * @param Integer[] $asteroids\n * @return Boolean\n */\n function asteroidsDestroyed($mass, $asteroids) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func asteroidsDestroyed(_ mass: Int, _ asteroids: [Int]) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun asteroidsDestroyed(mass: Int, asteroids: IntArray): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool asteroidsDestroyed(int mass, List<int> asteroids) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func asteroidsDestroyed(mass int, asteroids []int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} mass\n# @param {Integer[]} asteroids\n# @return {Boolean}\ndef asteroids_destroyed(mass, asteroids)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def asteroidsDestroyed(mass: Int, asteroids: Array[Int]): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn asteroids_destroyed(mass: i32, asteroids: Vec<i32>) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (asteroids-destroyed mass asteroids)\n (-> exact-integer? (listof exact-integer?) boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec asteroids_destroyed(Mass :: integer(), Asteroids :: [integer()]) -> boolean().\nasteroids_destroyed(Mass, Asteroids) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec asteroids_destroyed(mass :: integer, asteroids :: [integer]) :: boolean\n def asteroids_destroyed(mass, asteroids) do\n \n end\nend"}] | 10
[3,9,19,5,21] | {
"name": "asteroidsDestroyed",
"params": [
{
"name": "mass",
"type": "integer"
},
{
"type": "integer[]",
"name": "asteroids"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy', 'Sorting'] |
2,127 | Maximum Employees to Be Invited to a Meeting | maximum-employees-to-be-invited-to-a-meeting | <p>A company is organizing a meeting and has a list of <code>n</code> employees, waiting to be invited. They have arranged for a large <strong>circular</strong> table, capable of seating <strong>any number</strong> of employees.</p>
<p>The employees are numbered from <code>0</code> to <code>n - 1</code>. Each employee has a <strong>favorite</strong> person and they will attend the meeting <strong>only if</strong> they can sit next to their favorite person at the table. The favorite person of an employee is <strong>not</strong> themself.</p>
<p>Given a <strong>0-indexed</strong> integer array <code>favorite</code>, where <code>favorite[i]</code> denotes the favorite person of the <code>i<sup>th</sup></code> employee, return <em>the <strong>maximum number of employees</strong> that can be invited to the meeting</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/14/ex1.png" style="width: 236px; height: 195px;" />
<pre>
<strong>Input:</strong> favorite = [2,2,1,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
The above figure shows how the company can invite employees 0, 1, and 2, and seat them at the round table.
All employees cannot be invited because employee 2 cannot sit beside employees 0, 1, and 3, simultaneously.
Note that the company can also invite employees 1, 2, and 3, and give them their desired seats.
The maximum number of employees that can be invited to the meeting is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> favorite = [1,2,0]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
Each employee is the favorite person of at least one other employee, and the only way the company can invite them is if they invite every employee.
The seating arrangement will be the same as that in the figure given in example 1:
- Employee 0 will sit between employees 2 and 1.
- Employee 1 will sit between employees 0 and 2.
- Employee 2 will sit between employees 1 and 0.
The maximum number of employees that can be invited to the meeting is 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/14/ex2.png" style="width: 219px; height: 220px;" />
<pre>
<strong>Input:</strong> favorite = [3,0,1,4,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
The above figure shows how the company will invite employees 0, 1, 3, and 4, and seat them at the round table.
Employee 2 cannot be invited because the two spots next to their favorite employee 1 are taken.
So the company leaves them out of the meeting.
The maximum number of employees that can be invited to the meeting is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == favorite.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>0 <= favorite[i] <= n - 1</code></li>
<li><code>favorite[i] != i</code></li>
</ul>
| Hard | 83.7K | 134.7K | 83,732 | 134,677 | 62.2% | ['redundant-connection', 'parallel-courses-iii', 'process-restricted-friend-requests'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumInvitations(vector<int>& favorite) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumInvitations(int[] favorite) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumInvitations(self, favorite):\n \"\"\"\n :type favorite: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumInvitations(self, favorite: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumInvitations(int* favorite, int favoriteSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumInvitations(int[] favorite) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} favorite\n * @return {number}\n */\nvar maximumInvitations = function(favorite) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumInvitations(favorite: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $favorite\n * @return Integer\n */\n function maximumInvitations($favorite) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumInvitations(_ favorite: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumInvitations(favorite: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumInvitations(List<int> favorite) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumInvitations(favorite []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} favorite\n# @return {Integer}\ndef maximum_invitations(favorite)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumInvitations(favorite: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_invitations(favorite: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-invitations favorite)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_invitations(Favorite :: [integer()]) -> integer().\nmaximum_invitations(Favorite) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_invitations(favorite :: [integer]) :: integer\n def maximum_invitations(favorite) do\n \n end\nend"}] | [2,2,1,2] | {
"name": "maximumInvitations",
"params": [
{
"name": "favorite",
"type": "integer[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Depth-First Search', 'Graph', 'Topological Sort'] |
2,128 | Remove All Ones With Row and Column Flips | remove-all-ones-with-row-and-column-flips | null | Medium | 34K | 44.7K | 34,034 | 44,674 | 76.2% | ['score-after-flipping-matrix', 'minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix', 'minimum-operations-to-remove-adjacent-ones-in-matrix', 'remove-all-ones-with-row-and-column-flips-ii', 'difference-between-ones-and-zeros-in-row-and-column'] | [] | Algorithms | null | [[0,1,0],[1,0,1],[0,1,0]] | {
"name": "removeOnes",
"params": [
{
"name": "grid",
"type": "integer[][]"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'Bit Manipulation', 'Matrix'] |
2,129 | Capitalize the Title | capitalize-the-title | <p>You are given a string <code>title</code> consisting of one or more words separated by a single space, where each word consists of English letters. <strong>Capitalize</strong> the string by changing the capitalization of each word such that:</p>
<ul>
<li>If the length of the word is <code>1</code> or <code>2</code> letters, change all letters to lowercase.</li>
<li>Otherwise, change the first letter to uppercase and the remaining letters to lowercase.</li>
</ul>
<p>Return <em>the <strong>capitalized</strong> </em><code>title</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> title = "capiTalIze tHe titLe"
<strong>Output:</strong> "Capitalize The Title"
<strong>Explanation:</strong>
Since all the words have a length of at least 3, the first letter of each word is uppercase, and the remaining letters are lowercase.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> title = "First leTTeR of EACH Word"
<strong>Output:</strong> "First Letter of Each Word"
<strong>Explanation:</strong>
The word "of" has length 2, so it is all lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> title = "i lOve leetcode"
<strong>Output:</strong> "i Love Leetcode"
<strong>Explanation:</strong>
The word "i" has length 1, so it is lowercase.
The remaining words have a length of at least 3, so the first letter of each remaining word is uppercase, and the remaining letters are lowercase.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= title.length <= 100</code></li>
<li><code>title</code> consists of words separated by a single space without any leading or trailing spaces.</li>
<li>Each word consists of uppercase and lowercase English letters and is <strong>non-empty</strong>.</li>
</ul>
| Easy | 78.2K | 118.2K | 78,199 | 118,222 | 66.1% | ['detect-capital', 'to-lower-case'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string capitalizeTitle(string title) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String capitalizeTitle(String title) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def capitalizeTitle(self, title):\n \"\"\"\n :type title: str\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def capitalizeTitle(self, title: str) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* capitalizeTitle(char* title) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string CapitalizeTitle(string title) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} title\n * @return {string}\n */\nvar capitalizeTitle = function(title) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function capitalizeTitle(title: string): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $title\n * @return String\n */\n function capitalizeTitle($title) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func capitalizeTitle(_ title: String) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun capitalizeTitle(title: String): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String capitalizeTitle(String title) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func capitalizeTitle(title string) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} title\n# @return {String}\ndef capitalize_title(title)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def capitalizeTitle(title: String): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn capitalize_title(title: String) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (capitalize-title title)\n (-> string? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec capitalize_title(Title :: unicode:unicode_binary()) -> unicode:unicode_binary().\ncapitalize_title(Title) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec capitalize_title(title :: String.t) :: String.t\n def capitalize_title(title) do\n \n end\nend"}] | "capiTalIze tHe titLe" | {
"name": "capitalizeTitle",
"params": [
{
"name": "title",
"type": "string"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String'] |
2,130 | Maximum Twin Sum of a Linked List | maximum-twin-sum-of-a-linked-list | <p>In a linked list of size <code>n</code>, where <code>n</code> is <strong>even</strong>, the <code>i<sup>th</sup></code> node (<strong>0-indexed</strong>) of the linked list is known as the <strong>twin</strong> of the <code>(n-1-i)<sup>th</sup></code> node, if <code>0 <= i <= (n / 2) - 1</code>.</p>
<ul>
<li>For example, if <code>n = 4</code>, then node <code>0</code> is the twin of node <code>3</code>, and node <code>1</code> is the twin of node <code>2</code>. These are the only nodes with twins for <code>n = 4</code>.</li>
</ul>
<p>The <strong>twin sum </strong>is defined as the sum of a node and its twin.</p>
<p>Given the <code>head</code> of a linked list with even length, return <em>the <strong>maximum twin sum</strong> of the linked list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png" style="width: 250px; height: 70px;" />
<pre>
<strong>Input:</strong> head = [5,4,2,1]
<strong>Output:</strong> 6
<strong>Explanation:</strong>
Nodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.
There are no other nodes with twins in the linked list.
Thus, the maximum twin sum of the linked list is 6.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png" style="width: 250px; height: 70px;" />
<pre>
<strong>Input:</strong> head = [4,2,2,3]
<strong>Output:</strong> 7
<strong>Explanation:</strong>
The nodes with twins present in this linked list are:
- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.
- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.
Thus, the maximum twin sum of the linked list is max(7, 4) = 7.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png" style="width: 200px; height: 88px;" />
<pre>
<strong>Input:</strong> head = [1,100000]
<strong>Output:</strong> 100001
<strong>Explanation:</strong>
There is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is an <strong>even</strong> integer in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
| Medium | 399K | 490.7K | 399,033 | 490,743 | 81.3% | ['reverse-linked-list', 'palindrome-linked-list', 'middle-of-the-linked-list'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n int pairSum(ListNode* head) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n public int pairSum(ListNode head) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution(object):\n def pairSum(self, head):\n \"\"\"\n :type head: Optional[ListNode]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def pairSum(self, head: Optional[ListNode]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * struct ListNode *next;\n * };\n */\nint pairSum(struct ListNode* head) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public int val;\n * public ListNode next;\n * public ListNode(int val=0, ListNode next=null) {\n * this.val = val;\n * this.next = next;\n * }\n * }\n */\npublic class Solution {\n public int PairSum(ListNode head) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {number}\n */\nvar pairSum = function(head) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode {\n * val: number\n * next: ListNode | null\n * constructor(val?: number, next?: ListNode | null) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n * }\n */\n\nfunction pairSum(head: ListNode | null): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "/**\n * Definition for a singly-linked list.\n * class ListNode {\n * public $val = 0;\n * public $next = null;\n * function __construct($val = 0, $next = null) {\n * $this->val = $val;\n * $this->next = $next;\n * }\n * }\n */\nclass Solution {\n\n /**\n * @param ListNode $head\n * @return Integer\n */\n function pairSum($head) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public var val: Int\n * public var next: ListNode?\n * public init() { self.val = 0; self.next = nil; }\n * public init(_ val: Int) { self.val = val; self.next = nil; }\n * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }\n * }\n */\nclass Solution {\n func pairSum(_ head: ListNode?) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "/**\n * Example:\n * var li = ListNode(5)\n * var v = li.`val`\n * Definition for singly-linked list.\n * class ListNode(var `val`: Int) {\n * var next: ListNode? = null\n * }\n */\nclass Solution {\n fun pairSum(head: ListNode?): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode {\n * int val;\n * ListNode? next;\n * ListNode([this.val = 0, this.next]);\n * }\n */\nclass Solution {\n int pairSum(ListNode? head) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "/**\n * Definition for singly-linked list.\n * type ListNode struct {\n * Val int\n * Next *ListNode\n * }\n */\nfunc pairSum(head *ListNode) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# Definition for singly-linked list.\n# class ListNode\n# attr_accessor :val, :next\n# def initialize(val = 0, _next = nil)\n# @val = val\n# @next = _next\n# end\n# end\n# @param {ListNode} head\n# @return {Integer}\ndef pair_sum(head)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode(_x: Int = 0, _next: ListNode = null) {\n * var next: ListNode = _next\n * var x: Int = _x\n * }\n */\nobject Solution {\n def pairSum(head: ListNode): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "// Definition for singly-linked list.\n// #[derive(PartialEq, Eq, Clone, Debug)]\n// pub struct ListNode {\n// pub val: i32,\n// pub next: Option<Box<ListNode>>\n// }\n// \n// impl ListNode {\n// #[inline]\n// fn new(val: i32) -> Self {\n// ListNode {\n// next: None,\n// val\n// }\n// }\n// }\nimpl Solution {\n pub fn pair_sum(head: Option<Box<ListNode>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "; Definition for singly-linked list:\n#|\n\n; val : integer?\n; next : (or/c list-node? #f)\n(struct list-node\n (val next) #:mutable #:transparent)\n\n; constructor\n(define (make-list-node [val 0])\n (list-node val #f))\n\n|#\n\n(define/contract (pair-sum head)\n (-> (or/c list-node? #f) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "%% Definition for singly-linked list.\n%%\n%% -record(list_node, {val = 0 :: integer(),\n%% next = null :: 'null' | #list_node{}}).\n\n-spec pair_sum(Head :: #list_node{} | null) -> integer().\npair_sum(Head) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "# Definition for singly-linked list.\n#\n# defmodule ListNode do\n# @type t :: %__MODULE__{\n# val: integer,\n# next: ListNode.t() | nil\n# }\n# defstruct val: 0, next: nil\n# end\n\ndefmodule Solution do\n @spec pair_sum(head :: ListNode.t | nil) :: integer\n def pair_sum(head) do\n \n end\nend"}] | [5,4,2,1] | {
"name": "pairSum",
"params": [
{
"name": "head",
"type": "ListNode"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Linked List', 'Two Pointers', 'Stack'] |
2,131 | Longest Palindrome by Concatenating Two Letter Words | longest-palindrome-by-concatenating-two-letter-words | <p>You are given an array of strings <code>words</code>. Each element of <code>words</code> consists of <strong>two</strong> lowercase English letters.</p>
<p>Create the <strong>longest possible palindrome</strong> by selecting some elements from <code>words</code> and concatenating them in <strong>any order</strong>. Each element can be selected <strong>at most once</strong>.</p>
<p>Return <em>the <strong>length</strong> of the longest palindrome that you can create</em>. If it is impossible to create any palindrome, return <code>0</code>.</p>
<p>A <strong>palindrome</strong> is a string that reads the same forward and backward.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["lc","cl","gg"]
<strong>Output:</strong> 6
<strong>Explanation:</strong> One longest palindrome is "lc" + "gg" + "cl" = "lcggcl", of length 6.
Note that "clgglc" is another longest palindrome that can be created.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["ab","ty","yt","lc","cl","ab"]
<strong>Output:</strong> 8
<strong>Explanation:</strong> One longest palindrome is "ty" + "lc" + "cl" + "yt" = "tylcclyt", of length 8.
Note that "lcyttycl" is another longest palindrome that can be created.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> words = ["cc","ll","xx"]
<strong>Output:</strong> 2
<strong>Explanation:</strong> One longest palindrome is "cc", of length 2.
Note that "ll" is another longest palindrome that can be created, and so is "xx".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 10<sup>5</sup></code></li>
<li><code>words[i].length == 2</code></li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>
| Medium | 128.8K | 269.1K | 128,761 | 269,143 | 47.8% | ['palindrome-pairs', 'longest-palindrome'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int longestPalindrome(vector<string>& words) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int longestPalindrome(String[] words) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestPalindrome(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def longestPalindrome(self, words: List[str]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int longestPalindrome(char** words, int wordsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LongestPalindrome(string[] words) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @return {number}\n */\nvar longestPalindrome = function(words) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestPalindrome(words: string[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @return Integer\n */\n function longestPalindrome($words) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestPalindrome(_ words: [String]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestPalindrome(words: Array<String>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int longestPalindrome(List<String> words) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestPalindrome(words []string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @return {Integer}\ndef longest_palindrome(words)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestPalindrome(words: Array[String]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_palindrome(words: Vec<String>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-palindrome words)\n (-> (listof string?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_palindrome(Words :: [unicode:unicode_binary()]) -> integer().\nlongest_palindrome(Words) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_palindrome(words :: [String.t]) :: integer\n def longest_palindrome(words) do\n \n end\nend"}] | ["lc","cl","gg"] | {
"name": "longestPalindrome",
"params": [
{
"name": "words",
"type": "string[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'String', 'Greedy', 'Counting'] |
2,132 | Stamping the Grid | stamping-the-grid | <p>You are given an <code>m x n</code> binary matrix <code>grid</code> where each cell is either <code>0</code> (empty) or <code>1</code> (occupied).</p>
<p>You are then given stamps of size <code>stampHeight x stampWidth</code>. We want to fit the stamps such that they follow the given <strong>restrictions</strong> and <strong>requirements</strong>:</p>
<ol>
<li>Cover all the <strong>empty</strong> cells.</li>
<li>Do not cover any of the <strong>occupied</strong> cells.</li>
<li>We can put as <strong>many</strong> stamps as we want.</li>
<li>Stamps can <strong>overlap</strong> with each other.</li>
<li>Stamps are not allowed to be <strong>rotated</strong>.</li>
<li>Stamps must stay completely <strong>inside</strong> the grid.</li>
</ol>
<p>Return <code>true</code> <em>if it is possible to fit the stamps while following the given restrictions and requirements. Otherwise, return</em> <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/ex1.png" style="width: 180px; height: 237px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]], stampHeight = 4, stampWidth = 3
<strong>Output:</strong> true
<strong>Explanation:</strong> We have two overlapping stamps (labeled 1 and 2 in the image) that are able to cover all the empty cells.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/11/03/ex2.png" style="width: 170px; height: 179px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]], stampHeight = 2, stampWidth = 2
<strong>Output:</strong> false
<strong>Explanation:</strong> There is no way to fit the stamps onto all the empty cells without the stamps going outside the grid.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 2 * 10<sup>5</sup></code></li>
<li><code>grid[r][c]</code> is either <code>0</code> or <code>1</code>.</li>
<li><code>1 <= stampHeight, stampWidth <= 10<sup>5</sup></code></li>
</ul>
| Hard | 8.4K | 25.5K | 8,360 | 25,503 | 32.8% | ['maximal-square', 'bomb-enemy', 'matrix-block-sum'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool possibleToStamp(vector<vector<int>>& grid, int stampHeight, int stampWidth) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean possibleToStamp(int[][] grid, int stampHeight, int stampWidth) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def possibleToStamp(self, grid, stampHeight, stampWidth):\n \"\"\"\n :type grid: List[List[int]]\n :type stampHeight: int\n :type stampWidth: int\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def possibleToStamp(self, grid: List[List[int]], stampHeight: int, stampWidth: int) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool possibleToStamp(int** grid, int gridSize, int* gridColSize, int stampHeight, int stampWidth) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool PossibleToStamp(int[][] grid, int stampHeight, int stampWidth) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @param {number} stampHeight\n * @param {number} stampWidth\n * @return {boolean}\n */\nvar possibleToStamp = function(grid, stampHeight, stampWidth) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function possibleToStamp(grid: number[][], stampHeight: number, stampWidth: number): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @param Integer $stampHeight\n * @param Integer $stampWidth\n * @return Boolean\n */\n function possibleToStamp($grid, $stampHeight, $stampWidth) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func possibleToStamp(_ grid: [[Int]], _ stampHeight: Int, _ stampWidth: Int) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun possibleToStamp(grid: Array<IntArray>, stampHeight: Int, stampWidth: Int): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool possibleToStamp(List<List<int>> grid, int stampHeight, int stampWidth) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func possibleToStamp(grid [][]int, stampHeight int, stampWidth int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @param {Integer} stamp_height\n# @param {Integer} stamp_width\n# @return {Boolean}\ndef possible_to_stamp(grid, stamp_height, stamp_width)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def possibleToStamp(grid: Array[Array[Int]], stampHeight: Int, stampWidth: Int): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn possible_to_stamp(grid: Vec<Vec<i32>>, stamp_height: i32, stamp_width: i32) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (possible-to-stamp grid stampHeight stampWidth)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec possible_to_stamp(Grid :: [[integer()]], StampHeight :: integer(), StampWidth :: integer()) -> boolean().\npossible_to_stamp(Grid, StampHeight, StampWidth) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec possible_to_stamp(grid :: [[integer]], stamp_height :: integer, stamp_width :: integer) :: boolean\n def possible_to_stamp(grid, stamp_height, stamp_width) do\n \n end\nend"}] | [[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0],[1,0,0,0]]
4
3 | {
"name": "possibleToStamp",
"params": [
{
"name": "grid",
"type": "integer[][]"
},
{
"type": "integer",
"name": "stampHeight"
},
{
"type": "integer",
"name": "stampWidth"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy', 'Matrix', 'Prefix Sum'] |
2,133 | Check if Every Row and Column Contains All Numbers | check-if-every-row-and-column-contains-all-numbers | <p>An <code>n x n</code> matrix is <strong>valid</strong> if every row and every column contains <strong>all</strong> the integers from <code>1</code> to <code>n</code> (<strong>inclusive</strong>).</p>
<p>Given an <code>n x n</code> integer matrix <code>matrix</code>, return <code>true</code> <em>if the matrix is <strong>valid</strong>.</em> Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/example1drawio.png" style="width: 250px; height: 251px;" />
<pre>
<strong>Input:</strong> matrix = [[1,2,3],[3,1,2],[2,3,1]]
<strong>Output:</strong> true
<strong>Explanation:</strong> In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
Hence, we return true.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/example2drawio.png" style="width: 250px; height: 251px;" />
<pre>
<strong>Input:</strong> matrix = [[1,1,1],[1,2,3],[1,2,3]]
<strong>Output:</strong> false
<strong>Explanation:</strong> In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
Hence, we return false.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == matrix.length == matrix[i].length</code></li>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= matrix[i][j] <= n</code></li>
</ul>
| Easy | 94K | 178.7K | 93,956 | 178,689 | 52.6% | ['valid-sudoku', 'matrix-diagonal-sum', 'first-completely-painted-row-or-column'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool checkValid(vector<vector<int>>& matrix) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean checkValid(int[][] matrix) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def checkValid(self, matrix):\n \"\"\"\n :type matrix: List[List[int]]\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def checkValid(self, matrix: List[List[int]]) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool checkValid(int** matrix, int matrixSize, int* matrixColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool CheckValid(int[][] matrix) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} matrix\n * @return {boolean}\n */\nvar checkValid = function(matrix) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function checkValid(matrix: number[][]): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $matrix\n * @return Boolean\n */\n function checkValid($matrix) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func checkValid(_ matrix: [[Int]]) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun checkValid(matrix: Array<IntArray>): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool checkValid(List<List<int>> matrix) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func checkValid(matrix [][]int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} matrix\n# @return {Boolean}\ndef check_valid(matrix)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def checkValid(matrix: Array[Array[Int]]): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn check_valid(matrix: Vec<Vec<i32>>) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (check-valid matrix)\n (-> (listof (listof exact-integer?)) boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec check_valid(Matrix :: [[integer()]]) -> boolean().\ncheck_valid(Matrix) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec check_valid(matrix :: [[integer]]) :: boolean\n def check_valid(matrix) do\n \n end\nend"}] | [[1,2,3],[3,1,2],[2,3,1]] | {
"name": "checkValid",
"params": [
{
"name": "matrix",
"type": "integer[][]"
}
],
"return": {
"type": "boolean"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Matrix'] |
2,134 | Minimum Swaps to Group All 1's Together II | minimum-swaps-to-group-all-1s-together-ii | <p>A <strong>swap</strong> is defined as taking two <strong>distinct</strong> positions in an array and swapping the values in them.</p>
<p>A <strong>circular</strong> array is defined as an array where we consider the <strong>first</strong> element and the <strong>last</strong> element to be <strong>adjacent</strong>.</p>
<p>Given a <strong>binary</strong> <strong>circular</strong> array <code>nums</code>, return <em>the minimum number of swaps required to group all </em><code>1</code><em>'s present in the array together at <strong>any location</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,0,1,1,0,0]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Here are a few of the ways to group all the 1's together:
[0,<u>0</u>,<u>1</u>,1,1,0,0] using 1 swap.
[0,1,<u>1</u>,1,<u>0</u>,0,0] using 1 swap.
[1,1,0,0,0,0,1] using 2 swaps (using the circular property of the array).
There is no way to group all 1's together with 0 swaps.
Thus, the minimum number of swaps required is 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1,1,0,0,1,1,0]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Here are a few of the ways to group all the 1's together:
[1,1,1,0,0,0,0,1,1] using 2 swaps (using the circular property of the array).
[1,1,1,1,1,0,0,0,0] using 2 swaps.
There is no way to group all 1's together with 0 or 1 swaps.
Thus, the minimum number of swaps required is 2.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,0,0,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All the 1's are already grouped together due to the circular property of the array.
Thus, the minimum number of swaps required is 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| Medium | 165.6K | 252.9K | 165,599 | 252,920 | 65.5% | ['minimum-swaps-to-group-all-1s-together', 'time-needed-to-rearrange-a-binary-string'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minSwaps(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minSwaps(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minSwaps(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minSwaps(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minSwaps(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinSwaps(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minSwaps = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minSwaps(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minSwaps($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minSwaps(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minSwaps(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minSwaps(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minSwaps(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef min_swaps(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minSwaps(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_swaps(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-swaps nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_swaps(Nums :: [integer()]) -> integer().\nmin_swaps(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_swaps(nums :: [integer]) :: integer\n def min_swaps(nums) do\n \n end\nend"}] | [0,1,0,1,1,0,0] | {
"name": "minSwaps",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Sliding Window'] |
2,135 | Count Words Obtained After Adding a Letter | count-words-obtained-after-adding-a-letter | <p>You are given two <strong>0-indexed</strong> arrays of strings <code>startWords</code> and <code>targetWords</code>. Each string consists of <strong>lowercase English letters</strong> only.</p>
<p>For each string in <code>targetWords</code>, check if it is possible to choose a string from <code>startWords</code> and perform a <strong>conversion operation</strong> on it to be equal to that from <code>targetWords</code>.</p>
<p>The <strong>conversion operation</strong> is described in the following two steps:</p>
<ol>
<li><strong>Append</strong> any lowercase letter that is <strong>not present</strong> in the string to its end.
<ul>
<li>For example, if the string is <code>"abc"</code>, the letters <code>'d'</code>, <code>'e'</code>, or <code>'y'</code> can be added to it, but not <code>'a'</code>. If <code>'d'</code> is added, the resulting string will be <code>"abcd"</code>.</li>
</ul>
</li>
<li><strong>Rearrange</strong> the letters of the new string in <strong>any</strong> arbitrary order.
<ul>
<li>For example, <code>"abcd"</code> can be rearranged to <code>"acbd"</code>, <code>"bacd"</code>, <code>"cbda"</code>, and so on. Note that it can also be rearranged to <code>"abcd"</code> itself.</li>
</ul>
</li>
</ol>
<p>Return <em>the <strong>number of strings</strong> in </em><code>targetWords</code><em> that can be obtained by performing the operations on <strong>any</strong> string of </em><code>startWords</code>.</p>
<p><strong>Note</strong> that you will only be verifying if the string in <code>targetWords</code> can be obtained from a string in <code>startWords</code> by performing the operations. The strings in <code>startWords</code> <strong>do not</strong> actually change during this process.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> startWords = ["ant","act","tack"], targetWords = ["tack","act","acti"]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
- In order to form targetWords[0] = "tack", we use startWords[1] = "act", append 'k' to it, and rearrange "actk" to "tack".
- There is no string in startWords that can be used to obtain targetWords[1] = "act".
Note that "act" does exist in startWords, but we <strong>must</strong> append one letter to the string before rearranging it.
- In order to form targetWords[2] = "acti", we use startWords[1] = "act", append 'i' to it, and rearrange "acti" to "acti" itself.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> startWords = ["ab","a"], targetWords = ["abc","abcd"]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- In order to form targetWords[0] = "abc", we use startWords[0] = "ab", add 'c' to it, and rearrange it to "abc".
- There is no string in startWords that can be used to obtain targetWords[1] = "abcd".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= startWords.length, targetWords.length <= 5 * 10<sup>4</sup></code></li>
<li><code>1 <= startWords[i].length, targetWords[j].length <= 26</code></li>
<li>Each string of <code>startWords</code> and <code>targetWords</code> consists of lowercase English letters only.</li>
<li>No letter occurs more than once in any string of <code>startWords</code> or <code>targetWords</code>.</li>
</ul>
| Medium | 37.5K | 86.5K | 37,503 | 86,507 | 43.4% | ['strings-differ-by-one-character', 'count-substrings-that-differ-by-one-character', 'maximum-score-from-removing-substrings'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int wordCount(vector<string>& startWords, vector<string>& targetWords) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int wordCount(String[] startWords, String[] targetWords) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def wordCount(self, startWords, targetWords):\n \"\"\"\n :type startWords: List[str]\n :type targetWords: List[str]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def wordCount(self, startWords: List[str], targetWords: List[str]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int wordCount(char** startWords, int startWordsSize, char** targetWords, int targetWordsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int WordCount(string[] startWords, string[] targetWords) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} startWords\n * @param {string[]} targetWords\n * @return {number}\n */\nvar wordCount = function(startWords, targetWords) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function wordCount(startWords: string[], targetWords: string[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $startWords\n * @param String[] $targetWords\n * @return Integer\n */\n function wordCount($startWords, $targetWords) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func wordCount(_ startWords: [String], _ targetWords: [String]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun wordCount(startWords: Array<String>, targetWords: Array<String>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int wordCount(List<String> startWords, List<String> targetWords) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func wordCount(startWords []string, targetWords []string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} start_words\n# @param {String[]} target_words\n# @return {Integer}\ndef word_count(start_words, target_words)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def wordCount(startWords: Array[String], targetWords: Array[String]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn word_count(start_words: Vec<String>, target_words: Vec<String>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (word-count startWords targetWords)\n (-> (listof string?) (listof string?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec word_count(StartWords :: [unicode:unicode_binary()], TargetWords :: [unicode:unicode_binary()]) -> integer().\nword_count(StartWords, TargetWords) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec word_count(start_words :: [String.t], target_words :: [String.t]) :: integer\n def word_count(start_words, target_words) do\n \n end\nend"}] | ["ant","act","tack"]
["tack","act","acti"] | {
"name": "wordCount",
"params": [
{
"name": "startWords",
"type": "string[]"
},
{
"type": "string[]",
"name": "targetWords"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'String', 'Bit Manipulation', 'Sorting'] |
2,136 | Earliest Possible Day of Full Bloom | earliest-possible-day-of-full-bloom | <p>You have <code>n</code> flower seeds. Every seed must be planted first before it can begin to grow, then bloom. Planting a seed takes time and so does the growth of a seed. You are given two <strong>0-indexed</strong> integer arrays <code>plantTime</code> and <code>growTime</code>, of length <code>n</code> each:</p>
<ul>
<li><code>plantTime[i]</code> is the number of <strong>full days</strong> it takes you to <strong>plant</strong> the <code>i<sup>th</sup></code> seed. Every day, you can work on planting exactly one seed. You <strong>do not</strong> have to work on planting the same seed on consecutive days, but the planting of a seed is not complete <strong>until</strong> you have worked <code>plantTime[i]</code> days on planting it in total.</li>
<li><code>growTime[i]</code> is the number of <strong>full days</strong> it takes the <code>i<sup>th</sup></code> seed to grow after being completely planted. <strong>After</strong> the last day of its growth, the flower <strong>blooms</strong> and stays bloomed forever.</li>
</ul>
<p>From the beginning of day <code>0</code>, you can plant the seeds in <strong>any</strong> order.</p>
<p>Return <em>the <strong>earliest</strong> possible day where <strong>all</strong> seeds are blooming</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/1.png" style="width: 453px; height: 149px;" />
<pre>
<strong>Input:</strong> plantTime = [1,4,3], growTime = [2,3,1]
<strong>Output:</strong> 9
<strong>Explanation:</strong> The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 0, plant the 0<sup>th</sup> seed. The seed grows for 2 full days and blooms on day 3.
On days 1, 2, 3, and 4, plant the 1<sup>st</sup> seed. The seed grows for 3 full days and blooms on day 8.
On days 5, 6, and 7, plant the 2<sup>nd</sup> seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/21/2.png" style="width: 454px; height: 184px;" />
<pre>
<strong>Input:</strong> plantTime = [1,2,3,2], growTime = [2,1,2,1]
<strong>Output:</strong> 9
<strong>Explanation:</strong> The grayed out pots represent planting days, colored pots represent growing days, and the flower represents the day it blooms.
One optimal way is:
On day 1, plant the 0<sup>th</sup> seed. The seed grows for 2 full days and blooms on day 4.
On days 0 and 3, plant the 1<sup>st</sup> seed. The seed grows for 1 full day and blooms on day 5.
On days 2, 4, and 5, plant the 2<sup>nd</sup> seed. The seed grows for 2 full days and blooms on day 8.
On days 6 and 7, plant the 3<sup>rd</sup> seed. The seed grows for 1 full day and blooms on day 9.
Thus, on day 9, all the seeds are blooming.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> plantTime = [1], growTime = [1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> On day 0, plant the 0<sup>th</sup> seed. The seed grows for 1 full day and blooms on day 2.
Thus, on day 2, all the seeds are blooming.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == plantTime.length == growTime.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= plantTime[i], growTime[i] <= 10<sup>4</sup></code></li>
</ul>
| Hard | 58.3K | 82.1K | 58,344 | 82,104 | 71.1% | ['minimum-number-of-days-to-make-m-bouquets'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int earliestFullBloom(vector<int>& plantTime, vector<int>& growTime) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int earliestFullBloom(int[] plantTime, int[] growTime) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def earliestFullBloom(self, plantTime, growTime):\n \"\"\"\n :type plantTime: List[int]\n :type growTime: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def earliestFullBloom(self, plantTime: List[int], growTime: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int earliestFullBloom(int* plantTime, int plantTimeSize, int* growTime, int growTimeSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int EarliestFullBloom(int[] plantTime, int[] growTime) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} plantTime\n * @param {number[]} growTime\n * @return {number}\n */\nvar earliestFullBloom = function(plantTime, growTime) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function earliestFullBloom(plantTime: number[], growTime: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $plantTime\n * @param Integer[] $growTime\n * @return Integer\n */\n function earliestFullBloom($plantTime, $growTime) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func earliestFullBloom(_ plantTime: [Int], _ growTime: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun earliestFullBloom(plantTime: IntArray, growTime: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int earliestFullBloom(List<int> plantTime, List<int> growTime) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func earliestFullBloom(plantTime []int, growTime []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} plant_time\n# @param {Integer[]} grow_time\n# @return {Integer}\ndef earliest_full_bloom(plant_time, grow_time)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def earliestFullBloom(plantTime: Array[Int], growTime: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn earliest_full_bloom(plant_time: Vec<i32>, grow_time: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (earliest-full-bloom plantTime growTime)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec earliest_full_bloom(PlantTime :: [integer()], GrowTime :: [integer()]) -> integer().\nearliest_full_bloom(PlantTime, GrowTime) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec earliest_full_bloom(plant_time :: [integer], grow_time :: [integer]) :: integer\n def earliest_full_bloom(plant_time, grow_time) do\n \n end\nend"}] | [1,4,3]
[2,3,1] | {
"name": "earliestFullBloom",
"params": [
{
"name": "plantTime",
"type": "integer[]"
},
{
"type": "integer[]",
"name": "growTime"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy', 'Sorting'] |
2,137 | Pour Water Between Buckets to Make Water Levels Equal | pour-water-between-buckets-to-make-water-levels-equal | null | Medium | 3.1K | 4.7K | 3,109 | 4,650 | 66.9% | ['search-in-rotated-sorted-array', 'find-peak-element', 'minimum-moves-to-equal-array-elements'] | [] | Algorithms | null | [1,2,7]
80 | {
"name": "equalizeWater",
"params": [
{
"name": "buckets",
"type": "integer[]"
},
{
"type": "integer",
"name": "loss"
}
],
"return": {
"type": "double"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search'] |
2,138 | Divide a String Into Groups of Size k | divide-a-string-into-groups-of-size-k | <p>A string <code>s</code> can be partitioned into groups of size <code>k</code> using the following procedure:</p>
<ul>
<li>The first group consists of the first <code>k</code> characters of the string, the second group consists of the next <code>k</code> characters of the string, and so on. Each element can be a part of <strong>exactly one</strong> group.</li>
<li>For the last group, if the string <strong>does not</strong> have <code>k</code> characters remaining, a character <code>fill</code> is used to complete the group.</li>
</ul>
<p>Note that the partition is done so that after removing the <code>fill</code> character from the last group (if it exists) and concatenating all the groups in order, the resultant string should be <code>s</code>.</p>
<p>Given the string <code>s</code>, the size of each group <code>k</code> and the character <code>fill</code>, return <em>a string array denoting the <strong>composition of every group</strong> </em><code>s</code><em> has been divided into, using the above procedure</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abcdefghi", k = 3, fill = "x"
<strong>Output:</strong> ["abc","def","ghi"]
<strong>Explanation:</strong>
The first 3 characters "abc" form the first group.
The next 3 characters "def" form the second group.
The last 3 characters "ghi" form the third group.
Since all groups can be completely filled by characters from the string, we do not need to use fill.
Thus, the groups formed are "abc", "def", and "ghi".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "abcdefghij", k = 3, fill = "x"
<strong>Output:</strong> ["abc","def","ghi","jxx"]
<strong>Explanation:</strong>
Similar to the previous example, we are forming the first three groups "abc", "def", and "ghi".
For the last group, we can only use the character 'j' from the string. To complete this group, we add 'x' twice.
Thus, the 4 groups formed are "abc", "def", "ghi", and "jxx".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists of lowercase English letters only.</li>
<li><code>1 <= k <= 100</code></li>
<li><code>fill</code> is a lowercase English letter.</li>
</ul>
| Easy | 54.3K | 80.6K | 54,261 | 80,602 | 67.3% | ['text-justification', 'positions-of-large-groups'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<string> divideString(string s, int k, char fill) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String[] divideString(String s, int k, char fill) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def divideString(self, s, k, fill):\n \"\"\"\n :type s: str\n :type k: int\n :type fill: str\n :rtype: List[str]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def divideString(self, s: str, k: int, fill: str) -> List[str]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nchar** divideString(char* s, int k, char fill, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string[] DivideString(string s, int k, char fill) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} k\n * @param {character} fill\n * @return {string[]}\n */\nvar divideString = function(s, k, fill) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function divideString(s: string, k: number, fill: string): string[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @param String $fill\n * @return String[]\n */\n function divideString($s, $k, $fill) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func divideString(_ s: String, _ k: Int, _ fill: Character) -> [String] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun divideString(s: String, k: Int, fill: Char): Array<String> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<String> divideString(String s, int k, String fill) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func divideString(s string, k int, fill byte) []string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @param {Character} fill\n# @return {String[]}\ndef divide_string(s, k, fill)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def divideString(s: String, k: Int, fill: Char): Array[String] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn divide_string(s: String, k: i32, fill: char) -> Vec<String> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (divide-string s k fill)\n (-> string? exact-integer? char? (listof string?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec divide_string(S :: unicode:unicode_binary(), K :: integer(), Fill :: char()) -> [unicode:unicode_binary()].\ndivide_string(S, K, Fill) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec divide_string(s :: String.t, k :: integer, fill :: char) :: [String.t]\n def divide_string(s, k, fill) do\n \n end\nend"}] | "abcdefghi"
3
"x" | {
"name": "divideString",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "integer",
"name": "k"
},
{
"type": "character",
"name": "fill"
}
],
"return": {
"type": "string[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Simulation'] |
2,139 | Minimum Moves to Reach Target Score | minimum-moves-to-reach-target-score | <p>You are playing a game with integers. You start with the integer <code>1</code> and you want to reach the integer <code>target</code>.</p>
<p>In one move, you can either:</p>
<ul>
<li><strong>Increment</strong> the current integer by one (i.e., <code>x = x + 1</code>).</li>
<li><strong>Double</strong> the current integer (i.e., <code>x = 2 * x</code>).</li>
</ul>
<p>You can use the <strong>increment</strong> operation <strong>any</strong> number of times, however, you can only use the <strong>double</strong> operation <strong>at most</strong> <code>maxDoubles</code> times.</p>
<p>Given the two integers <code>target</code> and <code>maxDoubles</code>, return <em>the minimum number of moves needed to reach </em><code>target</code><em> starting with </em><code>1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> target = 5, maxDoubles = 0
<strong>Output:</strong> 4
<strong>Explanation:</strong> Keep incrementing by 1 until you reach target.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> target = 19, maxDoubles = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong> Initially, x = 1
Increment 3 times so x = 4
Double once so x = 8
Increment once so x = 9
Double again so x = 18
Increment once so x = 19
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> target = 10, maxDoubles = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong><b> </b>Initially, x = 1
Increment once so x = 2
Double once so x = 4
Increment once so x = 5
Double again so x = 10
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= target <= 10<sup>9</sup></code></li>
<li><code>0 <= maxDoubles <= 100</code></li>
</ul>
| Medium | 50.2K | 97.8K | 50,223 | 97,809 | 51.3% | ['number-of-steps-to-reduce-a-number-to-zero', 'number-of-steps-to-reduce-a-number-in-binary-representation-to-one'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minMoves(int target, int maxDoubles) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minMoves(int target, int maxDoubles) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMoves(self, target, maxDoubles):\n \"\"\"\n :type target: int\n :type maxDoubles: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minMoves(self, target: int, maxDoubles: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minMoves(int target, int maxDoubles) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinMoves(int target, int maxDoubles) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} target\n * @param {number} maxDoubles\n * @return {number}\n */\nvar minMoves = function(target, maxDoubles) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMoves(target: number, maxDoubles: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $target\n * @param Integer $maxDoubles\n * @return Integer\n */\n function minMoves($target, $maxDoubles) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMoves(_ target: Int, _ maxDoubles: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMoves(target: Int, maxDoubles: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMoves(int target, int maxDoubles) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMoves(target int, maxDoubles int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} target\n# @param {Integer} max_doubles\n# @return {Integer}\ndef min_moves(target, max_doubles)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMoves(target: Int, maxDoubles: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_moves(target: i32, max_doubles: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-moves target maxDoubles)\n (-> exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_moves(Target :: integer(), MaxDoubles :: integer()) -> integer().\nmin_moves(Target, MaxDoubles) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_moves(target :: integer, max_doubles :: integer) :: integer\n def min_moves(target, max_doubles) do\n \n end\nend"}] | 5
0 | {
"name": "minMoves",
"params": [
{
"name": "target",
"type": "integer"
},
{
"type": "integer",
"name": "maxDoubles"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Greedy'] |
2,140 | Solving Questions With Brainpower | solving-questions-with-brainpower | <p>You are given a <strong>0-indexed</strong> 2D integer array <code>questions</code> where <code>questions[i] = [points<sub>i</sub>, brainpower<sub>i</sub>]</code>.</p>
<p>The array describes the questions of an exam, where you have to process the questions <strong>in order</strong> (i.e., starting from question <code>0</code>) and make a decision whether to <strong>solve</strong> or <strong>skip</strong> each question. Solving question <code>i</code> will <strong>earn</strong> you <code>points<sub>i</sub></code> points but you will be <strong>unable</strong> to solve each of the next <code>brainpower<sub>i</sub></code> questions. If you skip question <code>i</code>, you get to make the decision on the next question.</p>
<ul>
<li>For example, given <code>questions = [[3, 2], [4, 3], [4, 4], [2, 5]]</code>:
<ul>
<li>If question <code>0</code> is solved, you will earn <code>3</code> points but you will be unable to solve questions <code>1</code> and <code>2</code>.</li>
<li>If instead, question <code>0</code> is skipped and question <code>1</code> is solved, you will earn <code>4</code> points but you will be unable to solve questions <code>2</code> and <code>3</code>.</li>
</ul>
</li>
</ul>
<p>Return <em>the <strong>maximum</strong> points you can earn for the exam</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> questions = [[3,2],[4,3],[4,4],[2,5]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= questions.length <= 10<sup>5</sup></code></li>
<li><code>questions[i].length == 2</code></li>
<li><code>1 <= points<sub>i</sub>, brainpower<sub>i</sub> <= 10<sup>5</sup></code></li>
</ul>
| Medium | 232.7K | 385.6K | 232,717 | 385,571 | 60.4% | ['house-robber', 'frog-jump'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long mostPoints(vector<vector<int>>& questions) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long mostPoints(int[][] questions) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def mostPoints(self, questions):\n \"\"\"\n :type questions: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def mostPoints(self, questions: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long mostPoints(int** questions, int questionsSize, int* questionsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MostPoints(int[][] questions) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} questions\n * @return {number}\n */\nvar mostPoints = function(questions) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function mostPoints(questions: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $questions\n * @return Integer\n */\n function mostPoints($questions) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func mostPoints(_ questions: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun mostPoints(questions: Array<IntArray>): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int mostPoints(List<List<int>> questions) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func mostPoints(questions [][]int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} questions\n# @return {Integer}\ndef most_points(questions)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def mostPoints(questions: Array[Array[Int]]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn most_points(questions: Vec<Vec<i32>>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (most-points questions)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec most_points(Questions :: [[integer()]]) -> integer().\nmost_points(Questions) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec most_points(questions :: [[integer]]) :: integer\n def most_points(questions) do\n \n end\nend"}] | [[3,2],[4,3],[4,4],[2,5]] | {
"name": "mostPoints",
"params": [
{
"name": "questions",
"type": "integer[][]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming'] |
2,141 | Maximum Running Time of N Computers | maximum-running-time-of-n-computers | <p>You have <code>n</code> computers. You are given the integer <code>n</code> and a <strong>0-indexed</strong> integer array <code>batteries</code> where the <code>i<sup>th</sup></code> battery can <strong>run</strong> a computer for <code>batteries[i]</code> minutes. You are interested in running <strong>all</strong> <code>n</code> computers <strong>simultaneously</strong> using the given batteries.</p>
<p>Initially, you can insert <strong>at most one battery</strong> into each computer. After that and at any integer time moment, you can remove a battery from a computer and insert another battery <strong>any number of times</strong>. The inserted battery can be a totally new battery or a battery from another computer. You may assume that the removing and inserting processes take no time.</p>
<p>Note that the batteries cannot be recharged.</p>
<p>Return <em>the <strong>maximum</strong> number of minutes you can run all the </em><code>n</code><em> computers simultaneously.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/06/example1-fit.png" style="width: 762px; height: 150px;" />
<pre>
<strong>Input:</strong> n = 2, batteries = [3,3,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Initially, insert battery 0 into the first computer and battery 1 into the second computer.
After two minutes, remove battery 1 from the second computer and insert battery 2 instead. Note that battery 1 can still run for one minute.
At the end of the third minute, battery 0 is drained, and you need to remove it from the first computer and insert battery 1 instead.
By the end of the fourth minute, battery 1 is also drained, and the first computer is no longer running.
We can run the two computers simultaneously for at most 4 minutes, so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/06/example2.png" style="width: 629px; height: 150px;" />
<pre>
<strong>Input:</strong> n = 2, batteries = [1,1,1,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
Initially, insert battery 0 into the first computer and battery 2 into the second computer.
After one minute, battery 0 and battery 2 are drained so you need to remove them and insert battery 1 into the first computer and battery 3 into the second computer.
After another minute, battery 1 and battery 3 are also drained so the first and second computers are no longer running.
We can run the two computers simultaneously for at most 2 minutes, so we return 2.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= batteries.length <= 10<sup>5</sup></code></li>
<li><code>1 <= batteries[i] <= 10<sup>9</sup></code></li>
</ul>
| Hard | 64.1K | 128.6K | 64,112 | 128,646 | 49.8% | ['minimum-moves-to-equal-array-elements', 'sell-diminishing-valued-colored-balls', 'maximum-number-of-tasks-you-can-assign', 'minimum-time-to-complete-trips', 'minimum-amount-of-time-to-fill-cups'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxRunTime(int n, vector<int>& batteries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxRunTime(int n, int[] batteries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxRunTime(self, n, batteries):\n \"\"\"\n :type n: int\n :type batteries: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxRunTime(self, n: int, batteries: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxRunTime(int n, int* batteries, int batteriesSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxRunTime(int n, int[] batteries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[]} batteries\n * @return {number}\n */\nvar maxRunTime = function(n, batteries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxRunTime(n: number, batteries: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[] $batteries\n * @return Integer\n */\n function maxRunTime($n, $batteries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxRunTime(_ n: Int, _ batteries: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxRunTime(n: Int, batteries: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxRunTime(int n, List<int> batteries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxRunTime(n int, batteries []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[]} batteries\n# @return {Integer}\ndef max_run_time(n, batteries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxRunTime(n: Int, batteries: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_run_time(n: i32, batteries: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-run-time n batteries)\n (-> exact-integer? (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_run_time(N :: integer(), Batteries :: [integer()]) -> integer().\nmax_run_time(N, Batteries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_run_time(n :: integer, batteries :: [integer]) :: integer\n def max_run_time(n, batteries) do\n \n end\nend"}] | 2
[3,3,3] | {
"name": "maxRunTime",
"params": [
{
"name": "n",
"type": "integer"
},
{
"type": "integer[]",
"name": "batteries"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search', 'Greedy', 'Sorting'] |
2,142 | The Number of Passengers in Each Bus I | the-number-of-passengers-in-each-bus-i | null | Medium | 10.5K | 21.6K | 10,527 | 21,566 | 48.8% | [] | ['Create table If Not Exists Buses (bus_id int, arrival_time int)', 'Create table If Not Exists Passengers (passenger_id int, arrival_time int)', 'Truncate table Buses', "insert into Buses (bus_id, arrival_time) values ('1', '2')", "insert into Buses (bus_id, arrival_time) values ('2', '4')", "insert into Buses (bus_id, arrival_time) values ('3', '7')", 'Truncate table Passengers', "insert into Passengers (passenger_id, arrival_time) values ('11', '1')", "insert into Passengers (passenger_id, arrival_time) values ('12', '5')", "insert into Passengers (passenger_id, arrival_time) values ('13', '6')", "insert into Passengers (passenger_id, arrival_time) values ('14', '7')"] | Database | null | {"headers":{"Buses":["bus_id","arrival_time"],"Passengers":["passenger_id","arrival_time"]},"rows":{"Buses":[[1,2],[2,4],[3,7]],"Passengers":[[11,1],[12,5],[13,6],[14,7]]}} | {"mysql": ["Create table If Not Exists Buses (bus_id int, arrival_time int)", "Create table If Not Exists Passengers (passenger_id int, arrival_time int)"], "mssql": ["Create table Buses (bus_id int, arrival_time int)", "Create table Passengers (passenger_id int, arrival_time int)"], "oraclesql": ["Create table Buses (bus_id int, arrival_time int)", "Create table Passengers (passenger_id int, arrival_time int)"], "database": true, "name": "count_passengers_in_bus", "pythondata": ["Buses = pd.DataFrame([], columns=['bus_id', 'arrival_time']).astype({'bus_id':'Int64', 'arrival_time':'Int64'})", "Passengers = pd.DataFrame([], columns=['passenger_id', 'arrival_time']).astype({'passenger_id':'Int64', 'arrival_time':'Int64'})"], "postgresql": ["Create table If Not Exists Buses (bus_id int, arrival_time int)", "Create table If Not Exists Passengers (passenger_id int, arrival_time int)"], "database_schema": {"Buses": {"bus_id": "INT", "arrival_time": "INT"}, "Passengers": {"passenger_id": "INT", "arrival_time": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,143 | Choose Numbers From Two Arrays in Range | choose-numbers-from-two-arrays-in-range | null | Hard | 1.2K | 2.4K | 1,244 | 2,377 | 52.3% | ['intersection-of-two-arrays', 'intersection-of-two-arrays-ii', 'minimum-xor-sum-of-two-arrays', 'minimize-product-sum-of-two-arrays'] | [] | Algorithms | null | [1,2,5]
[2,6,3] | {
"name": "countSubranges",
"params": [
{
"name": "nums1",
"type": "integer[]"
},
{
"type": "integer[]",
"name": "nums2"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming'] |
2,144 | Minimum Cost of Buying Candies With Discount | minimum-cost-of-buying-candies-with-discount | <p>A shop is selling candies at a discount. For <strong>every two</strong> candies sold, the shop gives a <strong>third</strong> candy for <strong>free</strong>.</p>
<p>The customer can choose <strong>any</strong> candy to take away for free as long as the cost of the chosen candy is less than or equal to the <strong>minimum</strong> cost of the two candies bought.</p>
<ul>
<li>For example, if there are <code>4</code> candies with costs <code>1</code>, <code>2</code>, <code>3</code>, and <code>4</code>, and the customer buys candies with costs <code>2</code> and <code>3</code>, they can take the candy with cost <code>1</code> for free, but not the candy with cost <code>4</code>.</li>
</ul>
<p>Given a <strong>0-indexed</strong> integer array <code>cost</code>, where <code>cost[i]</code> denotes the cost of the <code>i<sup>th</sup></code> candy, return <em>the <strong>minimum cost</strong> of buying <strong>all</strong> the candies</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> cost = [1,2,3]
<strong>Output:</strong> 5
<strong>Explanation:</strong> We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
The total cost of buying all candies is 2 + 3 = 5. This is the <strong>only</strong> way we can buy the candies.
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> cost = [6,5,7,9,2,2]
<strong>Output:</strong> 23
<strong>Explanation:</strong> The way in which we can get the minimum cost is described below:
- Buy candies with costs 9 and 7
- Take the candy with cost 6 for free
- We buy candies with costs 5 and 2
- Take the last remaining candy with cost 2 for free
Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> cost = [5,5]
<strong>Output:</strong> 10
<strong>Explanation:</strong> Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
Hence, the minimum cost to buy all candies is 5 + 5 = 10.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= cost.length <= 100</code></li>
<li><code>1 <= cost[i] <= 100</code></li>
</ul>
| Easy | 63K | 101.8K | 62,984 | 101,752 | 61.9% | ['array-partition', 'minimum-absolute-difference', 'minimum-number-of-operations-to-satisfy-conditions', 'check-if-grid-satisfies-conditions'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumCost(vector<int>& cost) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumCost(int[] cost) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumCost(self, cost):\n \"\"\"\n :type cost: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumCost(self, cost: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumCost(int* cost, int costSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumCost(int[] cost) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} cost\n * @return {number}\n */\nvar minimumCost = function(cost) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumCost(cost: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $cost\n * @return Integer\n */\n function minimumCost($cost) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumCost(_ cost: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumCost(cost: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumCost(List<int> cost) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumCost(cost []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} cost\n# @return {Integer}\ndef minimum_cost(cost)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumCost(cost: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_cost(cost: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-cost cost)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_cost(Cost :: [integer()]) -> integer().\nminimum_cost(Cost) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_cost(cost :: [integer]) :: integer\n def minimum_cost(cost) do\n \n end\nend"}] | [1,2,3] | {
"name": "minimumCost",
"params": [
{
"name": "cost",
"type": "integer[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy', 'Sorting'] |
2,145 | Count the Hidden Sequences | count-the-hidden-sequences | <p>You are given a <strong>0-indexed</strong> array of <code>n</code> integers <code>differences</code>, which describes the <strong>differences </strong>between each pair of <strong>consecutive </strong>integers of a <strong>hidden</strong> sequence of length <code>(n + 1)</code>. More formally, call the hidden sequence <code>hidden</code>, then we have that <code>differences[i] = hidden[i + 1] - hidden[i]</code>.</p>
<p>You are further given two integers <code>lower</code> and <code>upper</code> that describe the <strong>inclusive</strong> range of values <code>[lower, upper]</code> that the hidden sequence can contain.</p>
<ul>
<li>For example, given <code>differences = [1, -3, 4]</code>, <code>lower = 1</code>, <code>upper = 6</code>, the hidden sequence is a sequence of length <code>4</code> whose elements are in between <code>1</code> and <code>6</code> (<strong>inclusive</strong>).
<ul>
<li><code>[3, 4, 1, 5]</code> and <code>[4, 5, 2, 6]</code> are possible hidden sequences.</li>
<li><code>[5, 6, 3, 7]</code> is not possible since it contains an element greater than <code>6</code>.</li>
<li><code>[1, 2, 3, 4]</code> is not possible since the differences are not correct.</li>
</ul>
</li>
</ul>
<p>Return <em>the number of <strong>possible</strong> hidden sequences there are.</em> If there are no possible sequences, return <code>0</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> differences = [1,-3,4], lower = 1, upper = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> The possible hidden sequences are:
- [3, 4, 1, 5]
- [4, 5, 2, 6]
Thus, we return 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> differences = [3,-4,5,1,-2], lower = -4, upper = 5
<strong>Output:</strong> 4
<strong>Explanation:</strong> The possible hidden sequences are:
- [-3, 0, -4, 1, 2, 0]
- [-2, 1, -3, 2, 3, 1]
- [-1, 2, -2, 3, 4, 2]
- [0, 3, -1, 4, 5, 3]
Thus, we return 4.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> differences = [4,-7,2], lower = 3, upper = 6
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no possible hidden sequences. Thus, we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == differences.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>-10<sup>5</sup> <= differences[i] <= 10<sup>5</sup></code></li>
<li><code>-10<sup>5</sup> <= lower <= upper <= 10<sup>5</sup></code></li>
</ul>
| Medium | 20.7K | 52.8K | 20,729 | 52,763 | 39.3% | [] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numberOfArrays(vector<int>& differences, int lower, int upper) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numberOfArrays(int[] differences, int lower, int upper) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numberOfArrays(self, differences, lower, upper):\n \"\"\"\n :type differences: List[int]\n :type lower: int\n :type upper: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numberOfArrays(self, differences: List[int], lower: int, upper: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numberOfArrays(int* differences, int differencesSize, int lower, int upper) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumberOfArrays(int[] differences, int lower, int upper) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} differences\n * @param {number} lower\n * @param {number} upper\n * @return {number}\n */\nvar numberOfArrays = function(differences, lower, upper) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numberOfArrays(differences: number[], lower: number, upper: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $differences\n * @param Integer $lower\n * @param Integer $upper\n * @return Integer\n */\n function numberOfArrays($differences, $lower, $upper) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numberOfArrays(_ differences: [Int], _ lower: Int, _ upper: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numberOfArrays(differences: IntArray, lower: Int, upper: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numberOfArrays(List<int> differences, int lower, int upper) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numberOfArrays(differences []int, lower int, upper int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} differences\n# @param {Integer} lower\n# @param {Integer} upper\n# @return {Integer}\ndef number_of_arrays(differences, lower, upper)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numberOfArrays(differences: Array[Int], lower: Int, upper: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn number_of_arrays(differences: Vec<i32>, lower: i32, upper: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (number-of-arrays differences lower upper)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec number_of_arrays(Differences :: [integer()], Lower :: integer(), Upper :: integer()) -> integer().\nnumber_of_arrays(Differences, Lower, Upper) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec number_of_arrays(differences :: [integer], lower :: integer, upper :: integer) :: integer\n def number_of_arrays(differences, lower, upper) do\n \n end\nend"}] | [1,-3,4]
1
6 | {
"name": "numberOfArrays",
"params": [
{
"name": "differences",
"type": "integer[]"
},
{
"type": "integer",
"name": "lower"
},
{
"type": "integer",
"name": "upper"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Prefix Sum'] |
2,146 | K Highest Ranked Items Within a Price Range | k-highest-ranked-items-within-a-price-range | <p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> that represents a map of the items in a shop. The integers in the grid represent the following:</p>
<ul>
<li><code>0</code> represents a wall that you cannot pass through.</li>
<li><code>1</code> represents an empty cell that you can freely move to and from.</li>
<li>All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells.</li>
</ul>
<p>It takes <code>1</code> step to travel between adjacent grid cells.</p>
<p>You are also given integer arrays <code>pricing</code> and <code>start</code> where <code>pricing = [low, high]</code> and <code>start = [row, col]</code> indicates that you start at the position <code>(row, col)</code> and are interested only in items with a price in the range of <code>[low, high]</code> (<strong>inclusive</strong>). You are further given an integer <code>k</code>.</p>
<p>You are interested in the <strong>positions</strong> of the <code>k</code> <strong>highest-ranked</strong> items whose prices are <strong>within</strong> the given price range. The rank is determined by the <strong>first</strong> of these criteria that is different:</p>
<ol>
<li>Distance, defined as the length of the shortest path from the <code>start</code> (<strong>shorter</strong> distance has a higher rank).</li>
<li>Price (<strong>lower</strong> price has a higher rank, but it must be <strong>in the price range</strong>).</li>
<li>The row number (<strong>smaller</strong> row number has a higher rank).</li>
<li>The column number (<strong>smaller</strong> column number has a higher rank).</li>
</ol>
<p>Return <em>the </em><code>k</code><em> highest-ranked items within the price range <strong>sorted</strong> by their rank (highest to lowest)</em>. If there are fewer than <code>k</code> reachable items within the price range, return <em><strong>all</strong> of them</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example1drawio.png" style="width: 200px; height: 151px;" />
<pre>
<strong>Input:</strong> grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3
<strong>Output:</strong> [[0,1],[1,1],[2,1]]
<strong>Explanation:</strong> You start at (0,0).
With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2).
The ranks of these items are:
- (0,1) with distance 1
- (1,1) with distance 2
- (2,1) with distance 3
- (2,2) with distance 4
Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1).
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/16/example2drawio1.png" style="width: 200px; height: 151px;" />
<pre>
<strong>Input:</strong> grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2
<strong>Output:</strong> [[2,1],[1,2]]
<strong>Explanation:</strong> You start at (2,3).
With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1).
The ranks of these items are:
- (2,1) with distance 2, price 2
- (1,2) with distance 2, price 3
- (1,1) with distance 3
- (0,1) with distance 4
Thus, the 2 highest ranked items in the price range are (2,1) and (1,2).
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/example3.png" style="width: 149px; height: 150px;" />
<pre>
<strong>Input:</strong> grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3
<strong>Output:</strong> [[2,1],[2,0]]
<strong>Explanation:</strong> You start at (0,0).
With a price range of [2,3], we can take items from (2,0) and (2,1).
The ranks of these items are:
- (2,1) with distance 5
- (2,0) with distance 6
Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).
Note that k = 3 but there are only 2 reachable items within the price range.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == grid.length</code></li>
<li><code>n == grid[i].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= grid[i][j] <= 10<sup>5</sup></code></li>
<li><code>pricing.length == 2</code></li>
<li><code>2 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>start.length == 2</code></li>
<li><code>0 <= row <= m - 1</code></li>
<li><code>0 <= col <= n - 1</code></li>
<li><code>grid[row][col] > 0</code></li>
<li><code>1 <= k <= m * n</code></li>
</ul>
| Medium | 18.2K | 41.2K | 18,238 | 41,188 | 44.3% | ['kth-largest-element-in-an-array', 'as-far-from-land-as-possible', 'reward-top-k-students'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> highestRankedKItems(vector<vector<int>>& grid, vector<int>& pricing, vector<int>& start, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<List<Integer>> highestRankedKItems(int[][] grid, int[] pricing, int[] start, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def highestRankedKItems(self, grid, pricing, start, k):\n \"\"\"\n :type grid: List[List[int]]\n :type pricing: List[int]\n :type start: List[int]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def highestRankedKItems(self, grid: List[List[int]], pricing: List[int], start: List[int], k: int) -> List[List[int]]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nint** highestRankedKItems(int** grid, int gridSize, int* gridColSize, int* pricing, int pricingSize, int* start, int startSize, int k, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<IList<int>> HighestRankedKItems(int[][] grid, int[] pricing, int[] start, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @param {number[]} pricing\n * @param {number[]} start\n * @param {number} k\n * @return {number[][]}\n */\nvar highestRankedKItems = function(grid, pricing, start, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function highestRankedKItems(grid: number[][], pricing: number[], start: number[], k: number): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @param Integer[] $pricing\n * @param Integer[] $start\n * @param Integer $k\n * @return Integer[][]\n */\n function highestRankedKItems($grid, $pricing, $start, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func highestRankedKItems(_ grid: [[Int]], _ pricing: [Int], _ start: [Int], _ k: Int) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun highestRankedKItems(grid: Array<IntArray>, pricing: IntArray, start: IntArray, k: Int): List<List<Int>> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> highestRankedKItems(List<List<int>> grid, List<int> pricing, List<int> start, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func highestRankedKItems(grid [][]int, pricing []int, start []int, k int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @param {Integer[]} pricing\n# @param {Integer[]} start\n# @param {Integer} k\n# @return {Integer[][]}\ndef highest_ranked_k_items(grid, pricing, start, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def highestRankedKItems(grid: Array[Array[Int]], pricing: Array[Int], start: Array[Int], k: Int): List[List[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn highest_ranked_k_items(grid: Vec<Vec<i32>>, pricing: Vec<i32>, start: Vec<i32>, k: i32) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (highest-ranked-k-items grid pricing start k)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) (listof exact-integer?) exact-integer? (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec highest_ranked_k_items(Grid :: [[integer()]], Pricing :: [integer()], Start :: [integer()], K :: integer()) -> [[integer()]].\nhighest_ranked_k_items(Grid, Pricing, Start, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec highest_ranked_k_items(grid :: [[integer]], pricing :: [integer], start :: [integer], k :: integer) :: [[integer]]\n def highest_ranked_k_items(grid, pricing, start, k) do\n \n end\nend"}] | [[1,2,0,1],[1,3,0,1],[0,2,5,1]]
[2,5]
[0,0]
3 | {
"name": "highestRankedKItems",
"params": [
{
"name": "grid",
"type": "integer[][]"
},
{
"type": "integer[]",
"name": "pricing"
},
{
"type": "integer[]",
"name": "start"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "list<list<integer>>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Breadth-First Search', 'Sorting', 'Heap (Priority Queue)', 'Matrix'] |
2,147 | Number of Ways to Divide a Long Corridor | number-of-ways-to-divide-a-long-corridor | <p>Along a long library corridor, there is a line of seats and decorative plants. You are given a <strong>0-indexed</strong> string <code>corridor</code> of length <code>n</code> consisting of letters <code>'S'</code> and <code>'P'</code> where each <code>'S'</code> represents a seat and each <code>'P'</code> represents a plant.</p>
<p>One room divider has <strong>already</strong> been installed to the left of index <code>0</code>, and <strong>another</strong> to the right of index <code>n - 1</code>. Additional room dividers can be installed. For each position between indices <code>i - 1</code> and <code>i</code> (<code>1 <= i <= n - 1</code>), at most one divider can be installed.</p>
<p>Divide the corridor into non-overlapping sections, where each section has <strong>exactly two seats</strong> with any number of plants. There may be multiple ways to perform the division. Two ways are <strong>different</strong> if there is a position with a room divider installed in the first way but not in the second way.</p>
<p>Return <em>the number of ways to divide the corridor</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>. If there is no way, return <code>0</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/1.png" style="width: 410px; height: 199px;" />
<pre>
<strong>Input:</strong> corridor = "SSPPSPS"
<strong>Output:</strong> 3
<strong>Explanation:</strong> There are 3 different ways to divide the corridor.
The black bars in the above image indicate the two room dividers already installed.
Note that in each of the ways, <strong>each</strong> section has exactly <strong>two</strong> seats.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/04/2.png" style="width: 357px; height: 68px;" />
<pre>
<strong>Input:</strong> corridor = "PPSPSP"
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is only 1 way to divide the corridor, by not installing any additional dividers.
Installing any would create some section that does not have exactly two seats.
</pre>
<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/12/3.png" style="width: 115px; height: 68px;" />
<pre>
<strong>Input:</strong> corridor = "S"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == corridor.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>corridor[i]</code> is either <code>'S'</code> or <code>'P'</code>.</li>
</ul>
| Hard | 70.9K | 145.2K | 70,912 | 145,250 | 48.8% | ['decode-ways-ii', 'minimum-cost-to-cut-a-stick', 'ways-to-split-array-into-three-subarrays'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numberOfWays(string corridor) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numberOfWays(String corridor) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numberOfWays(self, corridor):\n \"\"\"\n :type corridor: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numberOfWays(self, corridor: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numberOfWays(char* corridor) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumberOfWays(string corridor) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} corridor\n * @return {number}\n */\nvar numberOfWays = function(corridor) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numberOfWays(corridor: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $corridor\n * @return Integer\n */\n function numberOfWays($corridor) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numberOfWays(_ corridor: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numberOfWays(corridor: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numberOfWays(String corridor) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numberOfWays(corridor string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} corridor\n# @return {Integer}\ndef number_of_ways(corridor)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numberOfWays(corridor: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn number_of_ways(corridor: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (number-of-ways corridor)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec number_of_ways(Corridor :: unicode:unicode_binary()) -> integer().\nnumber_of_ways(Corridor) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec number_of_ways(corridor :: String.t) :: integer\n def number_of_ways(corridor) do\n \n end\nend"}] | "SSPPSPS" | {
"name": "numberOfWays",
"params": [
{
"name": "corridor",
"type": "string"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'String', 'Dynamic Programming'] |
2,148 | Count Elements With Strictly Smaller and Greater Elements | count-elements-with-strictly-smaller-and-greater-elements | <p>Given an integer array <code>nums</code>, return <em>the number of elements that have <strong>both</strong> a strictly smaller and a strictly greater element appear in </em><code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [11,7,2,15]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The element 7 has the element 2 strictly smaller than it and the element 11 strictly greater than it.
Element 11 has element 7 strictly smaller than it and element 15 strictly greater than it.
In total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums</code>.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-3,3,3,90]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The element 3 has the element -3 strictly smaller than it and the element 90 strictly greater than it.
Since there are two elements with the value 3, in total there are 2 elements having both a strictly smaller and a strictly greater element appear in <code>nums</code>.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| Easy | 67.9K | 114.8K | 67,879 | 114,813 | 59.1% | ['find-smallest-letter-greater-than-target'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countElements(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countElements(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countElements(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countElements(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countElements(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountElements(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar countElements = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countElements(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function countElements($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countElements(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countElements(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countElements(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countElements(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef count_elements(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countElements(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_elements(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-elements nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_elements(Nums :: [integer()]) -> integer().\ncount_elements(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_elements(nums :: [integer]) :: integer\n def count_elements(nums) do\n \n end\nend"}] | [11,7,2,15] | {
"name": "countElements",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Sorting', 'Counting'] |
2,149 | Rearrange Array Elements by Sign | rearrange-array-elements-by-sign | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p>
<p>You should return the array of nums such that the the array follows the given conditions:</p>
<ol>
<li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li>
<li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li>
<li>The rearranged array begins with a positive integer.</li>
</ol>
<p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,-2,-5,2,-4]
<strong>Output:</strong> [3,-2,1,-5,2,-4]
<strong>Explanation:</strong>
The positive integers in nums are [3,1,2]. The negative integers are [-2,-5,-4].
The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].
Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,1]
<strong>Output:</strong> [1,-1]
<strong>Explanation:</strong>
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 2 * 10<sup>5</sup></code></li>
<li><code>nums.length</code> is <strong>even</strong></li>
<li><code>1 <= |nums[i]| <= 10<sup>5</sup></code></li>
<li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li>
</ul>
<p> </p>
It is not required to do the modifications in-place. | Medium | 552.9K | 656.2K | 552,851 | 656,241 | 84.2% | ['wiggle-subsequence', 'sort-array-by-parity-ii', 'partition-array-according-to-given-pivot', 'largest-number-after-digit-swaps-by-parity'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> rearrangeArray(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] rearrangeArray(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def rearrangeArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def rearrangeArray(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* rearrangeArray(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] RearrangeArray(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar rearrangeArray = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function rearrangeArray(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function rearrangeArray($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func rearrangeArray(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun rearrangeArray(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> rearrangeArray(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func rearrangeArray(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef rearrange_array(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def rearrangeArray(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (rearrange-array nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec rearrange_array(Nums :: [integer()]) -> [integer()].\nrearrange_array(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec rearrange_array(nums :: [integer]) :: [integer]\n def rearrange_array(nums) do\n \n end\nend"}] | [3,1,-2,-5,2,-4] | {
"name": "rearrangeArray",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers', 'Simulation'] |
2,150 | Find All Lonely Numbers in the Array | find-all-lonely-numbers-in-the-array | <p>You are given an integer array <code>nums</code>. A number <code>x</code> is <strong>lonely</strong> when it appears only <strong>once</strong>, and no <strong>adjacent</strong> numbers (i.e. <code>x + 1</code> and <code>x - 1)</code> appear in the array.</p>
<p>Return <em><strong>all</strong> lonely numbers in </em><code>nums</code>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [10,6,5,8]
<strong>Output:</strong> [10,8]
<strong>Explanation:</strong>
- 10 is a lonely number since it appears exactly once and 9 and 11 does not appear in nums.
- 8 is a lonely number since it appears exactly once and 7 and 9 does not appear in nums.
- 5 is not a lonely number since 6 appears in nums and vice versa.
Hence, the lonely numbers in nums are [10, 8].
Note that [8, 10] may also be returned.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,3]
<strong>Output:</strong> [1,5]
<strong>Explanation:</strong>
- 1 is a lonely number since it appears exactly once and 0 and 2 does not appear in nums.
- 5 is a lonely number since it appears exactly once and 4 and 6 does not appear in nums.
- 3 is not a lonely number since it appears twice.
Hence, the lonely numbers in nums are [1, 5].
Note that [5, 1] may also be returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i] <= 10<sup>6</sup></code></li>
</ul>
| Medium | 57.3K | 93.4K | 57,255 | 93,369 | 61.3% | ['frequency-of-the-most-frequent-element'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> findLonely(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> findLonely(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findLonely(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findLonely(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* findLonely(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> FindLonely(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar findLonely = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findLonely(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function findLonely($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findLonely(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findLonely(nums: IntArray): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> findLonely(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findLonely(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef find_lonely(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findLonely(nums: Array[Int]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_lonely(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-lonely nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_lonely(Nums :: [integer()]) -> [integer()].\nfind_lonely(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_lonely(nums :: [integer]) :: [integer]\n def find_lonely(nums) do\n \n end\nend"}] | [10,6,5,8] | {
"name": "findLonely",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "list<integer>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Counting'] |
2,151 | Maximum Good People Based on Statements | maximum-good-people-based-on-statements | <p>There are two types of persons:</p>
<ul>
<li>The <strong>good person</strong>: The person who always tells the truth.</li>
<li>The <strong>bad person</strong>: The person who might tell the truth and might lie.</li>
</ul>
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>statements</code> of size <code>n x n</code> that represents the statements made by <code>n</code> people about each other. More specifically, <code>statements[i][j]</code> could be one of the following:</p>
<ul>
<li><code>0</code> which represents a statement made by person <code>i</code> that person <code>j</code> is a <strong>bad</strong> person.</li>
<li><code>1</code> which represents a statement made by person <code>i</code> that person <code>j</code> is a <strong>good</strong> person.</li>
<li><code>2</code> represents that <strong>no statement</strong> is made by person <code>i</code> about person <code>j</code>.</li>
</ul>
<p>Additionally, no person ever makes a statement about themselves. Formally, we have that <code>statements[i][i] = 2</code> for all <code>0 <= i < n</code>.</p>
<p>Return <em>the <strong>maximum</strong> number of people who can be <strong>good</strong> based on the statements made by the </em><code>n</code><em> people</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/15/logic1.jpg" style="width: 600px; height: 262px;" />
<pre>
<strong>Input:</strong> statements = [[2,1,2],[1,2,2],[2,0,2]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Each person makes a single statement.
- Person 0 states that person 1 is good.
- Person 1 states that person 0 is good.
- Person 2 states that person 1 is bad.
Let's take person 2 as the key.
- Assuming that person 2 is a good person:
- Based on the statement made by person 2, person 1 is a bad person.
- Now we know for sure that person 1 is bad and person 2 is good.
- Based on the statement made by person 1, and since person 1 is bad, they could be:
- telling the truth. There will be a contradiction in this case and this assumption is invalid.
- lying. In this case, person 0 is also a bad person and lied in their statement.
- <strong>Following that person 2 is a good person, there will be only one good person in the group</strong>.
- Assuming that person 2 is a bad person:
- Based on the statement made by person 2, and since person 2 is bad, they could be:
- telling the truth. Following this scenario, person 0 and 1 are both bad as explained before.
- <strong>Following that person 2 is bad but told the truth, there will be no good persons in the group</strong>.
- lying. In this case person 1 is a good person.
- Since person 1 is a good person, person 0 is also a good person.
- <strong>Following that person 2 is bad and lied, there will be two good persons in the group</strong>.
We can see that at most 2 persons are good in the best case, so we return 2.
Note that there is more than one way to arrive at this conclusion.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/01/15/logic2.jpg" style="width: 600px; height: 262px;" />
<pre>
<strong>Input:</strong> statements = [[2,0],[0,2]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Each person makes a single statement.
- Person 0 states that person 1 is bad.
- Person 1 states that person 0 is bad.
Let's take person 0 as the key.
- Assuming that person 0 is a good person:
- Based on the statement made by person 0, person 1 is a bad person and was lying.
- <strong>Following that person 0 is a good person, there will be only one good person in the group</strong>.
- Assuming that person 0 is a bad person:
- Based on the statement made by person 0, and since person 0 is bad, they could be:
- telling the truth. Following this scenario, person 0 and 1 are both bad.
- <strong>Following that person 0 is bad but told the truth, there will be no good persons in the group</strong>.
- lying. In this case person 1 is a good person.
- <strong>Following that person 0 is bad and lied, there will be only one good person in the group</strong>.
We can see that at most, one person is good in the best case, so we return 1.
Note that there is more than one way to arrive at this conclusion.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == statements.length == statements[i].length</code></li>
<li><code>2 <= n <= 15</code></li>
<li><code>statements[i][j]</code> is either <code>0</code>, <code>1</code>, or <code>2</code>.</li>
<li><code>statements[i][i] == 2</code></li>
</ul>
| Hard | 15.2K | 30K | 15,200 | 29,988 | 50.7% | ['maximum-score-words-formed-by-letters'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumGood(vector<vector<int>>& statements) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumGood(int[][] statements) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumGood(self, statements):\n \"\"\"\n :type statements: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumGood(self, statements: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumGood(int** statements, int statementsSize, int* statementsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumGood(int[][] statements) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} statements\n * @return {number}\n */\nvar maximumGood = function(statements) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumGood(statements: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $statements\n * @return Integer\n */\n function maximumGood($statements) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumGood(_ statements: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumGood(statements: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumGood(List<List<int>> statements) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumGood(statements [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} statements\n# @return {Integer}\ndef maximum_good(statements)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumGood(statements: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_good(statements: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-good statements)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_good(Statements :: [[integer()]]) -> integer().\nmaximum_good(Statements) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_good(statements :: [[integer]]) :: integer\n def maximum_good(statements) do\n \n end\nend"}] | [[2,1,2],[1,2,2],[2,0,2]] | {
"name": "maximumGood",
"params": [
{
"name": "statements",
"type": "integer[][]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Backtracking', 'Bit Manipulation', 'Enumeration'] |
2,152 | Minimum Number of Lines to Cover Points | minimum-number-of-lines-to-cover-points | null | Medium | 2.4K | 5.6K | 2,379 | 5,582 | 42.6% | ['max-points-on-a-line', 'min-cost-to-connect-all-points', 'minimum-lines-to-represent-a-line-chart'] | [] | Algorithms | null | [[0,1],[2,3],[4,5],[4,3]] | {
"name": "minimumLines",
"params": [
{
"name": "points",
"type": "integer[][]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Math', 'Dynamic Programming', 'Backtracking', 'Bit Manipulation', 'Geometry', 'Bitmask'] |
2,153 | The Number of Passengers in Each Bus II | the-number-of-passengers-in-each-bus-ii | null | Hard | 3.5K | 8.7K | 3,455 | 8,698 | 39.7% | [] | ['Create table If Not Exists Buses (bus_id int, arrival_time int, capacity int)', 'Create table If Not Exists Passengers (passenger_id int, arrival_time int)', 'Truncate table Buses', "insert into Buses (bus_id, arrival_time, capacity) values ('1', '2', '1')", "insert into Buses (bus_id, arrival_time, capacity) values ('2', '4', '10')", "insert into Buses (bus_id, arrival_time, capacity) values ('3', '7', '2')", 'Truncate table Passengers', "insert into Passengers (passenger_id, arrival_time) values ('11', '1')", "insert into Passengers (passenger_id, arrival_time) values ('12', '1')", "insert into Passengers (passenger_id, arrival_time) values ('13', '5')", "insert into Passengers (passenger_id, arrival_time) values ('14', '6')", "insert into Passengers (passenger_id, arrival_time) values ('15', '7')"] | Database | null | {"headers":{"Buses":["bus_id","arrival_time","capacity"],"Passengers":["passenger_id","arrival_time"]},"rows":{"Buses":[[1,2,1],[2,4,10],[3,7,2]],"Passengers":[[11,1],[12,1],[13,5],[14,6],[15,7]]}} | {"mysql": ["Create table If Not Exists Buses (bus_id int, arrival_time int, capacity int)", "Create table If Not Exists Passengers (passenger_id int, arrival_time int)"], "mssql": ["Create table Buses (bus_id int, arrival_time int, capacity int)", "Create table Passengers (passenger_id int, arrival_time int)"], "oraclesql": ["Create table Buses (bus_id int, arrival_time int, capacity int)", "Create table Passengers (passenger_id int, arrival_time int)"], "database": true, "name": "number_of_passengers", "pythondata": ["Buses = pd.DataFrame([], columns=['bus_id', 'arrival_time', 'capacity']).astype({'bus_id':'Int64', 'arrival_time':'Int64', 'capacity':'Int64'})", "Passengers = pd.DataFrame([], columns=['passenger_id', 'arrival_time']).astype({'passenger_id':'Int64', 'arrival_time':'Int64'})"], "postgresql": ["Create table If Not Exists Buses (bus_id int, arrival_time int, capacity int)", "Create table If Not Exists Passengers (passenger_id int, arrival_time int)"], "database_schema": {"Buses": {"bus_id": "INT", "arrival_time": "INT", "capacity": "INT"}, "Passengers": {"passenger_id": "INT", "arrival_time": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,154 | Keep Multiplying Found Values by Two | keep-multiplying-found-values-by-two | <p>You are given an array of integers <code>nums</code>. You are also given an integer <code>original</code> which is the first number that needs to be searched for in <code>nums</code>.</p>
<p>You then do the following steps:</p>
<ol>
<li>If <code>original</code> is found in <code>nums</code>, <strong>multiply</strong> it by two (i.e., set <code>original = 2 * original</code>).</li>
<li>Otherwise, <strong>stop</strong> the process.</li>
<li><strong>Repeat</strong> this process with the new number as long as you keep finding the number.</li>
</ol>
<p>Return <em>the <strong>final</strong> value of </em><code>original</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,3,6,1,12], original = 3
<strong>Output:</strong> 24
<strong>Explanation:</strong>
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,7,9], original = 4
<strong>Output:</strong> 4
<strong>Explanation:</strong>
- 4 is not found in nums. Thus, 4 is returned.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i], original <= 1000</code></li>
</ul>
| Easy | 116.6K | 163.6K | 116,582 | 163,577 | 71.3% | ['largest-number-at-least-twice-of-others', 'check-if-n-and-its-double-exist'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findFinalValue(vector<int>& nums, int original) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findFinalValue(int[] nums, int original) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findFinalValue(self, nums, original):\n \"\"\"\n :type nums: List[int]\n :type original: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findFinalValue(self, nums: List[int], original: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findFinalValue(int* nums, int numsSize, int original) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindFinalValue(int[] nums, int original) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} original\n * @return {number}\n */\nvar findFinalValue = function(nums, original) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findFinalValue(nums: number[], original: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $original\n * @return Integer\n */\n function findFinalValue($nums, $original) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findFinalValue(_ nums: [Int], _ original: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findFinalValue(nums: IntArray, original: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findFinalValue(List<int> nums, int original) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findFinalValue(nums []int, original int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} original\n# @return {Integer}\ndef find_final_value(nums, original)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findFinalValue(nums: Array[Int], original: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_final_value(nums: Vec<i32>, original: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-final-value nums original)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_final_value(Nums :: [integer()], Original :: integer()) -> integer().\nfind_final_value(Nums, Original) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_final_value(nums :: [integer], original :: integer) :: integer\n def find_final_value(nums, original) do\n \n end\nend"}] | [5,3,6,1,12]
3 | {
"name": "findFinalValue",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "original"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Sorting', 'Simulation'] |
2,155 | All Divisions With the Highest Score of a Binary Array | all-divisions-with-the-highest-score-of-a-binary-array | <p>You are given a <strong>0-indexed</strong> binary array <code>nums</code> of length <code>n</code>. <code>nums</code> can be divided at index <code>i</code> (where <code>0 <= i <= n)</code> into two arrays (possibly empty) <code>nums<sub>left</sub></code> and <code>nums<sub>right</sub></code>:</p>
<ul>
<li><code>nums<sub>left</sub></code> has all the elements of <code>nums</code> between index <code>0</code> and <code>i - 1</code> <strong>(inclusive)</strong>, while <code>nums<sub>right</sub></code> has all the elements of nums between index <code>i</code> and <code>n - 1</code> <strong>(inclusive)</strong>.</li>
<li>If <code>i == 0</code>, <code>nums<sub>left</sub></code> is <strong>empty</strong>, while <code>nums<sub>right</sub></code> has all the elements of <code>nums</code>.</li>
<li>If <code>i == n</code>, <code>nums<sub>left</sub></code> has all the elements of nums, while <code>nums<sub>right</sub></code> is <strong>empty</strong>.</li>
</ul>
<p>The <strong>division score</strong> of an index <code>i</code> is the <strong>sum</strong> of the number of <code>0</code>'s in <code>nums<sub>left</sub></code> and the number of <code>1</code>'s in <code>nums<sub>right</sub></code>.</p>
<p>Return <em><strong>all distinct indices</strong> that have the <strong>highest</strong> possible <strong>division score</strong></em>. You may return the answer in <strong>any order</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,1,0]
<strong>Output:</strong> [2,4]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,<u><strong>1</strong></u>,0]. The score is 0 + 1 = 1.
- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,<u><strong>1</strong></u>,0]. The score is 1 + 1 = 2.
- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [<u><strong>1</strong></u>,0]. The score is 2 + 1 = 3.
- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
- 4: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
Indices 2 and 4 both have the highest possible division score 3.
Note the answer [4,2] would also be accepted.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [3]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,0]. The score is 0 + 0 = 0.
- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,0]. The score is 1 + 0 = 1.
- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.
- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.
Only index 3 has the highest possible division score 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1]
<strong>Output:</strong> [0]
<strong>Explanation:</strong> Division at index
- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [<u><strong>1</strong></u>,<u><strong>1</strong></u>]. The score is 0 + 2 = 2.
- 1: nums<sub>left</sub> is [1]. nums<sub>right</sub> is [<u><strong>1</strong></u>]. The score is 0 + 1 = 1.
- 2: nums<sub>left</sub> is [1,1]. nums<sub>right</sub> is []. The score is 0 + 0 = 0.
Only index 0 has the highest possible division score 2.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>
| Medium | 32.9K | 51.3K | 32,914 | 51,266 | 64.2% | ['ones-and-zeroes', 'max-consecutive-ones-ii', 'count-subarrays-with-more-ones-than-zeros', 'array-partition', 'divide-array-in-sets-of-k-consecutive-numbers'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> maxScoreIndices(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> maxScoreIndices(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxScoreIndices(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxScoreIndices(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* maxScoreIndices(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> MaxScoreIndices(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar maxScoreIndices = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxScoreIndices(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function maxScoreIndices($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxScoreIndices(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxScoreIndices(nums: IntArray): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> maxScoreIndices(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxScoreIndices(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef max_score_indices(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxScoreIndices(nums: Array[Int]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_score_indices(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-score-indices nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_score_indices(Nums :: [integer()]) -> [integer()].\nmax_score_indices(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_score_indices(nums :: [integer]) :: [integer]\n def max_score_indices(nums) do\n \n end\nend"}] | [0,0,1,0] | {
"name": "maxScoreIndices",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "list<integer>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array'] |
2,156 | Find Substring With Given Hash Value | find-substring-with-given-hash-value | <p>The hash of a <strong>0-indexed</strong> string <code>s</code> of length <code>k</code>, given integers <code>p</code> and <code>m</code>, is computed using the following function:</p>
<ul>
<li><code>hash(s, p, m) = (val(s[0]) * p<sup>0</sup> + val(s[1]) * p<sup>1</sup> + ... + val(s[k-1]) * p<sup>k-1</sup>) mod m</code>.</li>
</ul>
<p>Where <code>val(s[i])</code> represents the index of <code>s[i]</code> in the alphabet from <code>val('a') = 1</code> to <code>val('z') = 26</code>.</p>
<p>You are given a string <code>s</code> and the integers <code>power</code>, <code>modulo</code>, <code>k</code>, and <code>hashValue.</code> Return <code>sub</code>,<em> the <strong>first</strong> <strong>substring</strong> of </em><code>s</code><em> of length </em><code>k</code><em> such that </em><code>hash(sub, power, modulo) == hashValue</code>.</p>
<p>The test cases will be generated such that an answer always <strong>exists</strong>.</p>
<p>A <b>substring</b> is a contiguous non-empty sequence of characters within a string.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
<strong>Output:</strong> "ee"
<strong>Explanation:</strong> The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0.
"ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
<strong>Output:</strong> "fbx"
<strong>Explanation:</strong> The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 * 1 + 2 * 31 + 24 * 31<sup>2</sup>) mod 100 = 23132 mod 100 = 32.
The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 * 1 + 24 * 31 + 26 * 31<sup>2</sup>) mod 100 = 25732 mod 100 = 32.
"fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
Note that "bxz" also has a hash of 32 but it appears later than "fbx".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= k <= s.length <= 2 * 10<sup>4</sup></code></li>
<li><code>1 <= power, modulo <= 10<sup>9</sup></code></li>
<li><code>0 <= hashValue < modulo</code></li>
<li><code>s</code> consists of lowercase English letters only.</li>
<li>The test cases are generated such that an answer always <strong>exists</strong>.</li>
</ul>
| Hard | 14.7K | 59.9K | 14,685 | 59,925 | 24.5% | ['distinct-echo-substrings'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string subStrHash(string s, int power, int modulo, int k, int hashValue) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String subStrHash(String s, int power, int modulo, int k, int hashValue) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def subStrHash(self, s, power, modulo, k, hashValue):\n \"\"\"\n :type s: str\n :type power: int\n :type modulo: int\n :type k: int\n :type hashValue: int\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def subStrHash(self, s: str, power: int, modulo: int, k: int, hashValue: int) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* subStrHash(char* s, int power, int modulo, int k, int hashValue) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string SubStrHash(string s, int power, int modulo, int k, int hashValue) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} power\n * @param {number} modulo\n * @param {number} k\n * @param {number} hashValue\n * @return {string}\n */\nvar subStrHash = function(s, power, modulo, k, hashValue) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function subStrHash(s: string, power: number, modulo: number, k: number, hashValue: number): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $power\n * @param Integer $modulo\n * @param Integer $k\n * @param Integer $hashValue\n * @return String\n */\n function subStrHash($s, $power, $modulo, $k, $hashValue) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func subStrHash(_ s: String, _ power: Int, _ modulo: Int, _ k: Int, _ hashValue: Int) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String subStrHash(String s, int power, int modulo, int k, int hashValue) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func subStrHash(s string, power int, modulo int, k int, hashValue int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} power\n# @param {Integer} modulo\n# @param {Integer} k\n# @param {Integer} hash_value\n# @return {String}\ndef sub_str_hash(s, power, modulo, k, hash_value)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sub_str_hash(s: String, power: i32, modulo: i32, k: i32, hash_value: i32) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sub-str-hash s power modulo k hashValue)\n (-> string? exact-integer? exact-integer? exact-integer? exact-integer? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sub_str_hash(S :: unicode:unicode_binary(), Power :: integer(), Modulo :: integer(), K :: integer(), HashValue :: integer()) -> unicode:unicode_binary().\nsub_str_hash(S, Power, Modulo, K, HashValue) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sub_str_hash(s :: String.t, power :: integer, modulo :: integer, k :: integer, hash_value :: integer) :: String.t\n def sub_str_hash(s, power, modulo, k, hash_value) do\n \n end\nend"}] | "leetcode"
7
20
2
0 | {
"name": "subStrHash",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "integer",
"name": "power"
},
{
"type": "integer",
"name": "modulo"
},
{
"type": "integer",
"name": "k"
},
{
"type": "integer",
"name": "hashValue"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Sliding Window', 'Rolling Hash', 'Hash Function'] |
2,157 | Groups of Strings | groups-of-strings | <p>You are given a <strong>0-indexed</strong> array of strings <code>words</code>. Each string consists of <strong>lowercase English letters</strong> only. No letter occurs more than once in any string of <code>words</code>.</p>
<p>Two strings <code>s1</code> and <code>s2</code> are said to be <strong>connected</strong> if the set of letters of <code>s2</code> can be obtained from the set of letters of <code>s1</code> by any <strong>one</strong> of the following operations:</p>
<ul>
<li>Adding exactly one letter to the set of the letters of <code>s1</code>.</li>
<li>Deleting exactly one letter from the set of the letters of <code>s1</code>.</li>
<li>Replacing exactly one letter from the set of the letters of <code>s1</code> with any letter, <strong>including</strong> itself.</li>
</ul>
<p>The array <code>words</code> can be divided into one or more non-intersecting <strong>groups</strong>. A string belongs to a group if any <strong>one</strong> of the following is true:</p>
<ul>
<li>It is connected to <strong>at least one</strong> other string of the group.</li>
<li>It is the <strong>only</strong> string present in the group.</li>
</ul>
<p>Note that the strings in <code>words</code> should be grouped in such a manner that a string belonging to a group cannot be connected to a string present in any other group. It can be proved that such an arrangement is always unique.</p>
<p>Return <em>an array</em> <code>ans</code> <em>of size</em> <code>2</code> <em>where:</em></p>
<ul>
<li><code>ans[0]</code> <em>is the <strong>maximum number</strong> of groups</em> <code>words</code> <em>can be divided into, and</em></li>
<li><code>ans[1]</code> <em>is the <strong>size of the largest</strong> group</em>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["a","b","ab","cde"]
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong>
- words[0] can be used to obtain words[1] (by replacing 'a' with 'b'), and words[2] (by adding 'b'). So words[0] is connected to words[1] and words[2].
- words[1] can be used to obtain words[0] (by replacing 'b' with 'a'), and words[2] (by adding 'a'). So words[1] is connected to words[0] and words[2].
- words[2] can be used to obtain words[0] (by deleting 'b'), and words[1] (by deleting 'a'). So words[2] is connected to words[0] and words[1].
- words[3] is not connected to any string in words.
Thus, words can be divided into 2 groups ["a","b","ab"] and ["cde"]. The size of the largest group is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["a","ab","abc"]
<strong>Output:</strong> [1,3]
<strong>Explanation:</strong>
- words[0] is connected to words[1].
- words[1] is connected to words[0] and words[2].
- words[2] is connected to words[1].
Since all strings are connected to each other, they should be grouped together.
Thus, the size of the largest group is 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 2 * 10<sup>4</sup></code></li>
<li><code>1 <= words[i].length <= 26</code></li>
<li><code>words[i]</code> consists of lowercase English letters only.</li>
<li>No letter occurs more than once in <code>words[i]</code>.</li>
</ul>
| Hard | 10.5K | 39.6K | 10,472 | 39,580 | 26.5% | ['word-ladder-ii', 'similar-string-groups', 'largest-component-size-by-common-factor'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> groupStrings(vector<string>& words) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] groupStrings(String[] words) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def groupStrings(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def groupStrings(self, words: List[str]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* groupStrings(char** words, int wordsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] GroupStrings(string[] words) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @return {number[]}\n */\nvar groupStrings = function(words) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function groupStrings(words: string[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @return Integer[]\n */\n function groupStrings($words) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func groupStrings(_ words: [String]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun groupStrings(words: Array<String>): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> groupStrings(List<String> words) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func groupStrings(words []string) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @return {Integer[]}\ndef group_strings(words)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def groupStrings(words: Array[String]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn group_strings(words: Vec<String>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (group-strings words)\n (-> (listof string?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec group_strings(Words :: [unicode:unicode_binary()]) -> [integer()].\ngroup_strings(Words) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec group_strings(words :: [String.t]) :: [integer]\n def group_strings(words) do\n \n end\nend"}] | ["a","b","ab","cde"] | {
"name": "groupStrings",
"params": [
{
"name": "words",
"type": "string[]"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Bit Manipulation', 'Union Find'] |
2,158 | Amount of New Area Painted Each Day | amount-of-new-area-painted-each-day | null | Hard | 31.7K | 57.1K | 31,672 | 57,134 | 55.4% | ['merge-intervals', 'describe-the-painting', 'average-height-of-buildings-in-each-segment'] | [] | Algorithms | null | [[1,4],[4,7],[5,8]] | {
"name": "amountPainted",
"params": [
{
"name": "paint",
"type": "integer[][]"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Segment Tree', 'Ordered Set'] |
2,159 | Order Two Columns Independently | order-two-columns-independently | null | Medium | 9.2K | 15.2K | 9,216 | 15,154 | 60.8% | [] | ['Create table If Not Exists Data (first_col int, second_col int)', 'Truncate table Data', "insert into Data (first_col, second_col) values ('4', '2')", "insert into Data (first_col, second_col) values ('2', '3')", "insert into Data (first_col, second_col) values ('3', '1')", "insert into Data (first_col, second_col) values ('1', '4')"] | Database | null | {"headers":{"Data":["first_col","second_col"]},"rows":{"Data":[[4,2],[2,3],[3,1],[1,4]]}} | {"mysql": ["Create table If Not Exists Data (first_col int, second_col int)"], "mssql": ["Create table Data (first_col int, second_col int)"], "oraclesql": ["Create table Data (first_col int, second_col int)"], "database": true, "name": "order_two_columns", "pythondata": ["Data = pd.DataFrame([], columns=['first_col', 'second_col']).astype({'first_col':'Int64', 'second_col':'Int64'})"], "postgresql": ["Create table If Not Exists Data (first_col int, second_col int)"], "database_schema": {"Data": {"first_col": "INT", "second_col": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,160 | Minimum Sum of Four Digit Number After Splitting Digits | minimum-sum-of-four-digit-number-after-splitting-digits | <p>You are given a <strong>positive</strong> integer <code>num</code> consisting of exactly four digits. Split <code>num</code> into two new integers <code>new1</code> and <code>new2</code> by using the <strong>digits</strong> found in <code>num</code>. <strong>Leading zeros</strong> are allowed in <code>new1</code> and <code>new2</code>, and <strong>all</strong> the digits found in <code>num</code> must be used.</p>
<ul>
<li>For example, given <code>num = 2932</code>, you have the following digits: two <code>2</code>'s, one <code>9</code> and one <code>3</code>. Some of the possible pairs <code>[new1, new2]</code> are <code>[22, 93]</code>, <code>[23, 92]</code>, <code>[223, 9]</code> and <code>[2, 329]</code>.</li>
</ul>
<p>Return <em>the <strong>minimum</strong> possible sum of </em><code>new1</code><em> and </em><code>new2</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 2932
<strong>Output:</strong> 52
<strong>Explanation:</strong> Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 4009
<strong>Output:</strong> 13
<strong>Explanation:</strong> Some possible pairs [new1, new2] are [0, 49], [490, 0], etc.
The minimum sum can be obtained by the pair [4, 9]: 4 + 9 = 13.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1000 <= num <= 9999</code></li>
</ul>
| Easy | 149.9K | 174.3K | 149,887 | 174,256 | 86.0% | ['add-digits', 'difference-between-element-sum-and-digit-sum-of-an-array', 'alternating-digit-sum'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumSum(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumSum(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumSum(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumSum(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumSum(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumSum(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar minimumSum = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumSum(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function minimumSum($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumSum(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumSum(num: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumSum(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumSum(num int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef minimum_sum(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumSum(num: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_sum(num: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-sum num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_sum(Num :: integer()) -> integer().\nminimum_sum(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_sum(num :: integer) :: integer\n def minimum_sum(num) do\n \n end\nend"}] | 2932 | {
"name": "minimumSum",
"params": [
{
"name": "num",
"type": "integer"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Greedy', 'Sorting'] |
2,161 | Partition Array According to Given Pivot | partition-array-according-to-given-pivot | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>pivot</code>. Rearrange <code>nums</code> such that the following conditions are satisfied:</p>
<ul>
<li>Every element less than <code>pivot</code> appears <strong>before</strong> every element greater than <code>pivot</code>.</li>
<li>Every element equal to <code>pivot</code> appears <strong>in between</strong> the elements less than and greater than <code>pivot</code>.</li>
<li>The <strong>relative order</strong> of the elements less than <code>pivot</code> and the elements greater than <code>pivot</code> is maintained.
<ul>
<li>More formally, consider every <code>p<sub>i</sub></code>, <code>p<sub>j</sub></code> where <code>p<sub>i</sub></code> is the new position of the <code>i<sup>th</sup></code> element and <code>p<sub>j</sub></code> is the new position of the <code>j<sup>th</sup></code> element. If <code>i < j</code> and <strong>both</strong> elements are smaller (<em>or larger</em>) than <code>pivot</code>, then <code>p<sub>i</sub> < p<sub>j</sub></code>.</li>
</ul>
</li>
</ul>
<p>Return <code>nums</code><em> after the rearrangement.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [9,12,5,10,14,3,10], pivot = 10
<strong>Output:</strong> [9,5,3,10,10,12,14]
<strong>Explanation:</strong>
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-3,4,3,2], pivot = 2
<strong>Output:</strong> [-3,2,4,3]
<strong>Explanation:</strong>
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>6</sup> <= nums[i] <= 10<sup>6</sup></code></li>
<li><code>pivot</code> equals to an element of <code>nums</code>.</li>
</ul>
| Medium | 259.8K | 288.8K | 259,750 | 288,803 | 89.9% | ['partition-list', 'rearrange-array-elements-by-sign'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> pivotArray(vector<int>& nums, int pivot) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] pivotArray(int[] nums, int pivot) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def pivotArray(self, nums, pivot):\n \"\"\"\n :type nums: List[int]\n :type pivot: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def pivotArray(self, nums: List[int], pivot: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* pivotArray(int* nums, int numsSize, int pivot, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] PivotArray(int[] nums, int pivot) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} pivot\n * @return {number[]}\n */\nvar pivotArray = function(nums, pivot) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function pivotArray(nums: number[], pivot: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $pivot\n * @return Integer[]\n */\n function pivotArray($nums, $pivot) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func pivotArray(_ nums: [Int], _ pivot: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun pivotArray(nums: IntArray, pivot: Int): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> pivotArray(List<int> nums, int pivot) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func pivotArray(nums []int, pivot int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} pivot\n# @return {Integer[]}\ndef pivot_array(nums, pivot)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def pivotArray(nums: Array[Int], pivot: Int): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn pivot_array(nums: Vec<i32>, pivot: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (pivot-array nums pivot)\n (-> (listof exact-integer?) exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec pivot_array(Nums :: [integer()], Pivot :: integer()) -> [integer()].\npivot_array(Nums, Pivot) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec pivot_array(nums :: [integer], pivot :: integer) :: [integer]\n def pivot_array(nums, pivot) do\n \n end\nend"}] | [9,12,5,10,14,3,10]
10 | {
"name": "pivotArray",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "pivot"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers', 'Simulation'] |
2,162 | Minimum Cost to Set Cooking Time | minimum-cost-to-set-cooking-time | <p>A generic microwave supports cooking times for:</p>
<ul>
<li>at least <code>1</code> second.</li>
<li>at most <code>99</code> minutes and <code>99</code> seconds.</li>
</ul>
<p>To set the cooking time, you push <strong>at most four digits</strong>. The microwave normalizes what you push as four digits by <strong>prepending zeroes</strong>. It interprets the <strong>first</strong> two digits as the minutes and the <strong>last</strong> two digits as the seconds. It then <strong>adds</strong> them up as the cooking time. For example,</p>
<ul>
<li>You push <code>9</code> <code>5</code> <code>4</code> (three digits). It is normalized as <code>0954</code> and interpreted as <code>9</code> minutes and <code>54</code> seconds.</li>
<li>You push <code>0</code> <code>0</code> <code>0</code> <code>8</code> (four digits). It is interpreted as <code>0</code> minutes and <code>8</code> seconds.</li>
<li>You push <code>8</code> <code>0</code> <code>9</code> <code>0</code>. It is interpreted as <code>80</code> minutes and <code>90</code> seconds.</li>
<li>You push <code>8</code> <code>1</code> <code>3</code> <code>0</code>. It is interpreted as <code>81</code> minutes and <code>30</code> seconds.</li>
</ul>
<p>You are given integers <code>startAt</code>, <code>moveCost</code>, <code>pushCost</code>, and <code>targetSeconds</code>. <strong>Initially</strong>, your finger is on the digit <code>startAt</code>. Moving the finger above <strong>any specific digit</strong> costs <code>moveCost</code> units of fatigue. Pushing the digit below the finger <strong>once</strong> costs <code>pushCost</code> units of fatigue.</p>
<p>There can be multiple ways to set the microwave to cook for <code>targetSeconds</code> seconds but you are interested in the way with the minimum cost.</p>
<p>Return <em>the <strong>minimum cost</strong> to set</em> <code>targetSeconds</code> <em>seconds of cooking time</em>.</p>
<p>Remember that one minute consists of <code>60</code> seconds.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/1.png" style="width: 506px; height: 210px;" />
<pre>
<strong>Input:</strong> startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600
<strong>Output:</strong> 6
<strong>Explanation:</strong> The following are the possible ways to set the cooking time.
- 1 0 0 0, interpreted as 10 minutes and 0 seconds.
The finger is already on digit 1, pushes 1 (with cost 1), moves to 0 (with cost 2), pushes 0 (with cost 1), pushes 0 (with cost 1), and pushes 0 (with cost 1).
The cost is: 1 + 2 + 1 + 1 + 1 = 6. This is the minimum cost.
- 0 9 6 0, interpreted as 9 minutes and 60 seconds. That is also 600 seconds.
The finger moves to 0 (with cost 2), pushes 0 (with cost 1), moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
The cost is: 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12.
- 9 6 0, normalized as 0960 and interpreted as 9 minutes and 60 seconds.
The finger moves to 9 (with cost 2), pushes 9 (with cost 1), moves to 6 (with cost 2), pushes 6 (with cost 1), moves to 0 (with cost 2), and pushes 0 (with cost 1).
The cost is: 2 + 1 + 2 + 1 + 2 + 1 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/30/2.png" style="width: 505px; height: 73px;" />
<pre>
<strong>Input:</strong> startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76
<strong>Output:</strong> 6
<strong>Explanation:</strong> The optimal way is to push two digits: 7 6, interpreted as 76 seconds.
The finger moves to 7 (with cost 1), pushes 7 (with cost 2), moves to 6 (with cost 1), and pushes 6 (with cost 2). The total cost is: 1 + 2 + 1 + 2 = 6
Note other possible ways are 0076, 076, 0116, and 116, but none of them produces the minimum cost.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= startAt <= 9</code></li>
<li><code>1 <= moveCost, pushCost <= 10<sup>5</sup></code></li>
<li><code>1 <= targetSeconds <= 6039</code></li>
</ul>
| Medium | 17.8K | 43.7K | 17,849 | 43,738 | 40.8% | ['minimum-time-difference'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCostSetTime(self, startAt, moveCost, pushCost, targetSeconds):\n \"\"\"\n :type startAt: int\n :type moveCost: int\n :type pushCost: int\n :type targetSeconds: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} startAt\n * @param {number} moveCost\n * @param {number} pushCost\n * @param {number} targetSeconds\n * @return {number}\n */\nvar minCostSetTime = function(startAt, moveCost, pushCost, targetSeconds) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCostSetTime(startAt: number, moveCost: number, pushCost: number, targetSeconds: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $startAt\n * @param Integer $moveCost\n * @param Integer $pushCost\n * @param Integer $targetSeconds\n * @return Integer\n */\n function minCostSetTime($startAt, $moveCost, $pushCost, $targetSeconds) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCostSetTime(_ startAt: Int, _ moveCost: Int, _ pushCost: Int, _ targetSeconds: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCostSetTime(startAt int, moveCost int, pushCost int, targetSeconds int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} start_at\n# @param {Integer} move_cost\n# @param {Integer} push_cost\n# @param {Integer} target_seconds\n# @return {Integer}\ndef min_cost_set_time(start_at, move_cost, push_cost, target_seconds)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCostSetTime(startAt: Int, moveCost: Int, pushCost: Int, targetSeconds: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost-set-time startAt moveCost pushCost targetSeconds)\n (-> exact-integer? exact-integer? exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost_set_time(StartAt :: integer(), MoveCost :: integer(), PushCost :: integer(), TargetSeconds :: integer()) -> integer().\nmin_cost_set_time(StartAt, MoveCost, PushCost, TargetSeconds) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost_set_time(start_at :: integer, move_cost :: integer, push_cost :: integer, target_seconds :: integer) :: integer\n def min_cost_set_time(start_at, move_cost, push_cost, target_seconds) do\n \n end\nend"}] | 1
2
1
600 | {
"name": "minCostSetTime",
"params": [
{
"name": "startAt",
"type": "integer"
},
{
"type": "integer",
"name": "moveCost"
},
{
"type": "integer",
"name": "pushCost"
},
{
"type": "integer",
"name": "targetSeconds"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Enumeration'] |
2,163 | Minimum Difference in Sums After Removal of Elements | minimum-difference-in-sums-after-removal-of-elements | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> consisting of <code>3 * n</code> elements.</p>
<p>You are allowed to remove any <strong>subsequence</strong> of elements of size <strong>exactly</strong> <code>n</code> from <code>nums</code>. The remaining <code>2 * n</code> elements will be divided into two <strong>equal</strong> parts:</p>
<ul>
<li>The first <code>n</code> elements belonging to the first part and their sum is <code>sum<sub>first</sub></code>.</li>
<li>The next <code>n</code> elements belonging to the second part and their sum is <code>sum<sub>second</sub></code>.</li>
</ul>
<p>The <strong>difference in sums</strong> of the two parts is denoted as <code>sum<sub>first</sub> - sum<sub>second</sub></code>.</p>
<ul>
<li>For example, if <code>sum<sub>first</sub> = 3</code> and <code>sum<sub>second</sub> = 2</code>, their difference is <code>1</code>.</li>
<li>Similarly, if <code>sum<sub>first</sub> = 2</code> and <code>sum<sub>second</sub> = 3</code>, their difference is <code>-1</code>.</li>
</ul>
<p>Return <em>the <strong>minimum difference</strong> possible between the sums of the two parts after the removal of </em><code>n</code><em> elements</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2]
<strong>Output:</strong> -1
<strong>Explanation:</strong> Here, nums has 3 elements, so n = 1.
Thus we have to remove 1 element from nums and divide the array into two equal parts.
- If we remove nums[0] = 3, the array will be [1,2]. The difference in sums of the two parts will be 1 - 2 = -1.
- If we remove nums[1] = 1, the array will be [3,2]. The difference in sums of the two parts will be 3 - 2 = 1.
- If we remove nums[2] = 2, the array will be [3,1]. The difference in sums of the two parts will be 3 - 1 = 2.
The minimum difference between sums of the two parts is min(-1,1,2) = -1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [7,9,5,8,1,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong> Here n = 2. So we must remove 2 elements and divide the remaining array into two parts containing two elements each.
If we remove nums[2] = 5 and nums[3] = 8, the resultant array will be [7,9,1,3]. The difference in sums will be (7+9) - (1+3) = 12.
To obtain the minimum difference, we should remove nums[1] = 9 and nums[4] = 1. The resultant array becomes [7,5,8,3]. The difference in sums of the two parts is (7+5) - (8+3) = 1.
It can be shown that it is not possible to obtain a difference smaller than 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>nums.length == 3 * n</code></li>
<li><code>1 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| Hard | 13K | 26.6K | 12,954 | 26,603 | 48.7% | ['product-of-array-except-self', 'find-subsequence-of-length-k-with-the-largest-sum', 'find-minimum-cost-to-remove-array-elements'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minimumDifference(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minimumDifference(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumDifference(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minimumDifference(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinimumDifference(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minimumDifference = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumDifference(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minimumDifference($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumDifference(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumDifference(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumDifference(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumDifference(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef minimum_difference(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumDifference(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_difference(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-difference nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_difference(Nums :: [integer()]) -> integer().\nminimum_difference(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_difference(nums :: [integer]) :: integer\n def minimum_difference(nums) do\n \n end\nend"}] | [3,1,2] | {
"name": "minimumDifference",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming', 'Heap (Priority Queue)'] |
2,164 | Sort Even and Odd Indices Independently | sort-even-and-odd-indices-independently | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. Rearrange the values of <code>nums</code> according to the following rules:</p>
<ol>
<li>Sort the values at <strong>odd indices</strong> of <code>nums</code> in <strong>non-increasing</strong> order.
<ul>
<li>For example, if <code>nums = [4,<strong><u>1</u></strong>,2,<u><strong>3</strong></u>]</code> before this step, it becomes <code>[4,<u><strong>3</strong></u>,2,<strong><u>1</u></strong>]</code> after. The values at odd indices <code>1</code> and <code>3</code> are sorted in non-increasing order.</li>
</ul>
</li>
<li>Sort the values at <strong>even indices</strong> of <code>nums</code> in <strong>non-decreasing</strong> order.
<ul>
<li>For example, if <code>nums = [<u><strong>4</strong></u>,1,<u><strong>2</strong></u>,3]</code> before this step, it becomes <code>[<u><strong>2</strong></u>,1,<u><strong>4</strong></u>,3]</code> after. The values at even indices <code>0</code> and <code>2</code> are sorted in non-decreasing order.</li>
</ul>
</li>
</ol>
<p>Return <em>the array formed after rearranging the values of</em> <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [4,1,2,3]
<strong>Output:</strong> [2,3,4,1]
<strong>Explanation:</strong>
First, we sort the values present at odd indices (1 and 3) in non-increasing order.
So, nums changes from [4,<strong><u>1</u></strong>,2,<strong><u>3</u></strong>] to [4,<u><strong>3</strong></u>,2,<strong><u>1</u></strong>].
Next, we sort the values present at even indices (0 and 2) in non-decreasing order.
So, nums changes from [<u><strong>4</strong></u>,1,<strong><u>2</u></strong>,3] to [<u><strong>2</strong></u>,3,<u><strong>4</strong></u>,1].
Thus, the array formed after rearranging the values is [2,3,4,1].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1]
<strong>Output:</strong> [2,1]
<strong>Explanation:</strong>
Since there is exactly one odd index and one even index, no rearrangement of values takes place.
The resultant array formed is [2,1], which is the same as the initial array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i] <= 100</code></li>
</ul>
| Easy | 76.1K | 121.8K | 76,079 | 121,806 | 62.5% | ['sort-array-by-parity', 'sort-array-by-parity-ii'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> sortEvenOdd(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] sortEvenOdd(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sortEvenOdd(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def sortEvenOdd(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* sortEvenOdd(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] SortEvenOdd(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortEvenOdd = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sortEvenOdd(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function sortEvenOdd($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sortEvenOdd(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sortEvenOdd(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> sortEvenOdd(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sortEvenOdd(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef sort_even_odd(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sortEvenOdd(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sort_even_odd(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sort-even-odd nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sort_even_odd(Nums :: [integer()]) -> [integer()].\nsort_even_odd(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sort_even_odd(nums :: [integer]) :: [integer]\n def sort_even_odd(nums) do\n \n end\nend"}] | [4,1,2,3] | {
"name": "sortEvenOdd",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer[]"
},
"manual": false
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Sorting'] |
2,165 | Smallest Value of the Rearranged Number | smallest-value-of-the-rearranged-number | <p>You are given an integer <code>num.</code> <strong>Rearrange</strong> the digits of <code>num</code> such that its value is <strong>minimized</strong> and it does not contain <strong>any</strong> leading zeros.</p>
<p>Return <em>the rearranged number with minimal value</em>.</p>
<p>Note that the sign of the number does not change after rearranging the digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 310
<strong>Output:</strong> 103
<strong>Explanation:</strong> The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310.
The arrangement with the smallest value that does not contain any leading zeros is 103.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = -7605
<strong>Output:</strong> -7650
<strong>Explanation:</strong> Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567.
The arrangement with the smallest value that does not contain any leading zeros is -7650.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-10<sup>15</sup> <= num <= 10<sup>15</sup></code></li>
</ul>
| Medium | 42K | 80K | 42,003 | 80,037 | 52.5% | ['largest-number'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long smallestNumber(long long num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long smallestNumber(long num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def smallestNumber(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def smallestNumber(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long smallestNumber(long long num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long SmallestNumber(long num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar smallestNumber = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function smallestNumber(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function smallestNumber($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func smallestNumber(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun smallestNumber(num: Long): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int smallestNumber(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func smallestNumber(num int64) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef smallest_number(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def smallestNumber(num: Long): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn smallest_number(num: i64) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (smallest-number num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec smallest_number(Num :: integer()) -> integer().\nsmallest_number(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec smallest_number(num :: integer) :: integer\n def smallest_number(num) do\n \n end\nend"}] | 310 | {
"name": "smallestNumber",
"params": [
{
"name": "num",
"type": "long"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Sorting'] |
2,166 | Design Bitset | design-bitset | <p>A <strong>Bitset</strong> is a data structure that compactly stores bits.</p>
<p>Implement the <code>Bitset</code> class:</p>
<ul>
<li><code>Bitset(int size)</code> Initializes the Bitset with <code>size</code> bits, all of which are <code>0</code>.</li>
<li><code>void fix(int idx)</code> Updates the value of the bit at the index <code>idx</code> to <code>1</code>. If the value was already <code>1</code>, no change occurs.</li>
<li><code>void unfix(int idx)</code> Updates the value of the bit at the index <code>idx</code> to <code>0</code>. If the value was already <code>0</code>, no change occurs.</li>
<li><code>void flip()</code> Flips the values of each bit in the Bitset. In other words, all bits with value <code>0</code> will now have value <code>1</code> and vice versa.</li>
<li><code>boolean all()</code> Checks if the value of <strong>each</strong> bit in the Bitset is <code>1</code>. Returns <code>true</code> if it satisfies the condition, <code>false</code> otherwise.</li>
<li><code>boolean one()</code> Checks if there is <strong>at least one</strong> bit in the Bitset with value <code>1</code>. Returns <code>true</code> if it satisfies the condition, <code>false</code> otherwise.</li>
<li><code>int count()</code> Returns the <strong>total number</strong> of bits in the Bitset which have value <code>1</code>.</li>
<li><code>String toString()</code> Returns the current composition of the Bitset. Note that in the resultant string, the character at the <code>i<sup>th</sup></code> index should coincide with the value at the <code>i<sup>th</sup></code> bit of the Bitset.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"]
[[5], [3], [1], [], [], [0], [], [], [0], [], []]
<strong>Output</strong>
[null, null, null, null, false, null, null, true, null, 2, "01010"]
<strong>Explanation</strong>
Bitset bs = new Bitset(5); // bitset = "00000".
bs.fix(3); // the value at idx = 3 is updated to 1, so bitset = "00010".
bs.fix(1); // the value at idx = 1 is updated to 1, so bitset = "01010".
bs.flip(); // the value of each bit is flipped, so bitset = "10101".
bs.all(); // return False, as not all values of the bitset are 1.
bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "00101".
bs.flip(); // the value of each bit is flipped, so bitset = "11010".
bs.one(); // return True, as there is at least 1 index with value 1.
bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "01010".
bs.count(); // return 2, as there are 2 bits with value 1.
bs.toString(); // return "01010", which is the composition of bitset.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= size <= 10<sup>5</sup></code></li>
<li><code>0 <= idx <= size - 1</code></li>
<li>At most <code>10<sup>5</sup></code> calls will be made <strong>in total</strong> to <code>fix</code>, <code>unfix</code>, <code>flip</code>, <code>all</code>, <code>one</code>, <code>count</code>, and <code>toString</code>.</li>
<li>At least one call will be made to <code>all</code>, <code>one</code>, <code>count</code>, or <code>toString</code>.</li>
<li>At most <code>5</code> calls will be made to <code>toString</code>.</li>
</ul>
| Medium | 25.2K | 76.3K | 25,198 | 76,323 | 33.0% | ['design-underground-system'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Bitset {\npublic:\n Bitset(int size) {\n \n }\n \n void fix(int idx) {\n \n }\n \n void unfix(int idx) {\n \n }\n \n void flip() {\n \n }\n \n bool all() {\n \n }\n \n bool one() {\n \n }\n \n int count() {\n \n }\n \n string toString() {\n \n }\n};\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * Bitset* obj = new Bitset(size);\n * obj->fix(idx);\n * obj->unfix(idx);\n * obj->flip();\n * bool param_4 = obj->all();\n * bool param_5 = obj->one();\n * int param_6 = obj->count();\n * string param_7 = obj->toString();\n */"}, {"value": "java", "text": "Java", "defaultCode": "class Bitset {\n\n public Bitset(int size) {\n \n }\n \n public void fix(int idx) {\n \n }\n \n public void unfix(int idx) {\n \n }\n \n public void flip() {\n \n }\n \n public boolean all() {\n \n }\n \n public boolean one() {\n \n }\n \n public int count() {\n \n }\n \n public String toString() {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * Bitset obj = new Bitset(size);\n * obj.fix(idx);\n * obj.unfix(idx);\n * obj.flip();\n * boolean param_4 = obj.all();\n * boolean param_5 = obj.one();\n * int param_6 = obj.count();\n * String param_7 = obj.toString();\n */"}, {"value": "python", "text": "Python", "defaultCode": "class Bitset(object):\n\n def __init__(self, size):\n \"\"\"\n :type size: int\n \"\"\"\n \n\n def fix(self, idx):\n \"\"\"\n :type idx: int\n :rtype: None\n \"\"\"\n \n\n def unfix(self, idx):\n \"\"\"\n :type idx: int\n :rtype: None\n \"\"\"\n \n\n def flip(self):\n \"\"\"\n :rtype: None\n \"\"\"\n \n\n def all(self):\n \"\"\"\n :rtype: bool\n \"\"\"\n \n\n def one(self):\n \"\"\"\n :rtype: bool\n \"\"\"\n \n\n def count(self):\n \"\"\"\n :rtype: int\n \"\"\"\n \n\n def toString(self):\n \"\"\"\n :rtype: str\n \"\"\"\n \n\n\n# Your Bitset object will be instantiated and called as such:\n# obj = Bitset(size)\n# obj.fix(idx)\n# obj.unfix(idx)\n# obj.flip()\n# param_4 = obj.all()\n# param_5 = obj.one()\n# param_6 = obj.count()\n# param_7 = obj.toString()"}, {"value": "python3", "text": "Python3", "defaultCode": "class Bitset:\n\n def __init__(self, size: int):\n \n\n def fix(self, idx: int) -> None:\n \n\n def unfix(self, idx: int) -> None:\n \n\n def flip(self) -> None:\n \n\n def all(self) -> bool:\n \n\n def one(self) -> bool:\n \n\n def count(self) -> int:\n \n\n def toString(self) -> str:\n \n\n\n# Your Bitset object will be instantiated and called as such:\n# obj = Bitset(size)\n# obj.fix(idx)\n# obj.unfix(idx)\n# obj.flip()\n# param_4 = obj.all()\n# param_5 = obj.one()\n# param_6 = obj.count()\n# param_7 = obj.toString()"}, {"value": "c", "text": "C", "defaultCode": "\n\n\ntypedef struct {\n \n} Bitset;\n\n\nBitset* bitsetCreate(int size) {\n \n}\n\nvoid bitsetFix(Bitset* obj, int idx) {\n \n}\n\nvoid bitsetUnfix(Bitset* obj, int idx) {\n \n}\n\nvoid bitsetFlip(Bitset* obj) {\n \n}\n\nbool bitsetAll(Bitset* obj) {\n \n}\n\nbool bitsetOne(Bitset* obj) {\n \n}\n\nint bitsetCount(Bitset* obj) {\n \n}\n\nchar* bitsetToString(Bitset* obj) {\n \n}\n\nvoid bitsetFree(Bitset* obj) {\n \n}\n\n/**\n * Your Bitset struct will be instantiated and called as such:\n * Bitset* obj = bitsetCreate(size);\n * bitsetFix(obj, idx);\n \n * bitsetUnfix(obj, idx);\n \n * bitsetFlip(obj);\n \n * bool param_4 = bitsetAll(obj);\n \n * bool param_5 = bitsetOne(obj);\n \n * int param_6 = bitsetCount(obj);\n \n * char* param_7 = bitsetToString(obj);\n \n * bitsetFree(obj);\n*/"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Bitset {\n\n public Bitset(int size) {\n \n }\n \n public void Fix(int idx) {\n \n }\n \n public void Unfix(int idx) {\n \n }\n \n public void Flip() {\n \n }\n \n public bool All() {\n \n }\n \n public bool One() {\n \n }\n \n public int Count() {\n \n }\n \n public string ToString() {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * Bitset obj = new Bitset(size);\n * obj.Fix(idx);\n * obj.Unfix(idx);\n * obj.Flip();\n * bool param_4 = obj.All();\n * bool param_5 = obj.One();\n * int param_6 = obj.Count();\n * string param_7 = obj.ToString();\n */"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} size\n */\nvar Bitset = function(size) {\n \n};\n\n/** \n * @param {number} idx\n * @return {void}\n */\nBitset.prototype.fix = function(idx) {\n \n};\n\n/** \n * @param {number} idx\n * @return {void}\n */\nBitset.prototype.unfix = function(idx) {\n \n};\n\n/**\n * @return {void}\n */\nBitset.prototype.flip = function() {\n \n};\n\n/**\n * @return {boolean}\n */\nBitset.prototype.all = function() {\n \n};\n\n/**\n * @return {boolean}\n */\nBitset.prototype.one = function() {\n \n};\n\n/**\n * @return {number}\n */\nBitset.prototype.count = function() {\n \n};\n\n/**\n * @return {string}\n */\nBitset.prototype.toString = function() {\n \n};\n\n/** \n * Your Bitset object will be instantiated and called as such:\n * var obj = new Bitset(size)\n * obj.fix(idx)\n * obj.unfix(idx)\n * obj.flip()\n * var param_4 = obj.all()\n * var param_5 = obj.one()\n * var param_6 = obj.count()\n * var param_7 = obj.toString()\n */"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "class Bitset {\n constructor(size: number) {\n \n }\n\n fix(idx: number): void {\n \n }\n\n unfix(idx: number): void {\n \n }\n\n flip(): void {\n \n }\n\n all(): boolean {\n \n }\n\n one(): boolean {\n \n }\n\n count(): number {\n \n }\n\n toString(): string {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * var obj = new Bitset(size)\n * obj.fix(idx)\n * obj.unfix(idx)\n * obj.flip()\n * var param_4 = obj.all()\n * var param_5 = obj.one()\n * var param_6 = obj.count()\n * var param_7 = obj.toString()\n */"}, {"value": "php", "text": "PHP", "defaultCode": "class Bitset {\n /**\n * @param Integer $size\n */\n function __construct($size) {\n \n }\n \n /**\n * @param Integer $idx\n * @return NULL\n */\n function fix($idx) {\n \n }\n \n /**\n * @param Integer $idx\n * @return NULL\n */\n function unfix($idx) {\n \n }\n \n /**\n * @return NULL\n */\n function flip() {\n \n }\n \n /**\n * @return Boolean\n */\n function all() {\n \n }\n \n /**\n * @return Boolean\n */\n function one() {\n \n }\n \n /**\n * @return Integer\n */\n function count() {\n \n }\n \n /**\n * @return String\n */\n function toString() {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * $obj = Bitset($size);\n * $obj->fix($idx);\n * $obj->unfix($idx);\n * $obj->flip();\n * $ret_4 = $obj->all();\n * $ret_5 = $obj->one();\n * $ret_6 = $obj->count();\n * $ret_7 = $obj->toString();\n */"}, {"value": "swift", "text": "Swift", "defaultCode": "\nclass Bitset {\n\n init(_ size: Int) {\n \n }\n \n func fix(_ idx: Int) {\n \n }\n \n func unfix(_ idx: Int) {\n \n }\n \n func flip() {\n \n }\n \n func all() -> Bool {\n \n }\n \n func one() -> Bool {\n \n }\n \n func count() -> Int {\n \n }\n \n func toString() -> String {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * let obj = Bitset(size)\n * obj.fix(idx)\n * obj.unfix(idx)\n * obj.flip()\n * let ret_4: Bool = obj.all()\n * let ret_5: Bool = obj.one()\n * let ret_6: Int = obj.count()\n * let ret_7: String = obj.toString()\n */"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Bitset(size: Int) {\n\n fun fix(idx: Int) {\n \n }\n\n fun unfix(idx: Int) {\n \n }\n\n fun flip() {\n \n }\n\n fun all(): Boolean {\n \n }\n\n fun one(): Boolean {\n \n }\n\n fun count(): Int {\n \n }\n\n fun toString(): String {\n \n }\n\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * var obj = Bitset(size)\n * obj.fix(idx)\n * obj.unfix(idx)\n * obj.flip()\n * var param_4 = obj.all()\n * var param_5 = obj.one()\n * var param_6 = obj.count()\n * var param_7 = obj.toString()\n */"}, {"value": "dart", "text": "Dart", "defaultCode": "class Bitset {\n\n Bitset(int size) {\n \n }\n \n void fix(int idx) {\n \n }\n \n void unfix(int idx) {\n \n }\n \n void flip() {\n \n }\n \n bool all() {\n \n }\n \n bool one() {\n \n }\n \n int count() {\n \n }\n \n String toString() {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * Bitset obj = Bitset(size);\n * obj.fix(idx);\n * obj.unfix(idx);\n * obj.flip();\n * bool param4 = obj.all();\n * bool param5 = obj.one();\n * int param6 = obj.count();\n * String param7 = obj.toString();\n */"}, {"value": "golang", "text": "Go", "defaultCode": "type Bitset struct {\n \n}\n\n\nfunc Constructor(size int) Bitset {\n \n}\n\n\nfunc (this *Bitset) Fix(idx int) {\n \n}\n\n\nfunc (this *Bitset) Unfix(idx int) {\n \n}\n\n\nfunc (this *Bitset) Flip() {\n \n}\n\n\nfunc (this *Bitset) All() bool {\n \n}\n\n\nfunc (this *Bitset) One() bool {\n \n}\n\n\nfunc (this *Bitset) Count() int {\n \n}\n\n\nfunc (this *Bitset) ToString() string {\n \n}\n\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * obj := Constructor(size);\n * obj.Fix(idx);\n * obj.Unfix(idx);\n * obj.Flip();\n * param_4 := obj.All();\n * param_5 := obj.One();\n * param_6 := obj.Count();\n * param_7 := obj.ToString();\n */"}, {"value": "ruby", "text": "Ruby", "defaultCode": "class Bitset\n\n=begin\n :type size: Integer\n=end\n def initialize(size)\n \n end\n\n\n=begin\n :type idx: Integer\n :rtype: Void\n=end\n def fix(idx)\n \n end\n\n\n=begin\n :type idx: Integer\n :rtype: Void\n=end\n def unfix(idx)\n \n end\n\n\n=begin\n :rtype: Void\n=end\n def flip()\n \n end\n\n\n=begin\n :rtype: Boolean\n=end\n def all()\n \n end\n\n\n=begin\n :rtype: Boolean\n=end\n def one()\n \n end\n\n\n=begin\n :rtype: Integer\n=end\n def count()\n \n end\n\n\n=begin\n :rtype: String\n=end\n def to_string()\n \n end\n\n\nend\n\n# Your Bitset object will be instantiated and called as such:\n# obj = Bitset.new(size)\n# obj.fix(idx)\n# obj.unfix(idx)\n# obj.flip()\n# param_4 = obj.all()\n# param_5 = obj.one()\n# param_6 = obj.count()\n# param_7 = obj.to_string()"}, {"value": "scala", "text": "Scala", "defaultCode": "class Bitset(_size: Int) {\n\n def fix(idx: Int): Unit = {\n \n }\n\n def unfix(idx: Int): Unit = {\n \n }\n\n def flip(): Unit = {\n \n }\n\n def all(): Boolean = {\n \n }\n\n def one(): Boolean = {\n \n }\n\n def count(): Int = {\n \n }\n\n def toString(): String = {\n \n }\n\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * val obj = new Bitset(size)\n * obj.fix(idx)\n * obj.unfix(idx)\n * obj.flip()\n * val param_4 = obj.all()\n * val param_5 = obj.one()\n * val param_6 = obj.count()\n * val param_7 = obj.toString()\n */"}, {"value": "rust", "text": "Rust", "defaultCode": "struct Bitset {\n\n}\n\n\n/** \n * `&self` means the method takes an immutable reference.\n * If you need a mutable reference, change it to `&mut self` instead.\n */\nimpl Bitset {\n\n fn new(size: i32) -> Self {\n \n }\n \n fn fix(&self, idx: i32) {\n \n }\n \n fn unfix(&self, idx: i32) {\n \n }\n \n fn flip(&self) {\n \n }\n \n fn all(&self) -> bool {\n \n }\n \n fn one(&self) -> bool {\n \n }\n \n fn count(&self) -> i32 {\n \n }\n \n fn to_string(&self) -> String {\n \n }\n}\n\n/**\n * Your Bitset object will be instantiated and called as such:\n * let obj = Bitset::new(size);\n * obj.fix(idx);\n * obj.unfix(idx);\n * obj.flip();\n * let ret_4: bool = obj.all();\n * let ret_5: bool = obj.one();\n * let ret_6: i32 = obj.count();\n * let ret_7: String = obj.to_string();\n */"}, {"value": "racket", "text": "Racket", "defaultCode": "(define bitset%\n (class object%\n (super-new)\n \n ; size : exact-integer?\n (init-field\n size)\n \n ; fix : exact-integer? -> void?\n (define/public (fix idx)\n )\n ; unfix : exact-integer? -> void?\n (define/public (unfix idx)\n )\n ; flip : -> void?\n (define/public (flip)\n )\n ; all : -> boolean?\n (define/public (all)\n )\n ; one : -> boolean?\n (define/public (one)\n )\n ; count : -> exact-integer?\n (define/public (count)\n )\n ; to-string : -> string?\n (define/public (to-string)\n )))\n\n;; Your bitset% object will be instantiated and called as such:\n;; (define obj (new bitset% [size size]))\n;; (send obj fix idx)\n;; (send obj unfix idx)\n;; (send obj flip)\n;; (define param_4 (send obj all))\n;; (define param_5 (send obj one))\n;; (define param_6 (send obj count))\n;; (define param_7 (send obj to-string))"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec bitset_init_(Size :: integer()) -> any().\nbitset_init_(Size) ->\n .\n\n-spec bitset_fix(Idx :: integer()) -> any().\nbitset_fix(Idx) ->\n .\n\n-spec bitset_unfix(Idx :: integer()) -> any().\nbitset_unfix(Idx) ->\n .\n\n-spec bitset_flip() -> any().\nbitset_flip() ->\n .\n\n-spec bitset_all() -> boolean().\nbitset_all() ->\n .\n\n-spec bitset_one() -> boolean().\nbitset_one() ->\n .\n\n-spec bitset_count() -> integer().\nbitset_count() ->\n .\n\n-spec bitset_to_string() -> unicode:unicode_binary().\nbitset_to_string() ->\n .\n\n\n%% Your functions will be called as such:\n%% bitset_init_(Size),\n%% bitset_fix(Idx),\n%% bitset_unfix(Idx),\n%% bitset_flip(),\n%% Param_4 = bitset_all(),\n%% Param_5 = bitset_one(),\n%% Param_6 = bitset_count(),\n%% Param_7 = bitset_to_string(),\n\n%% bitset_init_ will be called before every test case, in which you can do some necessary initializations."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Bitset do\n @spec init_(size :: integer) :: any\n def init_(size) do\n \n end\n\n @spec fix(idx :: integer) :: any\n def fix(idx) do\n \n end\n\n @spec unfix(idx :: integer) :: any\n def unfix(idx) do\n \n end\n\n @spec flip() :: any\n def flip() do\n \n end\n\n @spec all() :: boolean\n def all() do\n \n end\n\n @spec one() :: boolean\n def one() do\n \n end\n\n @spec count() :: integer\n def count() do\n \n end\n\n @spec to_string() :: String.t\n def to_string() do\n \n end\nend\n\n# Your functions will be called as such:\n# Bitset.init_(size)\n# Bitset.fix(idx)\n# Bitset.unfix(idx)\n# Bitset.flip()\n# param_4 = Bitset.all()\n# param_5 = Bitset.one()\n# param_6 = Bitset.count()\n# param_7 = Bitset.to_string()\n\n# Bitset.init_ will be called before every test case, in which you can do some necessary initializations."}] | ["Bitset","fix","fix","flip","all","unfix","flip","one","unfix","count","toString"]
[[5],[3],[1],[],[],[0],[],[],[0],[],[]] | {
"classname": "Bitset",
"constructor": {
"params": [
{
"type": "integer",
"name": "size"
}
]
},
"methods": [
{
"params": [
{
"type": "integer",
"name": "idx"
}
],
"name": "fix",
"return": {
"type": "void"
}
},
{
"params": [
{
"type": "integer",
"name": "idx"
}
],
"name": "unfix",
"return": {
"type": "void"
}
},
{
"params": [],
"name": "flip",
"return": {
"type": "void"
}
},
{
"params": [],
"name": "all",
"return": {
"type": "boolean"
}
},
{
"params": [],
"name": "one",
"return": {
"type": "boolean"
}
},
{
"params": [],
"name": "count",
"return": {
"type": "integer"
}
},
{
"params": [],
"name": "toString",
"return": {
"type": "string"
}
}
],
"return": {
"type": "boolean"
},
"systemdesign": true,
"manual": false
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'String', 'Design'] |
2,167 | Minimum Time to Remove All Cars Containing Illegal Goods | minimum-time-to-remove-all-cars-containing-illegal-goods | <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> which represents a sequence of train cars. <code>s[i] = '0'</code> denotes that the <code>i<sup>th</sup></code> car does <strong>not</strong> contain illegal goods and <code>s[i] = '1'</code> denotes that the <code>i<sup>th</sup></code> car does contain illegal goods.</p>
<p>As the train conductor, you would like to get rid of all the cars containing illegal goods. You can do any of the following three operations <strong>any</strong> number of times:</p>
<ol>
<li>Remove a train car from the <strong>left</strong> end (i.e., remove <code>s[0]</code>) which takes 1 unit of time.</li>
<li>Remove a train car from the <strong>right</strong> end (i.e., remove <code>s[s.length - 1]</code>) which takes 1 unit of time.</li>
<li>Remove a train car from <strong>anywhere</strong> in the sequence which takes 2 units of time.</li>
</ol>
<p>Return <em>the <strong>minimum</strong> time to remove all the cars containing illegal goods</em>.</p>
<p>Note that an empty sequence of cars is considered to have no cars containing illegal goods.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "<strong><u>11</u></strong>00<strong><u>1</u></strong>0<strong><u>1</u></strong>"
<strong>Output:</strong> 5
<strong>Explanation:</strong>
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 2 times. Time taken is 2 * 1 = 2.
- remove a car from the right end. Time taken is 1.
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2 + 1 + 2 = 5.
An alternative way is to
- remove a car from the left end 2 times. Time taken is 2 * 1 = 2.
- remove a car from the right end 3 times. Time taken is 3 * 1 = 3.
This also obtains a total time of 2 + 3 = 5.
5 is the minimum time taken to remove all the cars containing illegal goods.
There are no other ways to remove them with less time.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "00<strong><u>1</u></strong>0"
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 3 times. Time taken is 3 * 1 = 3.
This obtains a total time of 3.
Another way to remove all the cars containing illegal goods from the sequence is to
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2.
Another way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the right end 2 times. Time taken is 2 * 1 = 2.
This obtains a total time of 2.
2 is the minimum time taken to remove all the cars containing illegal goods.
There are no other ways to remove them with less time.</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 2 * 10<sup>5</sup></code></li>
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
</ul>
| Hard | 13.9K | 33.8K | 13,908 | 33,819 | 41.1% | ['minimum-number-of-k-consecutive-bit-flips'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumTime(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumTime(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumTime(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumTime(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumTime(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumTime(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar minimumTime = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumTime(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minimumTime($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumTime(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumTime(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumTime(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumTime(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef minimum_time(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumTime(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_time(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-time s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_time(S :: unicode:unicode_binary()) -> integer().\nminimum_time(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_time(s :: String.t) :: integer\n def minimum_time(s) do\n \n end\nend"}] | "1100101" | {
"name": "minimumTime",
"params": [
{
"name": "s",
"type": "string"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String', 'Dynamic Programming'] |
2,168 | Unique Substrings With Equal Digit Frequency | unique-substrings-with-equal-digit-frequency | null | Medium | 9.1K | 14.2K | 9,082 | 14,181 | 64.0% | ['number-of-equal-count-substrings', 'substrings-that-begin-and-end-with-the-same-letter'] | [] | Algorithms | null | "1212" | {
"name": "equalDigitFrequency",
"params": [
{
"name": "s",
"type": "string"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Hash Table', 'String', 'Rolling Hash', 'Counting', 'Hash Function'] |
2,169 | Count Operations to Obtain Zero | count-operations-to-obtain-zero | <p>You are given two <strong>non-negative</strong> integers <code>num1</code> and <code>num2</code>.</p>
<p>In one <strong>operation</strong>, if <code>num1 >= num2</code>, you must subtract <code>num2</code> from <code>num1</code>, otherwise subtract <code>num1</code> from <code>num2</code>.</p>
<ul>
<li>For example, if <code>num1 = 5</code> and <code>num2 = 4</code>, subtract <code>num2</code> from <code>num1</code>, thus obtaining <code>num1 = 1</code> and <code>num2 = 4</code>. However, if <code>num1 = 4</code> and <code>num2 = 5</code>, after one operation, <code>num1 = 4</code> and <code>num2 = 1</code>.</li>
</ul>
<p>Return <em>the <strong>number of operations</strong> required to make either</em> <code>num1 = 0</code> <em>or</em> <code>num2 = 0</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num1 = 2, num2 = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
So the total number of operations required is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num1 = 10, num2 = 10
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
So the total number of operations required is 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= num1, num2 <= 10<sup>5</sup></code></li>
</ul>
| Easy | 90K | 120.5K | 89,955 | 120,450 | 74.7% | ['number-of-steps-to-reduce-a-number-to-zero'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countOperations(int num1, int num2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countOperations(int num1, int num2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countOperations(self, num1, num2):\n \"\"\"\n :type num1: int\n :type num2: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countOperations(self, num1: int, num2: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countOperations(int num1, int num2) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountOperations(int num1, int num2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num1\n * @param {number} num2\n * @return {number}\n */\nvar countOperations = function(num1, num2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countOperations(num1: number, num2: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num1\n * @param Integer $num2\n * @return Integer\n */\n function countOperations($num1, $num2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countOperations(_ num1: Int, _ num2: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countOperations(num1: Int, num2: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countOperations(int num1, int num2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countOperations(num1 int, num2 int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num1\n# @param {Integer} num2\n# @return {Integer}\ndef count_operations(num1, num2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countOperations(num1: Int, num2: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_operations(num1: i32, num2: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-operations num1 num2)\n (-> exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_operations(Num1 :: integer(), Num2 :: integer()) -> integer().\ncount_operations(Num1, Num2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_operations(num1 :: integer, num2 :: integer) :: integer\n def count_operations(num1, num2) do\n \n end\nend"}] | 2
3 | {
"name": "countOperations",
"params": [
{
"name": "num1",
"type": "integer"
},
{
"type": "integer",
"name": "num2"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Simulation'] |
2,170 | Minimum Operations to Make the Array Alternating | minimum-operations-to-make-the-array-alternating | <p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <code>n</code> positive integers.</p>
<p>The array <code>nums</code> is called <strong>alternating</strong> if:</p>
<ul>
<li><code>nums[i - 2] == nums[i]</code>, where <code>2 <= i <= n - 1</code>.</li>
<li><code>nums[i - 1] != nums[i]</code>, where <code>1 <= i <= n - 1</code>.</li>
</ul>
<p>In one <strong>operation</strong>, you can choose an index <code>i</code> and <strong>change</strong> <code>nums[i]</code> into <strong>any</strong> positive integer.</p>
<p>Return <em>the <strong>minimum number of operations</strong> required to make the array alternating</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,3,2,4,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [3,1,3,<u><strong>1</strong></u>,<u><strong>3</strong></u>,<u><strong>1</strong></u>].
The number of operations required in this case is 3.
It can be proven that it is not possible to make the array alternating in less than 3 operations.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,2,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One way to make the array alternating is by converting it to [1,2,<u><strong>1</strong></u>,2,<u><strong>1</strong></u>].
The number of operations required in this case is 2.
Note that the array cannot be converted to [<u><strong>2</strong></u>,2,2,2,2] because in this case nums[0] == nums[1] which violates the conditions of an alternating array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| Medium | 24.8K | 73.5K | 24,805 | 73,486 | 33.8% | ['minimum-deletions-to-make-array-beautiful', 'minimum-number-of-flips-to-make-the-binary-string-alternating'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumOperations(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumOperations(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumOperations(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumOperations(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumOperations(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumOperations(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minimumOperations = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumOperations(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minimumOperations($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumOperations(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumOperations(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumOperations(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumOperations(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef minimum_operations(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumOperations(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_operations(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-operations nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_operations(Nums :: [integer()]) -> integer().\nminimum_operations(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_operations(nums :: [integer]) :: integer\n def minimum_operations(nums) do\n \n end\nend"}] | [3,1,3,2,4,3] | {
"name": "minimumOperations",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Greedy', 'Counting'] |
2,171 | Removing Minimum Number of Magic Beans | removing-minimum-number-of-magic-beans | <p>You are given an array of <strong>positive</strong> integers <code>beans</code>, where each integer represents the number of magic beans found in a particular magic bag.</p>
<p><strong>Remove</strong> any number of beans (<strong>possibly none</strong>) from each bag such that the number of beans in each remaining <strong>non-empty</strong> bag (still containing <strong>at least one</strong> bean) is <strong>equal</strong>. Once a bean has been removed from a bag, you are <strong>not</strong> allowed to return it to any of the bags.</p>
<p>Return <em>the <strong>minimum</strong> number of magic beans that you have to remove</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> beans = [4,1,6,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
- We remove 1 bean from the bag with only 1 bean.
This results in the remaining bags: [4,<strong><u>0</u></strong>,6,5]
- Then we remove 2 beans from the bag with 6 beans.
This results in the remaining bags: [4,0,<strong><u>4</u></strong>,5]
- Then we remove 1 bean from the bag with 5 beans.
This results in the remaining bags: [4,0,4,<strong><u>4</u></strong>]
We removed a total of 1 + 2 + 1 = 4 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that remove 4 beans or fewer.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> beans = [2,10,3,2]
<strong>Output:</strong> 7
<strong>Explanation:</strong>
- We remove 2 beans from one of the bags with 2 beans.
This results in the remaining bags: [<u><strong>0</strong></u>,10,3,2]
- Then we remove 2 beans from the other bag with 2 beans.
This results in the remaining bags: [0,10,3,<u><strong>0</strong></u>]
- Then we remove 3 beans from the bag with 3 beans.
This results in the remaining bags: [0,10,<u><strong>0</strong></u>,0]
We removed a total of 2 + 2 + 3 = 7 beans to make the remaining non-empty bags have an equal number of beans.
There are no other solutions that removes 7 beans or fewer.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= beans.length <= 10<sup>5</sup></code></li>
<li><code>1 <= beans[i] <= 10<sup>5</sup></code></li>
</ul>
| Medium | 30.5K | 70.4K | 30,508 | 70,368 | 43.4% | ['minimum-moves-to-equal-array-elements-ii', 'minimum-operations-to-reduce-x-to-zero'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minimumRemoval(vector<int>& beans) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minimumRemoval(int[] beans) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumRemoval(self, beans):\n \"\"\"\n :type beans: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumRemoval(self, beans: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minimumRemoval(int* beans, int beansSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinimumRemoval(int[] beans) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} beans\n * @return {number}\n */\nvar minimumRemoval = function(beans) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumRemoval(beans: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $beans\n * @return Integer\n */\n function minimumRemoval($beans) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumRemoval(_ beans: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumRemoval(beans: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumRemoval(List<int> beans) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumRemoval(beans []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} beans\n# @return {Integer}\ndef minimum_removal(beans)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumRemoval(beans: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_removal(beans: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-removal beans)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_removal(Beans :: [integer()]) -> integer().\nminimum_removal(Beans) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_removal(beans :: [integer]) :: integer\n def minimum_removal(beans) do\n \n end\nend"}] | [4,1,6,5] | {
"name": "minimumRemoval",
"params": [
{
"name": "beans",
"type": "integer[]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy', 'Sorting', 'Enumeration', 'Prefix Sum'] |
2,172 | Maximum AND Sum of Array | maximum-and-sum-of-array | <p>You are given an integer array <code>nums</code> of length <code>n</code> and an integer <code>numSlots</code> such that <code>2 * numSlots >= n</code>. There are <code>numSlots</code> slots numbered from <code>1</code> to <code>numSlots</code>.</p>
<p>You have to place all <code>n</code> integers into the slots such that each slot contains at <strong>most</strong> two numbers. The <strong>AND sum</strong> of a given placement is the sum of the <strong>bitwise</strong> <code>AND</code> of every number with its respective slot number.</p>
<ul>
<li>For example, the <strong>AND sum</strong> of placing the numbers <code>[1, 3]</code> into slot <u><code>1</code></u> and <code>[4, 6]</code> into slot <u><code>2</code></u> is equal to <code>(1 AND <u>1</u>) + (3 AND <u>1</u>) + (4 AND <u>2</u>) + (6 AND <u>2</u>) = 1 + 1 + 0 + 2 = 4</code>.</li>
</ul>
<p>Return <em>the maximum possible <strong>AND sum</strong> of </em><code>nums</code><em> given </em><code>numSlots</code><em> slots.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6], numSlots = 3
<strong>Output:</strong> 9
<strong>Explanation:</strong> One possible placement is [1, 4] into slot <u>1</u>, [2, 6] into slot <u>2</u>, and [3, 5] into slot <u>3</u>.
This gives the maximum AND sum of (1 AND <u>1</u>) + (4 AND <u>1</u>) + (2 AND <u>2</u>) + (6 AND <u>2</u>) + (3 AND <u>3</u>) + (5 AND <u>3</u>) = 1 + 0 + 2 + 2 + 3 + 1 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,10,4,7,1], numSlots = 9
<strong>Output:</strong> 24
<strong>Explanation:</strong> One possible placement is [1, 1] into slot <u>1</u>, [3] into slot <u>3</u>, [4] into slot <u>4</u>, [7] into slot <u>7</u>, and [10] into slot <u>9</u>.
This gives the maximum AND sum of (1 AND <u>1</u>) + (1 AND <u>1</u>) + (3 AND <u>3</u>) + (4 AND <u>4</u>) + (7 AND <u>7</u>) + (10 AND <u>9</u>) = 1 + 1 + 3 + 4 + 7 + 8 = 24.
Note that slots 2, 5, 6, and 8 are empty which is permitted.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>1 <= numSlots <= 9</code></li>
<li><code>1 <= n <= 2 * numSlots</code></li>
<li><code>1 <= nums[i] <= 15</code></li>
</ul>
| Hard | 15.6K | 31.5K | 15,617 | 31,500 | 49.6% | ['minimum-xor-sum-of-two-arrays'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumANDSum(vector<int>& nums, int numSlots) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumANDSum(int[] nums, int numSlots) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumANDSum(self, nums, numSlots):\n \"\"\"\n :type nums: List[int]\n :type numSlots: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumANDSum(self, nums: List[int], numSlots: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumANDSum(int* nums, int numsSize, int numSlots) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumANDSum(int[] nums, int numSlots) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} numSlots\n * @return {number}\n */\nvar maximumANDSum = function(nums, numSlots) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumANDSum(nums: number[], numSlots: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $numSlots\n * @return Integer\n */\n function maximumANDSum($nums, $numSlots) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumANDSum(_ nums: [Int], _ numSlots: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumANDSum(nums: IntArray, numSlots: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumANDSum(List<int> nums, int numSlots) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumANDSum(nums []int, numSlots int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} num_slots\n# @return {Integer}\ndef maximum_and_sum(nums, num_slots)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumANDSum(nums: Array[Int], numSlots: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_and_sum(nums: Vec<i32>, num_slots: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-and-sum nums numSlots)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_and_sum(Nums :: [integer()], NumSlots :: integer()) -> integer().\nmaximum_and_sum(Nums, NumSlots) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_and_sum(nums :: [integer], num_slots :: integer) :: integer\n def maximum_and_sum(nums, num_slots) do\n \n end\nend"}] | [1,2,3,4,5,6]
3 | {
"name": "maximumANDSum",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "numSlots"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming', 'Bit Manipulation', 'Bitmask'] |
2,173 | Longest Winning Streak | longest-winning-streak | null | Hard | 6.2K | 11.5K | 6,162 | 11,470 | 53.7% | [] | ["Create table If Not Exists Matches (player_id int, match_day date, result ENUM('Win', 'Draw', 'Lose'))", 'Truncate table Matches', "insert into Matches (player_id, match_day, result) values ('1', '2022-01-17', 'Win')", "insert into Matches (player_id, match_day, result) values ('1', '2022-01-18', 'Win')", "insert into Matches (player_id, match_day, result) values ('1', '2022-01-25', 'Win')", "insert into Matches (player_id, match_day, result) values ('1', '2022-01-31', 'Draw')", "insert into Matches (player_id, match_day, result) values ('1', '2022-02-08', 'Win')", "insert into Matches (player_id, match_day, result) values ('2', '2022-02-06', 'Lose')", "insert into Matches (player_id, match_day, result) values ('2', '2022-02-08', 'Lose')", "insert into Matches (player_id, match_day, result) values ('3', '2022-03-30', 'Win')"] | Database | null | {"headers":{"Matches":["player_id","match_day","result"]},"rows":{"Matches":[[1,"2022-01-17","Win"],[1,"2022-01-18","Win"],[1,"2022-01-25","Win"],[1,"2022-01-31","Draw"],[1,"2022-02-08","Win"],[2,"2022-02-06","Lose"],[2,"2022-02-08","Lose"],[3,"2022-03-30","Win"]]}} | {"mysql": ["Create table If Not Exists Matches (player_id int, match_day date, result ENUM('Win', 'Draw', 'Lose'))"], "mssql": ["Create table Matches (player_id int, match_day date, result VARCHAR(4) NOT NULL CHECK (result IN ('Win', 'Draw', 'Lose')))"], "oraclesql": ["Create table Matches (player_id int, match_day date, result VARCHAR(4) NOT NULL CHECK (result IN ('Win', 'Draw', 'Lose')))", "ALTER SESSION SET nls_date_format='YYYY-MM-DD'"], "database": true, "name": "longest_winning_streak", "pythondata": ["Matches = pd.DataFrame([], columns=['player_id', 'match_day', 'result']).astype({'player_id':'Int64', 'match_day':'datetime64[ns]', 'result':'object'})"], "postgresql": ["Create table If Not Exists Matches (player_id int, match_day date, result VARCHAR(30) CHECK (result IN ('Win', 'Draw', 'Lose')))\n"], "database_schema": {"Matches": {"player_id": "INT", "match_day": "DATE", "result": "ENUM('Win', 'Draw', 'Lose')"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,174 | Remove All Ones With Row and Column Flips II | remove-all-ones-with-row-and-column-flips-ii | null | Medium | 5.3K | 7.9K | 5,274 | 7,889 | 66.9% | ['set-matrix-zeroes', 'minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix', 'remove-all-ones-with-row-and-column-flips'] | [] | Algorithms | null | [[1,1,1],[1,1,1],[0,1,0]] | {
"name": "removeOnes",
"params": [
{
"name": "grid",
"type": "integer[][]"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Bit Manipulation', 'Breadth-First Search', 'Matrix'] |
2,175 | The Change in Global Rankings | the-change-in-global-rankings | null | Medium | 7.6K | 12.6K | 7,640 | 12,599 | 60.6% | [] | ['Create table If Not Exists TeamPoints (team_id int, name varchar(100), points int)', 'Create table If Not Exists PointsChange (team_id int, points_change int)', 'Truncate table TeamPoints', "insert into TeamPoints (team_id, name, points) values ('3', 'Algeria', '1431')", "insert into TeamPoints (team_id, name, points) values ('1', 'Senegal', '2132')", "insert into TeamPoints (team_id, name, points) values ('2', 'New Zealand', '1402')", "insert into TeamPoints (team_id, name, points) values ('4', 'Croatia', '1817')", 'Truncate table PointsChange', "insert into PointsChange (team_id, points_change) values ('3', '399')", "insert into PointsChange (team_id, points_change) values ('2', '0')", "insert into PointsChange (team_id, points_change) values ('4', '13')", "insert into PointsChange (team_id, points_change) values ('1', '-22')"] | Database | null | {"headers": {"TeamPoints": ["team_id", "name", "points"], "PointsChange": ["team_id", "points_change"]}, "rows": {"TeamPoints": [[3, "Algeria", 1431], [1, "Senegal", 2132], [2, "New Zealand", 1402], [4, "Croatia", 1817]], "PointsChange": [[3, 399], [2, 0], [4, 13], [1, -22]]}} | {"mysql": ["Create table If Not Exists TeamPoints (team_id int, name varchar(100), points int)", "Create table If Not Exists PointsChange (team_id int, points_change int)"], "mssql": ["Create table TeamPoints (team_id int, name varchar(100), points int)", "Create table PointsChange (team_id int, points_change int)"], "oraclesql": ["Create table TeamPoints (team_id int, name varchar(100), points int)", "Create table PointsChange (team_id int, points_change int)"], "database": true, "name": "global_ratings_change", "pythondata": ["TeamPoints = pd.DataFrame([], columns=['team_id', 'name', 'points']).astype({'team_id':'Int64', 'name':'object', 'points':'Int64'})", "PointsChange = pd.DataFrame([], columns=['team_id', 'points_change']).astype({'team_id':'Int64', 'points_change':'Int64'})"], "postgresql": ["Create table If Not Exists TeamPoints (team_id int, name varchar(100), points int)", "Create table If Not Exists PointsChange (team_id int, points_change int)"], "database_schema": {"TeamPoints": {"team_id": "INT", "name": "VARCHAR(100)", "points": "INT"}, "PointsChange": {"team_id": "INT", "points_change": "INT"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,176 | Count Equal and Divisible Pairs in an Array | count-equal-and-divisible-pairs-in-an-array | Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>, return <em>the <strong>number of pairs</strong></em> <code>(i, j)</code> <em>where</em> <code>0 <= i < j < n</code>, <em>such that</em> <code>nums[i] == nums[j]</code> <em>and</em> <code>(i * j)</code> <em>is divisible by</em> <code>k</code>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,2,2,2,1,3], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong>
There are 4 pairs that meet all the requirements:
- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
- nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 100</code></li>
<li><code>1 <= nums[i], k <= 100</code></li>
</ul>
| Easy | 90.9K | 114.4K | 90,881 | 114,356 | 79.5% | ['count-number-of-pairs-with-absolute-difference-k', 'count-number-of-bad-pairs'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countPairs(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countPairs(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countPairs(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countPairs(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countPairs(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountPairs(int[] nums, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar countPairs = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countPairs(nums: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function countPairs($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countPairs(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countPairs(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countPairs(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countPairs(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef count_pairs(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countPairs(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-pairs nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_pairs(Nums :: [integer()], K :: integer()) -> integer().\ncount_pairs(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_pairs(nums :: [integer], k :: integer) :: integer\n def count_pairs(nums, k) do\n \n end\nend"}] | [3,1,2,2,2,1,3]
2 | {
"name": "countPairs",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array'] |
2,177 | Find Three Consecutive Integers That Sum to a Given Number | find-three-consecutive-integers-that-sum-to-a-given-number | <p>Given an integer <code>num</code>, return <em>three consecutive integers (as a sorted array)</em><em> that <strong>sum</strong> to </em><code>num</code>. If <code>num</code> cannot be expressed as the sum of three consecutive integers, return<em> an <strong>empty</strong> array.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 33
<strong>Output:</strong> [10,11,12]
<strong>Explanation:</strong> 33 can be expressed as 10 + 11 + 12 = 33.
10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 4
<strong>Output:</strong> []
<strong>Explanation:</strong> There is no way to express 4 as the sum of 3 consecutive integers.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= num <= 10<sup>15</sup></code></li>
</ul>
| Medium | 55.5K | 86K | 55,479 | 85,974 | 64.5% | ['longest-consecutive-sequence', 'number-of-ways-to-buy-pens-and-pencils'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> sumOfThree(long long num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long[] sumOfThree(long num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sumOfThree(self, num):\n \"\"\"\n :type num: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def sumOfThree(self, num: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nlong long* sumOfThree(long long num, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long[] SumOfThree(long num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number[]}\n */\nvar sumOfThree = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sumOfThree(num: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer[]\n */\n function sumOfThree($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sumOfThree(_ num: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sumOfThree(num: Long): LongArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> sumOfThree(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sumOfThree(num int64) []int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer[]}\ndef sum_of_three(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sumOfThree(num: Long): Array[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sum_of_three(num: i64) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sum-of-three num)\n (-> exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sum_of_three(Num :: integer()) -> [integer()].\nsum_of_three(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sum_of_three(num :: integer) :: [integer]\n def sum_of_three(num) do\n \n end\nend"}] | 33 | {
"name": "sumOfThree",
"params": [
{
"name": "num",
"type": "long"
}
],
"return": {
"type": "long[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Simulation'] |
2,178 | Maximum Split of Positive Even Integers | maximum-split-of-positive-even-integers | <p>You are given an integer <code>finalSum</code>. Split it into a sum of a <strong>maximum</strong> number of <strong>unique</strong> positive even integers.</p>
<ul>
<li>For example, given <code>finalSum = 12</code>, the following splits are <strong>valid</strong> (unique positive even integers summing up to <code>finalSum</code>): <code>(12)</code>, <code>(2 + 10)</code>, <code>(2 + 4 + 6)</code>, and <code>(4 + 8)</code>. Among them, <code>(2 + 4 + 6)</code> contains the maximum number of integers. Note that <code>finalSum</code> cannot be split into <code>(2 + 2 + 4 + 4)</code> as all the numbers should be unique.</li>
</ul>
<p>Return <em>a list of integers that represent a valid split containing a <strong>maximum</strong> number of integers</em>. If no valid split exists for <code>finalSum</code>, return <em>an <strong>empty</strong> list</em>. You may return the integers in <strong>any</strong> order.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> finalSum = 12
<strong>Output:</strong> [2,4,6]
<strong>Explanation:</strong> The following are valid splits: <code>(12)</code>, <code>(2 + 10)</code>, <code>(2 + 4 + 6)</code>, and <code>(4 + 8)</code>.
(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].
Note that [2,6,4], [6,2,4], etc. are also accepted.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> finalSum = 7
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no valid splits for the given finalSum.
Thus, we return an empty array.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> finalSum = 28
<strong>Output:</strong> [6,8,2,12]
<strong>Explanation:</strong> The following are valid splits: <code>(2 + 26)</code>, <code>(6 + 8 + 2 + 12)</code>, and <code>(4 + 24)</code>.
<code>(6 + 8 + 2 + 12)</code> has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].
Note that [10,2,4,12], [6,2,4,16], etc. are also accepted.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= finalSum <= 10<sup>10</sup></code></li>
</ul>
| Medium | 50.7K | 85.4K | 50,658 | 85,436 | 59.3% | [] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> maximumEvenSplit(long long finalSum) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Long> maximumEvenSplit(long finalSum) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumEvenSplit(self, finalSum):\n \"\"\"\n :type finalSum: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumEvenSplit(self, finalSum: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nlong long* maximumEvenSplit(long long finalSum, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<long> MaximumEvenSplit(long finalSum) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} finalSum\n * @return {number[]}\n */\nvar maximumEvenSplit = function(finalSum) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumEvenSplit(finalSum: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $finalSum\n * @return Integer[]\n */\n function maximumEvenSplit($finalSum) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumEvenSplit(_ finalSum: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumEvenSplit(finalSum: Long): List<Long> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> maximumEvenSplit(int finalSum) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumEvenSplit(finalSum int64) []int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} final_sum\n# @return {Integer[]}\ndef maximum_even_split(final_sum)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumEvenSplit(finalSum: Long): List[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_even_split(final_sum: i64) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-even-split finalSum)\n (-> exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_even_split(FinalSum :: integer()) -> [integer()].\nmaximum_even_split(FinalSum) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_even_split(final_sum :: integer) :: [integer]\n def maximum_even_split(final_sum) do\n \n end\nend"}] | 12 | {
"name": "maximumEvenSplit",
"params": [
{
"name": "finalSum",
"type": "long"
}
],
"return": {
"type": "list<long>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Backtracking', 'Greedy'] |
2,179 | Count Good Triplets in an Array | count-good-triplets-in-an-array | <p>You are given two <strong>0-indexed</strong> arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>, both of which are <strong>permutations</strong> of <code>[0, 1, ..., n - 1]</code>.</p>
<p>A <strong>good triplet</strong> is a set of <code>3</code> <strong>distinct</strong> values which are present in <strong>increasing order</strong> by position both in <code>nums1</code> and <code>nums2</code>. In other words, if we consider <code>pos1<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums1</code> and <code>pos2<sub>v</sub></code> as the index of the value <code>v</code> in <code>nums2</code>, then a good triplet will be a set <code>(x, y, z)</code> where <code>0 <= x, y, z <= n - 1</code>, such that <code>pos1<sub>x</sub> < pos1<sub>y</sub> < pos1<sub>z</sub></code> and <code>pos2<sub>x</sub> < pos2<sub>y</sub> < pos2<sub>z</sub></code>.</p>
<p>Return <em>the <strong>total number</strong> of good triplets</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [2,0,1,3], nums2 = [0,1,2,3]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
There are 4 triplets (x,y,z) such that pos1<sub>x</sub> < pos1<sub>y</sub> < pos1<sub>z</sub>. They are (2,0,1), (2,0,3), (2,1,3), and (0,1,3).
Out of those triplets, only the triplet (0,1,3) satisfies pos2<sub>x</sub> < pos2<sub>y</sub> < pos2<sub>z</sub>. Hence, there is only 1 good triplet.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums1 = [4,0,1,3,2], nums2 = [4,1,0,2,3]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The 4 good triplets are (4,0,3), (4,0,2), (4,1,3), and (4,1,2).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums1.length == nums2.length</code></li>
<li><code>3 <= n <= 10<sup>5</sup></code></li>
<li><code>0 <= nums1[i], nums2[i] <= n - 1</code></li>
<li><code>nums1</code> and <code>nums2</code> are permutations of <code>[0, 1, ..., n - 1]</code>.</li>
</ul>
| Hard | 9.9K | 23.4K | 9,873 | 23,418 | 42.2% | ['count-of-smaller-numbers-after-self', 'increasing-triplet-subsequence', 'create-sorted-array-through-instructions', 'number-of-good-paths', 'count-increasing-quadruplets'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long goodTriplets(vector<int>& nums1, vector<int>& nums2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long goodTriplets(int[] nums1, int[] nums2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def goodTriplets(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def goodTriplets(self, nums1: List[int], nums2: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long goodTriplets(int* nums1, int nums1Size, int* nums2, int nums2Size) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long GoodTriplets(int[] nums1, int[] nums2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar goodTriplets = function(nums1, nums2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function goodTriplets(nums1: number[], nums2: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @return Integer\n */\n function goodTriplets($nums1, $nums2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func goodTriplets(_ nums1: [Int], _ nums2: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun goodTriplets(nums1: IntArray, nums2: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int goodTriplets(List<int> nums1, List<int> nums2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func goodTriplets(nums1 []int, nums2 []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef good_triplets(nums1, nums2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def goodTriplets(nums1: Array[Int], nums2: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn good_triplets(nums1: Vec<i32>, nums2: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (good-triplets nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec good_triplets(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\ngood_triplets(Nums1, Nums2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec good_triplets(nums1 :: [integer], nums2 :: [integer]) :: integer\n def good_triplets(nums1, nums2) do\n \n end\nend"}] | [2,0,1,3]
[0,1,2,3] | {
"name": "goodTriplets",
"params": [
{
"name": "nums1",
"type": "integer[]"
},
{
"type": "integer[]",
"name": "nums2"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search', 'Divide and Conquer', 'Binary Indexed Tree', 'Segment Tree', 'Merge Sort', 'Ordered Set'] |
2,180 | Count Integers With Even Digit Sum | count-integers-with-even-digit-sum | <p>Given a positive integer <code>num</code>, return <em>the number of positive integers <strong>less than or equal to</strong></em> <code>num</code> <em>whose digit sums are <strong>even</strong></em>.</p>
<p>The <strong>digit sum</strong> of a positive integer is the sum of all its digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> num = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The only integers less than or equal to 4 whose digit sums are even are 2 and 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> num = 30
<strong>Output:</strong> 14
<strong>Explanation:</strong>
The 14 integers less than or equal to 30 whose digit sums are even are
2, 4, 6, 8, 11, 13, 15, 17, 19, 20, 22, 24, 26, and 28.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= num <= 1000</code></li>
</ul>
| Easy | 77.1K | 112.7K | 77,095 | 112,693 | 68.4% | ['sum-of-numbers-with-units-digit-k', 'sum-of-digits-of-string-after-convert', 'number-of-ways-to-buy-pens-and-pencils', 'separate-the-digits-in-an-array', 'find-if-digit-game-can-be-won'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countEven(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countEven(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countEven(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countEven(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countEven(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountEven(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar countEven = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countEven(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function countEven($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countEven(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countEven(num: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countEven(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countEven(num int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef count_even(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countEven(num: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_even(num: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-even num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_even(Num :: integer()) -> integer().\ncount_even(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_even(num :: integer) :: integer\n def count_even(num) do\n \n end\nend"}] | 4 | {
"name": "countEven",
"params": [
{
"name": "num",
"type": "integer"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Simulation'] |
2,181 | Merge Nodes in Between Zeros | merge-nodes-in-between-zeros | <p>You are given the <code>head</code> of a linked list, which contains a series of integers <strong>separated</strong> by <code>0</code>'s. The <strong>beginning</strong> and <strong>end</strong> of the linked list will have <code>Node.val == 0</code>.</p>
<p>For <strong>every </strong>two consecutive <code>0</code>'s, <strong>merge</strong> all the nodes lying in between them into a single node whose value is the <strong>sum</strong> of all the merged nodes. The modified list should not contain any <code>0</code>'s.</p>
<p>Return <em>the</em> <code>head</code> <em>of the modified linked list</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex1-1.png" style="width: 600px; height: 41px;" />
<pre>
<strong>Input:</strong> head = [0,3,1,0,4,5,2,0]
<strong>Output:</strong> [4,11]
<strong>Explanation:</strong>
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 3 + 1 = 4.
- The sum of the nodes marked in red: 4 + 5 + 2 = 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex2-1.png" style="width: 600px; height: 41px;" />
<pre>
<strong>Input:</strong> head = [0,1,0,3,0,2,2,0]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong>
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 1 = 1.
- The sum of the nodes marked in red: 3 = 3.
- The sum of the nodes marked in yellow: 2 + 2 = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the list is in the range <code>[3, 2 * 10<sup>5</sup>]</code>.</li>
<li><code>0 <= Node.val <= 1000</code></li>
<li>There are <strong>no</strong> two consecutive nodes with <code>Node.val == 0</code>.</li>
<li>The <strong>beginning</strong> and <strong>end</strong> of the linked list have <code>Node.val == 0</code>.</li>
</ul>
| Medium | 302K | 337.1K | 301,952 | 337,104 | 89.6% | ['linked-list-components'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* mergeNodes(ListNode* head) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * int val;\n * ListNode next;\n * ListNode() {}\n * ListNode(int val) { this.val = val; }\n * ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n public ListNode mergeNodes(ListNode head) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution(object):\n def mergeNodes(self, head):\n \"\"\"\n :type head: Optional[ListNode]\n :rtype: Optional[ListNode]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def mergeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * struct ListNode *next;\n * };\n */\nstruct ListNode* mergeNodes(struct ListNode* head) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public int val;\n * public ListNode next;\n * public ListNode(int val=0, ListNode next=null) {\n * this.val = val;\n * this.next = next;\n * }\n * }\n */\npublic class Solution {\n public ListNode MergeNodes(ListNode head) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar mergeNodes = function(head) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode {\n * val: number\n * next: ListNode | null\n * constructor(val?: number, next?: ListNode | null) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n * }\n */\n\nfunction mergeNodes(head: ListNode | null): ListNode | null {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "/**\n * Definition for a singly-linked list.\n * class ListNode {\n * public $val = 0;\n * public $next = null;\n * function __construct($val = 0, $next = null) {\n * $this->val = $val;\n * $this->next = $next;\n * }\n * }\n */\nclass Solution {\n\n /**\n * @param ListNode $head\n * @return ListNode\n */\n function mergeNodes($head) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "/**\n * Definition for singly-linked list.\n * public class ListNode {\n * public var val: Int\n * public var next: ListNode?\n * public init() { self.val = 0; self.next = nil; }\n * public init(_ val: Int) { self.val = val; self.next = nil; }\n * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }\n * }\n */\nclass Solution {\n func mergeNodes(_ head: ListNode?) -> ListNode? {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "/**\n * Example:\n * var li = ListNode(5)\n * var v = li.`val`\n * Definition for singly-linked list.\n * class ListNode(var `val`: Int) {\n * var next: ListNode? = null\n * }\n */\nclass Solution {\n fun mergeNodes(head: ListNode?): ListNode? {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode {\n * int val;\n * ListNode? next;\n * ListNode([this.val = 0, this.next]);\n * }\n */\nclass Solution {\n ListNode? mergeNodes(ListNode? head) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "/**\n * Definition for singly-linked list.\n * type ListNode struct {\n * Val int\n * Next *ListNode\n * }\n */\nfunc mergeNodes(head *ListNode) *ListNode {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# Definition for singly-linked list.\n# class ListNode\n# attr_accessor :val, :next\n# def initialize(val = 0, _next = nil)\n# @val = val\n# @next = _next\n# end\n# end\n# @param {ListNode} head\n# @return {ListNode}\ndef merge_nodes(head)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "/**\n * Definition for singly-linked list.\n * class ListNode(_x: Int = 0, _next: ListNode = null) {\n * var next: ListNode = _next\n * var x: Int = _x\n * }\n */\nobject Solution {\n def mergeNodes(head: ListNode): ListNode = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "// Definition for singly-linked list.\n// #[derive(PartialEq, Eq, Clone, Debug)]\n// pub struct ListNode {\n// pub val: i32,\n// pub next: Option<Box<ListNode>>\n// }\n// \n// impl ListNode {\n// #[inline]\n// fn new(val: i32) -> Self {\n// ListNode {\n// next: None,\n// val\n// }\n// }\n// }\nimpl Solution {\n pub fn merge_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "; Definition for singly-linked list:\n#|\n\n; val : integer?\n; next : (or/c list-node? #f)\n(struct list-node\n (val next) #:mutable #:transparent)\n\n; constructor\n(define (make-list-node [val 0])\n (list-node val #f))\n\n|#\n\n(define/contract (merge-nodes head)\n (-> (or/c list-node? #f) (or/c list-node? #f))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "%% Definition for singly-linked list.\n%%\n%% -record(list_node, {val = 0 :: integer(),\n%% next = null :: 'null' | #list_node{}}).\n\n-spec merge_nodes(Head :: #list_node{} | null) -> #list_node{} | null.\nmerge_nodes(Head) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "# Definition for singly-linked list.\n#\n# defmodule ListNode do\n# @type t :: %__MODULE__{\n# val: integer,\n# next: ListNode.t() | nil\n# }\n# defstruct val: 0, next: nil\n# end\n\ndefmodule Solution do\n @spec merge_nodes(head :: ListNode.t | nil) :: ListNode.t | nil\n def merge_nodes(head) do\n \n end\nend"}] | [0,3,1,0,4,5,2,0] | {
"name": "mergeNodes",
"params": [
{
"name": "head",
"type": "ListNode"
}
],
"return": {
"type": "ListNode"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Linked List', 'Simulation'] |
2,182 | Construct String With Repeat Limit | construct-string-with-repeat-limit | <p>You are given a string <code>s</code> and an integer <code>repeatLimit</code>. Construct a new string <code>repeatLimitedString</code> using the characters of <code>s</code> such that no letter appears <strong>more than</strong> <code>repeatLimit</code> times <strong>in a row</strong>. You do <strong>not</strong> have to use all characters from <code>s</code>.</p>
<p>Return <em>the <strong>lexicographically largest</strong> </em><code>repeatLimitedString</code> <em>possible</em>.</p>
<p>A string <code>a</code> is <strong>lexicographically larger</strong> than a string <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears later in the alphabet than the corresponding letter in <code>b</code>. If the first <code>min(a.length, b.length)</code> characters do not differ, then the longer string is the lexicographically larger one.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "cczazcc", repeatLimit = 3
<strong>Output:</strong> "zzcccac"
<strong>Explanation:</strong> We use all of the characters from s to construct the repeatLimitedString "zzcccac".
The letter 'a' appears at most 1 time in a row.
The letter 'c' appears at most 3 times in a row.
The letter 'z' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "zzcccac".
Note that the string "zzcccca" is lexicographically larger but the letter 'c' appears more than 3 times in a row, so it is not a valid repeatLimitedString.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "aababab", repeatLimit = 2
<strong>Output:</strong> "bbabaa"
<strong>Explanation:</strong> We use only some of the characters from s to construct the repeatLimitedString "bbabaa".
The letter 'a' appears at most 2 times in a row.
The letter 'b' appears at most 2 times in a row.
Hence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.
The string is the lexicographically largest repeatLimitedString possible so we return "bbabaa".
Note that the string "bbabaaa" is lexicographically larger but the letter 'a' appears more than 2 times in a row, so it is not a valid repeatLimitedString.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= repeatLimit <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>
| Medium | 120.5K | 170K | 120,481 | 170,017 | 70.9% | ['rearrange-string-k-distance-apart'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string repeatLimitedString(string s, int repeatLimit) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String repeatLimitedString(String s, int repeatLimit) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def repeatLimitedString(self, s, repeatLimit):\n \"\"\"\n :type s: str\n :type repeatLimit: int\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def repeatLimitedString(self, s: str, repeatLimit: int) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* repeatLimitedString(char* s, int repeatLimit) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string RepeatLimitedString(string s, int repeatLimit) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} repeatLimit\n * @return {string}\n */\nvar repeatLimitedString = function(s, repeatLimit) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function repeatLimitedString(s: string, repeatLimit: number): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $repeatLimit\n * @return String\n */\n function repeatLimitedString($s, $repeatLimit) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func repeatLimitedString(_ s: String, _ repeatLimit: Int) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun repeatLimitedString(s: String, repeatLimit: Int): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String repeatLimitedString(String s, int repeatLimit) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func repeatLimitedString(s string, repeatLimit int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} repeat_limit\n# @return {String}\ndef repeat_limited_string(s, repeat_limit)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def repeatLimitedString(s: String, repeatLimit: Int): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn repeat_limited_string(s: String, repeat_limit: i32) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (repeat-limited-string s repeatLimit)\n (-> string? exact-integer? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec repeat_limited_string(S :: unicode:unicode_binary(), RepeatLimit :: integer()) -> unicode:unicode_binary().\nrepeat_limited_string(S, RepeatLimit) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec repeat_limited_string(s :: String.t, repeat_limit :: integer) :: String.t\n def repeat_limited_string(s, repeat_limit) do\n \n end\nend"}] | "cczazcc"
3 | {
"name": "repeatLimitedString",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "integer",
"name": "repeatLimit"
}
],
"return": {
"type": "string"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Hash Table', 'String', 'Greedy', 'Heap (Priority Queue)', 'Counting'] |
2,183 | Count Array Pairs Divisible by K | count-array-pairs-divisible-by-k | <p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>, return <em>the <strong>number of pairs</strong></em> <code>(i, j)</code> <em>such that:</em></p>
<ul>
<li><code>0 <= i < j <= n - 1</code> <em>and</em></li>
<li><code>nums[i] * nums[j]</code> <em>is divisible by</em> <code>k</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 2
<strong>Output:</strong> 7
<strong>Explanation:</strong>
The 7 pairs of indices whose corresponding products are divisible by 2 are
(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).
Their products are 2, 4, 6, 8, 10, 12, and 20 respectively.
Other pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 5
<strong>Output:</strong> 0
<strong>Explanation:</strong> There does not exist any pair of indices whose corresponding product is divisible by 5.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], k <= 10<sup>5</sup></code></li>
</ul>
| Hard | 19.6K | 66.2K | 19,625 | 66,237 | 29.6% | ['number-of-single-divisor-triplets', 'check-if-array-pairs-are-divisible-by-k', 'find-the-number-of-good-pairs-ii', 'find-the-number-of-good-pairs-i'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countPairs(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countPairs(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countPairs(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countPairs(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countPairs(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountPairs(int[] nums, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar countPairs = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countPairs(nums: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function countPairs($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countPairs(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countPairs(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countPairs(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countPairs(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef count_pairs(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countPairs(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_pairs(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-pairs nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_pairs(Nums :: [integer()], K :: integer()) -> integer().\ncount_pairs(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_pairs(nums :: [integer], k :: integer) :: integer\n def count_pairs(nums, k) do\n \n end\nend"}] | [1,2,3,4,5]
2 | {
"name": "countPairs",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'Number Theory'] |
2,184 | Number of Ways to Build Sturdy Brick Wall | number-of-ways-to-build-sturdy-brick-wall | null | Medium | 9.3K | 18.9K | 9,340 | 18,880 | 49.5% | ['brick-wall', 'bricks-falling-when-hit', 'minimum-time-to-kill-all-monsters'] | [] | Algorithms | null | 2
3
[1,2] | {
"name": "buildWall",
"params": [
{
"name": "height",
"type": "integer"
},
{
"type": "integer",
"name": "width"
},
{
"type": "integer[]",
"name": "bricks"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming', 'Bit Manipulation', 'Bitmask'] |
2,185 | Counting Words With a Given Prefix | counting-words-with-a-given-prefix | <p>You are given an array of strings <code>words</code> and a string <code>pref</code>.</p>
<p>Return <em>the number of strings in </em><code>words</code><em> that contain </em><code>pref</code><em> as a <strong>prefix</strong></em>.</p>
<p>A <strong>prefix</strong> of a string <code>s</code> is any leading contiguous substring of <code>s</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> words = ["pay","<strong><u>at</u></strong>tention","practice","<u><strong>at</strong></u>tend"], <code>pref </code>= "at"
<strong>Output:</strong> 2
<strong>Explanation:</strong> The 2 strings that contain "at" as a prefix are: "<u><strong>at</strong></u>tention" and "<u><strong>at</strong></u>tend".
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> words = ["leetcode","win","loops","success"], <code>pref </code>= "code"
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no strings that contain "code" as a prefix.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= words.length <= 100</code></li>
<li><code>1 <= words[i].length, pref.length <= 100</code></li>
<li><code>words[i]</code> and <code>pref</code> consist of lowercase English letters.</li>
</ul>
| Easy | 277.5K | 328.3K | 277,471 | 328,266 | 84.5% | ['check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence', 'count-prefixes-of-a-given-string'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int prefixCount(vector<string>& words, string pref) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int prefixCount(String[] words, String pref) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def prefixCount(self, words, pref):\n \"\"\"\n :type words: List[str]\n :type pref: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def prefixCount(self, words: List[str], pref: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int prefixCount(char** words, int wordsSize, char* pref) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int PrefixCount(string[] words, string pref) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @param {string} pref\n * @return {number}\n */\nvar prefixCount = function(words, pref) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function prefixCount(words: string[], pref: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @param String $pref\n * @return Integer\n */\n function prefixCount($words, $pref) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func prefixCount(_ words: [String], _ pref: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun prefixCount(words: Array<String>, pref: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int prefixCount(List<String> words, String pref) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func prefixCount(words []string, pref string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @param {String} pref\n# @return {Integer}\ndef prefix_count(words, pref)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def prefixCount(words: Array[String], pref: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn prefix_count(words: Vec<String>, pref: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (prefix-count words pref)\n (-> (listof string?) string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec prefix_count(Words :: [unicode:unicode_binary()], Pref :: unicode:unicode_binary()) -> integer().\nprefix_count(Words, Pref) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec prefix_count(words :: [String.t], pref :: String.t) :: integer\n def prefix_count(words, pref) do\n \n end\nend"}] | ["pay","attention","practice","attend"]
"at" | {
"name": "prefixCount",
"params": [
{
"name": "words",
"type": "string[]"
},
{
"type": "string",
"name": "pref"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'String', 'String Matching'] |
2,186 | Minimum Number of Steps to Make Two Strings Anagram II | minimum-number-of-steps-to-make-two-strings-anagram-ii | <p>You are given two strings <code>s</code> and <code>t</code>. In one step, you can append <strong>any character</strong> to either <code>s</code> or <code>t</code>.</p>
<p>Return <em>the minimum number of steps to make </em><code>s</code><em> and </em><code>t</code><em> <strong>anagrams</strong> of each other.</em></p>
<p>An <strong>anagram</strong> of a string is a string that contains the same characters with a different (or the same) ordering.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "<strong><u>lee</u></strong>tco<u><strong>de</strong></u>", t = "co<u><strong>a</strong></u>t<u><strong>s</strong></u>"
<strong>Output:</strong> 7
<strong>Explanation:</strong>
- In 2 steps, we can append the letters in "as" onto s = "leetcode", forming s = "leetcode<strong><u>as</u></strong>".
- In 5 steps, we can append the letters in "leede" onto t = "coats", forming t = "coats<u><strong>leede</strong></u>".
"leetcodeas" and "coatsleede" are now anagrams of each other.
We used a total of 2 + 5 = 7 steps.
It can be shown that there is no way to make them anagrams of each other with less than 7 steps.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "night", t = "thing"
<strong>Output:</strong> 0
<strong>Explanation:</strong> The given strings are already anagrams of each other. Thus, we do not need any further steps.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length, t.length <= 2 * 10<sup>5</sup></code></li>
<li><code>s</code> and <code>t</code> consist of lowercase English letters.</li>
</ul>
| Medium | 48.4K | 66.6K | 48,372 | 66,594 | 72.6% | ['minimum-number-of-steps-to-make-two-strings-anagram'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minSteps(string s, string t) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minSteps(String s, String t) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minSteps(self, s, t):\n \"\"\"\n :type s: str\n :type t: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minSteps(self, s: str, t: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minSteps(char* s, char* t) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinSteps(string s, string t) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {string} t\n * @return {number}\n */\nvar minSteps = function(s, t) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minSteps(s: string, t: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param String $t\n * @return Integer\n */\n function minSteps($s, $t) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minSteps(_ s: String, _ t: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minSteps(s: String, t: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minSteps(String s, String t) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minSteps(s string, t string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} t\n# @return {Integer}\ndef min_steps(s, t)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minSteps(s: String, t: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_steps(s: String, t: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-steps s t)\n (-> string? string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_steps(S :: unicode:unicode_binary(), T :: unicode:unicode_binary()) -> integer().\nmin_steps(S, T) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_steps(s :: String.t, t :: String.t) :: integer\n def min_steps(s, t) do\n \n end\nend"}] | "leetcode"
"coats" | {
"name": "minSteps",
"params": [
{
"name": "s",
"type": "string"
},
{
"type": "string",
"name": "t"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Hash Table', 'String', 'Counting'] |
2,187 | Minimum Time to Complete Trips | minimum-time-to-complete-trips | <p>You are given an array <code>time</code> where <code>time[i]</code> denotes the time taken by the <code>i<sup>th</sup></code> bus to complete <strong>one trip</strong>.</p>
<p>Each bus can make multiple trips <strong>successively</strong>; that is, the next trip can start <strong>immediately after</strong> completing the current trip. Also, each bus operates <strong>independently</strong>; that is, the trips of one bus do not influence the trips of any other bus.</p>
<p>You are also given an integer <code>totalTrips</code>, which denotes the number of trips all buses should make <strong>in total</strong>. Return <em>the <strong>minimum time</strong> required for all buses to complete <strong>at least</strong> </em><code>totalTrips</code><em> trips</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> time = [1,2,3], totalTrips = 5
<strong>Output:</strong> 3
<strong>Explanation:</strong>
- At time t = 1, the number of trips completed by each bus are [1,0,0].
The total number of trips completed is 1 + 0 + 0 = 1.
- At time t = 2, the number of trips completed by each bus are [2,1,0].
The total number of trips completed is 2 + 1 + 0 = 3.
- At time t = 3, the number of trips completed by each bus are [3,1,1].
The total number of trips completed is 3 + 1 + 1 = 5.
So the minimum time needed for all buses to complete at least 5 trips is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> time = [2], totalTrips = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong>
There is only one bus, and it will complete its first trip at t = 2.
So the minimum time needed to complete 1 trip is 2.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= time.length <= 10<sup>5</sup></code></li>
<li><code>1 <= time[i], totalTrips <= 10<sup>7</sup></code></li>
</ul>
| Medium | 131K | 336.2K | 130,959 | 336,194 | 39.0% | ['maximum-candies-allocated-to-k-children', 'minimum-speed-to-arrive-on-time', 'minimized-maximum-of-products-distributed-to-any-store', 'maximum-running-time-of-n-computers', 'maximum-number-of-robots-within-budget', 'minimize-maximum-of-array', 'minimum-amount-of-damage-dealt-to-bob'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minimumTime(vector<int>& time, int totalTrips) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minimumTime(int[] time, int totalTrips) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumTime(self, time, totalTrips):\n \"\"\"\n :type time: List[int]\n :type totalTrips: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumTime(self, time: List[int], totalTrips: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minimumTime(int* time, int timeSize, int totalTrips) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinimumTime(int[] time, int totalTrips) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} time\n * @param {number} totalTrips\n * @return {number}\n */\nvar minimumTime = function(time, totalTrips) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumTime(time: number[], totalTrips: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $time\n * @param Integer $totalTrips\n * @return Integer\n */\n function minimumTime($time, $totalTrips) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumTime(_ time: [Int], _ totalTrips: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumTime(time: IntArray, totalTrips: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumTime(List<int> time, int totalTrips) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumTime(time []int, totalTrips int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} time\n# @param {Integer} total_trips\n# @return {Integer}\ndef minimum_time(time, total_trips)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumTime(time: Array[Int], totalTrips: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_time(time: Vec<i32>, total_trips: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-time time totalTrips)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_time(Time :: [integer()], TotalTrips :: integer()) -> integer().\nminimum_time(Time, TotalTrips) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_time(time :: [integer], total_trips :: integer) :: integer\n def minimum_time(time, total_trips) do\n \n end\nend"}] | [1,2,3]
5 | {
"name": "minimumTime",
"params": [
{
"name": "time",
"type": "integer[]"
},
{
"type": "integer",
"name": "totalTrips"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Binary Search'] |
2,188 | Minimum Time to Finish the Race | minimum-time-to-finish-the-race | <p>You are given a <strong>0-indexed</strong> 2D integer array <code>tires</code> where <code>tires[i] = [f<sub>i</sub>, r<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> tire can finish its <code>x<sup>th</sup></code> successive lap in <code>f<sub>i</sub> * r<sub>i</sub><sup>(x-1)</sup></code> seconds.</p>
<ul>
<li>For example, if <code>f<sub>i</sub> = 3</code> and <code>r<sub>i</sub> = 2</code>, then the tire would finish its <code>1<sup>st</sup></code> lap in <code>3</code> seconds, its <code>2<sup>nd</sup></code> lap in <code>3 * 2 = 6</code> seconds, its <code>3<sup>rd</sup></code> lap in <code>3 * 2<sup>2</sup> = 12</code> seconds, etc.</li>
</ul>
<p>You are also given an integer <code>changeTime</code> and an integer <code>numLaps</code>.</p>
<p>The race consists of <code>numLaps</code> laps and you may start the race with <strong>any</strong> tire. You have an <strong>unlimited</strong> supply of each tire and after every lap, you may <strong>change</strong> to any given tire (including the current tire type) if you wait <code>changeTime</code> seconds.</p>
<p>Return<em> the <strong>minimum</strong> time to finish the race.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> tires = [[2,3],[3,4]], changeTime = 5, numLaps = 4
<strong>Output:</strong> 21
<strong>Explanation:</strong>
Lap 1: Start with tire 0 and finish the lap in 2 seconds.
Lap 2: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Lap 3: Change tires to a new tire 0 for 5 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 0 and finish the lap in 2 * 3 = 6 seconds.
Total time = 2 + 6 + 5 + 2 + 6 = 21 seconds.
The minimum time to complete the race is 21 seconds.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> tires = [[1,10],[2,2],[3,4]], changeTime = 6, numLaps = 5
<strong>Output:</strong> 25
<strong>Explanation:</strong>
Lap 1: Start with tire 1 and finish the lap in 2 seconds.
Lap 2: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 3: Change tires to a new tire 1 for 6 seconds and then finish the lap in another 2 seconds.
Lap 4: Continue with tire 1 and finish the lap in 2 * 2 = 4 seconds.
Lap 5: Change tires to tire 0 for 6 seconds then finish the lap in another 1 second.
Total time = 2 + 4 + 6 + 2 + 4 + 6 + 1 = 25 seconds.
The minimum time to complete the race is 25 seconds.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= tires.length <= 10<sup>5</sup></code></li>
<li><code>tires[i].length == 2</code></li>
<li><code>1 <= f<sub>i</sub>, changeTime <= 10<sup>5</sup></code></li>
<li><code>2 <= r<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>1 <= numLaps <= 1000</code></li>
</ul>
| Hard | 13.1K | 31.3K | 13,145 | 31,330 | 42.0% | ['minimum-skips-to-arrive-at-meeting-on-time'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumFinishTime(vector<vector<int>>& tires, int changeTime, int numLaps) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumFinishTime(int[][] tires, int changeTime, int numLaps) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumFinishTime(self, tires, changeTime, numLaps):\n \"\"\"\n :type tires: List[List[int]]\n :type changeTime: int\n :type numLaps: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumFinishTime(self, tires: List[List[int]], changeTime: int, numLaps: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumFinishTime(int** tires, int tiresSize, int* tiresColSize, int changeTime, int numLaps) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumFinishTime(int[][] tires, int changeTime, int numLaps) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} tires\n * @param {number} changeTime\n * @param {number} numLaps\n * @return {number}\n */\nvar minimumFinishTime = function(tires, changeTime, numLaps) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumFinishTime(tires: number[][], changeTime: number, numLaps: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $tires\n * @param Integer $changeTime\n * @param Integer $numLaps\n * @return Integer\n */\n function minimumFinishTime($tires, $changeTime, $numLaps) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumFinishTime(_ tires: [[Int]], _ changeTime: Int, _ numLaps: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumFinishTime(tires: Array<IntArray>, changeTime: Int, numLaps: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumFinishTime(List<List<int>> tires, int changeTime, int numLaps) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumFinishTime(tires [][]int, changeTime int, numLaps int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} tires\n# @param {Integer} change_time\n# @param {Integer} num_laps\n# @return {Integer}\ndef minimum_finish_time(tires, change_time, num_laps)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumFinishTime(tires: Array[Array[Int]], changeTime: Int, numLaps: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_finish_time(tires: Vec<Vec<i32>>, change_time: i32, num_laps: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-finish-time tires changeTime numLaps)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_finish_time(Tires :: [[integer()]], ChangeTime :: integer(), NumLaps :: integer()) -> integer().\nminimum_finish_time(Tires, ChangeTime, NumLaps) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_finish_time(tires :: [[integer]], change_time :: integer, num_laps :: integer) :: integer\n def minimum_finish_time(tires, change_time, num_laps) do\n \n end\nend"}] | [[2,3],[3,4]]
5
4 | {
"name": "minimumFinishTime",
"params": [
{
"name": "tires",
"type": "integer[][]"
},
{
"type": "integer",
"name": "changeTime"
},
{
"type": "integer",
"name": "numLaps"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Dynamic Programming'] |
2,189 | Number of Ways to Build House of Cards | number-of-ways-to-build-house-of-cards | null | Medium | 3.1K | 5K | 3,092 | 4,993 | 61.9% | ['champagne-tower'] | [] | Algorithms | null | 16 | {
"name": "houseOfCards",
"params": [
{
"name": "n",
"type": "integer"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math', 'Dynamic Programming'] |
2,190 | Most Frequent Number Following Key In an Array | most-frequent-number-following-key-in-an-array | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.<strong> </strong>You are also given an integer <code>key</code>, which is present in <code>nums</code>.</p>
<p>For every unique integer <code>target</code> in <code>nums</code>, <strong>count</strong> the number of times <code>target</code> immediately follows an occurrence of <code>key</code> in <code>nums</code>. In other words, count the number of indices <code>i</code> such that:</p>
<ul>
<li><code>0 <= i <= nums.length - 2</code>,</li>
<li><code>nums[i] == key</code> and,</li>
<li><code>nums[i + 1] == target</code>.</li>
</ul>
<p>Return <em>the </em><code>target</code><em> with the <strong>maximum</strong> count</em>. The test cases will be generated such that the <code>target</code> with maximum count is unique.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,100,200,1,100], key = 1
<strong>Output:</strong> 100
<strong>Explanation:</strong> For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key.
No other integers follow an occurrence of key, so we return 100.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,3], key = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key.
For target = 3, there is only one occurrence at index 4 which follows an occurrence of key.
target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i] <= 1000</code></li>
<li>The test cases will be generated such that the answer is unique.</li>
</ul>
| Easy | 43.4K | 73.5K | 43,413 | 73,484 | 59.1% | ['sort-array-by-increasing-frequency'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int mostFrequent(vector<int>& nums, int key) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int mostFrequent(int[] nums, int key) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def mostFrequent(self, nums, key):\n \"\"\"\n :type nums: List[int]\n :type key: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def mostFrequent(self, nums: List[int], key: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int mostFrequent(int* nums, int numsSize, int key) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MostFrequent(int[] nums, int key) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} key\n * @return {number}\n */\nvar mostFrequent = function(nums, key) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function mostFrequent(nums: number[], key: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $key\n * @return Integer\n */\n function mostFrequent($nums, $key) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func mostFrequent(_ nums: [Int], _ key: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun mostFrequent(nums: IntArray, key: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int mostFrequent(List<int> nums, int key) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func mostFrequent(nums []int, key int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} key\n# @return {Integer}\ndef most_frequent(nums, key)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def mostFrequent(nums: Array[Int], key: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn most_frequent(nums: Vec<i32>, key: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (most-frequent nums key)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec most_frequent(Nums :: [integer()], Key :: integer()) -> integer().\nmost_frequent(Nums, Key) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec most_frequent(nums :: [integer], key :: integer) :: integer\n def most_frequent(nums, key) do\n \n end\nend"}] | [1,100,200,1,100]
1 | {
"name": "mostFrequent",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "key"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Counting'] |
2,191 | Sort the Jumbled Numbers | sort-the-jumbled-numbers | <p>You are given a <strong>0-indexed</strong> integer array <code>mapping</code> which represents the mapping rule of a shuffled decimal system. <code>mapping[i] = j</code> means digit <code>i</code> should be mapped to digit <code>j</code> in this system.</p>
<p>The <strong>mapped value</strong> of an integer is the new integer obtained by replacing each occurrence of digit <code>i</code> in the integer with <code>mapping[i]</code> for all <code>0 <= i <= 9</code>.</p>
<p>You are also given another integer array <code>nums</code>. Return <em>the array </em><code>nums</code><em> sorted in <strong>non-decreasing</strong> order based on the <strong>mapped values</strong> of its elements.</em></p>
<p><strong>Notes:</strong></p>
<ul>
<li>Elements with the same mapped values should appear in the <strong>same relative order</strong> as in the input.</li>
<li>The elements of <code>nums</code> should only be sorted based on their mapped values and <strong>not be replaced</strong> by them.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
<strong>Output:</strong> [338,38,991]
<strong>Explanation:</strong>
Map the number 991 as follows:
1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
Therefore, the mapped value of 991 is 669.
338 maps to 007, or 7 after removing the leading zeros.
38 maps to 07, which is also 7 after removing leading zeros.
Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
Thus, the sorted array is [338,38,991].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
<strong>Output:</strong> [123,456,789]
<strong>Explanation:</strong> 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>mapping.length == 10</code></li>
<li><code>0 <= mapping[i] <= 9</code></li>
<li>All the values of <code>mapping[i]</code> are <strong>unique</strong>.</li>
<li><code>1 <= nums.length <= 3 * 10<sup>4</sup></code></li>
<li><code>0 <= nums[i] < 10<sup>9</sup></code></li>
</ul>
| Medium | 150.7K | 251.3K | 150,721 | 251,279 | 60.0% | ['map-sum-pairs'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] sortJumbled(int[] mapping, int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sortJumbled(self, mapping, nums):\n \"\"\"\n :type mapping: List[int]\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* sortJumbled(int* mapping, int mappingSize, int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] SortJumbled(int[] mapping, int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} mapping\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortJumbled = function(mapping, nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sortJumbled(mapping: number[], nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $mapping\n * @param Integer[] $nums\n * @return Integer[]\n */\n function sortJumbled($mapping, $nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sortJumbled(_ mapping: [Int], _ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sortJumbled(mapping: IntArray, nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> sortJumbled(List<int> mapping, List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sortJumbled(mapping []int, nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} mapping\n# @param {Integer[]} nums\n# @return {Integer[]}\ndef sort_jumbled(mapping, nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sortJumbled(mapping: Array[Int], nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sort_jumbled(mapping: Vec<i32>, nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sort-jumbled mapping nums)\n (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sort_jumbled(Mapping :: [integer()], Nums :: [integer()]) -> [integer()].\nsort_jumbled(Mapping, Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sort_jumbled(mapping :: [integer], nums :: [integer]) :: [integer]\n def sort_jumbled(mapping, nums) do\n \n end\nend"}] | [8,9,4,0,2,1,3,5,7,6]
[991,338,38] | {
"name": "sortJumbled",
"params": [
{
"type": "integer[]",
"name": "mapping"
},
{
"type": "integer[]",
"name": "nums"
}
],
"return": {
"type": "integer[]"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Sorting'] |
2,192 | All Ancestors of a Node in a Directed Acyclic Graph | all-ancestors-of-a-node-in-a-directed-acyclic-graph | <p>You are given a positive integer <code>n</code> representing the number of nodes of a <strong>Directed Acyclic Graph</strong> (DAG). The nodes are numbered from <code>0</code> to <code>n - 1</code> (<strong>inclusive</strong>).</p>
<p>You are also given a 2D integer array <code>edges</code>, where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> denotes that there is a <strong>unidirectional</strong> edge from <code>from<sub>i</sub></code> to <code>to<sub>i</sub></code> in the graph.</p>
<p>Return <em>a list</em> <code>answer</code><em>, where </em><code>answer[i]</code><em> is the <strong>list of ancestors</strong> of the</em> <code>i<sup>th</sup></code> <em>node, sorted in <strong>ascending order</strong></em>.</p>
<p>A node <code>u</code> is an <strong>ancestor</strong> of another node <code>v</code> if <code>u</code> can reach <code>v</code> via a set of edges.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/12/e1.png" style="width: 322px; height: 265px;" />
<pre>
<strong>Input:</strong> n = 8, edgeList = [[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]]
<strong>Output:</strong> [[],[],[],[0,1],[0,2],[0,1,3],[0,1,2,3,4],[0,1,2,3]]
<strong>Explanation:</strong>
The above diagram represents the input graph.
- Nodes 0, 1, and 2 do not have any ancestors.
- Node 3 has two ancestors 0 and 1.
- Node 4 has two ancestors 0 and 2.
- Node 5 has three ancestors 0, 1, and 3.
- Node 6 has five ancestors 0, 1, 2, 3, and 4.
- Node 7 has four ancestors 0, 1, 2, and 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/12/e2.png" style="width: 343px; height: 299px;" />
<pre>
<strong>Input:</strong> n = 5, edgeList = [[0,1],[0,2],[0,3],[0,4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
<strong>Output:</strong> [[],[0],[0,1],[0,1,2],[0,1,2,3]]
<strong>Explanation:</strong>
The above diagram represents the input graph.
- Node 0 does not have any ancestor.
- Node 1 has one ancestor 0.
- Node 2 has two ancestors 0 and 1.
- Node 3 has three ancestors 0, 1, and 2.
- Node 4 has four ancestors 0, 1, 2, and 3.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1000</code></li>
<li><code>0 <= edges.length <= min(2000, n * (n - 1) / 2)</code></li>
<li><code>edges[i].length == 2</code></li>
<li><code>0 <= from<sub>i</sub>, to<sub>i</sub> <= n - 1</code></li>
<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
<li>There are no duplicate edges.</li>
<li>The graph is <strong>directed</strong> and <strong>acyclic</strong>.</li>
</ul>
| Medium | 143.5K | 232K | 143,534 | 231,991 | 61.9% | ['number-of-restricted-paths-from-first-to-last-node'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> getAncestors(int n, vector<vector<int>>& edges) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<List<Integer>> getAncestors(int n, int[][] edges) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def getAncestors(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().\n */\nint** getAncestors(int n, int** edges, int edgesSize, int* edgesColSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<IList<int>> GetAncestors(int n, int[][] edges) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} edges\n * @return {number[][]}\n */\nvar getAncestors = function(n, edges) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function getAncestors(n: number, edges: number[][]): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $edges\n * @return Integer[][]\n */\n function getAncestors($n, $edges) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func getAncestors(_ n: Int, _ edges: [[Int]]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun getAncestors(n: Int, edges: Array<IntArray>): List<List<Int>> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> getAncestors(int n, List<List<int>> edges) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func getAncestors(n int, edges [][]int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} edges\n# @return {Integer[][]}\ndef get_ancestors(n, edges)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def getAncestors(n: Int, edges: Array[Array[Int]]): List[List[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn get_ancestors(n: i32, edges: Vec<Vec<i32>>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (get-ancestors n edges)\n (-> exact-integer? (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec get_ancestors(N :: integer(), Edges :: [[integer()]]) -> [[integer()]].\nget_ancestors(N, Edges) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec get_ancestors(n :: integer, edges :: [[integer]]) :: [[integer]]\n def get_ancestors(n, edges) do\n \n end\nend"}] | 8
[[0,3],[0,4],[1,3],[2,4],[2,7],[3,5],[3,6],[3,7],[4,6]] | {
"name": "getAncestors",
"params": [
{
"name": "n",
"type": "integer"
},
{
"type": "integer[][]",
"name": "edges"
}
],
"return": {
"type": "list<list<integer>>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Depth-First Search', 'Breadth-First Search', 'Graph', 'Topological Sort'] |
2,193 | Minimum Number of Moves to Make Palindrome | minimum-number-of-moves-to-make-palindrome | <p>You are given a string <code>s</code> consisting only of lowercase English letters.</p>
<p>In one <strong>move</strong>, you can select any two <strong>adjacent</strong> characters of <code>s</code> and swap them.</p>
<p>Return <em>the <strong>minimum number of moves</strong> needed to make</em> <code>s</code> <em>a palindrome</em>.</p>
<p><strong>Note</strong> that the input will be generated such that <code>s</code> can always be converted to a palindrome.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "aabb"
<strong>Output:</strong> 2
<strong>Explanation:</strong>
We can obtain two palindromes from s, "abba" and "baab".
- We can obtain "abba" from s in 2 moves: "a<u><strong>ab</strong></u>b" -> "ab<u><strong>ab</strong></u>" -> "abba".
- We can obtain "baab" from s in 2 moves: "a<u><strong>ab</strong></u>b" -> "<u><strong>ab</strong></u>ab" -> "baab".
Thus, the minimum number of moves needed to make s a palindrome is 2.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "letelt"
<strong>Output:</strong> 2
<strong>Explanation:</strong>
One of the palindromes we can obtain from s in 2 moves is "lettel".
One of the ways we can obtain it is "lete<u><strong>lt</strong></u>" -> "let<u><strong>et</strong></u>l" -> "lettel".
Other palindromes such as "tleelt" can also be obtained in 2 moves.
It can be shown that it is not possible to obtain a palindrome in less than 2 moves.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 2000</code></li>
<li><code>s</code> consists only of lowercase English letters.</li>
<li><code>s</code> can be converted to a palindrome using a finite number of moves.</li>
</ul>
| Hard | 28.8K | 55.5K | 28,757 | 55,547 | 51.8% | ['minimum-insertion-steps-to-make-a-string-palindrome', 'minimum-number-of-flips-to-make-binary-grid-palindromic-i'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minMovesToMakePalindrome(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minMovesToMakePalindrome(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMovesToMakePalindrome(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minMovesToMakePalindrome(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minMovesToMakePalindrome(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinMovesToMakePalindrome(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar minMovesToMakePalindrome = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMovesToMakePalindrome(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function minMovesToMakePalindrome($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMovesToMakePalindrome(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMovesToMakePalindrome(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMovesToMakePalindrome(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMovesToMakePalindrome(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef min_moves_to_make_palindrome(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMovesToMakePalindrome(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_moves_to_make_palindrome(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-moves-to-make-palindrome s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_moves_to_make_palindrome(S :: unicode:unicode_binary()) -> integer().\nmin_moves_to_make_palindrome(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_moves_to_make_palindrome(s :: String.t) :: integer\n def min_moves_to_make_palindrome(s) do\n \n end\nend"}] | "aabb" | {
"name": "minMovesToMakePalindrome",
"params": [
{
"name": "s",
"type": "string"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Two Pointers', 'String', 'Greedy', 'Binary Indexed Tree'] |
2,194 | Cells in a Range on an Excel Sheet | cells-in-a-range-on-an-excel-sheet | <p>A cell <code>(r, c)</code> of an excel sheet is represented as a string <code>"<col><row>"</code> where:</p>
<ul>
<li><code><col></code> denotes the column number <code>c</code> of the cell. It is represented by <strong>alphabetical letters</strong>.
<ul>
<li>For example, the <code>1<sup>st</sup></code> column is denoted by <code>'A'</code>, the <code>2<sup>nd</sup></code> by <code>'B'</code>, the <code>3<sup>rd</sup></code> by <code>'C'</code>, and so on.</li>
</ul>
</li>
<li><code><row></code> is the row number <code>r</code> of the cell. The <code>r<sup>th</sup></code> row is represented by the <strong>integer</strong> <code>r</code>.</li>
</ul>
<p>You are given a string <code>s</code> in the format <code>"<col1><row1>:<col2><row2>"</code>, where <code><col1></code> represents the column <code>c1</code>, <code><row1></code> represents the row <code>r1</code>, <code><col2></code> represents the column <code>c2</code>, and <code><row2></code> represents the row <code>r2</code>, such that <code>r1 <= r2</code> and <code>c1 <= c2</code>.</p>
<p>Return <em>the <strong>list of cells</strong></em> <code>(x, y)</code> <em>such that</em> <code>r1 <= x <= r2</code> <em>and</em> <code>c1 <= y <= c2</code>. The cells should be represented as <strong>strings</strong> in the format mentioned above and be sorted in <strong>non-decreasing</strong> order first by columns and then by rows.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/08/ex1drawio.png" style="width: 250px; height: 160px;" />
<pre>
<strong>Input:</strong> s = "K1:L2"
<strong>Output:</strong> ["K1","K2","L1","L2"]
<strong>Explanation:</strong>
The above diagram shows the cells which should be present in the list.
The red arrows denote the order in which the cells should be presented.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/exam2drawio.png" style="width: 500px; height: 50px;" />
<pre>
<strong>Input:</strong> s = "A1:F1"
<strong>Output:</strong> ["A1","B1","C1","D1","E1","F1"]
<strong>Explanation:</strong>
The above diagram shows the cells which should be present in the list.
The red arrow denotes the order in which the cells should be presented.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>s.length == 5</code></li>
<li><code>'A' <= s[0] <= s[3] <= 'Z'</code></li>
<li><code>'1' <= s[1] <= s[4] <= '9'</code></li>
<li><code>s</code> consists of uppercase English letters, digits and <code>':'</code>.</li>
</ul>
| Easy | 78.8K | 94K | 78,759 | 93,992 | 83.8% | ['excel-sheet-column-title', 'excel-sheet-column-number', 'matrix-cells-in-distance-order'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<string> cellsInRange(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<String> cellsInRange(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def cellsInRange(self, s):\n \"\"\"\n :type s: str\n :rtype: List[str]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def cellsInRange(self, s: str) -> List[str]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nchar** cellsInRange(char* s, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<string> CellsInRange(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {string[]}\n */\nvar cellsInRange = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function cellsInRange(s: string): string[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return String[]\n */\n function cellsInRange($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func cellsInRange(_ s: String) -> [String] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun cellsInRange(s: String): List<String> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<String> cellsInRange(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func cellsInRange(s string) []string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {String[]}\ndef cells_in_range(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def cellsInRange(s: String): List[String] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn cells_in_range(s: String) -> Vec<String> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (cells-in-range s)\n (-> string? (listof string?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec cells_in_range(S :: unicode:unicode_binary()) -> [unicode:unicode_binary()].\ncells_in_range(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec cells_in_range(s :: String.t) :: [String.t]\n def cells_in_range(s) do\n \n end\nend"}] | "K1:L2" | {
"name": "cellsInRange",
"params": [
{
"name": "s",
"type": "string"
}
],
"return": {
"type": "list<string>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['String'] |
2,195 | Append K Integers With Minimal Sum | append-k-integers-with-minimal-sum | <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Append <code>k</code> <strong>unique positive</strong> integers that do <strong>not</strong> appear in <code>nums</code> to <code>nums</code> such that the resulting total sum is <strong>minimum</strong>.</p>
<p>Return<em> the sum of the</em> <code>k</code> <em>integers appended to</em> <code>nums</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,4,25,10,25], k = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The two unique positive integers that do not appear in nums which we append are 2 and 3.
The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum.
The sum of the two integers appended is 2 + 3 = 5, so we return 5.</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,6], k = 6
<strong>Output:</strong> 25
<strong>Explanation:</strong> The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8.
The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum.
The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>8</sup></code></li>
</ul>
| Medium | 36.9K | 142.2K | 36,878 | 142,158 | 25.9% | ['remove-k-digits', 'find-all-numbers-disappeared-in-an-array', 'kth-missing-positive-number', 'maximum-number-of-integers-to-choose-from-a-range-i', 'maximum-number-of-integers-to-choose-from-a-range-ii'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minimalKSum(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minimalKSum(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimalKSum(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimalKSum(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minimalKSum(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinimalKSum(int[] nums, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar minimalKSum = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimalKSum(nums: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function minimalKSum($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimalKSum(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimalKSum(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimalKSum(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimalKSum(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef minimal_k_sum(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimalKSum(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimal_k_sum(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimal-k-sum nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimal_k_sum(Nums :: [integer()], K :: integer()) -> integer().\nminimal_k_sum(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimal_k_sum(nums :: [integer], k :: integer) :: integer\n def minimal_k_sum(nums, k) do\n \n end\nend"}] | [1,4,25,10,25]
2 | {
"name": "minimalKSum",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'Greedy', 'Sorting'] |
2,196 | Create Binary Tree From Descriptions | create-binary-tree-from-descriptions | <p>You are given a 2D integer array <code>descriptions</code> where <code>descriptions[i] = [parent<sub>i</sub>, child<sub>i</sub>, isLeft<sub>i</sub>]</code> indicates that <code>parent<sub>i</sub></code> is the <strong>parent</strong> of <code>child<sub>i</sub></code> in a <strong>binary</strong> tree of <strong>unique</strong> values. Furthermore,</p>
<ul>
<li>If <code>isLeft<sub>i</sub> == 1</code>, then <code>child<sub>i</sub></code> is the left child of <code>parent<sub>i</sub></code>.</li>
<li>If <code>isLeft<sub>i</sub> == 0</code>, then <code>child<sub>i</sub></code> is the right child of <code>parent<sub>i</sub></code>.</li>
</ul>
<p>Construct the binary tree described by <code>descriptions</code> and return <em>its <strong>root</strong></em>.</p>
<p>The test cases will be generated such that the binary tree is <strong>valid</strong>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/example1drawio.png" style="width: 300px; height: 236px;" />
<pre>
<strong>Input:</strong> descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
<strong>Output:</strong> [50,20,80,15,17,19]
<strong>Explanation:</strong> The root node is the node with value 50 since it has no parent.
The resulting binary tree is shown in the diagram.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/09/example2drawio.png" style="width: 131px; height: 300px;" />
<pre>
<strong>Input:</strong> descriptions = [[1,2,1],[2,3,0],[3,4,1]]
<strong>Output:</strong> [1,2,null,null,3,4]
<strong>Explanation:</strong> The root node is the node with value 1 since it has no parent.
The resulting binary tree is shown in the diagram.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= descriptions.length <= 10<sup>4</sup></code></li>
<li><code>descriptions[i].length == 3</code></li>
<li><code>1 <= parent<sub>i</sub>, child<sub>i</sub> <= 10<sup>5</sup></code></li>
<li><code>0 <= isLeft<sub>i</sub> <= 1</code></li>
<li>The binary tree described by <code>descriptions</code> is valid.</li>
</ul>
| Medium | 159.1K | 194.9K | 159,099 | 194,862 | 81.6% | ['convert-sorted-list-to-binary-search-tree', 'number-of-ways-to-reconstruct-a-tree'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\nclass Solution {\n public TreeNode createBinaryTree(int[][] descriptions) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def createBinaryTree(self, descriptions):\n \"\"\"\n :type descriptions: List[List[int]]\n :rtype: Optional[TreeNode]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * struct TreeNode *left;\n * struct TreeNode *right;\n * };\n */\nstruct TreeNode* createBinaryTree(int** descriptions, int descriptionsSize, int* descriptionsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public int val;\n * public TreeNode left;\n * public TreeNode right;\n * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\npublic class Solution {\n public TreeNode CreateBinaryTree(int[][] descriptions) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n * this.val = (val===undefined ? 0 : val)\n * this.left = (left===undefined ? null : left)\n * this.right = (right===undefined ? null : right)\n * }\n */\n/**\n * @param {number[][]} descriptions\n * @return {TreeNode}\n */\nvar createBinaryTree = function(descriptions) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "/**\n * Definition for a binary tree node.\n * class TreeNode {\n * val: number\n * left: TreeNode | null\n * right: TreeNode | null\n * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n * this.val = (val===undefined ? 0 : val)\n * this.left = (left===undefined ? null : left)\n * this.right = (right===undefined ? null : right)\n * }\n * }\n */\n\nfunction createBinaryTree(descriptions: number[][]): TreeNode | null {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "/**\n * Definition for a binary tree node.\n * class TreeNode {\n * public $val = null;\n * public $left = null;\n * public $right = null;\n * function __construct($val = 0, $left = null, $right = null) {\n * $this->val = $val;\n * $this->left = $left;\n * $this->right = $right;\n * }\n * }\n */\nclass Solution {\n\n /**\n * @param Integer[][] $descriptions\n * @return TreeNode\n */\n function createBinaryTree($descriptions) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * public var val: Int\n * public var left: TreeNode?\n * public var right: TreeNode?\n * public init() { self.val = 0; self.left = nil; self.right = nil; }\n * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }\n * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {\n * self.val = val\n * self.left = left\n * self.right = right\n * }\n * }\n */\nclass Solution {\n func createBinaryTree(_ descriptions: [[Int]]) -> TreeNode? {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "/**\n * Example:\n * var ti = TreeNode(5)\n * var v = ti.`val`\n * Definition for a binary tree node.\n * class TreeNode(var `val`: Int) {\n * var left: TreeNode? = null\n * var right: TreeNode? = null\n * }\n */\nclass Solution {\n fun createBinaryTree(descriptions: Array<IntArray>): TreeNode? {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "/**\n * Definition for a binary tree node.\n * class TreeNode {\n * int val;\n * TreeNode? left;\n * TreeNode? right;\n * TreeNode([this.val = 0, this.left, this.right]);\n * }\n */\nclass Solution {\n TreeNode? createBinaryTree(List<List<int>> descriptions) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "/**\n * Definition for a binary tree node.\n * type TreeNode struct {\n * Val int\n * Left *TreeNode\n * Right *TreeNode\n * }\n */\nfunc createBinaryTree(descriptions [][]int) *TreeNode {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# Definition for a binary tree node.\n# class TreeNode\n# attr_accessor :val, :left, :right\n# def initialize(val = 0, left = nil, right = nil)\n# @val = val\n# @left = left\n# @right = right\n# end\n# end\n# @param {Integer[][]} descriptions\n# @return {TreeNode}\ndef create_binary_tree(descriptions)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "/**\n * Definition for a binary tree node.\n * class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {\n * var value: Int = _value\n * var left: TreeNode = _left\n * var right: TreeNode = _right\n * }\n */\nobject Solution {\n def createBinaryTree(descriptions: Array[Array[Int]]): TreeNode = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "// Definition for a binary tree node.\n// #[derive(Debug, PartialEq, Eq)]\n// pub struct TreeNode {\n// pub val: i32,\n// pub left: Option<Rc<RefCell<TreeNode>>>,\n// pub right: Option<Rc<RefCell<TreeNode>>>,\n// }\n// \n// impl TreeNode {\n// #[inline]\n// pub fn new(val: i32) -> Self {\n// TreeNode {\n// val,\n// left: None,\n// right: None\n// }\n// }\n// }\nuse std::rc::Rc;\nuse std::cell::RefCell;\nimpl Solution {\n pub fn create_binary_tree(descriptions: Vec<Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "; Definition for a binary tree node.\n#|\n\n; val : integer?\n; left : (or/c tree-node? #f)\n; right : (or/c tree-node? #f)\n(struct tree-node\n (val left right) #:mutable #:transparent)\n\n; constructor\n(define (make-tree-node [val 0])\n (tree-node val #f #f))\n\n|#\n\n(define/contract (create-binary-tree descriptions)\n (-> (listof (listof exact-integer?)) (or/c tree-node? #f))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "%% Definition for a binary tree node.\n%%\n%% -record(tree_node, {val = 0 :: integer(),\n%% left = null :: 'null' | #tree_node{},\n%% right = null :: 'null' | #tree_node{}}).\n\n-spec create_binary_tree(Descriptions :: [[integer()]]) -> #tree_node{} | null.\ncreate_binary_tree(Descriptions) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "# Definition for a binary tree node.\n#\n# defmodule TreeNode do\n# @type t :: %__MODULE__{\n# val: integer,\n# left: TreeNode.t() | nil,\n# right: TreeNode.t() | nil\n# }\n# defstruct val: 0, left: nil, right: nil\n# end\n\ndefmodule Solution do\n @spec create_binary_tree(descriptions :: [[integer]]) :: TreeNode.t | nil\n def create_binary_tree(descriptions) do\n \n end\nend"}] | [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]] | {
"name": "createBinaryTree",
"params": [
{
"name": "descriptions",
"type": "integer[][]"
}
],
"return": {
"type": "TreeNode"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Tree', 'Binary Tree'] |
2,197 | Replace Non-Coprime Numbers in Array | replace-non-coprime-numbers-in-array | <p>You are given an array of integers <code>nums</code>. Perform the following steps:</p>
<ol>
<li>Find <strong>any</strong> two <strong>adjacent</strong> numbers in <code>nums</code> that are <strong>non-coprime</strong>.</li>
<li>If no such numbers are found, <strong>stop</strong> the process.</li>
<li>Otherwise, delete the two numbers and <strong>replace</strong> them with their <strong>LCM (Least Common Multiple)</strong>.</li>
<li><strong>Repeat</strong> this process as long as you keep finding two adjacent non-coprime numbers.</li>
</ol>
<p>Return <em>the <strong>final</strong> modified array.</em> It can be shown that replacing adjacent non-coprime numbers in <strong>any</strong> arbitrary order will lead to the same result.</p>
<p>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</p>
<p>Two values <code>x</code> and <code>y</code> are <strong>non-coprime</strong> if <code>GCD(x, y) > 1</code> where <code>GCD(x, y)</code> is the <strong>Greatest Common Divisor</strong> of <code>x</code> and <code>y</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [6,4,3,2,7,6,2]
<strong>Output:</strong> [12,7,6]
<strong>Explanation:</strong>
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,<u><strong>6</strong></u>].
There are no more adjacent non-coprime numbers in nums.
Thus, the final modified array is [12,7,6].
Note that there are other ways to obtain the same resultant array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,1,1,3,3,3]
<strong>Output:</strong> [2,1,1,3]
<strong>Explanation:</strong>
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>,3].
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>].
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<u><strong>2</strong></u>,1,1,3].
There are no more adjacent non-coprime numbers in nums.
Thus, the final modified array is [2,1,1,3].
Note that there are other ways to obtain the same resultant array.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
<li>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</li>
</ul>
| Hard | 16.4K | 40.9K | 16,432 | 40,869 | 40.2% | ['remove-all-adjacent-duplicates-in-string-ii', 'number-of-pairs-of-interchangeable-rectangles', 'split-the-array-to-make-coprime-products'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> replaceNonCoprimes(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> replaceNonCoprimes(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def replaceNonCoprimes(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def replaceNonCoprimes(self, nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* replaceNonCoprimes(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> ReplaceNonCoprimes(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar replaceNonCoprimes = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function replaceNonCoprimes(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function replaceNonCoprimes($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func replaceNonCoprimes(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun replaceNonCoprimes(nums: IntArray): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> replaceNonCoprimes(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func replaceNonCoprimes(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef replace_non_coprimes(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def replaceNonCoprimes(nums: Array[Int]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (replace-non-coprimes nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec replace_non_coprimes(Nums :: [integer()]) -> [integer()].\nreplace_non_coprimes(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec replace_non_coprimes(nums :: [integer]) :: [integer]\n def replace_non_coprimes(nums) do\n \n end\nend"}] | [6,4,3,2,7,6,2] | {
"name": "replaceNonCoprimes",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "list<integer>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Math', 'Stack', 'Number Theory'] |
2,198 | Number of Single Divisor Triplets | number-of-single-divisor-triplets | null | Medium | 1.4K | 2.6K | 1,392 | 2,575 | 54.1% | ['count-array-pairs-divisible-by-k'] | [] | Algorithms | null | [4,6,7,3,2] | {
"name": "singleDivisorTriplet",
"params": [
{
"name": "nums",
"type": "integer[]"
}
],
"return": {
"type": "long"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Math'] |
2,199 | Finding the Topic of Each Post | finding-the-topic-of-each-post | null | Hard | 4.3K | 8.7K | 4,343 | 8,681 | 50.0% | ['group-sold-products-by-the-date'] | ['Create table If Not Exists Keywords (topic_id int, word varchar(25))', 'Create table If Not Exists Posts (post_id int, content varchar(100))', 'Truncate table Keywords', "insert into Keywords (topic_id, word) values ('1', 'handball')", "insert into Keywords (topic_id, word) values ('1', 'football')", "insert into Keywords (topic_id, word) values ('3', 'WAR')", "insert into Keywords (topic_id, word) values ('2', 'Vaccine')", 'Truncate table Posts', "insert into Posts (post_id, content) values ('1', 'We call it soccer They call it football hahaha')", "insert into Posts (post_id, content) values ('2', 'Americans prefer basketball while Europeans love handball and football')", "insert into Posts (post_id, content) values ('3', 'stop the war and play handball')", "insert into Posts (post_id, content) values ('4', 'warning I planted some flowers this morning and then got vaccinated')"] | Database | null | {"headers": {"Keywords": ["topic_id", "word"], "Posts": ["post_id", "content"]}, "rows": {"Keywords": [[1, "handball"], [1, "football"], [3, "WAR"], [2, "Vaccine"]], "Posts": [[1, "We call it soccer They call it football hahaha"], [2, "Americans prefer basketball while Europeans love handball and football"], [3, "stop the war and play handball"], [4, "warning I planted some flowers this morning and then got vaccinated"]]}} | {"mysql": ["Create table If Not Exists Keywords (topic_id int, word varchar(25))", "Create table If Not Exists Posts (post_id int, content varchar(100))"], "mssql": ["Create table Keywords (topic_id int, word varchar(25))", "Create table Posts (post_id int, content varchar(100))"], "oraclesql": ["Create table Keywords (topic_id int, word varchar(25))", "Create table Posts (post_id int, content varchar(100))"], "database": true, "name": "find_topic", "pythondata": ["Keywords = pd.DataFrame([], columns=['topic_id', 'word']).astype({'topic_id':'Int64', 'word':'object'})", "Posts = pd.DataFrame([], columns=['post_id', 'content']).astype({'post_id':'Int64', 'content':'object'})"], "postgresql": ["Create table If Not Exists Keywords (topic_id int, word varchar(25))", "Create table If Not Exists Posts (post_id int, content varchar(100))"], "database_schema": {"Keywords": {"topic_id": "INT", "word": "VARCHAR(25)"}, "Posts": {"post_id": "INT", "content": "VARCHAR(100)"}}} | {"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]} | ['Database'] |
2,200 | Find All K-Distant Indices in an Array | find-all-k-distant-indices-in-an-array | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>key</code> and <code>k</code>. A <strong>k-distant index</strong> is an index <code>i</code> of <code>nums</code> for which there exists at least one index <code>j</code> such that <code>|i - j| <= k</code> and <code>nums[j] == key</code>.</p>
<p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,4,9,1,3,9,5], key = 9, k = 1
<strong>Output:</strong> [1,2,3,4,5,6]
<strong>Explanation:</strong> Here, <code>nums[2] == key</code> and <code>nums[5] == key.
- For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j</code> where <code>|0 - j| <= k</code> and <code>nums[j] == key. Thus, 0 is not a k-distant index.
- For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
- For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
- For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
- For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
- For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
- For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
</code>Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], key = 2, k = 2
<strong>Output:</strong> [0,1,2,3,4]
<strong>Explanation:</strong> For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
Hence, we return [0,1,2,3,4].
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>1 <= nums[i] <= 1000</code></li>
<li><code>key</code> is an integer from the array <code>nums</code>.</li>
<li><code>1 <= k <= nums.length</code></li>
</ul>
| Easy | 46.8K | 69.9K | 46,828 | 69,905 | 67.0% | ['two-sum', 'shortest-word-distance', 'minimum-absolute-difference-between-elements-with-constraint'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> findKDistantIndices(int[] nums, int key, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findKDistantIndices(self, nums, key, k):\n \"\"\"\n :type nums: List[int]\n :type key: int\n :type k: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* findKDistantIndices(int* nums, int numsSize, int key, int k, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> FindKDistantIndices(int[] nums, int key, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} key\n * @param {number} k\n * @return {number[]}\n */\nvar findKDistantIndices = function(nums, key, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findKDistantIndices(nums: number[], key: number, k: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $key\n * @param Integer $k\n * @return Integer[]\n */\n function findKDistantIndices($nums, $key, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findKDistantIndices(_ nums: [Int], _ key: Int, _ k: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findKDistantIndices(nums: IntArray, key: Int, k: Int): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> findKDistantIndices(List<int> nums, int key, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findKDistantIndices(nums []int, key int, k int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} key\n# @param {Integer} k\n# @return {Integer[]}\ndef find_k_distant_indices(nums, key, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findKDistantIndices(nums: Array[Int], key: Int, k: Int): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-k-distant-indices nums key k)\n (-> (listof exact-integer?) exact-integer? exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_k_distant_indices(Nums :: [integer()], Key :: integer(), K :: integer()) -> [integer()].\nfind_k_distant_indices(Nums, Key, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_k_distant_indices(nums :: [integer], key :: integer, k :: integer) :: [integer]\n def find_k_distant_indices(nums, key, k) do\n \n end\nend"}] | [3,4,9,1,3,9,5]
9
1 | {
"name": "findKDistantIndices",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "key"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "list<integer>"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Two Pointers'] |
2,201 | Count Artifacts That Can Be Extracted | count-artifacts-that-can-be-extracted | <p>There is an <code>n x n</code> <strong>0-indexed</strong> grid with some artifacts buried in it. You are given the integer <code>n</code> and a <strong>0-indexed </strong>2D integer array <code>artifacts</code> describing the positions of the rectangular artifacts where <code>artifacts[i] = [r1<sub>i</sub>, c1<sub>i</sub>, r2<sub>i</sub>, c2<sub>i</sub>]</code> denotes that the <code>i<sup>th</sup></code> artifact is buried in the subgrid where:</p>
<ul>
<li><code>(r1<sub>i</sub>, c1<sub>i</sub>)</code> is the coordinate of the <strong>top-left</strong> cell of the <code>i<sup>th</sup></code> artifact and</li>
<li><code>(r2<sub>i</sub>, c2<sub>i</sub>)</code> is the coordinate of the <strong>bottom-right</strong> cell of the <code>i<sup>th</sup></code> artifact.</li>
</ul>
<p>You will excavate some cells of the grid and remove all the mud from them. If the cell has a part of an artifact buried underneath, it will be uncovered. If all the parts of an artifact are uncovered, you can extract it.</p>
<p>Given a <strong>0-indexed</strong> 2D integer array <code>dig</code> where <code>dig[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> indicates that you will excavate the cell <code>(r<sub>i</sub>, c<sub>i</sub>)</code>, return <em>the number of artifacts that you can extract</em>.</p>
<p>The test cases are generated such that:</p>
<ul>
<li>No two artifacts overlap.</li>
<li>Each artifact only covers at most <code>4</code> cells.</li>
<li>The entries of <code>dig</code> are unique.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/09/16/untitled-diagram.jpg" style="width: 216px; height: 216px;" />
<pre>
<strong>Input:</strong> n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The different colors represent different artifacts. Excavated cells are labeled with a 'D' in the grid.
There is 1 artifact that can be extracted, namely the red artifact.
The blue artifact has one part in cell (1,1) which remains uncovered, so we cannot extract it.
Thus, we return 1.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/09/16/untitled-diagram-1.jpg" style="width: 216px; height: 216px;" />
<pre>
<strong>Input:</strong> n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1],[1,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Both the red and blue artifacts have all parts uncovered (labeled with a 'D') and can be extracted, so we return 2.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 1000</code></li>
<li><code>1 <= artifacts.length, dig.length <= min(n<sup>2</sup>, 10<sup>5</sup>)</code></li>
<li><code>artifacts[i].length == 4</code></li>
<li><code>dig[i].length == 2</code></li>
<li><code>0 <= r1<sub>i</sub>, c1<sub>i</sub>, r2<sub>i</sub>, c2<sub>i</sub>, r<sub>i</sub>, c<sub>i</sub> <= n - 1</code></li>
<li><code>r1<sub>i</sub> <= r2<sub>i</sub></code></li>
<li><code>c1<sub>i</sub> <= c2<sub>i</sub></code></li>
<li>No two artifacts will overlap.</li>
<li>The number of cells covered by an artifact is <strong>at most</strong> <code>4</code>.</li>
<li>The entries of <code>dig</code> are unique.</li>
</ul>
| Medium | 21.3K | 38K | 21,334 | 37,970 | 56.2% | ['maximal-square'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int digArtifacts(int n, int[][] artifacts, int[][] dig) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def digArtifacts(self, n, artifacts, dig):\n \"\"\"\n :type n: int\n :type artifacts: List[List[int]]\n :type dig: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def digArtifacts(self, n: int, artifacts: List[List[int]], dig: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int digArtifacts(int n, int** artifacts, int artifactsSize, int* artifactsColSize, int** dig, int digSize, int* digColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DigArtifacts(int n, int[][] artifacts, int[][] dig) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} artifacts\n * @param {number[][]} dig\n * @return {number}\n */\nvar digArtifacts = function(n, artifacts, dig) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $artifacts\n * @param Integer[][] $dig\n * @return Integer\n */\n function digArtifacts($n, $artifacts, $dig) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func digArtifacts(_ n: Int, _ artifacts: [[Int]], _ dig: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun digArtifacts(n: Int, artifacts: Array<IntArray>, dig: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int digArtifacts(int n, List<List<int>> artifacts, List<List<int>> dig) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func digArtifacts(n int, artifacts [][]int, dig [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} artifacts\n# @param {Integer[][]} dig\n# @return {Integer}\ndef dig_artifacts(n, artifacts, dig)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def digArtifacts(n: Int, artifacts: Array[Array[Int]], dig: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn dig_artifacts(n: i32, artifacts: Vec<Vec<i32>>, dig: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (dig-artifacts n artifacts dig)\n (-> exact-integer? (listof (listof exact-integer?)) (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec dig_artifacts(N :: integer(), Artifacts :: [[integer()]], Dig :: [[integer()]]) -> integer().\ndig_artifacts(N, Artifacts, Dig) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec dig_artifacts(n :: integer, artifacts :: [[integer]], dig :: [[integer]]) :: integer\n def dig_artifacts(n, artifacts, dig) do\n \n end\nend"}] | 2
[[0,0,0,0],[0,1,1,1]]
[[0,0],[0,1]] | {
"name": "digArtifacts",
"params": [
{
"type": "integer",
"name": "n"
},
{
"type": "integer[][]",
"name": "artifacts"
},
{
"type": "integer[][]",
"name": "dig"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Hash Table', 'Simulation'] |
2,202 | Maximize the Topmost Element After K Moves | maximize-the-topmost-element-after-k-moves | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the contents of a <b>pile</b>, where <code>nums[0]</code> is the topmost element of the pile.</p>
<p>In one move, you can perform <strong>either</strong> of the following:</p>
<ul>
<li>If the pile is not empty, <strong>remove</strong> the topmost element of the pile.</li>
<li>If there are one or more removed elements, <strong>add</strong> any one of them back onto the pile. This element becomes the new topmost element.</li>
</ul>
<p>You are also given an integer <code>k</code>, which denotes the total number of moves to be made.</p>
<p>Return <em>the <strong>maximum value</strong> of the topmost element of the pile possible after <strong>exactly</strong></em> <code>k</code> <em>moves</em>. In case it is not possible to obtain a non-empty pile after <code>k</code> moves, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [5,2,2,4,0,6], k = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong>
One of the ways we can end with 5 at the top of the pile after 4 moves is as follows:
- Step 1: Remove the topmost element = 5. The pile becomes [2,2,4,0,6].
- Step 2: Remove the topmost element = 2. The pile becomes [2,4,0,6].
- Step 3: Remove the topmost element = 2. The pile becomes [4,0,6].
- Step 4: Add 5 back onto the pile. The pile becomes [5,4,0,6].
Note that this is not the only way to end with 5 at the top of the pile. It can be shown that 5 is the largest answer possible after 4 moves.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2], k = 1
<strong>Output:</strong> -1
<strong>Explanation:</strong>
In the first move, our only option is to pop the topmost element of the pile.
Since it is not possible to obtain a non-empty pile after one move, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>0 <= nums[i], k <= 10<sup>9</sup></code></li>
</ul>
| Medium | 33K | 141.3K | 32,965 | 141,317 | 23.3% | ['gas-station'] | [] | Algorithms | [{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumTop(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumTop(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumTop(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumTop(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumTop(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumTop(int[] nums, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar maximumTop = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumTop(nums: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @return Integer\n */\n function maximumTop($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumTop(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumTop(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumTop(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumTop(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef maximum_top(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumTop(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_top(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-top nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_top(Nums :: [integer()], K :: integer()) -> integer().\nmaximum_top(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_top(nums :: [integer], k :: integer) :: integer\n def maximum_top(nums, k) do\n \n end\nend"}] | [5,2,2,4,0,6]
4 | {
"name": "maximumTop",
"params": [
{
"name": "nums",
"type": "integer[]"
},
{
"type": "integer",
"name": "k"
}
],
"return": {
"type": "integer"
}
} | {"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]} | ['Array', 'Greedy'] |
Subsets and Splits