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,509
Cycle Length Queries in a Tree
cycle-length-queries-in-a-tree
<p>You are given an integer <code>n</code>. There is a <strong>complete binary tree</strong> with <code>2<sup>n</sup> - 1</code> nodes. The root of that tree is the node with the value <code>1</code>, and every node with a value <code>val</code> in the range <code>[1, 2<sup>n - 1</sup> - 1]</code> has two children where:</p> <ul> <li>The left node has the value <code>2 * val</code>, and</li> <li>The right node has the value <code>2 * val + 1</code>.</li> </ul> <p>You are also given a 2D integer array <code>queries</code> of length <code>m</code>, where <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>. For each query, solve the following problem:</p> <ol> <li>Add an edge between the nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li> <li>Find the length of the cycle in the graph.</li> <li>Remove the added edge between nodes with values <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</li> </ol> <p><strong>Note</strong> that:</p> <ul> <li>A <strong>cycle</strong> is a path that starts and ends at the same node, and each edge in the path is visited only once.</li> <li>The length of a cycle is the number of edges visited in the cycle.</li> <li>There could be multiple edges between two nodes in the tree after adding the edge of the query.</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 the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/bexample1.png" style="width: 647px; height: 128px;" /> <pre> <strong>Input:</strong> n = 3, queries = [[5,3],[4,7],[2,3]] <strong>Output:</strong> [4,5,3] <strong>Explanation:</strong> The diagrams above show the tree of 2<sup>3</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 3 and 5, the graph contains a cycle of nodes [5,2,1,3]. Thus answer to the first query is 4. We delete the added edge and process the next query. - After adding the edge between nodes 4 and 7, the graph contains a cycle of nodes [4,2,1,3,7]. Thus answer to the second query is 5. We delete the added edge and process the next query. - After adding the edge between nodes 2 and 3, the graph contains a cycle of nodes [2,1,3]. Thus answer to the third query is 3. We delete the added edge. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/10/25/aexample2.png" style="width: 146px; height: 71px;" /> <pre> <strong>Input:</strong> n = 2, queries = [[1,2]] <strong>Output:</strong> [2] <strong>Explanation:</strong> The diagram above shows the tree of 2<sup>2</sup> - 1 nodes. Nodes colored in red describe the nodes in the cycle after adding the edge. - After adding the edge between nodes 1 and 2, the graph contains a cycle of nodes [2,1]. Thus answer for the first query is 2. We delete the added edge. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 30</code></li> <li><code>m == queries.length</code></li> <li><code>1 &lt;= m &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>1 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= 2<sup>n</sup> - 1</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> </ul>
Hard
15.7K
27.1K
15,700
27,142
57.8%
['populating-next-right-pointers-in-each-node', 'lowest-common-ancestor-of-a-binary-tree', 'path-in-zigzag-labelled-binary-tree']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> cycleLengthQueries(int n, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] cycleLengthQueries(int n, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def cycleLengthQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def cycleLengthQueries(self, n: int, queries: List[List[int]]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* cycleLengthQueries(int n, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] CycleLengthQueries(int n, int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar cycleLengthQueries = function(n, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function cycleLengthQueries(n: number, queries: number[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function cycleLengthQueries($n, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func cycleLengthQueries(_ n: Int, _ queries: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun cycleLengthQueries(n: Int, queries: Array<IntArray>): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> cycleLengthQueries(int n, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func cycleLengthQueries(n int, queries [][]int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef cycle_length_queries(n, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def cycleLengthQueries(n: Int, queries: Array[Array[Int]]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn cycle_length_queries(n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (cycle-length-queries n queries)\n (-> exact-integer? (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec cycle_length_queries(N :: integer(), Queries :: [[integer()]]) -> [integer()].\ncycle_length_queries(N, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec cycle_length_queries(n :: integer, queries :: [[integer]]) :: [integer]\n def cycle_length_queries(n, queries) do\n \n end\nend"}]
3 [[5,3],[4,7],[2,3]]
{ "name": "cycleLengthQueries", "params": [ { "type": "integer", "name": "n" }, { "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', 'Tree', 'Binary Tree']
2,510
Check if There is a Path With Equal Number of 0's And 1's
check-if-there-is-a-path-with-equal-number-of-0s-and-1s
null
Medium
6.6K
12.9K
6,624
12,915
51.3%
['minimum-path-sum', 'dungeon-game', 'minimum-cost-homecoming-of-a-robot-in-a-grid', 'paths-in-matrix-whose-sum-is-divisible-by-k']
[]
Algorithms
null
[[0,1,0,0],[0,1,0,0],[1,0,1,0]]
{ "name": "isThereAPath", "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', 'Dynamic Programming', 'Matrix']
2,511
Maximum Enemy Forts That Can Be Captured
maximum-enemy-forts-that-can-be-captured
<p>You are given a <strong>0-indexed</strong> integer array <code>forts</code> of length <code>n</code> representing the positions of several forts. <code>forts[i]</code> can be <code>-1</code>, <code>0</code>, or <code>1</code> where:</p> <ul> <li><code>-1</code> represents there is <strong>no fort</strong> at the <code>i<sup>th</sup></code> position.</li> <li><code>0</code> indicates there is an <strong>enemy</strong> fort at the <code>i<sup>th</sup></code> position.</li> <li><code>1</code> indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.</li> </ul> <p>Now you have decided to move your army from one of your forts at position <code>i</code> to an empty position <code>j</code> such that:</p> <ul> <li><code>0 &lt;= i, j &lt;= n - 1</code></li> <li>The army travels over enemy forts <strong>only</strong>. Formally, for all <code>k</code> where <code>min(i,j) &lt; k &lt; max(i,j)</code>, <code>forts[k] == 0.</code></li> </ul> <p>While moving the army, all the enemy forts that come in the way are <strong>captured</strong>.</p> <p>Return<em> the <strong>maximum</strong> number of enemy forts that can be captured</em>. In case it is <strong>impossible</strong> to move your army, or you do not have any fort under your command, return <code>0</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2. - Moving the army from position 8 to position 3 captures 4 enemy forts. Since 4 is the maximum number of enemy forts that can be captured, we return 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> forts = [0,0,1,-1] <strong>Output:</strong> 0 <strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= forts.length &lt;= 1000</code></li> <li><code>-1 &lt;= forts[i] &lt;= 1</code></li> </ul>
Easy
28.9K
72.5K
28,852
72,498
39.8%
['max-consecutive-ones', 'max-consecutive-ones-iii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int captureForts(vector<int>& forts) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int captureForts(int[] forts) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def captureForts(self, forts):\n \"\"\"\n :type forts: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def captureForts(self, forts: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int captureForts(int* forts, int fortsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CaptureForts(int[] forts) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} forts\n * @return {number}\n */\nvar captureForts = function(forts) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function captureForts(forts: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $forts\n * @return Integer\n */\n function captureForts($forts) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func captureForts(_ forts: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun captureForts(forts: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int captureForts(List<int> forts) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func captureForts(forts []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} forts\n# @return {Integer}\ndef capture_forts(forts)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def captureForts(forts: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn capture_forts(forts: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (capture-forts forts)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec capture_forts(Forts :: [integer()]) -> integer().\ncapture_forts(Forts) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec capture_forts(forts :: [integer]) :: integer\n def capture_forts(forts) do\n \n end\nend"}]
[1,0,0,-1,0,0,0,0,1]
{ "name": "captureForts", "params": [ { "name": "forts", "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']
2,512
Reward Top K Students
reward-top-k-students
<p>You are given two string arrays <code>positive_feedback</code> and <code>negative_feedback</code>, containing the words denoting positive and negative feedback, respectively. Note that <strong>no</strong> word is both positive and negative.</p> <p>Initially every student has <code>0</code> points. Each positive word in a feedback report <strong>increases</strong> the points of a student by <code>3</code>, whereas each negative word <strong>decreases</strong> the points by <code>1</code>.</p> <p>You are given <code>n</code> feedback reports, represented by a <strong>0-indexed</strong> string array <code>report</code>&nbsp;and a <strong>0-indexed</strong> integer array <code>student_id</code>, where <code>student_id[i]</code> represents the ID of the student who has received the feedback report <code>report[i]</code>. The ID of each student is <strong>unique</strong>.</p> <p>Given an integer <code>k</code>, return <em>the top </em><code>k</code><em> students after ranking them in <strong>non-increasing</strong> order by their points</em>. In case more than one student has the same points, the one with the lower ID ranks higher.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> positive_feedback = [&quot;smart&quot;,&quot;brilliant&quot;,&quot;studious&quot;], negative_feedback = [&quot;not&quot;], report = [&quot;this student is studious&quot;,&quot;the student is smart&quot;], student_id = [1,2], k = 2 <strong>Output:</strong> [1,2] <strong>Explanation:</strong> Both the students have 1 positive feedback and 3 points but since student 1 has a lower ID he ranks higher. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> positive_feedback = [&quot;smart&quot;,&quot;brilliant&quot;,&quot;studious&quot;], negative_feedback = [&quot;not&quot;], report = [&quot;this student is not studious&quot;,&quot;the student is smart&quot;], student_id = [1,2], k = 2 <strong>Output:</strong> [2,1] <strong>Explanation:</strong> - The student with ID 1 has 1 positive feedback and 1 negative feedback, so he has 3-1=2 points. - The student with ID 2 has 1 positive feedback, so he has 3 points. Since student 2 has more points, [2,1] is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positive_feedback.length, negative_feedback.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= positive_feedback[i].length, negative_feedback[j].length &lt;= 100</code></li> <li>Both <code>positive_feedback[i]</code> and <code>negative_feedback[j]</code> consists of lowercase English letters.</li> <li>No word is present in both <code>positive_feedback</code> and <code>negative_feedback</code>.</li> <li><code>n == report.length == student_id.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>report[i]</code> consists of lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li>There is a single space between consecutive words of <code>report[i]</code>.</li> <li><code>1 &lt;= report[i].length &lt;= 100</code></li> <li><code>1 &lt;= student_id[i] &lt;= 10<sup>9</sup></code></li> <li>All the values of <code>student_id[i]</code> are <strong>unique</strong>.</li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
Medium
24.8K
54.2K
24,801
54,233
45.7%
['queue-reconstruction-by-height', 'k-highest-ranked-items-within-a-price-range']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> topStudents(vector<string>& positive_feedback, vector<string>& negative_feedback, vector<string>& report, vector<int>& student_id, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def topStudents(self, positive_feedback, negative_feedback, report, student_id, k):\n \"\"\"\n :type positive_feedback: List[str]\n :type negative_feedback: List[str]\n :type report: List[str]\n :type student_id: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def topStudents(self, positive_feedback: List[str], negative_feedback: List[str], report: List[str], student_id: List[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* topStudents(char** positive_feedback, int positive_feedbackSize, char** negative_feedback, int negative_feedbackSize, char** report, int reportSize, int* student_id, int student_idSize, int k, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> TopStudents(string[] positive_feedback, string[] negative_feedback, string[] report, int[] student_id, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} positive_feedback\n * @param {string[]} negative_feedback\n * @param {string[]} report\n * @param {number[]} student_id\n * @param {number} k\n * @return {number[]}\n */\nvar topStudents = function(positive_feedback, negative_feedback, report, student_id, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function topStudents(positive_feedback: string[], negative_feedback: string[], report: string[], student_id: number[], k: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $positive_feedback\n * @param String[] $negative_feedback\n * @param String[] $report\n * @param Integer[] $student_id\n * @param Integer $k\n * @return Integer[]\n */\n function topStudents($positive_feedback, $negative_feedback, $report, $student_id, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func topStudents(_ positive_feedback: [String], _ negative_feedback: [String], _ report: [String], _ student_id: [Int], _ k: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun topStudents(positive_feedback: Array<String>, negative_feedback: Array<String>, report: Array<String>, student_id: IntArray, k: Int): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> topStudents(List<String> positive_feedback, List<String> negative_feedback, List<String> report, List<int> student_id, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func topStudents(positive_feedback []string, negative_feedback []string, report []string, student_id []int, k int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} positive_feedback\n# @param {String[]} negative_feedback\n# @param {String[]} report\n# @param {Integer[]} student_id\n# @param {Integer} k\n# @return {Integer[]}\ndef top_students(positive_feedback, negative_feedback, report, student_id, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def topStudents(positive_feedback: Array[String], negative_feedback: Array[String], report: Array[String], student_id: Array[Int], k: Int): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn top_students(positive_feedback: Vec<String>, negative_feedback: Vec<String>, report: Vec<String>, student_id: Vec<i32>, k: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (top-students positive_feedback negative_feedback report student_id k)\n (-> (listof string?) (listof string?) (listof string?) (listof exact-integer?) exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec top_students(Positive_feedback :: [unicode:unicode_binary()], Negative_feedback :: [unicode:unicode_binary()], Report :: [unicode:unicode_binary()], Student_id :: [integer()], K :: integer()) -> [integer()].\ntop_students(Positive_feedback, Negative_feedback, Report, Student_id, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec top_students(positive_feedback :: [String.t], negative_feedback :: [String.t], report :: [String.t], student_id :: [integer], k :: integer) :: [integer]\n def top_students(positive_feedback, negative_feedback, report, student_id, k) do\n \n end\nend"}]
["smart","brilliant","studious"] ["not"] ["this student is studious","the student is smart"] [1,2] 2
{ "name": "topStudents", "params": [ { "name": "positive_feedback", "type": "string[]" }, { "type": "string[]", "name": "negative_feedback" }, { "type": "string[]", "name": "report" }, { "type": "integer[]", "name": "student_id" }, { "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', 'Hash Table', 'String', 'Sorting', 'Heap (Priority Queue)']
2,513
Minimize the Maximum of Two Arrays
minimize-the-maximum-of-two-arrays
<p>We have two arrays <code>arr1</code> and <code>arr2</code> which are initially empty. You need to add positive integers to them such that they satisfy all the following conditions:</p> <ul> <li><code>arr1</code> contains <code>uniqueCnt1</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor1</code>.</li> <li><code>arr2</code> contains <code>uniqueCnt2</code> <strong>distinct</strong> positive integers, each of which is <strong>not divisible</strong> by <code>divisor2</code>.</li> <li><strong>No</strong> integer is present in both <code>arr1</code> and <code>arr2</code>.</li> </ul> <p>Given <code>divisor1</code>, <code>divisor2</code>, <code>uniqueCnt1</code>, and <code>uniqueCnt2</code>, return <em>the <strong>minimum possible maximum</strong> integer that can be present in either array</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> divisor1 = 2, divisor2 = 7, uniqueCnt1 = 1, uniqueCnt2 = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can distribute the first 4 natural numbers into arr1 and arr2. arr1 = [1] and arr2 = [2,3,4]. We can see that both arrays satisfy all the conditions. Since the maximum value is 4, we return it. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> divisor1 = 3, divisor2 = 5, uniqueCnt1 = 2, uniqueCnt2 = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> Here arr1 = [1,2], and arr2 = [3] satisfy all conditions. Since the maximum value is 3, we return it.</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> divisor1 = 2, divisor2 = 4, uniqueCnt1 = 8, uniqueCnt2 = 2 <strong>Output:</strong> 15 <strong>Explanation:</strong> Here, the final possible arrays can be arr1 = [1,3,5,7,9,11,13,15], and arr2 = [2,6]. It can be shown that it is not possible to obtain a lower maximum satisfying all conditions. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= divisor1, divisor2 &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= uniqueCnt1, uniqueCnt2 &lt; 10<sup>9</sup></code></li> <li><code>2 &lt;= uniqueCnt1 + uniqueCnt2 &lt;= 10<sup>9</sup></code></li> </ul>
Medium
14.9K
47.8K
14,876
47,823
31.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimizeSet(self, divisor1, divisor2, uniqueCnt1, uniqueCnt2):\n \"\"\"\n :type divisor1: int\n :type divisor2: int\n :type uniqueCnt1: int\n :type uniqueCnt2: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimizeSet(self, divisor1: int, divisor2: int, uniqueCnt1: int, uniqueCnt2: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} divisor1\n * @param {number} divisor2\n * @param {number} uniqueCnt1\n * @param {number} uniqueCnt2\n * @return {number}\n */\nvar minimizeSet = function(divisor1, divisor2, uniqueCnt1, uniqueCnt2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimizeSet(divisor1: number, divisor2: number, uniqueCnt1: number, uniqueCnt2: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $divisor1\n * @param Integer $divisor2\n * @param Integer $uniqueCnt1\n * @param Integer $uniqueCnt2\n * @return Integer\n */\n function minimizeSet($divisor1, $divisor2, $uniqueCnt1, $uniqueCnt2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimizeSet(_ divisor1: Int, _ divisor2: Int, _ uniqueCnt1: Int, _ uniqueCnt2: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimizeSet(divisor1: Int, divisor2: Int, uniqueCnt1: Int, uniqueCnt2: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimizeSet(int divisor1, int divisor2, int uniqueCnt1, int uniqueCnt2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimizeSet(divisor1 int, divisor2 int, uniqueCnt1 int, uniqueCnt2 int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} divisor1\n# @param {Integer} divisor2\n# @param {Integer} unique_cnt1\n# @param {Integer} unique_cnt2\n# @return {Integer}\ndef minimize_set(divisor1, divisor2, unique_cnt1, unique_cnt2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimizeSet(divisor1: Int, divisor2: Int, uniqueCnt1: Int, uniqueCnt2: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimize_set(divisor1: i32, divisor2: i32, unique_cnt1: i32, unique_cnt2: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimize-set divisor1 divisor2 uniqueCnt1 uniqueCnt2)\n (-> exact-integer? exact-integer? exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimize_set(Divisor1 :: integer(), Divisor2 :: integer(), UniqueCnt1 :: integer(), UniqueCnt2 :: integer()) -> integer().\nminimize_set(Divisor1, Divisor2, UniqueCnt1, UniqueCnt2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimize_set(divisor1 :: integer, divisor2 :: integer, unique_cnt1 :: integer, unique_cnt2 :: integer) :: integer\n def minimize_set(divisor1, divisor2, unique_cnt1, unique_cnt2) do\n \n end\nend"}]
2 7 1 3
{ "name": "minimizeSet", "params": [ { "name": "divisor1", "type": "integer" }, { "type": "integer", "name": "divisor2" }, { "type": "integer", "name": "uniqueCnt1" }, { "type": "integer", "name": "uniqueCnt2" } ], "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', 'Binary Search', 'Number Theory']
2,514
Count Anagrams
count-anagrams
<p>You are given a string <code>s</code> containing one or more words. Every consecutive pair of words is separated by a single space <code>&#39; &#39;</code>.</p> <p>A string <code>t</code> is an <strong>anagram</strong> of string <code>s</code> if the <code>i<sup>th</sup></code> word of <code>t</code> is a <strong>permutation</strong> of the <code>i<sup>th</sup></code> word of <code>s</code>.</p> <ul> <li>For example, <code>&quot;acb dfe&quot;</code> is an anagram of <code>&quot;abc def&quot;</code>, but <code>&quot;def cab&quot;</code>&nbsp;and <code>&quot;adc bef&quot;</code> are not.</li> </ul> <p>Return <em>the number of <strong>distinct anagrams</strong> of </em><code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;too hot&quot; <strong>Output:</strong> 18 <strong>Explanation:</strong> Some of the anagrams of the given string are &quot;too hot&quot;, &quot;oot hot&quot;, &quot;oto toh&quot;, &quot;too toh&quot;, and &quot;too oht&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;aa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> There is only one anagram possible for the given string.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of lowercase English letters and spaces <code>&#39; &#39;</code>.</li> <li>There is single space between consecutive words.</li> </ul>
Hard
15.1K
43.5K
15,139
43,482
34.8%
['group-anagrams', 'count-ways-to-build-rooms-in-an-ant-colony']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countAnagrams(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countAnagrams(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countAnagrams(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countAnagrams(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countAnagrams(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountAnagrams(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar countAnagrams = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countAnagrams(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function countAnagrams($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countAnagrams(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countAnagrams(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countAnagrams(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countAnagrams(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef count_anagrams(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countAnagrams(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_anagrams(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-anagrams s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_anagrams(S :: unicode:unicode_binary()) -> integer().\ncount_anagrams(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_anagrams(s :: String.t) :: integer\n def count_anagrams(s) do\n \n end\nend"}]
"too hot"
{ "name": "countAnagrams", "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', 'Math', 'String', 'Combinatorics', 'Counting']
2,515
Shortest Distance to Target String in a Circular Array
shortest-distance-to-target-string-in-a-circular-array
<p>You are given a <strong>0-indexed</strong> <strong>circular</strong> string array <code>words</code> and a string <code>target</code>. A <strong>circular array</strong> means that the array&#39;s end connects to the array&#39;s beginning.</p> <ul> <li>Formally, the next element of <code>words[i]</code> is <code>words[(i + 1) % n]</code> and the previous element of <code>words[i]</code> is <code>words[(i - 1 + n) % n]</code>, where <code>n</code> is the length of <code>words</code>.</li> </ul> <p>Starting from <code>startIndex</code>, you can move to either the next word or the previous word with <code>1</code> step at a time.</p> <p>Return <em>the <strong>shortest</strong> distance needed to reach the string</em> <code>target</code>. If the string <code>target</code> does not exist in <code>words</code>, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;hello&quot;,&quot;i&quot;,&quot;am&quot;,&quot;leetcode&quot;,&quot;hello&quot;], target = &quot;hello&quot;, startIndex = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> We start from index 1 and can reach &quot;hello&quot; by - moving 3 units to the right to reach index 4. - moving 2 units to the left to reach index 4. - moving 4 units to the right to reach index 0. - moving 1 unit to the left to reach index 0. The shortest distance to reach &quot;hello&quot; is 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;a&quot;,&quot;b&quot;,&quot;leetcode&quot;], target = &quot;leetcode&quot;, startIndex = 0 <strong>Output:</strong> 1 <strong>Explanation:</strong> We start from index 0 and can reach &quot;leetcode&quot; by - moving 2 units to the right to reach index 3. - moving 1 unit to the left to reach index 3. The shortest distance to reach &quot;leetcode&quot; is 1.</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> words = [&quot;i&quot;,&quot;eat&quot;,&quot;leetcode&quot;], target = &quot;ate&quot;, startIndex = 0 <strong>Output:</strong> -1 <strong>Explanation:</strong> Since &quot;ate&quot; does not exist in <code>words</code>, we return -1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 100</code></li> <li><code>1 &lt;= words[i].length &lt;= 100</code></li> <li><code>words[i]</code> and <code>target</code> consist of only lowercase English letters.</li> <li><code>0 &lt;= startIndex &lt; words.length</code></li> </ul>
Easy
35.5K
70.7K
35,525
70,688
50.3%
['defuse-the-bomb']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int closestTarget(vector<string>& words, string target, int startIndex) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int closestTarget(String[] words, String target, int startIndex) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def closestTarget(self, words, target, startIndex):\n \"\"\"\n :type words: List[str]\n :type target: str\n :type startIndex: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def closestTarget(self, words: List[str], target: str, startIndex: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int closestTarget(char** words, int wordsSize, char* target, int startIndex) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int ClosestTarget(string[] words, string target, int startIndex) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @param {string} target\n * @param {number} startIndex\n * @return {number}\n */\nvar closestTarget = function(words, target, startIndex) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function closestTarget(words: string[], target: string, startIndex: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @param String $target\n * @param Integer $startIndex\n * @return Integer\n */\n function closestTarget($words, $target, $startIndex) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func closestTarget(_ words: [String], _ target: String, _ startIndex: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun closestTarget(words: Array<String>, target: String, startIndex: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int closestTarget(List<String> words, String target, int startIndex) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func closestTarget(words []string, target string, startIndex int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @param {String} target\n# @param {Integer} start_index\n# @return {Integer}\ndef closest_target(words, target, start_index)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def closestTarget(words: Array[String], target: String, startIndex: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn closest_target(words: Vec<String>, target: String, start_index: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (closest-target words target startIndex)\n (-> (listof string?) string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec closest_target(Words :: [unicode:unicode_binary()], Target :: unicode:unicode_binary(), StartIndex :: integer()) -> integer().\nclosest_target(Words, Target, StartIndex) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec closest_target(words :: [String.t], target :: String.t, start_index :: integer) :: integer\n def closest_target(words, target, start_index) do\n \n end\nend"}]
["hello","i","am","leetcode","hello"] "hello" 1
{ "name": "closestTarget", "params": [ { "name": "words", "type": "string[]" }, { "type": "string", "name": "target" }, { "type": "integer", "name": "startIndex" } ], "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,516
Take K of Each Character From Left and Right
take-k-of-each-character-from-left-and-right
<p>You are given a string <code>s</code> consisting of the characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code> and a non-negative integer <code>k</code>. Each minute, you may take either the <strong>leftmost</strong> character of <code>s</code>, or the <strong>rightmost</strong> character of <code>s</code>.</p> <p>Return<em> the <strong>minimum</strong> number of minutes needed for you to take <strong>at least</strong> </em><code>k</code><em> of each character, or return </em><code>-1</code><em> if it is not possible to take </em><code>k</code><em> of each character.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aabaaaacaabc&quot;, k = 2 <strong>Output:</strong> 8 <strong>Explanation:</strong> Take three characters from the left of s. You now have two &#39;a&#39; characters, and one &#39;b&#39; character. Take five characters from the right of s. You now have four &#39;a&#39; characters, two &#39;b&#39; characters, and two &#39;c&#39; characters. A total of 3 + 5 = 8 minutes is needed. It can be proven that 8 is the minimum number of minutes needed. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;a&quot;, k = 1 <strong>Output:</strong> -1 <strong>Explanation:</strong> It is not possible to take one &#39;b&#39; or &#39;c&#39; so return -1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only the letters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code>.</li> <li><code>0 &lt;= k &lt;= s.length</code></li> </ul>
Medium
107.3K
207.6K
107,335
207,559
51.7%
['merge-sorted-array', 'reorder-list', 'defuse-the-bomb']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int takeCharacters(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int takeCharacters(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def takeCharacters(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def takeCharacters(self, s: str, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int takeCharacters(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int TakeCharacters(string s, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} k\n * @return {number}\n */\nvar takeCharacters = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function takeCharacters(s: string, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @return Integer\n */\n function takeCharacters($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func takeCharacters(_ s: String, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun takeCharacters(s: String, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int takeCharacters(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func takeCharacters(s string, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef take_characters(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def takeCharacters(s: String, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn take_characters(s: String, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (take-characters s k)\n (-> string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec take_characters(S :: unicode:unicode_binary(), K :: integer()) -> integer().\ntake_characters(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec take_characters(s :: String.t, k :: integer) :: integer\n def take_characters(s, k) do\n \n end\nend"}]
"aabaaaacaabc" 2
{ "name": "takeCharacters", "params": [ { "name": "s", "type": "string" }, { "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>"]}
['Hash Table', 'String', 'Sliding Window']
2,517
Maximum Tastiness of Candy Basket
maximum-tastiness-of-candy-basket
<p>You are given an array of positive integers <code>price</code> where <code>price[i]</code> denotes the price of the <code>i<sup>th</sup></code> candy and a positive integer <code>k</code>.</p> <p>The store sells baskets of <code>k</code> <strong>distinct</strong> candies. The <strong>tastiness</strong> of a candy basket is the smallest absolute difference of the <strong>prices</strong> of any two candies in the basket.</p> <p>Return <em>the <strong>maximum</strong> tastiness of a candy basket.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> price = [13,5,1,8,21,2], k = 3 <strong>Output:</strong> 8 <strong>Explanation:</strong> Choose the candies with the prices [13,5,21]. The tastiness of the candy basket is: min(|13 - 5|, |13 - 21|, |5 - 21|) = min(8, 8, 16) = 8. It can be proven that 8 is the maximum tastiness that can be achieved. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> price = [1,3,1], k = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> Choose the candies with the prices [1,3]. The tastiness of the candy basket is: min(|1 - 3|) = min(2) = 2. It can be proven that 2 is the maximum tastiness that can be achieved. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> price = [7,7,7,7], k = 2 <strong>Output:</strong> 0 <strong>Explanation:</strong> Choosing any two distinct candies from the candies we have will result in a tastiness of 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= k &lt;= price.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= price[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
28.3K
43.1K
28,342
43,135
65.7%
['container-with-most-water', 'sliding-window-maximum']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumTastiness(vector<int>& price, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumTastiness(int[] price, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumTastiness(self, price, k):\n \"\"\"\n :type price: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumTastiness(self, price: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumTastiness(int* price, int priceSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumTastiness(int[] price, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} price\n * @param {number} k\n * @return {number}\n */\nvar maximumTastiness = function(price, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumTastiness(price: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $price\n * @param Integer $k\n * @return Integer\n */\n function maximumTastiness($price, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumTastiness(_ price: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumTastiness(price: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumTastiness(List<int> price, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumTastiness(price []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} price\n# @param {Integer} k\n# @return {Integer}\ndef maximum_tastiness(price, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumTastiness(price: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_tastiness(price: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-tastiness price k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_tastiness(Price :: [integer()], K :: integer()) -> integer().\nmaximum_tastiness(Price, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_tastiness(price :: [integer], k :: integer) :: integer\n def maximum_tastiness(price, k) do\n \n end\nend"}]
[13,5,1,8,21,2] 3
{ "name": "maximumTastiness", "params": [ { "type": "integer[]", "name": "price" }, { "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', 'Greedy', 'Sorting']
2,518
Number of Great Partitions
number-of-great-partitions
<p>You are given an array <code>nums</code> consisting of <strong>positive</strong> integers and an integer <code>k</code>.</p> <p><strong>Partition</strong> the array into two ordered <strong>groups</strong> such that each element is in exactly <strong>one</strong> group. A partition is called great if the <strong>sum</strong> of elements of each group is greater than or equal to <code>k</code>.</p> <p>Return <em>the number of <strong>distinct</strong> great partitions</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>Two partitions are considered distinct if some element <code>nums[i]</code> is in different groups in the two partitions.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4], k = 4 <strong>Output:</strong> 6 <strong>Explanation:</strong> The great partitions are: ([1,2,3], [4]), ([1,3], [2,4]), ([1,4], [2,3]), ([2,3], [1,4]), ([2,4], [1,3]) and ([4], [1,2,3]). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,3,3], k = 4 <strong>Output:</strong> 0 <strong>Explanation:</strong> There are no great partitions for this array. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [6,6], k = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> We can either put nums[0] in the first partition or in the second partition. The great partitions will be ([6], [6]) and ([6], [6]). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length, k &lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Hard
11.1K
34.5K
11,136
34,533
32.2%
['palindrome-partitioning-ii', 'partition-equal-subset-sum', 'find-the-punishment-number-of-an-integer']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countPartitions(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countPartitions(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countPartitions(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 countPartitions(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countPartitions(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountPartitions(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 countPartitions = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countPartitions(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 countPartitions($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countPartitions(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countPartitions(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countPartitions(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countPartitions(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef count_partitions(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countPartitions(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_partitions(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-partitions nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_partitions(Nums :: [integer()], K :: integer()) -> integer().\ncount_partitions(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_partitions(nums :: [integer], k :: integer) :: integer\n def count_partitions(nums, k) do\n \n end\nend"}]
[1,2,3,4] 4
{ "name": "countPartitions", "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', 'Dynamic Programming']
2,519
Count the Number of K-Big Indices
count-the-number-of-k-big-indices
null
Hard
9.1K
17.1K
9,098
17,129
53.1%
['count-of-smaller-numbers-after-self', 'find-all-good-indices']
[]
Algorithms
null
[2,3,6,5,2,3] 2
{ "name": "kBigIndices", "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', 'Binary Search', 'Divide and Conquer', 'Binary Indexed Tree', 'Segment Tree', 'Merge Sort', 'Ordered Set']
2,520
Count the Digits That Divide a Number
count-the-digits-that-divide-a-number
<p>Given an integer <code>num</code>, return <em>the number of digits in <code>num</code> that divide </em><code>num</code>.</p> <p>An integer <code>val</code> divides <code>nums</code> if <code>nums % val == 0</code>.</p> <p>&nbsp;</p> <p><strong>Example 1:</strong></p> <pre> <strong>Input:</strong> num = 7 <strong>Output:</strong> 1 <strong>Explanation:</strong> 7 divides itself, hence the answer is 1. </pre> <p><strong>Example 2:</strong></p> <pre> <strong>Input:</strong> num = 121 <strong>Output:</strong> 2 <strong>Explanation:</strong> 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2. </pre> <p><strong>Example 3:</strong></p> <pre> <strong>Input:</strong> num = 1248 <strong>Output:</strong> 4 <strong>Explanation:</strong> 1248 is divisible by all of its digits, hence the answer is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li> <li><code>num</code> does not contain <code>0</code> as one of its digits.</li> </ul>
Easy
113K
132K
112,959
131,952
85.6%
['happy-number', 'self-dividing-numbers']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countDigits(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countDigits(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countDigits(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countDigits(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countDigits(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountDigits(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar countDigits = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countDigits(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function countDigits($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countDigits(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countDigits(num: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countDigits(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countDigits(num int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef count_digits(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countDigits(num: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_digits(num: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-digits num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_digits(Num :: integer()) -> integer().\ncount_digits(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_digits(num :: integer) :: integer\n def count_digits(num) do\n \n end\nend"}]
7
{ "name": "countDigits", "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']
2,521
Distinct Prime Factors of Product of Array
distinct-prime-factors-of-product-of-array
<p>Given an array of positive integers <code>nums</code>, return <em>the number of <strong>distinct prime factors</strong> in the product of the elements of</em> <code>nums</code>.</p> <p><strong>Note</strong> that:</p> <ul> <li>A number greater than <code>1</code> is called <strong>prime</strong> if it is divisible by only <code>1</code> and itself.</li> <li>An integer <code>val1</code> is a factor of another integer <code>val2</code> if <code>val2 / val1</code> is an integer.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,3,7,10,6] <strong>Output:</strong> 4 <strong>Explanation:</strong> The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 2<sup>5</sup> * 3<sup>2</sup> * 5 * 7. There are 4 distinct prime factors so we return 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,8,16] <strong>Output:</strong> 1 <strong>Explanation:</strong> The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 2<sup>10</sup>. There is 1 distinct prime factor so we return 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li> <li><code>2 &lt;= nums[i] &lt;= 1000</code></li> </ul>
Medium
37.7K
71.6K
37,669
71,607
52.6%
['2-keys-keyboard', 'largest-component-size-by-common-factor', 'closest-divisors', 'smallest-value-after-replacing-with-sum-of-prime-factors', 'count-the-number-of-square-free-subsets']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int distinctPrimeFactors(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int distinctPrimeFactors(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def distinctPrimeFactors(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def distinctPrimeFactors(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int distinctPrimeFactors(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DistinctPrimeFactors(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar distinctPrimeFactors = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function distinctPrimeFactors(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function distinctPrimeFactors($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func distinctPrimeFactors(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun distinctPrimeFactors(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int distinctPrimeFactors(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func distinctPrimeFactors(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef distinct_prime_factors(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def distinctPrimeFactors(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn distinct_prime_factors(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (distinct-prime-factors nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec distinct_prime_factors(Nums :: [integer()]) -> integer().\ndistinct_prime_factors(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec distinct_prime_factors(nums :: [integer]) :: integer\n def distinct_prime_factors(nums) do\n \n end\nend"}]
[2,4,3,7,10,6]
{ "name": "distinctPrimeFactors", "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', 'Math', 'Number Theory']
2,522
Partition String Into Substrings With Values at Most K
partition-string-into-substrings-with-values-at-most-k
<p>You are given a string <code>s</code> consisting of digits from <code>1</code> to <code>9</code> and an integer <code>k</code>.</p> <p>A partition of a string <code>s</code> is called <strong>good</strong> if:</p> <ul> <li>Each digit of <code>s</code> is part of <strong>exactly</strong> one substring.</li> <li>The value of each substring is less than or equal to <code>k</code>.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of substrings in a <strong>good</strong> partition of</em> <code>s</code>. If no <strong>good</strong> partition of <code>s</code> exists, return <code>-1</code>.</p> <p><b>Note</b> that:</p> <ul> <li>The <strong>value</strong> of a string is its result when interpreted as an integer. For example, the value of <code>&quot;123&quot;</code> is <code>123</code> and the value of <code>&quot;1&quot;</code> is <code>1</code>.</li> <li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;165462&quot;, k = 60 <strong>Output:</strong> 4 <strong>Explanation:</strong> We can partition the string into substrings &quot;16&quot;, &quot;54&quot;, &quot;6&quot;, and &quot;2&quot;. Each substring has a value less than or equal to k = 60. It can be shown that we cannot partition the string into less than 4 substrings. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;238182&quot;, k = 5 <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no good partition for this string. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is a digit from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Medium
23.9K
51.5K
23,897
51,546
46.4%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumPartition(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumPartition(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumPartition(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumPartition(self, s: str, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumPartition(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumPartition(string s, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} k\n * @return {number}\n */\nvar minimumPartition = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumPartition(s: string, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @return Integer\n */\n function minimumPartition($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumPartition(_ s: String, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumPartition(s: String, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumPartition(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumPartition(s string, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef minimum_partition(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumPartition(s: String, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_partition(s: String, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-partition s k)\n (-> string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_partition(S :: unicode:unicode_binary(), K :: integer()) -> integer().\nminimum_partition(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_partition(s :: String.t, k :: integer) :: integer\n def minimum_partition(s, k) do\n \n end\nend"}]
"165462" 60
{ "name": "minimumPartition", "params": [ { "name": "s", "type": "string" }, { "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>"]}
['String', 'Dynamic Programming', 'Greedy']
2,523
Closest Prime Numbers in Range
closest-prime-numbers-in-range
<p>Given two positive integers <code>left</code> and <code>right</code>, find the two integers <code>num1</code> and <code>num2</code> such that:</p> <ul> <li><code>left &lt;= num1 &lt; num2 &lt;= right </code>.</li> <li>Both <code>num1</code> and <code>num2</code> are <span data-keyword="prime-number">prime numbers</span>.</li> <li><code>num2 - num1</code> is the <strong>minimum</strong> amongst all other pairs satisfying the above conditions.</li> </ul> <p>Return the positive integer array <code>ans = [num1, num2]</code>. If there are multiple pairs satisfying these conditions, return the one with the <strong>smallest</strong> <code>num1</code> value. If no such numbers exist, return <code>[-1, -1]</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 10, right = 19 <strong>Output:</strong> [11,13] <strong>Explanation:</strong> The prime numbers between 10 and 19 are 11, 13, 17, and 19. The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19]. Since 11 is smaller than 17, we return the first pair. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> left = 4, right = 6 <strong>Output:</strong> [-1,-1] <strong>Explanation:</strong> There exists only one prime number in the given range, so the conditions cannot be satisfied. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= left &lt;= right &lt;= 10<sup>6</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;  } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Medium
176.3K
342K
176,294
342,033
51.5%
['count-ways-to-make-array-with-product']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> closestPrimes(int left, int right) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] closestPrimes(int left, int right) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def closestPrimes(self, left, right):\n \"\"\"\n :type left: int\n :type right: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def closestPrimes(self, left: int, right: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* closestPrimes(int left, int right, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] ClosestPrimes(int left, int right) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} left\n * @param {number} right\n * @return {number[]}\n */\nvar closestPrimes = function(left, right) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function closestPrimes(left: number, right: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $left\n * @param Integer $right\n * @return Integer[]\n */\n function closestPrimes($left, $right) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func closestPrimes(_ left: Int, _ right: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun closestPrimes(left: Int, right: Int): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> closestPrimes(int left, int right) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func closestPrimes(left int, right int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} left\n# @param {Integer} right\n# @return {Integer[]}\ndef closest_primes(left, right)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def closestPrimes(left: Int, right: Int): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn closest_primes(left: i32, right: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (closest-primes left right)\n (-> exact-integer? exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec closest_primes(Left :: integer(), Right :: integer()) -> [integer()].\nclosest_primes(Left, Right) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec closest_primes(left :: integer, right :: integer) :: [integer]\n def closest_primes(left, right) do\n \n end\nend"}]
10 19
{ "name": "closestPrimes", "params": [ { "name": "left", "type": "integer" }, { "type": "integer", "name": "right" } ], "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', 'Number Theory']
2,524
Maximum Frequency Score of a Subarray
maximum-frequency-score-of-a-subarray
null
Hard
1.3K
3.9K
1,336
3,876
34.5%
[]
[]
Algorithms
null
[1,1,1,2,1,2] 3
{ "name": "maxFrequencyScore", "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', 'Hash Table', 'Math', 'Stack', 'Sliding Window']
2,525
Categorize Box According to Criteria
categorize-box-according-to-criteria
<p>Given four integers <code>length</code>, <code>width</code>, <code>height</code>, and <code>mass</code>, representing the dimensions and mass of a box, respectively, return <em>a string representing the <strong>category</strong> of the box</em>.</p> <ul> <li>The box is <code>&quot;Bulky&quot;</code> if: <ul> <li><strong>Any</strong> of the dimensions of the box is greater or equal to <code>10<sup>4</sup></code>.</li> <li>Or, the <strong>volume</strong> of the box is greater or equal to <code>10<sup>9</sup></code>.</li> </ul> </li> <li>If the mass of the box is greater or equal to <code>100</code>, it is <code>&quot;Heavy&quot;.</code></li> <li>If the box is both <code>&quot;Bulky&quot;</code> and <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Both&quot;</code>.</li> <li>If the box is neither <code>&quot;Bulky&quot;</code> nor <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Neither&quot;</code>.</li> <li>If the box is <code>&quot;Bulky&quot;</code> but not <code>&quot;Heavy&quot;</code>, then its category is <code>&quot;Bulky&quot;</code>.</li> <li>If the box is <code>&quot;Heavy&quot;</code> but not <code>&quot;Bulky&quot;</code>, then its category is <code>&quot;Heavy&quot;</code>.</li> </ul> <p><strong>Note</strong> that the volume of the box is the product of its length, width and height.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> length = 1000, width = 35, height = 700, mass = 300 <strong>Output:</strong> &quot;Heavy&quot; <strong>Explanation:</strong> None of the dimensions of the box is greater or equal to 10<sup>4</sup>. Its volume = 24500000 &lt;= 10<sup>9</sup>. So it cannot be categorized as &quot;Bulky&quot;. However mass &gt;= 100, so the box is &quot;Heavy&quot;. Since the box is not &quot;Bulky&quot; but &quot;Heavy&quot;, we return &quot;Heavy&quot;.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> length = 200, width = 50, height = 800, mass = 50 <strong>Output:</strong> &quot;Neither&quot; <strong>Explanation:</strong> None of the dimensions of the box is greater or equal to 10<sup>4</sup>. Its volume = 8 * 10<sup>6</sup> &lt;= 10<sup>9</sup>. So it cannot be categorized as &quot;Bulky&quot;. Its mass is also less than 100, so it cannot be categorized as &quot;Heavy&quot; either. Since its neither of the two above categories, we return &quot;Neither&quot;.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= length, width, height &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= mass &lt;= 10<sup>3</sup></code></li> </ul>
Easy
40K
107.8K
40,042
107,782
37.2%
['fizz-buzz', 'find-winner-on-a-tic-tac-toe-game', 'best-poker-hand']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string categorizeBox(int length, int width, int height, int mass) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String categorizeBox(int length, int width, int height, int mass) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def categorizeBox(self, length, width, height, mass):\n \"\"\"\n :type length: int\n :type width: int\n :type height: int\n :type mass: int\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* categorizeBox(int length, int width, int height, int mass) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string CategorizeBox(int length, int width, int height, int mass) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} length\n * @param {number} width\n * @param {number} height\n * @param {number} mass\n * @return {string}\n */\nvar categorizeBox = function(length, width, height, mass) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function categorizeBox(length: number, width: number, height: number, mass: number): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $length\n * @param Integer $width\n * @param Integer $height\n * @param Integer $mass\n * @return String\n */\n function categorizeBox($length, $width, $height, $mass) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func categorizeBox(_ length: Int, _ width: Int, _ height: Int, _ mass: Int) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun categorizeBox(length: Int, width: Int, height: Int, mass: Int): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String categorizeBox(int length, int width, int height, int mass) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func categorizeBox(length int, width int, height int, mass int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} length\n# @param {Integer} width\n# @param {Integer} height\n# @param {Integer} mass\n# @return {String}\ndef categorize_box(length, width, height, mass)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def categorizeBox(length: Int, width: Int, height: Int, mass: Int): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn categorize_box(length: i32, width: i32, height: i32, mass: i32) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (categorize-box length width height mass)\n (-> exact-integer? exact-integer? exact-integer? exact-integer? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec categorize_box(Length :: integer(), Width :: integer(), Height :: integer(), Mass :: integer()) -> unicode:unicode_binary().\ncategorize_box(Length, Width, Height, Mass) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec categorize_box(length :: integer, width :: integer, height :: integer, mass :: integer) :: String.t\n def categorize_box(length, width, height, mass) do\n \n end\nend"}]
1000 35 700 300
{ "name": "categorizeBox", "params": [ { "name": "length", "type": "integer" }, { "type": "integer", "name": "width" }, { "type": "integer", "name": "height" }, { "type": "integer", "name": "mass" } ], "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,526
Find Consecutive Integers from a Data Stream
find-consecutive-integers-from-a-data-stream
<p>For a stream of integers, implement a data structure that checks if the last <code>k</code> integers parsed in the stream are <strong>equal</strong> to <code>value</code>.</p> <p>Implement the <strong>DataStream</strong> class:</p> <ul> <li><code>DataStream(int value, int k)</code> Initializes the object with an empty integer stream and the two integers <code>value</code> and <code>k</code>.</li> <li><code>boolean consec(int num)</code> Adds <code>num</code> to the stream of integers. Returns <code>true</code> if the last <code>k</code> integers are equal to <code>value</code>, and <code>false</code> otherwise. If there are less than <code>k</code> integers, the condition does not hold true, so returns <code>false</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input</strong> [&quot;DataStream&quot;, &quot;consec&quot;, &quot;consec&quot;, &quot;consec&quot;, &quot;consec&quot;] [[4, 3], [4], [4], [4], [3]] <strong>Output</strong> [null, false, false, true, false] <strong>Explanation</strong> DataStream dataStream = new DataStream(4, 3); //value = 4, k = 3 dataStream.consec(4); // Only 1 integer is parsed, so returns False. dataStream.consec(4); // Only 2 integers are parsed. // Since 2 is less than k, returns False. dataStream.consec(4); // The 3 integers parsed are all equal to value, so returns True. dataStream.consec(3); // The last k integers parsed in the stream are [4,4,3]. // Since 3 is not equal to value, it returns False. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= value, num &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> <li>At most <code>10<sup>5</sup></code> calls will be made to <code>consec</code>.</li> </ul>
Medium
30.9K
61.2K
30,915
61,191
50.5%
['number-of-zero-filled-subarrays']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class DataStream {\npublic:\n DataStream(int value, int k) {\n \n }\n \n bool consec(int num) {\n \n }\n};\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * DataStream* obj = new DataStream(value, k);\n * bool param_1 = obj->consec(num);\n */"}, {"value": "java", "text": "Java", "defaultCode": "class DataStream {\n\n public DataStream(int value, int k) {\n \n }\n \n public boolean consec(int num) {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * DataStream obj = new DataStream(value, k);\n * boolean param_1 = obj.consec(num);\n */"}, {"value": "python", "text": "Python", "defaultCode": "class DataStream(object):\n\n def __init__(self, value, k):\n \"\"\"\n :type value: int\n :type k: int\n \"\"\"\n \n\n def consec(self, num):\n \"\"\"\n :type num: int\n :rtype: bool\n \"\"\"\n \n\n\n# Your DataStream object will be instantiated and called as such:\n# obj = DataStream(value, k)\n# param_1 = obj.consec(num)"}, {"value": "python3", "text": "Python3", "defaultCode": "class DataStream:\n\n def __init__(self, value: int, k: int):\n \n\n def consec(self, num: int) -> bool:\n \n\n\n# Your DataStream object will be instantiated and called as such:\n# obj = DataStream(value, k)\n# param_1 = obj.consec(num)"}, {"value": "c", "text": "C", "defaultCode": "\n\n\ntypedef struct {\n \n} DataStream;\n\n\nDataStream* dataStreamCreate(int value, int k) {\n \n}\n\nbool dataStreamConsec(DataStream* obj, int num) {\n \n}\n\nvoid dataStreamFree(DataStream* obj) {\n \n}\n\n/**\n * Your DataStream struct will be instantiated and called as such:\n * DataStream* obj = dataStreamCreate(value, k);\n * bool param_1 = dataStreamConsec(obj, num);\n \n * dataStreamFree(obj);\n*/"}, {"value": "csharp", "text": "C#", "defaultCode": "public class DataStream {\n\n public DataStream(int value, int k) {\n \n }\n \n public bool Consec(int num) {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * DataStream obj = new DataStream(value, k);\n * bool param_1 = obj.Consec(num);\n */"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} value\n * @param {number} k\n */\nvar DataStream = function(value, k) {\n \n};\n\n/** \n * @param {number} num\n * @return {boolean}\n */\nDataStream.prototype.consec = function(num) {\n \n};\n\n/** \n * Your DataStream object will be instantiated and called as such:\n * var obj = new DataStream(value, k)\n * var param_1 = obj.consec(num)\n */"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "class DataStream {\n constructor(value: number, k: number) {\n \n }\n\n consec(num: number): boolean {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * var obj = new DataStream(value, k)\n * var param_1 = obj.consec(num)\n */"}, {"value": "php", "text": "PHP", "defaultCode": "class DataStream {\n /**\n * @param Integer $value\n * @param Integer $k\n */\n function __construct($value, $k) {\n \n }\n \n /**\n * @param Integer $num\n * @return Boolean\n */\n function consec($num) {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * $obj = DataStream($value, $k);\n * $ret_1 = $obj->consec($num);\n */"}, {"value": "swift", "text": "Swift", "defaultCode": "\nclass DataStream {\n\n init(_ value: Int, _ k: Int) {\n \n }\n \n func consec(_ num: Int) -> Bool {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * let obj = DataStream(value, k)\n * let ret_1: Bool = obj.consec(num)\n */"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class DataStream(value: Int, k: Int) {\n\n fun consec(num: Int): Boolean {\n \n }\n\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * var obj = DataStream(value, k)\n * var param_1 = obj.consec(num)\n */"}, {"value": "dart", "text": "Dart", "defaultCode": "class DataStream {\n\n DataStream(int value, int k) {\n \n }\n \n bool consec(int num) {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * DataStream obj = DataStream(value, k);\n * bool param1 = obj.consec(num);\n */"}, {"value": "golang", "text": "Go", "defaultCode": "type DataStream struct {\n \n}\n\n\nfunc Constructor(value int, k int) DataStream {\n \n}\n\n\nfunc (this *DataStream) Consec(num int) bool {\n \n}\n\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * obj := Constructor(value, k);\n * param_1 := obj.Consec(num);\n */"}, {"value": "ruby", "text": "Ruby", "defaultCode": "class DataStream\n\n=begin\n :type value: Integer\n :type k: Integer\n=end\n def initialize(value, k)\n \n end\n\n\n=begin\n :type num: Integer\n :rtype: Boolean\n=end\n def consec(num)\n \n end\n\n\nend\n\n# Your DataStream object will be instantiated and called as such:\n# obj = DataStream.new(value, k)\n# param_1 = obj.consec(num)"}, {"value": "scala", "text": "Scala", "defaultCode": "class DataStream(_value: Int, _k: Int) {\n\n def consec(num: Int): Boolean = {\n \n }\n\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * val obj = new DataStream(value, k)\n * val param_1 = obj.consec(num)\n */"}, {"value": "rust", "text": "Rust", "defaultCode": "struct DataStream {\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 DataStream {\n\n fn new(value: i32, k: i32) -> Self {\n \n }\n \n fn consec(&self, num: i32) -> bool {\n \n }\n}\n\n/**\n * Your DataStream object will be instantiated and called as such:\n * let obj = DataStream::new(value, k);\n * let ret_1: bool = obj.consec(num);\n */"}, {"value": "racket", "text": "Racket", "defaultCode": "(define data-stream%\n (class object%\n (super-new)\n \n ; value : exact-integer?\n ; k : exact-integer?\n (init-field\n value\n k)\n \n ; consec : exact-integer? -> boolean?\n (define/public (consec num)\n )))\n\n;; Your data-stream% object will be instantiated and called as such:\n;; (define obj (new data-stream% [value value] [k k]))\n;; (define param_1 (send obj consec num))"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec data_stream_init_(Value :: integer(), K :: integer()) -> any().\ndata_stream_init_(Value, K) ->\n .\n\n-spec data_stream_consec(Num :: integer()) -> boolean().\ndata_stream_consec(Num) ->\n .\n\n\n%% Your functions will be called as such:\n%% data_stream_init_(Value, K),\n%% Param_1 = data_stream_consec(Num),\n\n%% data_stream_init_ will be called before every test case, in which you can do some necessary initializations."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule DataStream do\n @spec init_(value :: integer, k :: integer) :: any\n def init_(value, k) do\n \n end\n\n @spec consec(num :: integer) :: boolean\n def consec(num) do\n \n end\nend\n\n# Your functions will be called as such:\n# DataStream.init_(value, k)\n# param_1 = DataStream.consec(num)\n\n# DataStream.init_ will be called before every test case, in which you can do some necessary initializations."}]
["DataStream","consec","consec","consec","consec"] [[4,3],[4],[4],[4],[3]]
{ "classname": "DataStream", "constructor": { "params": [ { "type": "integer", "name": "value" }, { "name": "k", "type": "integer" } ] }, "methods": [ { "params": [ { "type": "integer", "name": "num" } ], "name": "consec", "return": { "type": "boolean" } } ], "return": { "type": "boolean" }, "systemdesign": true }
{"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', 'Design', 'Queue', 'Counting', 'Data Stream']
2,527
Find Xor-Beauty of Array
find-xor-beauty-of-array
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>The <strong>effective value</strong> of three indices <code>i</code>, <code>j</code>, and <code>k</code> is defined as <code>((nums[i] | nums[j]) &amp; nums[k])</code>.</p> <p>The <strong>xor-beauty</strong> of the array is the XORing of <strong>the effective values of all the possible triplets</strong> of indices <code>(i, j, k)</code> where <code>0 &lt;= i, j, k &lt; n</code>.</p> <p>Return <em>the xor-beauty of</em> <code>nums</code>.</p> <p><strong>Note</strong> that:</p> <ul> <li><code>val1 | val2</code> is bitwise OR of <code>val1</code> and <code>val2</code>.</li> <li><code>val1 &amp; val2</code> is bitwise AND of <code>val1</code> and <code>val2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,4] <strong>Output:</strong> 5 <strong>Explanation:</strong> The triplets and their corresponding effective values are listed below: - (0,0,0) with effective value ((1 | 1) &amp; 1) = 1 - (0,0,1) with effective value ((1 | 1) &amp; 4) = 0 - (0,1,0) with effective value ((1 | 4) &amp; 1) = 1 - (0,1,1) with effective value ((1 | 4) &amp; 4) = 4 - (1,0,0) with effective value ((4 | 1) &amp; 1) = 1 - (1,0,1) with effective value ((4 | 1) &amp; 4) = 4 - (1,1,0) with effective value ((4 | 4) &amp; 1) = 0 - (1,1,1) with effective value ((4 | 4) &amp; 4) = 4 Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [15,45,20,2,34,35,5,44,32,30] <strong>Output:</strong> 34 <strong>Explanation:</strong> <code>The xor-beauty of the given array is 34.</code> </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length&nbsp;&lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
22.3K
32.4K
22,322
32,374
69.0%
['decode-xored-permutation']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int xorBeauty(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int xorBeauty(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def xorBeauty(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def xorBeauty(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int xorBeauty(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int XorBeauty(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar xorBeauty = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function xorBeauty(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function xorBeauty($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func xorBeauty(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun xorBeauty(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int xorBeauty(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func xorBeauty(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef xor_beauty(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def xorBeauty(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn xor_beauty(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (xor-beauty nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec xor_beauty(Nums :: [integer()]) -> integer().\nxor_beauty(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec xor_beauty(nums :: [integer]) :: integer\n def xor_beauty(nums) do\n \n end\nend"}]
[1,4]
{ "name": "xorBeauty", "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', 'Math', 'Bit Manipulation']
2,528
Maximize the Minimum Powered City
maximize-the-minimum-powered-city
<p>You are given a <strong>0-indexed</strong> integer array <code>stations</code> of length <code>n</code>, where <code>stations[i]</code> represents the number of power stations in the <code>i<sup>th</sup></code> city.</p> <p>Each power station can provide power to every city in a fixed <strong>range</strong>. In other words, if the range is denoted by <code>r</code>, then a power station at city <code>i</code> can provide power to all cities <code>j</code> such that <code>|i - j| &lt;= r</code> and <code>0 &lt;= i, j &lt;= n - 1</code>.</p> <ul> <li>Note that <code>|x|</code> denotes <strong>absolute</strong> value. For example, <code>|7 - 5| = 2</code> and <code>|3 - 10| = 7</code>.</li> </ul> <p>The <strong>power</strong> of a city is the total number of power stations it is being provided power from.</p> <p>The government has sanctioned building <code>k</code> more power stations, each of which can be built in any city, and have the same range as the pre-existing ones.</p> <p>Given the two integers <code>r</code> and <code>k</code>, return <em>the <strong>maximum possible minimum power</strong> of a city, if the additional power stations are built optimally.</em></p> <p><strong>Note</strong> that you can build the <code>k</code> power stations in multiple cities.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> stations = [1,2,4,5,0], r = 1, k = 2 <strong>Output:</strong> 5 <strong>Explanation:</strong> One of the optimal ways is to install both the power stations at city 1. So stations will become [1,4,4,5,0]. - City 0 is provided by 1 + 4 = 5 power stations. - City 1 is provided by 1 + 4 + 4 = 9 power stations. - City 2 is provided by 4 + 4 + 5 = 13 power stations. - City 3 is provided by 5 + 4 = 9 power stations. - City 4 is provided by 5 + 0 = 5 power stations. So the minimum power of a city is 5. Since it is not possible to obtain a larger power, we return 5. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> stations = [4,4,4,4], r = 0, k = 3 <strong>Output:</strong> 4 <strong>Explanation:</strong> It can be proved that we cannot make the minimum power of a city greater than 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == stations.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= stations[i] &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= r&nbsp;&lt;= n - 1</code></li> <li><code>0 &lt;= k&nbsp;&lt;= 10<sup>9</sup></code></li> </ul>
Hard
8.3K
25.3K
8,316
25,284
32.9%
['maximum-number-of-tasks-you-can-assign']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxPower(vector<int>& stations, int r, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxPower(int[] stations, int r, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxPower(self, stations, r, k):\n \"\"\"\n :type stations: List[int]\n :type r: int\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxPower(self, stations: List[int], r: int, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxPower(int* stations, int stationsSize, int r, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxPower(int[] stations, int r, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} stations\n * @param {number} r\n * @param {number} k\n * @return {number}\n */\nvar maxPower = function(stations, r, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxPower(stations: number[], r: number, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $stations\n * @param Integer $r\n * @param Integer $k\n * @return Integer\n */\n function maxPower($stations, $r, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxPower(_ stations: [Int], _ r: Int, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxPower(stations: IntArray, r: Int, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxPower(List<int> stations, int r, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxPower(stations []int, r int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} stations\n# @param {Integer} r\n# @param {Integer} k\n# @return {Integer}\ndef max_power(stations, r, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxPower(stations: Array[Int], r: Int, k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_power(stations: Vec<i32>, r: i32, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-power stations r k)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_power(Stations :: [integer()], R :: integer(), K :: integer()) -> integer().\nmax_power(Stations, R, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_power(stations :: [integer], r :: integer, k :: integer) :: integer\n def max_power(stations, r, k) do\n \n end\nend"}]
[1,2,4,5,0] 1 2
{ "name": "maxPower", "params": [ { "name": "stations", "type": "integer[]" }, { "type": "integer", "name": "r" }, { "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', 'Binary Search', 'Greedy', 'Queue', 'Sliding Window', 'Prefix Sum']
2,529
Maximum Count of Positive Integer and Negative Integer
maximum-count-of-positive-integer-and-negative-integer
<p>Given an array <code>nums</code> sorted in <strong>non-decreasing</strong> order, return <em>the maximum between the number of positive integers and the number of negative integers.</em></p> <ul> <li>In other words, if the number of positive integers in <code>nums</code> is <code>pos</code> and the number of negative integers is <code>neg</code>, then return the maximum of <code>pos</code> and <code>neg</code>.</li> </ul> <p><strong>Note</strong> that <code>0</code> is neither positive nor negative.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [-2,-1,-1,1,2,3] <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 positive integers and 3 negative integers. The maximum count among them is 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-3,-2,-1,0,0,1,2] <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 2 positive integers and 3 negative integers. The maximum count among them is 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,20,66,1314] <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 positive integers and 0 negative integers. The maximum count among them is 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2000</code></li> <li><code>-2000 &lt;= nums[i] &lt;= 2000</code></li> <li><code>nums</code> is sorted in a <strong>non-decreasing order</strong>.</li> </ul> <p>&nbsp;</p> <p><strong>Follow up:</strong> Can you solve the problem in <code>O(log(n))</code> time complexity?</p>
Easy
306.4K
411K
306,413
411,018
74.5%
['binary-search', 'count-negative-numbers-in-a-sorted-matrix']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumCount(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumCount(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumCount(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumCount(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumCount(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumCount(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumCount = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumCount(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maximumCount($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumCount(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumCount(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumCount(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumCount(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef maximum_count(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumCount(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_count(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-count nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_count(Nums :: [integer()]) -> integer().\nmaximum_count(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_count(nums :: [integer]) :: integer\n def maximum_count(nums) do\n \n end\nend"}]
[-2,-1,-1,1,2,3]
{ "name": "maximumCount", "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', 'Binary Search', 'Counting']
2,530
Maximal Score After Applying K Operations
maximal-score-after-applying-k-operations
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>. You have a <strong>starting score</strong> of <code>0</code>.</p> <p>In one <strong>operation</strong>:</p> <ol> <li>choose an index <code>i</code> such that <code>0 &lt;= i &lt; nums.length</code>,</li> <li>increase your <strong>score</strong> by <code>nums[i]</code>, and</li> <li>replace <code>nums[i]</code> with <code>ceil(nums[i] / 3)</code>.</li> </ol> <p>Return <em>the maximum possible <strong>score</strong> you can attain after applying <strong>exactly</strong></em> <code>k</code> <em>operations</em>.</p> <p>The ceiling function <code>ceil(val)</code> is the least integer greater than or equal to <code>val</code>.</p> <p>&nbsp;</p> <p><strong>Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [10,10,10,10,10], k = 5 <strong>Output:</strong> 50 <strong>Explanation:</strong> Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50. </pre> <p><strong>Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,10,3,3,3], k = 3 <strong>Output:</strong> 17 <strong>Explanation: </strong>You can do the following operations: Operation 1: Select i = 1, so nums becomes [1,<strong><u>4</u></strong>,3,3,3]. Your score increases by 10. Operation 2: Select i = 1, so nums becomes [1,<strong><u>2</u></strong>,3,3,3]. Your score increases by 4. Operation 3: Select i = 2, so nums becomes [1,2,<u><strong>1</strong></u>,3,3]. Your score increases by 3. The final score is 10 + 4 + 3 = 17. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length, k &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
155.6K
244.1K
155,648
244,101
63.8%
['sliding-window-maximum', 'remove-stones-to-minimize-the-total']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxKelements(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxKelements(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxKelements(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 maxKelements(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxKelements(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxKelements(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 maxKelements = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxKelements(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 maxKelements($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxKelements(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxKelements(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxKelements(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxKelements(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef max_kelements(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxKelements(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-kelements nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_kelements(Nums :: [integer()], K :: integer()) -> integer().\nmax_kelements(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_kelements(nums :: [integer], k :: integer) :: integer\n def max_kelements(nums, k) do\n \n end\nend"}]
[10,10,10,10,10] 5
{ "name": "maxKelements", "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', 'Greedy', 'Heap (Priority Queue)']
2,531
Make Number of Distinct Characters Equal
make-number-of-distinct-characters-equal
<p>You are given two <strong>0-indexed</strong> strings <code>word1</code> and <code>word2</code>.</p> <p>A <strong>move</strong> consists of choosing two indices <code>i</code> and <code>j</code> such that <code>0 &lt;= i &lt; word1.length</code> and <code>0 &lt;= j &lt; word2.length</code> and swapping <code>word1[i]</code> with <code>word2[j]</code>.</p> <p>Return <code>true</code> <em>if it is possible to get the number of distinct characters in</em> <code>word1</code> <em>and</em> <code>word2</code> <em>to be equal with <strong>exactly one</strong> move. </em>Return <code>false</code> <em>otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;ac&quot;, word2 = &quot;b&quot; <strong>Output:</strong> false <strong>Explanation:</strong> Any pair of swaps would yield two distinct characters in the first string, and one in the second string. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcc&quot;, word2 = &quot;aab&quot; <strong>Output:</strong> true <strong>Explanation:</strong> We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = &quot;abac&quot; and word2 = &quot;cab&quot;, which both have 3 distinct characters. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abcde&quot;, word2 = &quot;fghij&quot; <strong>Output:</strong> true <strong>Explanation:</strong> Both resulting strings will have 5 distinct characters, regardless of which indices we swap. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> consist of only lowercase English letters.</li> </ul>
Medium
21.7K
80.8K
21,662
80,752
26.8%
['bulls-and-cows', 'buddy-strings', 'minimum-swaps-to-make-strings-equal', 'check-if-one-string-swap-can-make-strings-equal', 'check-if-all-characters-have-equal-number-of-occurrences']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool isItPossible(string word1, string word2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean isItPossible(String word1, String word2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def isItPossible(self, word1, word2):\n \"\"\"\n :type word1: str\n :type word2: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def isItPossible(self, word1: str, word2: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool isItPossible(char* word1, char* word2) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool IsItPossible(string word1, string word2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} word1\n * @param {string} word2\n * @return {boolean}\n */\nvar isItPossible = function(word1, word2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function isItPossible(word1: string, word2: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $word1\n * @param String $word2\n * @return Boolean\n */\n function isItPossible($word1, $word2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func isItPossible(_ word1: String, _ word2: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun isItPossible(word1: String, word2: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool isItPossible(String word1, String word2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func isItPossible(word1 string, word2 string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} word1\n# @param {String} word2\n# @return {Boolean}\ndef is_it_possible(word1, word2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def isItPossible(word1: String, word2: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn is_it_possible(word1: String, word2: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (is-it-possible word1 word2)\n (-> string? string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec is_it_possible(Word1 :: unicode:unicode_binary(), Word2 :: unicode:unicode_binary()) -> boolean().\nis_it_possible(Word1, Word2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec is_it_possible(word1 :: String.t, word2 :: String.t) :: boolean\n def is_it_possible(word1, word2) do\n \n end\nend"}]
"ac" "b"
{ "name": "isItPossible", "params": [ { "name": "word1", "type": "string" }, { "type": "string", "name": "word2" } ], "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>"]}
['Hash Table', 'String', 'Counting']
2,532
Time to Cross a Bridge
time-to-cross-a-bridge
<p>There are <code>k</code> workers who want to move <code>n</code> boxes from the right (old) warehouse to the left (new) warehouse. You are given the two integers <code>n</code> and <code>k</code>, and a 2D integer array <code>time</code> of size <code>k x 4</code> where <code>time[i] = [right<sub>i</sub>, pick<sub>i</sub>, left<sub>i</sub>, put<sub>i</sub>]</code>.</p> <p>The warehouses are separated by a river and connected by a bridge. Initially, all <code>k</code> workers are waiting on the left side of the bridge. To move the boxes, the <code>i<sup>th</sup></code> worker can do the following:</p> <ul> <li>Cross the bridge to the right side in <code>right<sub>i</sub></code> minutes.</li> <li>Pick a box from the right warehouse in <code>pick<sub>i</sub></code> minutes.</li> <li>Cross the bridge to the left side in <code>left<sub>i</sub></code> minutes.</li> <li>Put the box into the left warehouse in <code>put<sub>i</sub></code> minutes.</li> </ul> <p>The <code>i<sup>th</sup></code> worker is <strong>less efficient</strong> than the j<code><sup>th</sup></code> worker if either condition is met:</p> <ul> <li><code>left<sub>i</sub> + right<sub>i</sub> &gt; left<sub>j</sub> + right<sub>j</sub></code></li> <li><code>left<sub>i</sub> + right<sub>i</sub> == left<sub>j</sub> + right<sub>j</sub></code> and <code>i &gt; j</code></li> </ul> <p>The following rules regulate the movement of the workers through the bridge:</p> <ul> <li>Only one worker can use the bridge at a time.</li> <li>When the bridge is unused prioritize the <strong>least efficient</strong> worker (who have picked up the box) on the right side to cross. If not,&nbsp;prioritize the <strong>least efficient</strong> worker on the left side to cross.</li> <li>If enough workers have already been dispatched from the left side to pick up all the remaining boxes, <strong>no more</strong> workers will be sent from the left side.</li> </ul> <p>Return the <strong>elapsed minutes</strong> at which the last box reaches the <strong>left side of the bridge</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <pre> From 0 to 1 minutes: worker 2 crosses the bridge to the right. From 1 to 2 minutes: worker 2 picks up a box from the right warehouse. From 2 to 6 minutes: worker 2 crosses the bridge to the left. From 6 to 7 minutes: worker 2 puts a box at the left warehouse. The whole process ends after 7 minutes. We return 6 because the problem asks for the instance of time at which the last worker reaches the left side of the bridge. </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 2, time =</span> [[1,5,1,8],[10,10,10,10]]</p> <p><strong>Output:</strong> 37</p> <p><strong>Explanation:</strong></p> <pre> <img src="https://assets.leetcode.com/uploads/2024/11/21/378539249-c6ce3c73-40e7-4670-a8b5-7ddb9abede11.png" style="width: 450px; height: 176px;" /> </pre> <p>The last box reaches the left side at 37 seconds. Notice, how we <strong>do not</strong> put the last boxes down, as that would take more time, and they are already on the left with the workers.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 10<sup>4</sup></code></li> <li><code>time.length == k</code></li> <li><code>time[i].length == 4</code></li> <li><code>1 &lt;= left<sub>i</sub>, pick<sub>i</sub>, right<sub>i</sub>, put<sub>i</sub> &lt;= 1000</code></li> </ul>
Hard
5K
11.5K
4,991
11,497
43.4%
['the-latest-time-to-catch-a-bus', 'total-cost-to-hire-k-workers']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findCrossingTime(int n, int k, vector<vector<int>>& time) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findCrossingTime(int n, int k, int[][] time) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findCrossingTime(self, n, k, time):\n \"\"\"\n :type n: int\n :type k: int\n :type time: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findCrossingTime(self, n: int, k: int, time: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findCrossingTime(int n, int k, int** time, int timeSize, int* timeColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindCrossingTime(int n, int k, int[][] time) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number} k\n * @param {number[][]} time\n * @return {number}\n */\nvar findCrossingTime = function(n, k, time) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findCrossingTime(n: number, k: number, time: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $k\n * @param Integer[][] $time\n * @return Integer\n */\n function findCrossingTime($n, $k, $time) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findCrossingTime(_ n: Int, _ k: Int, _ time: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findCrossingTime(n: Int, k: Int, time: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findCrossingTime(int n, int k, List<List<int>> time) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findCrossingTime(n int, k int, time [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer} k\n# @param {Integer[][]} time\n# @return {Integer}\ndef find_crossing_time(n, k, time)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findCrossingTime(n: Int, k: Int, time: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_crossing_time(n: i32, k: i32, time: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-crossing-time n k time)\n (-> exact-integer? exact-integer? (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_crossing_time(N :: integer(), K :: integer(), Time :: [[integer()]]) -> integer().\nfind_crossing_time(N, K, Time) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_crossing_time(n :: integer, k :: integer, time :: [[integer]]) :: integer\n def find_crossing_time(n, k, time) do\n \n end\nend"}]
1 3 [[1,1,2,1],[1,1,3,1],[1,1,4,1]]
{ "name": "findCrossingTime", "params": [ { "name": "n", "type": "integer" }, { "type": "integer", "name": "k" }, { "type": "integer[][]", "name": "time" } ], "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', 'Heap (Priority Queue)', 'Simulation']
2,533
Number of Good Binary Strings
number-of-good-binary-strings
null
Medium
6.9K
13.2K
6,901
13,176
52.4%
['special-binary-string']
[]
Algorithms
null
2 3 1 2
{ "name": "goodBinaryStrings", "params": [ { "name": "minLength", "type": "integer" }, { "type": "integer", "name": "maxLength" }, { "type": "integer", "name": "oneGroup" }, { "type": "integer", "name": "zeroGroup" } ], "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>"]}
['Dynamic Programming']
2,534
Time Taken to Cross the Door
time-taken-to-cross-the-door
null
Hard
11.8K
24.1K
11,827
24,145
49.0%
['the-latest-time-to-catch-a-bus']
[]
Algorithms
null
[0,1,1,2,4] [0,1,0,0,1]
{ "name": "timeTaken", "params": [ { "name": "arrival", "type": "integer[]" }, { "type": "integer[]", "name": "state" } ], "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', 'Queue', 'Simulation']
2,535
Difference Between Element Sum and Digit Sum of an Array
difference-between-element-sum-and-digit-sum-of-an-array
<p>You are given a positive integer array <code>nums</code>.</p> <ul> <li>The <strong>element sum</strong> is the sum of all the elements in <code>nums</code>.</li> <li>The <strong>digit sum</strong> is the sum of all the digits (not necessarily distinct) that appear in <code>nums</code>.</li> </ul> <p>Return <em>the <strong>absolute</strong> difference between the <strong>element sum</strong> and <strong>digit sum</strong> of </em><code>nums</code>.</p> <p><strong>Note</strong> that the absolute difference between two integers <code>x</code> and <code>y</code> is defined as <code>|x - y|</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,15,6,3] <strong>Output:</strong> 9 <strong>Explanation:</strong> The element sum of nums is 1 + 15 + 6 + 3 = 25. The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16. The absolute difference between the element sum and digit sum is |25 - 16| = 9. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> 0 <strong>Explanation:</strong> The element sum of nums is 1 + 2 + 3 + 4 = 10. The digit sum of nums is 1 + 2 + 3 + 4 = 10. The absolute difference between the element sum and digit sum is |10 - 10| = 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2000</code></li> <li><code>1 &lt;= nums[i] &lt;= 2000</code></li> </ul>
Easy
157K
185.3K
157,019
185,327
84.7%
['add-digits', 'minimum-sum-of-four-digit-number-after-splitting-digits']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int differenceOfSum(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int differenceOfSum(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def differenceOfSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def differenceOfSum(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int differenceOfSum(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DifferenceOfSum(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar differenceOfSum = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function differenceOfSum(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function differenceOfSum($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func differenceOfSum(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun differenceOfSum(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int differenceOfSum(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func differenceOfSum(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef difference_of_sum(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def differenceOfSum(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn difference_of_sum(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (difference-of-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec difference_of_sum(Nums :: [integer()]) -> integer().\ndifference_of_sum(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec difference_of_sum(nums :: [integer]) :: integer\n def difference_of_sum(nums) do\n \n end\nend"}]
[1,15,6,3]
{ "name": "differenceOfSum", "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', 'Math']
2,536
Increment Submatrices by One
increment-submatrices-by-one
<p>You are given a positive integer <code>n</code>, indicating that we initially have an <code>n x n</code>&nbsp;<strong>0-indexed</strong> integer matrix <code>mat</code> filled with zeroes.</p> <p>You are also given a 2D integer array <code>query</code>. For each <code>query[i] = [row1<sub>i</sub>, col1<sub>i</sub>, row2<sub>i</sub>, col2<sub>i</sub>]</code>, you should do the following operation:</p> <ul> <li>Add <code>1</code> to <strong>every element</strong> in the submatrix with the <strong>top left</strong> corner <code>(row1<sub>i</sub>, col1<sub>i</sub>)</code> and the <strong>bottom right</strong> corner <code>(row2<sub>i</sub>, col2<sub>i</sub>)</code>. That is, add <code>1</code> to <code>mat[x][y]</code> for all <code>row1<sub>i</sub> &lt;= x &lt;= row2<sub>i</sub></code> and <code>col1<sub>i</sub> &lt;= y &lt;= col2<sub>i</sub></code>.</li> </ul> <p>Return<em> the matrix</em> <code>mat</code><em> after performing every query.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example11.png" style="width: 531px; height: 121px;" /> <pre> <strong>Input:</strong> n = 3, queries = [[1,1,2,2],[0,0,1,1]] <strong>Output:</strong> [[1,1,0],[1,2,1],[0,1,1]] <strong>Explanation:</strong> The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query. - In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2). - In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1). </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p2example22.png" style="width: 261px; height: 82px;" /> <pre> <strong>Input:</strong> n = 2, queries = [[0,0,1,1]] <strong>Output:</strong> [[1,1],[1,1]] <strong>Explanation:</strong> The diagram above shows the initial matrix and the matrix after the first query. - In the first query we add 1 to every element in the matrix. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 500</code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>4</sup></code></li> <li><code>0 &lt;= row1<sub>i</sub> &lt;= row2<sub>i</sub> &lt; n</code></li> <li><code>0 &lt;= col1<sub>i</sub> &lt;= col2<sub>i</sub> &lt; n</code></li> </ul>
Medium
23.5K
45.8K
23,484
45,806
51.3%
['range-sum-query-2d-mutable', 'count-positions-on-street-with-required-brightness']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> rangeAddQueries(int n, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[][] rangeAddQueries(int n, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def rangeAddQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def rangeAddQueries(self, n: int, queries: 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** rangeAddQueries(int n, int** queries, int queriesSize, int* queriesColSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[][] RangeAddQueries(int n, int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} queries\n * @return {number[][]}\n */\nvar rangeAddQueries = function(n, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function rangeAddQueries(n: number, queries: number[][]): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $queries\n * @return Integer[][]\n */\n function rangeAddQueries($n, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func rangeAddQueries(_ n: Int, _ queries: [[Int]]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun rangeAddQueries(n: Int, queries: Array<IntArray>): Array<IntArray> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> rangeAddQueries(int n, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func rangeAddQueries(n int, queries [][]int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} queries\n# @return {Integer[][]}\ndef range_add_queries(n, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def rangeAddQueries(n: Int, queries: Array[Array[Int]]): Array[Array[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn range_add_queries(n: i32, queries: Vec<Vec<i32>>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (range-add-queries n queries)\n (-> exact-integer? (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec range_add_queries(N :: integer(), Queries :: [[integer()]]) -> [[integer()]].\nrange_add_queries(N, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec range_add_queries(n :: integer, queries :: [[integer]]) :: [[integer]]\n def range_add_queries(n, queries) do\n \n end\nend"}]
3 [[1,1,2,2],[0,0,1,1]]
{ "name": "rangeAddQueries", "params": [ { "name": "n", "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', 'Matrix', 'Prefix Sum']
2,537
Count the Number of Good Subarrays
count-the-number-of-good-subarrays
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good</strong> subarrays of</em> <code>nums</code>.</p> <p>A subarray <code>arr</code> is <strong>good</strong> if there are <strong>at least </strong><code>k</code> pairs of indices <code>(i, j)</code> such that <code>i &lt; j</code> and <code>arr[i] == arr[j]</code>.</p> <p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,1,1,1], k = 10 <strong>Output:</strong> 1 <strong>Explanation:</strong> The only good subarray is the array nums itself. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,4,3,2,2,4], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 different good subarrays: - [3,1,4,3,2,2] that has 2 pairs. - [3,1,4,3,2,2,4] that has 3 pairs. - [1,4,3,2,2,4] that has 2 pairs. - [4,3,2,2,4] that has 2 pairs. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li> </ul>
Medium
26.4K
49.9K
26,435
49,926
52.9%
['count-number-of-homogenous-substrings', 'maximum-sum-of-distinct-subarrays-with-length-k']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countGood(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countGood(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countGood(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 countGood(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countGood(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountGood(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 countGood = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countGood(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 countGood($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countGood(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countGood(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countGood(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countGood(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef count_good(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countGood(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_good(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-good nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_good(Nums :: [integer()], K :: integer()) -> integer().\ncount_good(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_good(nums :: [integer], k :: integer) :: integer\n def count_good(nums, k) do\n \n end\nend"}]
[1,1,1,1,1] 10
{ "name": "countGood", "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', 'Hash Table', 'Sliding Window']
2,538
Difference Between Maximum and Minimum Price Sum
difference-between-maximum-and-minimum-price-sum
<p>There exists an undirected and initially unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p> <p>Each node has an associated price. You are given an integer array <code>price</code>, where <code>price[i]</code> is the price of the <code>i<sup>th</sup></code> node.</p> <p>The <strong>price sum</strong> of a given path is the sum of the prices of all nodes lying on that path.</p> <p>The tree can be rooted at any node <code>root</code> of your choice. The incurred <strong>cost</strong> after choosing <code>root</code> is the difference between the maximum and minimum <strong>price sum</strong> amongst all paths starting at <code>root</code>.</p> <p>Return <em>the <strong>maximum</strong> possible <strong>cost</strong></em> <em>amongst all possible root choices</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/01/example14.png" style="width: 556px; height: 231px;" /> <pre> <strong>Input:</strong> n = 6, edges = [[0,1],[1,2],[1,3],[3,4],[3,5]], price = [9,8,7,6,10,5] <strong>Output:</strong> 24 <strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 2. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum. - The first path contains nodes [2,1,3,4]: the prices are [7,8,6,10], and the sum of the prices is 31. - The second path contains the node [2] with the price [7]. The difference between the maximum and minimum price sum is 24. It can be proved that 24 is the maximum cost. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/24/p1_example2.png" style="width: 352px; height: 184px;" /> <pre> <strong>Input:</strong> n = 3, edges = [[0,1],[1,2]], price = [1,1,1] <strong>Output:</strong> 2 <strong>Explanation:</strong> The diagram above denotes the tree after rooting it at node 0. The first part (colored in red) shows the path with the maximum price sum. The second part (colored in blue) shows the path with the minimum price sum. - The first path contains nodes [0,1,2]: the prices are [1,1,1], and the sum of the prices is 3. - The second path contains node [0] with a price [1]. The difference between the maximum and minimum price sum is 2. It can be proved that 2 is the maximum cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li> <li><code>edges</code> represents a valid tree.</li> <li><code>price.length == n</code></li> <li><code>1 &lt;= price[i] &lt;= 10<sup>5</sup></code></li> </ul>
Hard
8.9K
28.3K
8,938
28,285
31.6%
['binary-tree-maximum-path-sum']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxOutput(int n, vector<vector<int>>& edges, vector<int>& price) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxOutput(int n, int[][] edges, int[] price) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxOutput(self, n, edges, price):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type price: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxOutput(self, n: int, edges: List[List[int]], price: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxOutput(int n, int** edges, int edgesSize, int* edgesColSize, int* price, int priceSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxOutput(int n, int[][] edges, int[] price) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} edges\n * @param {number[]} price\n * @return {number}\n */\nvar maxOutput = function(n, edges, price) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxOutput(n: number, edges: number[][], price: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $edges\n * @param Integer[] $price\n * @return Integer\n */\n function maxOutput($n, $edges, $price) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxOutput(_ n: Int, _ edges: [[Int]], _ price: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxOutput(n: Int, edges: Array<IntArray>, price: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxOutput(int n, List<List<int>> edges, List<int> price) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxOutput(n int, edges [][]int, price []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} edges\n# @param {Integer[]} price\n# @return {Integer}\ndef max_output(n, edges, price)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxOutput(n: Int, edges: Array[Array[Int]], price: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_output(n: i32, edges: Vec<Vec<i32>>, price: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-output n edges price)\n (-> exact-integer? (listof (listof exact-integer?)) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_output(N :: integer(), Edges :: [[integer()]], Price :: [integer()]) -> integer().\nmax_output(N, Edges, Price) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_output(n :: integer, edges :: [[integer]], price :: [integer]) :: integer\n def max_output(n, edges, price) do\n \n end\nend"}]
6 [[0,1],[1,2],[1,3],[3,4],[3,5]] [9,8,7,6,10,5]
{ "name": "maxOutput", "params": [ { "name": "n", "type": "integer" }, { "type": "integer[][]", "name": "edges" }, { "type": "integer[]", "name": "price" } ], "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', 'Tree', 'Depth-First Search']
2,539
Count the Number of Good Subsequences
count-the-number-of-good-subsequences
null
Medium
3.2K
6.2K
3,190
6,211
51.4%
['number-of-music-playlists', 'count-ways-to-build-rooms-in-an-ant-colony']
[]
Algorithms
null
"aabb"
{ "name": "countGoodSubsequences", "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', 'Math', 'String', 'Combinatorics', 'Counting']
2,540
Minimum Common Value
minimum-common-value
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in non-decreasing order, return <em>the <strong>minimum integer common</strong> to both arrays</em>. If there is no common integer amongst <code>nums1</code> and <code>nums2</code>, return <code>-1</code>.</p> <p>Note that an integer is said to be <strong>common</strong> to <code>nums1</code> and <code>nums2</code> if both arrays have <strong>at least one</strong> occurrence of that integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2,3], nums2 = [2,4] <strong>Output:</strong> 2 <strong>Explanation:</strong> The smallest element common to both arrays is 2, so we return 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,2,3,6], nums2 = [2,3,4,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li> <li>Both <code>nums1</code> and <code>nums2</code> are sorted in <strong>non-decreasing</strong> order.</li> </ul>
Easy
273.2K
468.9K
273,198
468,906
58.3%
['intersection-of-two-arrays', 'intersection-of-two-arrays-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int getCommon(vector<int>& nums1, vector<int>& nums2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int getCommon(int[] nums1, int[] nums2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def getCommon(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 getCommon(self, nums1: List[int], nums2: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int getCommon(int* nums1, int nums1Size, int* nums2, int nums2Size) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int GetCommon(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 getCommon = function(nums1, nums2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function getCommon(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 getCommon($nums1, $nums2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func getCommon(_ nums1: [Int], _ nums2: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun getCommon(nums1: IntArray, nums2: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int getCommon(List<int> nums1, List<int> nums2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func getCommon(nums1 []int, nums2 []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef get_common(nums1, nums2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def getCommon(nums1: Array[Int], nums2: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn get_common(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (get-common nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec get_common(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nget_common(Nums1, Nums2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec get_common(nums1 :: [integer], nums2 :: [integer]) :: integer\n def get_common(nums1, nums2) do\n \n end\nend"}]
[1,2,3] [2,4]
{ "name": "getCommon", "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', 'Hash Table', 'Two Pointers', 'Binary Search']
2,541
Minimum Operations to Make Array Equal II
minimum-operations-to-make-array-equal-ii
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and an integer <code>k</code>. You can perform the following operation on <code>nums1</code>:</p> <ul> <li>Choose two indexes <code>i</code> and <code>j</code> and increment <code>nums1[i]</code> by <code>k</code> and decrement <code>nums1[j]</code> by <code>k</code>. In other words, <code>nums1[i] = nums1[i] + k</code> and <code>nums1[j] = nums1[j] - k</code>.</li> </ul> <p><code>nums1</code> is said to be <strong>equal</strong> to <code>nums2</code> if for all indices <code>i</code> such that <code>0 &lt;= i &lt; n</code>, <code>nums1[i] == nums2[i]</code>.</p> <p>Return <em>the <strong>minimum</strong> number of operations required to make </em><code>nums1</code><em> equal to </em><code>nums2</code>. If it is impossible to make them equal, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3 <strong>Output:</strong> 2 <strong>Explanation:</strong> In 2 operations, we can transform nums1 to nums2. 1<sup>st</sup> operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4]. 2<sup>nd</sup> operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1]. One can prove that it is impossible to make arrays equal in fewer operations.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1 <strong>Output:</strong> -1 <strong>Explanation:</strong> It can be proved that it is impossible to make the two arrays equal. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length == nums2.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
Medium
25.1K
78K
25,130
78,005
32.2%
['minimum-operations-to-make-array-equal', 'minimum-number-of-operations-to-make-arrays-similar']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minOperations(vector<int>& nums1, vector<int>& nums2, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minOperations(int[] nums1, int[] nums2, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minOperations(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minOperations(self, nums1: List[int], nums2: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minOperations(int* nums1, int nums1Size, int* nums2, int nums2Size, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinOperations(int[] nums1, int[] nums2, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @param {number} k\n * @return {number}\n */\nvar minOperations = function(nums1, nums2, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minOperations(nums1: number[], nums2: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @param Integer $k\n * @return Integer\n */\n function minOperations($nums1, $nums2, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minOperations(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minOperations(nums1: IntArray, nums2: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minOperations(List<int> nums1, List<int> nums2, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minOperations(nums1 []int, nums2 []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @param {Integer} k\n# @return {Integer}\ndef min_operations(nums1, nums2, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minOperations(nums1: Array[Int], nums2: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_operations(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-operations nums1 nums2 k)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_operations(Nums1 :: [integer()], Nums2 :: [integer()], K :: integer()) -> integer().\nmin_operations(Nums1, Nums2, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_operations(nums1 :: [integer], nums2 :: [integer], k :: integer) :: integer\n def min_operations(nums1, nums2, k) do\n \n end\nend"}]
[4,3,1,4] [1,3,7,1] 3
{ "name": "minOperations", "params": [ { "name": "nums1", "type": "integer[]" }, { "type": "integer[]", "name": "nums2" }, { "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']
2,542
Maximum Subsequence Score
maximum-subsequence-score
<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and a positive integer <code>k</code>. You must choose a <strong>subsequence</strong> of indices from <code>nums1</code> of length <code>k</code>.</p> <p>For chosen indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ..., <code>i<sub>k - 1</sub></code>, your <strong>score</strong> is defined as:</p> <ul> <li>The sum of the selected elements from <code>nums1</code> multiplied with the <strong>minimum</strong> of the selected elements from <code>nums2</code>.</li> <li>It can defined simply as: <code>(nums1[i<sub>0</sub>] + nums1[i<sub>1</sub>] +...+ nums1[i<sub>k - 1</sub>]) * min(nums2[i<sub>0</sub>] , nums2[i<sub>1</sub>], ... ,nums2[i<sub>k - 1</sub>])</code>.</li> </ul> <p>Return <em>the <strong>maximum</strong> possible score.</em></p> <p>A <strong>subsequence</strong> of indices of an array is a set that can be derived from the set <code>{0, 1, ..., n-1}</code> by deleting some or no elements.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3 <strong>Output:</strong> 12 <strong>Explanation:</strong> The four possible subsequence scores are: - We choose the indices 0, 1, and 2 with score = (1+3+3) * min(2,1,3) = 7. - We choose the indices 0, 1, and 3 with score = (1+3+2) * min(2,1,4) = 6. - We choose the indices 0, 2, and 3 with score = (1+3+2) * min(2,3,4) = 12. - We choose the indices 1, 2, and 3 with score = (3+3+2) * min(1,3,4) = 8. Therefore, we return the max score, which is 12. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1 <strong>Output:</strong> 30 <strong>Explanation:</strong> Choosing index 2 is optimal: nums1[2] * nums2[2] = 3 * 10 = 30 is the maximum possible score. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums1.length == nums2.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums1[i], nums2[j] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
Medium
122.9K
226.8K
122,852
226,835
54.2%
['ipo', 'minimum-cost-to-hire-k-workers']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxScore(vector<int>& nums1, vector<int>& nums2, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxScore(int[] nums1, int[] nums2, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxScore(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxScore(self, nums1: List[int], nums2: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxScore(int* nums1, int nums1Size, int* nums2, int nums2Size, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxScore(int[] nums1, int[] nums2, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @param {number} k\n * @return {number}\n */\nvar maxScore = function(nums1, nums2, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxScore(nums1: number[], nums2: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @param Integer $k\n * @return Integer\n */\n function maxScore($nums1, $nums2, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxScore(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxScore(nums1: IntArray, nums2: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxScore(List<int> nums1, List<int> nums2, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxScore(nums1 []int, nums2 []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @param {Integer} k\n# @return {Integer}\ndef max_score(nums1, nums2, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxScore(nums1: Array[Int], nums2: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_score(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-score nums1 nums2 k)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_score(Nums1 :: [integer()], Nums2 :: [integer()], K :: integer()) -> integer().\nmax_score(Nums1, Nums2, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_score(nums1 :: [integer], nums2 :: [integer], k :: integer) :: integer\n def max_score(nums1, nums2, k) do\n \n end\nend"}]
[1,3,3,2] [2,1,3,4] 3
{ "name": "maxScore", "params": [ { "name": "nums1", "type": "integer[]" }, { "type": "integer[]", "name": "nums2" }, { "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', 'Greedy', 'Sorting', 'Heap (Priority Queue)']
2,543
Check if Point Is Reachable
check-if-point-is-reachable
<p>There exists an infinitely large grid. You are currently at point <code>(1, 1)</code>, and you need to reach the point <code>(targetX, targetY)</code> using a finite number of steps.</p> <p>In one <strong>step</strong>, you can move from point <code>(x, y)</code> to any one of the following points:</p> <ul> <li><code>(x, y - x)</code></li> <li><code>(x - y, y)</code></li> <li><code>(2 * x, y)</code></li> <li><code>(x, 2 * y)</code></li> </ul> <p>Given two integers <code>targetX</code> and <code>targetY</code> representing the X-coordinate and Y-coordinate of your final position, return <code>true</code> <em>if you can reach the point from</em> <code>(1, 1)</code> <em>using some number of steps, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> targetX = 6, targetY = 9 <strong>Output:</strong> false <strong>Explanation:</strong> It is impossible to reach (6,9) from (1,1) using any sequence of moves, so false is returned. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> targetX = 4, targetY = 7 <strong>Output:</strong> true <strong>Explanation:</strong> You can follow the path (1,1) -&gt; (1,2) -&gt; (1,4) -&gt; (1,8) -&gt; (1,7) -&gt; (2,7) -&gt; (4,7). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= targetX, targetY&nbsp;&lt;= 10<sup>9</sup></code></li> </ul>
Hard
9.2K
21.3K
9,194
21,309
43.1%
['reaching-points', 'check-if-the-rectangle-corner-is-reachable']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool isReachable(int targetX, int targetY) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean isReachable(int targetX, int targetY) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def isReachable(self, targetX, targetY):\n \"\"\"\n :type targetX: int\n :type targetY: int\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def isReachable(self, targetX: int, targetY: int) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool isReachable(int targetX, int targetY) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool IsReachable(int targetX, int targetY) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} targetX\n * @param {number} targetY\n * @return {boolean}\n */\nvar isReachable = function(targetX, targetY) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function isReachable(targetX: number, targetY: number): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $targetX\n * @param Integer $targetY\n * @return Boolean\n */\n function isReachable($targetX, $targetY) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func isReachable(_ targetX: Int, _ targetY: Int) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun isReachable(targetX: Int, targetY: Int): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool isReachable(int targetX, int targetY) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func isReachable(targetX int, targetY int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} target_x\n# @param {Integer} target_y\n# @return {Boolean}\ndef is_reachable(target_x, target_y)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def isReachable(targetX: Int, targetY: Int): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn is_reachable(target_x: i32, target_y: i32) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (is-reachable targetX targetY)\n (-> exact-integer? exact-integer? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec is_reachable(TargetX :: integer(), TargetY :: integer()) -> boolean().\nis_reachable(TargetX, TargetY) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec is_reachable(target_x :: integer, target_y :: integer) :: boolean\n def is_reachable(target_x, target_y) do\n \n end\nend"}]
6 9
{ "name": "isReachable", "params": [ { "name": "targetX", "type": "integer" }, { "type": "integer", "name": "targetY" } ], "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', 'Number Theory']
2,544
Alternating Digit Sum
alternating-digit-sum
<p>You are given a positive integer <code>n</code>. Each digit of <code>n</code> has a sign according to the following rules:</p> <ul> <li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li> <li>Each other digit has an opposite sign to its adjacent digits.</li> </ul> <p>Return <em>the sum of all digits with their corresponding sign</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 521 <strong>Output:</strong> 4 <strong>Explanation:</strong> (+5) + (-2) + (+1) = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 111 <strong>Output:</strong> 1 <strong>Explanation:</strong> (+1) + (-1) + (+1) = 1. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> n = 886996 <strong>Output:</strong> 0 <strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Easy
77.2K
112.9K
77,168
112,869
68.4%
['add-digits', 'minimum-sum-of-four-digit-number-after-splitting-digits', 'separate-the-digits-in-an-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int alternateDigitSum(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int alternateDigitSum(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def alternateDigitSum(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def alternateDigitSum(self, n: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int alternateDigitSum(int n) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int AlternateDigitSum(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number}\n */\nvar alternateDigitSum = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function alternateDigitSum(n: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function alternateDigitSum($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func alternateDigitSum(_ n: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun alternateDigitSum(n: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int alternateDigitSum(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func alternateDigitSum(n int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer}\ndef alternate_digit_sum(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def alternateDigitSum(n: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn alternate_digit_sum(n: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (alternate-digit-sum n)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec alternate_digit_sum(N :: integer()) -> integer().\nalternate_digit_sum(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec alternate_digit_sum(n :: integer) :: integer\n def alternate_digit_sum(n) do\n \n end\nend"}]
521
{ "name": "alternateDigitSum", "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']
2,545
Sort the Students by Their Kth Score
sort-the-students-by-their-kth-score
<p>There is a class with <code>m</code> students and <code>n</code> exams. You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>score</code>, where each row represents one student and <code>score[i][j]</code> denotes the score the <code>i<sup>th</sup></code> student got in the <code>j<sup>th</sup></code> exam. The matrix <code>score</code> contains <strong>distinct</strong> integers only.</p> <p>You are also given an integer <code>k</code>. Sort the students (i.e., the rows of the matrix) by their scores in the <code>k<sup>th</sup></code>&nbsp;(<strong>0-indexed</strong>) exam from the highest to the lowest.</p> <p>Return <em>the matrix after sorting it.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example1.png" style="width: 600px; height: 136px;" /> <pre> <strong>Input:</strong> score = [[10,6,9,1],[7,5,11,2],[4,8,3,15]], k = 2 <strong>Output:</strong> [[7,5,11,2],[10,6,9,1],[4,8,3,15]] <strong>Explanation:</strong> In the above diagram, S denotes the student, while E denotes the exam. - The student with index 1 scored 11 in exam 2, which is the highest score, so they got first place. - The student with index 0 scored 9 in exam 2, which is the second highest score, so they got second place. - The student with index 2 scored 3 in exam 2, which is the lowest score, so they got third place. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/11/30/example2.png" style="width: 486px; height: 121px;" /> <pre> <strong>Input:</strong> score = [[3,4],[5,6]], k = 0 <strong>Output:</strong> [[5,6],[3,4]] <strong>Explanation:</strong> In the above diagram, S denotes the student, while E denotes the exam. - The student with index 1 scored 5 in exam 0, which is the highest score, so they got first place. - The student with index 0 scored 3 in exam 0, which is the lowest score, so they got second place. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == score.length</code></li> <li><code>n == score[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 250</code></li> <li><code>1 &lt;= score[i][j] &lt;= 10<sup>5</sup></code></li> <li><code>score</code> consists of <strong>distinct</strong> integers.</li> <li><code>0 &lt;= k &lt; n</code></li> </ul>
Medium
75.4K
88.2K
75,447
88,193
85.5%
['erect-the-fence', 'custom-sort-string', 'sort-the-people']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> sortTheStudents(vector<vector<int>>& score, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[][] sortTheStudents(int[][] score, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sortTheStudents(self, score, k):\n \"\"\"\n :type score: List[List[int]]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def sortTheStudents(self, score: List[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** sortTheStudents(int** score, int scoreSize, int* scoreColSize, int k, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[][] SortTheStudents(int[][] score, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} score\n * @param {number} k\n * @return {number[][]}\n */\nvar sortTheStudents = function(score, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sortTheStudents(score: number[][], k: number): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $score\n * @param Integer $k\n * @return Integer[][]\n */\n function sortTheStudents($score, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sortTheStudents(_ score: [[Int]], _ k: Int) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sortTheStudents(score: Array<IntArray>, k: Int): Array<IntArray> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> sortTheStudents(List<List<int>> score, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sortTheStudents(score [][]int, k int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} score\n# @param {Integer} k\n# @return {Integer[][]}\ndef sort_the_students(score, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sortTheStudents(score: Array[Array[Int]], k: Int): Array[Array[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sort_the_students(score: Vec<Vec<i32>>, k: i32) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sort-the-students score k)\n (-> (listof (listof exact-integer?)) exact-integer? (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sort_the_students(Score :: [[integer()]], K :: integer()) -> [[integer()]].\nsort_the_students(Score, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sort_the_students(score :: [[integer]], k :: integer) :: [[integer]]\n def sort_the_students(score, k) do\n \n end\nend"}]
[[10,6,9,1],[7,5,11,2],[4,8,3,15]] 2
{ "name": "sortTheStudents", "params": [ { "name": "score", "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', 'Sorting', 'Matrix']
2,546
Apply Bitwise Operations to Make Strings Equal
apply-bitwise-operations-to-make-strings-equal
<p>You are given two <strong>0-indexed binary</strong> strings <code>s</code> and <code>target</code> of the same length <code>n</code>. You can do the following operation on <code>s</code> <strong>any</strong> number of times:</p> <ul> <li>Choose two <strong>different</strong> indices <code>i</code> and <code>j</code> where <code>0 &lt;= i, j &lt; n</code>.</li> <li>Simultaneously, replace <code>s[i]</code> with (<code>s[i]</code> <strong>OR</strong> <code>s[j]</code>) and <code>s[j]</code> with (<code>s[i]</code> <strong>XOR</strong> <code>s[j]</code>).</li> </ul> <p>For example, if <code>s = &quot;0110&quot;</code>, you can choose <code>i = 0</code> and <code>j = 2</code>, then simultaneously replace <code>s[0]</code> with (<code>s[0]</code> <strong>OR</strong> <code>s[2]</code> = <code>0</code> <strong>OR</strong> <code>1</code> = <code>1</code>), and <code>s[2]</code> with (<code>s[0]</code> <strong>XOR</strong> <code>s[2]</code> = <code>0</code> <strong>XOR</strong> <code>1</code> = <code>1</code>), so we will have <code>s = &quot;1110&quot;</code>.</p> <p>Return <code>true</code> <em>if you can make the string </em><code>s</code><em> equal to </em><code>target</code><em>, or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;1010&quot;, target = &quot;0110&quot; <strong>Output:</strong> true <strong>Explanation:</strong> We can do the following operations: - Choose i = 2 and j = 0. We have now s = &quot;<strong><u>0</u></strong>0<strong><u>1</u></strong>0&quot;. - Choose i = 2 and j = 1. We have now s = &quot;0<strong><u>11</u></strong>0&quot;. Since we can make s equal to target, we return true. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;11&quot;, target = &quot;00&quot; <strong>Output:</strong> false <strong>Explanation:</strong> It is not possible to make s equal to target with any number of operations. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == s.length == target.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>s</code> and <code>target</code> consist of only the digits <code>0</code> and <code>1</code>.</li> </ul>
Medium
20.7K
49.9K
20,708
49,877
41.5%
['minimum-one-bit-operations-to-make-integers-zero']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool makeStringsEqual(string s, string target) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean makeStringsEqual(String s, String target) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def makeStringsEqual(self, s, target):\n \"\"\"\n :type s: str\n :type target: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def makeStringsEqual(self, s: str, target: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool makeStringsEqual(char* s, char* target) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool MakeStringsEqual(string s, string target) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {string} target\n * @return {boolean}\n */\nvar makeStringsEqual = function(s, target) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function makeStringsEqual(s: string, target: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param String $target\n * @return Boolean\n */\n function makeStringsEqual($s, $target) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func makeStringsEqual(_ s: String, _ target: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun makeStringsEqual(s: String, target: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool makeStringsEqual(String s, String target) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func makeStringsEqual(s string, target string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} target\n# @return {Boolean}\ndef make_strings_equal(s, target)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def makeStringsEqual(s: String, target: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn make_strings_equal(s: String, target: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (make-strings-equal s target)\n (-> string? string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec make_strings_equal(S :: unicode:unicode_binary(), Target :: unicode:unicode_binary()) -> boolean().\nmake_strings_equal(S, Target) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec make_strings_equal(s :: String.t, target :: String.t) :: boolean\n def make_strings_equal(s, target) do\n \n end\nend"}]
"1010" "0110"
{ "name": "makeStringsEqual", "params": [ { "name": "s", "type": "string" }, { "type": "string", "name": "target" } ], "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', 'Bit Manipulation']
2,547
Minimum Cost to Split an Array
minimum-cost-to-split-an-array
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>Split the array into some number of non-empty subarrays. The <strong>cost</strong> of a split is the sum of the <strong>importance value</strong> of each subarray in the split.</p> <p>Let <code>trimmed(subarray)</code> be the version of the subarray where all numbers which appear only once are removed.</p> <ul> <li>For example, <code>trimmed([3,1,2,4,3,4]) = [3,4,3,4].</code></li> </ul> <p>The <strong>importance value</strong> of a subarray is <code>k + trimmed(subarray).length</code>.</p> <ul> <li>For example, if a subarray is <code>[1,2,3,3,3,4,4]</code>, then <font face="monospace">trimmed(</font><code>[1,2,3,3,3,4,4]) = [3,3,3,4,4].</code>The importance value of this subarray will be <code>k + 5</code>.</li> </ul> <p>Return <em>the minimum possible cost of a split of </em><code>nums</code>.</p> <p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,1,2,1,3,3], k = 2 <strong>Output:</strong> 8 <strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1,3,3]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1,3,3] is 2 + (2 + 2) = 6. The cost of the split is 2 + 6 = 8. It can be shown that this is the minimum possible cost among all the possible splits. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,1,2,1], k = 2 <strong>Output:</strong> 6 <strong>Explanation:</strong> We split nums to have two subarrays: [1,2], [1,2,1]. The importance value of [1,2] is 2 + (0) = 2. The importance value of [1,2,1] is 2 + (2) = 4. The cost of the split is 2 + 4 = 6. It can be shown that this is the minimum possible cost among all the possible splits. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,1,2,1], k = 5 <strong>Output:</strong> 10 <strong>Explanation:</strong> We split nums to have one subarray: [1,2,1,2,1]. The importance value of [1,2,1,2,1] is 5 + (3 + 2) = 10. The cost of the split is 10. It can be shown that this is the minimum possible cost among all the possible splits. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>0 &lt;= nums[i] &lt; nums.length</code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;  } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Hard
14.1K
33.7K
14,100
33,720
41.8%
['coin-change', 'split-array-largest-sum', 'divide-an-array-into-subarrays-with-minimum-cost-ii', 'minimum-sum-of-values-by-dividing-array', 'minimum-cost-to-divide-array-into-subarrays']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minCost(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minCost(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCost(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 minCost(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minCost(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinCost(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 minCost = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCost(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 minCost($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCost(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCost(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCost(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCost(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef min_cost(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCost(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost(Nums :: [integer()], K :: integer()) -> integer().\nmin_cost(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost(nums :: [integer], k :: integer) :: integer\n def min_cost(nums, k) do\n \n end\nend"}]
[1,2,1,2,1,3,3] 2
{ "name": "minCost", "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', 'Hash Table', 'Dynamic Programming', 'Counting']
2,548
Maximum Price to Fill a Bag
maximum-price-to-fill-a-bag
null
Medium
1.7K
2.6K
1,674
2,637
63.5%
[]
[]
Algorithms
null
[[50,1],[10,8]] 5
{ "name": "maxPrice", "params": [ { "name": "items", "type": "integer[][]" }, { "type": "integer", "name": "capacity" } ], "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', 'Greedy', 'Sorting']
2,549
Count Distinct Numbers on Board
count-distinct-numbers-on-board
<p>You are given a positive integer <code>n</code>, that is initially placed on a board. Every day, for <code>10<sup>9</sup></code> days, you perform the following procedure:</p> <ul> <li>For each number <code>x</code> present on the board, find all numbers <code>1 &lt;= i &lt;= n</code> such that <code>x % i == 1</code>.</li> <li>Then, place those numbers on the board.</li> </ul> <p>Return<em> the number of <strong>distinct</strong> integers present on the board after</em> <code>10<sup>9</sup></code> <em>days have elapsed</em>.</p> <p><strong>Note:</strong></p> <ul> <li>Once a number is placed on the board, it will remain on it until the end.</li> <li><code>%</code>&nbsp;stands&nbsp;for the modulo operation. For example,&nbsp;<code>14 % 3</code> is <code>2</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 5 <strong>Output:</strong> 4 <strong>Explanation:</strong> Initially, 5 is present on the board. The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1. After that day, 3 will be added to the board because 4 % 3 == 1. At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 3 <strong>Output:</strong> 2 <strong>Explanation:</strong> Since 3 % 2 == 1, 2 will be added to the board. After a billion days, the only two distinct numbers on the board are 2 and 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 100</code></li> </ul>
Easy
50.7K
82.9K
50,739
82,909
61.2%
['count-of-matches-in-tournament']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int distinctIntegers(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int distinctIntegers(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def distinctIntegers(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def distinctIntegers(self, n: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int distinctIntegers(int n) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DistinctIntegers(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number}\n */\nvar distinctIntegers = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function distinctIntegers(n: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function distinctIntegers($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func distinctIntegers(_ n: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun distinctIntegers(n: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int distinctIntegers(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func distinctIntegers(n int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer}\ndef distinct_integers(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def distinctIntegers(n: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn distinct_integers(n: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (distinct-integers n)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec distinct_integers(N :: integer()) -> integer().\ndistinct_integers(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec distinct_integers(n :: integer) :: integer\n def distinct_integers(n) do\n \n end\nend"}]
5
{ "name": "distinctIntegers", "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>"]}
['Array', 'Hash Table', 'Math', 'Simulation']
2,550
Count Collisions of Monkeys on a Polygon
count-collisions-of-monkeys-on-a-polygon
<p>There is a regular convex polygon with <code>n</code> vertices. The vertices are labeled from <code>0</code> to <code>n - 1</code> in a clockwise direction, and each vertex has <strong>exactly one monkey</strong>. The following figure shows a convex polygon of <code>6</code> vertices.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/01/22/hexagon.jpg" style="width: 300px; height: 293px;" /> <p>Simultaneously, each monkey moves to a neighboring vertex. A <strong>collision</strong> happens if at least two monkeys reside on the same vertex after the movement or intersect on an edge.</p> <p>Return the number of ways the monkeys can move so that at least <strong>one collision</strong> happens. Since the answer may be very large, return it modulo <code>10<sup>9 </sup>+ 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>There are 8 total possible movements.<br /> Two ways such that they collide at some point are:</p> <ul> <li>Monkey 1 moves in a clockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 2 collide.</li> <li>Monkey 1 moves in an anticlockwise direction; monkey 2 moves in an anticlockwise direction; monkey 3 moves in a clockwise direction. Monkeys 1 and 3 collide.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">14</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= n &lt;= 10<sup>9</sup></code></li> </ul>
Medium
23.4K
81.5K
23,350
81,541
28.6%
['powx-n']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int monkeyMove(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int monkeyMove(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def monkeyMove(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def monkeyMove(self, n: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int monkeyMove(int n) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MonkeyMove(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number}\n */\nvar monkeyMove = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function monkeyMove(n: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function monkeyMove($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func monkeyMove(_ n: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun monkeyMove(n: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int monkeyMove(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func monkeyMove(n int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer}\ndef monkey_move(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def monkeyMove(n: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn monkey_move(n: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (monkey-move n)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec monkey_move(N :: integer()) -> integer().\nmonkey_move(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec monkey_move(n :: integer) :: integer\n def monkey_move(n) do\n \n end\nend"}]
3
{ "name": "monkeyMove", "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', 'Recursion']
2,551
Put Marbles in Bags
put-marbles-in-bags
<p>You have <code>k</code> bags. You are given a <strong>0-indexed</strong> integer array <code>weights</code> where <code>weights[i]</code> is the weight of the <code>i<sup>th</sup></code> marble. You are also given the integer <code>k.</code></p> <p>Divide the marbles into the <code>k</code> bags according to the following rules:</p> <ul> <li>No bag is empty.</li> <li>If the <code>i<sup>th</sup></code> marble and <code>j<sup>th</sup></code> marble are in a bag, then all marbles with an index between the <code>i<sup>th</sup></code> and <code>j<sup>th</sup></code> indices should also be in that same bag.</li> <li>If a bag consists of all the marbles with an index from <code>i</code> to <code>j</code> inclusively, then the cost of the bag is <code>weights[i] + weights[j]</code>.</li> </ul> <p>The <strong>score</strong> after distributing the marbles is the sum of the costs of all the <code>k</code> bags.</p> <p>Return <em>the <strong>difference</strong> between the <strong>maximum</strong> and <strong>minimum</strong> scores among marble distributions</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> weights = [1,3,5,1], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6. The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10. Thus, we return their difference 10 - 6 = 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> weights = [1, 3], k = 2 <strong>Output:</strong> 0 <strong>Explanation:</strong> The only distribution possible is [1],[3]. Since both the maximal and minimal score are the same, we return 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= weights.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= weights[i] &lt;= 10<sup>9</sup></code></li> </ul>
Hard
143K
196.9K
142,954
196,945
72.6%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long putMarbles(vector<int>& weights, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long putMarbles(int[] weights, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def putMarbles(self, weights, k):\n \"\"\"\n :type weights: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def putMarbles(self, weights: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long putMarbles(int* weights, int weightsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long PutMarbles(int[] weights, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} weights\n * @param {number} k\n * @return {number}\n */\nvar putMarbles = function(weights, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function putMarbles(weights: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $weights\n * @param Integer $k\n * @return Integer\n */\n function putMarbles($weights, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func putMarbles(_ weights: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun putMarbles(weights: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int putMarbles(List<int> weights, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func putMarbles(weights []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} weights\n# @param {Integer} k\n# @return {Integer}\ndef put_marbles(weights, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def putMarbles(weights: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn put_marbles(weights: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (put-marbles weights k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec put_marbles(Weights :: [integer()], K :: integer()) -> integer().\nput_marbles(Weights, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec put_marbles(weights :: [integer], k :: integer) :: integer\n def put_marbles(weights, k) do\n \n end\nend"}]
[1,3,5,1] 2
{ "name": "putMarbles", "params": [ { "name": "weights", "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', 'Greedy', 'Sorting', 'Heap (Priority Queue)']
2,552
Count Increasing Quadruplets
count-increasing-quadruplets
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> containing all numbers from <code>1</code> to <code>n</code>, return <em>the number of increasing quadruplets</em>.</p> <p>A quadruplet <code>(i, j, k, l)</code> is increasing if:</p> <ul> <li><code>0 &lt;= i &lt; j &lt; k &lt; l &lt; n</code>, and</li> <li><code>nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,2,4,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> - When i = 0, j = 1, k = 2, and l = 3, nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l]. - When i = 0, j = 1, k = 2, and l = 4, nums[i] &lt; nums[k] &lt; nums[j] &lt; nums[l]. There are no other quadruplets, so we return 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> 0 <strong>Explanation:</strong> There exists only one quadruplet with i = 0, j = 1, k = 2, l = 3, but since nums[j] &lt; nums[k], we return 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>4 &lt;= nums.length &lt;= 4000</code></li> <li><code>1 &lt;= nums[i] &lt;= nums.length</code></li> <li>All the integers of <code>nums</code> are <strong>unique</strong>. <code>nums</code> is a permutation.</li> </ul>
Hard
10.8K
31.9K
10,815
31,858
33.9%
['increasing-triplet-subsequence', 'count-special-quadruplets', 'count-good-triplets-in-an-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countQuadruplets(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countQuadruplets(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countQuadruplets(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countQuadruplets(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countQuadruplets(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountQuadruplets(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar countQuadruplets = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countQuadruplets(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function countQuadruplets($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countQuadruplets(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countQuadruplets(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countQuadruplets(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countQuadruplets(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef count_quadruplets(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countQuadruplets(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_quadruplets(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-quadruplets nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_quadruplets(Nums :: [integer()]) -> integer().\ncount_quadruplets(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_quadruplets(nums :: [integer]) :: integer\n def count_quadruplets(nums) do\n \n end\nend"}]
[1,3,2,4,5]
{ "name": "countQuadruplets", "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', 'Binary Indexed Tree', 'Enumeration', 'Prefix Sum']
2,553
Separate the Digits in an Array
separate-the-digits-in-an-array
<p>Given an array of positive integers <code>nums</code>, return <em>an array </em><code>answer</code><em> that consists of the digits of each integer in </em><code>nums</code><em> after separating them in <strong>the same order</strong> they appear in </em><code>nums</code>.</p> <p>To separate the digits of an integer is to get all the digits it has in the same order.</p> <ul> <li>For example, for the integer <code>10921</code>, the separation of its digits is <code>[1,0,9,2,1]</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [13,25,83,77] <strong>Output:</strong> [1,3,2,5,8,3,7,7] <strong>Explanation:</strong> - The separation of 13 is [1,3]. - The separation of 25 is [2,5]. - The separation of 83 is [8,3]. - The separation of 77 is [7,7]. answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [7,1,3,9] <strong>Output:</strong> [7,1,3,9] <strong>Explanation:</strong> The separation of each integer in nums is itself. answer = [7,1,3,9]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Easy
87.6K
109.4K
87,624
109,390
80.1%
['count-integers-with-even-digit-sum', 'alternating-digit-sum']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> separateDigits(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] separateDigits(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def separateDigits(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def separateDigits(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* separateDigits(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] SeparateDigits(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar separateDigits = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function separateDigits(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function separateDigits($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func separateDigits(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun separateDigits(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> separateDigits(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func separateDigits(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef separate_digits(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def separateDigits(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn separate_digits(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (separate-digits nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec separate_digits(Nums :: [integer()]) -> [integer()].\nseparate_digits(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec separate_digits(nums :: [integer]) :: [integer]\n def separate_digits(nums) do\n \n end\nend"}]
[13,25,83,77]
{ "name": "separateDigits", "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', 'Simulation']
2,554
Maximum Number of Integers to Choose From a Range I
maximum-number-of-integers-to-choose-from-a-range-i
<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p> <ul> <li>The chosen integers have to be in the range <code>[1, n]</code>.</li> <li>Each integer can be chosen <strong>at most once</strong>.</li> <li>The chosen integers should not be in the array <code>banned</code>.</li> <li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li> </ul> <p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6 <strong>Output:</strong> 2 <strong>Explanation:</strong> You can choose the integers 2 and 4. 2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1 <strong>Output:</strong> 0 <strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> banned = [11], n = 7, maxSum = 50 <strong>Output:</strong> 7 <strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7. They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= banned.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= banned[i], n &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= maxSum &lt;= 10<sup>9</sup></code></li> </ul>
Medium
160.2K
236.1K
160,223
236,113
67.9%
['first-missing-positive', 'find-all-numbers-disappeared-in-an-array', 'append-k-integers-with-minimal-sum', 'replace-elements-in-an-array', 'maximum-number-of-integers-to-choose-from-a-range-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxCount(vector<int>& banned, int n, int maxSum) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxCount(int[] banned, int n, int maxSum) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxCount(self, banned, n, maxSum):\n \"\"\"\n :type banned: List[int]\n :type n: int\n :type maxSum: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxCount(int* banned, int bannedSize, int n, int maxSum) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxCount(int[] banned, int n, int maxSum) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} banned\n * @param {number} n\n * @param {number} maxSum\n * @return {number}\n */\nvar maxCount = function(banned, n, maxSum) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxCount(banned: number[], n: number, maxSum: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $banned\n * @param Integer $n\n * @param Integer $maxSum\n * @return Integer\n */\n function maxCount($banned, $n, $maxSum) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxCount(_ banned: [Int], _ n: Int, _ maxSum: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxCount(banned: IntArray, n: Int, maxSum: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxCount(List<int> banned, int n, int maxSum) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxCount(banned []int, n int, maxSum int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} banned\n# @param {Integer} n\n# @param {Integer} max_sum\n# @return {Integer}\ndef max_count(banned, n, max_sum)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxCount(banned: Array[Int], n: Int, maxSum: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_count(banned: Vec<i32>, n: i32, max_sum: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-count banned n maxSum)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_count(Banned :: [integer()], N :: integer(), MaxSum :: integer()) -> integer().\nmax_count(Banned, N, MaxSum) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_count(banned :: [integer], n :: integer, max_sum :: integer) :: integer\n def max_count(banned, n, max_sum) do\n \n end\nend"}]
[1,6,5] 5 6
{ "name": "maxCount", "params": [ { "name": "banned", "type": "integer[]" }, { "type": "integer", "name": "n" }, { "type": "integer", "name": "maxSum" } ], "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', 'Binary Search', 'Greedy', 'Sorting']
2,555
Maximize Win From Two Segments
maximize-win-from-two-segments
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p> <p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p> <ul> <li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 &lt;= prizePositions[i] &lt;= 3</code> or <code>2 &lt;= prizePositions[i] &lt;= 4</code>.</li> </ul> <p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2 <strong>Output:</strong> 7 <strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> prizePositions = [1,2,3,4], k = 0 <strong>Output:</strong> 2 <strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= prizePositions.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= prizePositions[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= 10<sup>9</sup> </code></li> <li><code>prizePositions</code> is sorted in non-decreasing order.</li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Medium
12.6K
36.1K
12,557
36,086
34.8%
['best-time-to-buy-and-sell-stock-iii', 'two-best-non-overlapping-events']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximizeWin(vector<int>& prizePositions, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximizeWin(int[] prizePositions, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximizeWin(self, prizePositions, k):\n \"\"\"\n :type prizePositions: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximizeWin(self, prizePositions: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximizeWin(int* prizePositions, int prizePositionsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximizeWin(int[] prizePositions, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} prizePositions\n * @param {number} k\n * @return {number}\n */\nvar maximizeWin = function(prizePositions, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximizeWin(prizePositions: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $prizePositions\n * @param Integer $k\n * @return Integer\n */\n function maximizeWin($prizePositions, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximizeWin(_ prizePositions: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximizeWin(prizePositions: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximizeWin(List<int> prizePositions, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximizeWin(prizePositions []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} prize_positions\n# @param {Integer} k\n# @return {Integer}\ndef maximize_win(prize_positions, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximizeWin(prizePositions: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximize_win(prize_positions: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximize-win prizePositions k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximize_win(PrizePositions :: [integer()], K :: integer()) -> integer().\nmaximize_win(PrizePositions, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximize_win(prize_positions :: [integer], k :: integer) :: integer\n def maximize_win(prize_positions, k) do\n \n end\nend"}]
[1,1,2,2,3,3,5] 2
{ "name": "maximizeWin", "params": [ { "name": "prizePositions", "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', 'Sliding Window']
2,556
Disconnect Path in a Binary Matrix by at Most One Flip
disconnect-path-in-a-binary-matrix-by-at-most-one-flip
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> <strong>binary</strong> matrix <code>grid</code>. You can move from a cell <code>(row, col)</code> to any of the cells <code>(row + 1, col)</code> or <code>(row, col + 1)</code> that has the value <code>1</code>.&nbsp;The matrix is <strong>disconnected</strong> if there is no path from <code>(0, 0)</code> to <code>(m - 1, n - 1)</code>.</p> <p>You can flip the value of <strong>at most one</strong> (possibly none) cell. You <strong>cannot flip</strong> the cells <code>(0, 0)</code> and <code>(m - 1, n - 1)</code>.</p> <p>Return <code>true</code> <em>if it is possible to make the matrix disconnect or </em><code>false</code><em> otherwise</em>.</p> <p><strong>Note</strong> that flipping a cell changes its value from <code>0</code> to <code>1</code> or from <code>1</code> to <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" /> <pre> <strong>Input:</strong> grid = [[1,1,1],[1,0,0],[1,1,1]] <strong>Output:</strong> true <strong>Explanation:</strong> We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" /> <pre> <strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]] <strong>Output:</strong> false <strong>Explanation:</strong> It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2). </pre> <p>&nbsp;</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 &lt;= m, n &lt;= 1000</code></li> <li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li> <li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li> </ul>
Medium
16.4K
60.1K
16,378
60,137
27.2%
['number-of-submatrices-that-sum-to-target', 'minimum-cost-to-make-at-least-one-valid-path-in-a-grid', 'minimum-number-of-days-to-disconnect-island', 'minimum-weighted-subgraph-with-the-required-paths']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool isPossibleToCutPath(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean isPossibleToCutPath(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def isPossibleToCutPath(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def isPossibleToCutPath(self, grid: List[List[int]]) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool isPossibleToCutPath(int** grid, int gridSize, int* gridColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool IsPossibleToCutPath(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {boolean}\n */\nvar isPossibleToCutPath = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function isPossibleToCutPath(grid: number[][]): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Boolean\n */\n function isPossibleToCutPath($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func isPossibleToCutPath(_ grid: [[Int]]) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun isPossibleToCutPath(grid: Array<IntArray>): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool isPossibleToCutPath(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func isPossibleToCutPath(grid [][]int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Boolean}\ndef is_possible_to_cut_path(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def isPossibleToCutPath(grid: Array[Array[Int]]): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn is_possible_to_cut_path(grid: Vec<Vec<i32>>) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (is-possible-to-cut-path grid)\n (-> (listof (listof exact-integer?)) boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec is_possible_to_cut_path(Grid :: [[integer()]]) -> boolean().\nis_possible_to_cut_path(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec is_possible_to_cut_path(grid :: [[integer]]) :: boolean\n def is_possible_to_cut_path(grid) do\n \n end\nend"}]
[[1,1,1],[1,0,0],[1,1,1]]
{ "name": "isPossibleToCutPath", "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', 'Dynamic Programming', 'Depth-First Search', 'Breadth-First Search', 'Matrix']
2,557
Maximum Number of Integers to Choose From a Range II
maximum-number-of-integers-to-choose-from-a-range-ii
null
Medium
2.5K
7K
2,491
7,048
35.3%
['first-missing-positive', 'find-all-numbers-disappeared-in-an-array', 'append-k-integers-with-minimal-sum', 'replace-elements-in-an-array', 'maximum-number-of-integers-to-choose-from-a-range-i', 'maximize-the-distance-between-points-on-a-square']
[]
Algorithms
null
[1,4,6] 6 4
{ "name": "maxCount", "params": [ { "name": "banned", "type": "integer[]" }, { "type": "integer", "name": "n" }, { "type": "long", "name": "maxSum" } ], "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', 'Greedy', 'Sorting']
2,558
Take Gifts From the Richest Pile
take-gifts-from-the-richest-pile
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p> <ul> <li>Choose the pile with the maximum number of gifts.</li> <li>If there is more than one pile with the maximum number of gifts, choose any.</li> <li>Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.</li> </ul> <p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> gifts = [25,64,9,4,100], k = 4 <strong>Output:</strong> 29 <strong>Explanation:</strong> The gifts are taken in the following way: - In the first second, the last pile is chosen and 10 gifts are left behind. - Then the second pile is chosen and 8 gifts are left behind. - After that the first pile is chosen and 5 gifts are left behind. - Finally, the last pile is chosen again and 3 gifts are left behind. The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> gifts = [1,1,1,1], k = 4 <strong>Output:</strong> 4 <strong>Explanation:</strong> In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. That is, you can&#39;t take any pile with you. So, the total gifts remaining are 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= gifts.length &lt;= 10<sup>3</sup></code></li> <li><code>1 &lt;= gifts[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>3</sup></code></li> </ul>
Easy
178.1K
235.3K
178,109
235,284
75.7%
['remove-stones-to-minimize-the-total']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long pickGifts(vector<int>& gifts, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long pickGifts(int[] gifts, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def pickGifts(self, gifts, k):\n \"\"\"\n :type gifts: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def pickGifts(self, gifts: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long pickGifts(int* gifts, int giftsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long PickGifts(int[] gifts, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} gifts\n * @param {number} k\n * @return {number}\n */\nvar pickGifts = function(gifts, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function pickGifts(gifts: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $gifts\n * @param Integer $k\n * @return Integer\n */\n function pickGifts($gifts, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func pickGifts(_ gifts: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun pickGifts(gifts: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int pickGifts(List<int> gifts, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func pickGifts(gifts []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} gifts\n# @param {Integer} k\n# @return {Integer}\ndef pick_gifts(gifts, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def pickGifts(gifts: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn pick_gifts(gifts: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (pick-gifts gifts k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec pick_gifts(Gifts :: [integer()], K :: integer()) -> integer().\npick_gifts(Gifts, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec pick_gifts(gifts :: [integer], k :: integer) :: integer\n def pick_gifts(gifts, k) do\n \n end\nend"}]
[25,64,9,4,100] 4
{ "name": "pickGifts", "params": [ { "name": "gifts", "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', 'Heap (Priority Queue)', 'Simulation']
2,559
Count Vowel Strings in Ranges
count-vowel-strings-in-ranges
<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a 2D array of integers <code>queries</code>.</p> <p>Each query <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> asks us to find the number of strings present at the indices ranging from <code>l<sub>i</sub></code> to <code>r<sub>i</sub></code> (both <strong>inclusive</strong>) of <code>words</code> that start and end with a vowel.</p> <p>Return <em>an array </em><code>ans</code><em> of size </em><code>queries.length</code><em>, where </em><code>ans[i]</code><em> is the answer to the </em><code>i</code><sup>th</sup><em> query</em>.</p> <p><strong>Note</strong> that the vowel letters are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;aba&quot;,&quot;bcb&quot;,&quot;ece&quot;,&quot;aa&quot;,&quot;e&quot;], queries = [[0,2],[1,4],[1,1]] <strong>Output:</strong> [2,3,0] <strong>Explanation:</strong> The strings starting and ending with a vowel are &quot;aba&quot;, &quot;ece&quot;, &quot;aa&quot; and &quot;e&quot;. The answer to the query [0,2] is 2 (strings &quot;aba&quot; and &quot;ece&quot;). to query [1,4] is 3 (strings &quot;ece&quot;, &quot;aa&quot;, &quot;e&quot;). to query [1,1] is 0. We return [2,3,0]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;a&quot;,&quot;e&quot;,&quot;i&quot;], queries = [[0,2],[0,1],[2,2]] <strong>Output:</strong> [3,2,1] <strong>Explanation:</strong> Every string satisfies the conditions, so we return [3,2,1].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 40</code></li> <li><code>words[i]</code> consists only of lowercase English letters.</li> <li><code>sum(words[i].length) &lt;= 3 * 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;&nbsp;words.length</code></li> </ul>
Medium
185.1K
272.6K
185,127
272,593
67.9%
['jump-game-vii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] vowelStrings(String[] words, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def vowelStrings(self, words, queries):\n \"\"\"\n :type words: List[str]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* vowelStrings(char** words, int wordsSize, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] VowelStrings(string[] words, int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar vowelStrings = function(words, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function vowelStrings(words: string[], queries: number[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function vowelStrings($words, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func vowelStrings(_ words: [String], _ queries: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun vowelStrings(words: Array<String>, queries: Array<IntArray>): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> vowelStrings(List<String> words, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func vowelStrings(words []string, queries [][]int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef vowel_strings(words, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def vowelStrings(words: Array[String], queries: Array[Array[Int]]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn vowel_strings(words: Vec<String>, queries: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (vowel-strings words queries)\n (-> (listof string?) (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec vowel_strings(Words :: [unicode:unicode_binary()], Queries :: [[integer()]]) -> [integer()].\nvowel_strings(Words, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec vowel_strings(words :: [String.t], queries :: [[integer]]) :: [integer]\n def vowel_strings(words, queries) do\n \n end\nend"}]
["aba","bcb","ece","aa","e"] [[0,2],[1,4],[1,1]]
{ "name": "vowelStrings", "params": [ { "name": "words", "type": "string[]" }, { "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', 'String', 'Prefix Sum']
2,560
House Robber IV
house-robber-iv
<p>There are several consecutive houses along a street, each of which has some money inside. There is also a robber, who wants to steal money from the homes, but he <strong>refuses to steal from adjacent homes</strong>.</p> <p>The <strong>capability</strong> of the robber is the maximum amount of money he steals from one house of all the houses he robbed.</p> <p>You are given an integer array <code>nums</code> representing how much money is stashed in each house. More formally, the <code>i<sup>th</sup></code> house from the left has <code>nums[i]</code> dollars.</p> <p>You are also given an integer <code>k</code>, representing the <strong>minimum</strong> number of houses the robber will steal from. It is always possible to steal at least <code>k</code> houses.</p> <p>Return <em>the <strong>minimum</strong> capability of the robber out of all the possible ways to steal at least </em><code>k</code><em> houses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,3,5,9], k = 2 <strong>Output:</strong> 5 <strong>Explanation:</strong> There are three ways to rob at least 2 houses: - Rob the houses at indices 0 and 2. Capability is max(nums[0], nums[2]) = 5. - Rob the houses at indices 0 and 3. Capability is max(nums[0], nums[3]) = 9. - Rob the houses at indices 1 and 3. Capability is max(nums[1], nums[3]) = 9. Therefore, we return min(5, 9, 9) = 5. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,7,9,3,1], k = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 7 ways to rob the houses. The way which leads to minimum capability is to rob the house at index 0 and 4. Return max(nums[0], nums[4]) = 2. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= k &lt;= (nums.length + 1)/2</code></li> </ul>
Medium
124.5K
191.1K
124,541
191,072
65.2%
['container-with-most-water', 'house-robber']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minCapability(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minCapability(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCapability(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 minCapability(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minCapability(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinCapability(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 minCapability = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCapability(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 minCapability($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCapability(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCapability(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCapability(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCapability(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef min_capability(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCapability(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_capability(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-capability nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_capability(Nums :: [integer()], K :: integer()) -> integer().\nmin_capability(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_capability(nums :: [integer], k :: integer) :: integer\n def min_capability(nums, k) do\n \n end\nend"}]
[2,3,5,9] 2
{ "name": "minCapability", "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', 'Binary Search']
2,561
Rearranging Fruits
rearranging-fruits
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p> <ul> <li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<font size="1">th</font>&nbsp;</code>fruit of <code>basket1</code> with the <code>j<font size="1">th</font></code>&nbsp;fruit of <code>basket2</code>.</li> <li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li> </ul> <p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p> <p>Return <em>the minimum cost to make both the baskets equal or </em><code>-1</code><em> if impossible.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2] <strong>Output:</strong> 1 <strong>Explanation:</strong> Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 = [4,1,2,2] and basket2 = [2,4,1,2]. Rearranging both the arrays makes them equal. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1] <strong>Output:</strong> -1 <strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>basket1.length == basket2.length</code></li> <li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= basket1[i],basket2[i]&nbsp;&lt;= 10<sup>9</sup></code></li> </ul>
Hard
11.8K
33.6K
11,754
33,650
34.9%
['the-latest-time-to-catch-a-bus', 'minimum-number-of-operations-to-make-arrays-similar']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minCost(vector<int>& basket1, vector<int>& basket2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minCost(int[] basket1, int[] basket2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCost(self, basket1, basket2):\n \"\"\"\n :type basket1: List[int]\n :type basket2: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCost(self, basket1: List[int], basket2: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minCost(int* basket1, int basket1Size, int* basket2, int basket2Size) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinCost(int[] basket1, int[] basket2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} basket1\n * @param {number[]} basket2\n * @return {number}\n */\nvar minCost = function(basket1, basket2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCost(basket1: number[], basket2: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $basket1\n * @param Integer[] $basket2\n * @return Integer\n */\n function minCost($basket1, $basket2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCost(_ basket1: [Int], _ basket2: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCost(basket1: IntArray, basket2: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCost(List<int> basket1, List<int> basket2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCost(basket1 []int, basket2 []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} basket1\n# @param {Integer[]} basket2\n# @return {Integer}\ndef min_cost(basket1, basket2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCost(basket1: Array[Int], basket2: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost(basket1: Vec<i32>, basket2: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost basket1 basket2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost(Basket1 :: [integer()], Basket2 :: [integer()]) -> integer().\nmin_cost(Basket1, Basket2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost(basket1 :: [integer], basket2 :: [integer]) :: integer\n def min_cost(basket1, basket2) do\n \n end\nend"}]
[4,2,2,2] [1,4,1,2]
{ "name": "minCost", "params": [ { "name": "basket1", "type": "integer[]" }, { "type": "integer[]", "name": "basket2" } ], "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', 'Greedy']
2,562
Find the Array Concatenation Value
find-the-array-concatenation-value
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>The <strong>concatenation</strong> of two numbers is the number formed by concatenating their numerals.</p> <ul> <li>For example, the concatenation of <code>15</code>, <code>49</code> is <code>1549</code>.</li> </ul> <p>The <strong>concatenation value</strong> of <code>nums</code> is initially equal to <code>0</code>. Perform this operation until <code>nums</code> becomes empty:</p> <ul> <li>If <code>nums</code> has a size greater than one, add the value of the concatenation of the first and the last element to the <strong>concatenation value</strong> of <code>nums</code>, and remove those two elements from <code>nums</code>. For example, if the <code>nums</code> was <code>[1, 2, 4, 5, 6]</code>, add 16 to the <code>concatenation value</code>.</li> <li>If only one element exists in <code>nums</code>, add its value to the <strong>concatenation value</strong> of <code>nums</code>, then remove it.</li> </ul> <p>Return<em> the concatenation value of <code>nums</code></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [7,52,2,4] <strong>Output:</strong> 596 <strong>Explanation:</strong> Before performing any operation, nums is [7,52,2,4] and concatenation value is 0. - In the first operation: We pick the first element, 7, and the last element, 4. Their concatenation is 74, and we add it to the concatenation value, so it becomes equal to 74. Then we delete them from nums, so nums becomes equal to [52,2]. - In the second operation: We pick the first element, 52, and the last element, 2. Their concatenation is 522, and we add it to the concatenation value, so it becomes equal to 596. Then we delete them from the nums, so nums becomes empty. Since the concatenation value is 596 so the answer is 596. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,14,13,8,12] <strong>Output:</strong> 673 <strong>Explanation:</strong> Before performing any operation, nums is [5,14,13,8,12] and concatenation value is 0. - In the first operation: We pick the first element, 5, and the last element, 12. Their concatenation is 512, and we add it to the concatenation value, so it becomes equal to 512. Then we delete them from the nums, so nums becomes equal to [14,13,8]. - In the second operation: We pick the first element, 14, and the last element, 8. Their concatenation is 148, and we add it to the concatenation value, so it becomes equal to 660. Then we delete them from the nums, so nums becomes equal to [13]. - In the third operation: nums has only one element, so we pick 13 and add it to the concatenation value, so it becomes equal to 673. Then we delete it from nums, so nums become empty. Since the concatenation value is 673 so the answer is 673. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;  } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Easy
58.5K
82.6K
58,473
82,631
70.8%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long findTheArrayConcVal(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long findTheArrayConcVal(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findTheArrayConcVal(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findTheArrayConcVal(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long findTheArrayConcVal(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long FindTheArrayConcVal(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findTheArrayConcVal = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findTheArrayConcVal(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function findTheArrayConcVal($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findTheArrayConcVal(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findTheArrayConcVal(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findTheArrayConcVal(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findTheArrayConcVal(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef find_the_array_conc_val(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findTheArrayConcVal(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_the_array_conc_val(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-the-array-conc-val nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_the_array_conc_val(Nums :: [integer()]) -> integer().\nfind_the_array_conc_val(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_the_array_conc_val(nums :: [integer]) :: integer\n def find_the_array_conc_val(nums) do\n \n end\nend"}]
[7,52,2,4]
{ "name": "findTheArrayConcVal", "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', 'Two Pointers', 'Simulation']
2,563
Count the Number of Fair Pairs
count-the-number-of-fair-pairs
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and two integers <code>lower</code> and <code>upper</code>, return <em>the number of fair pairs</em>.</p> <p>A pair <code>(i, j)</code> is <b>fair </b>if:</p> <ul> <li><code>0 &lt;= i &lt; j &lt; n</code>, and</li> <li><code>lower &lt;= nums[i] + nums[j] &lt;= upper</code></li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1,7,4,4,5], lower = 3, upper = 6 <strong>Output:</strong> 6 <strong>Explanation:</strong> There are 6 fair pairs: (0,3), (0,4), (0,5), (1,3), (1,4), and (1,5). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,7,9,2,5], lower = 11, upper = 11 <strong>Output:</strong> 1 <strong>Explanation:</strong> There is a single fair pair: (2,3). </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>nums.length == n</code></li> <li><code><font face="monospace">-10<sup>9</sup></font>&nbsp;&lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code><font face="monospace">-10<sup>9</sup>&nbsp;&lt;= lower &lt;= upper &lt;= 10<sup>9</sup></font></code></li> </ul>
Medium
130K
266.5K
129,958
266,506
48.8%
['count-of-range-sum', 'finding-pairs-with-a-certain-sum', 'count-number-of-pairs-with-absolute-difference-k', 'count-pairs-whose-sum-is-less-than-target']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countFairPairs(vector<int>& nums, int lower, int upper) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countFairPairs(int[] nums, int lower, int upper) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countFairPairs(self, nums, lower, upper):\n \"\"\"\n :type nums: List[int]\n :type lower: int\n :type upper: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countFairPairs(self, nums: List[int], lower: int, upper: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countFairPairs(int* nums, int numsSize, int lower, int upper) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountFairPairs(int[] nums, int lower, int upper) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} lower\n * @param {number} upper\n * @return {number}\n */\nvar countFairPairs = function(nums, lower, upper) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countFairPairs(nums: number[], lower: number, upper: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $lower\n * @param Integer $upper\n * @return Integer\n */\n function countFairPairs($nums, $lower, $upper) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countFairPairs(_ nums: [Int], _ lower: Int, _ upper: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countFairPairs(nums: IntArray, lower: Int, upper: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countFairPairs(List<int> nums, int lower, int upper) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countFairPairs(nums []int, lower int, upper int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} lower\n# @param {Integer} upper\n# @return {Integer}\ndef count_fair_pairs(nums, lower, upper)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countFairPairs(nums: Array[Int], lower: Int, upper: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_fair_pairs(nums: Vec<i32>, lower: i32, upper: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-fair-pairs nums lower upper)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_fair_pairs(Nums :: [integer()], Lower :: integer(), Upper :: integer()) -> integer().\ncount_fair_pairs(Nums, Lower, Upper) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_fair_pairs(nums :: [integer], lower :: integer, upper :: integer) :: integer\n def count_fair_pairs(nums, lower, upper) do\n \n end\nend"}]
[0,1,7,4,4,5] 3 6
{ "name": "countFairPairs", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "lower" }, { "type": "integer", "name": "upper" } ], "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', 'Two Pointers', 'Binary Search', 'Sorting']
2,564
Substring XOR Queries
substring-xor-queries
<p>You are given a <strong>binary string</strong> <code>s</code>, and a <strong>2D</strong> integer array <code>queries</code> where <code>queries[i] = [first<sub>i</sub>, second<sub>i</sub>]</code>.</p> <p>For the <code>i<sup>th</sup></code> query, find the <strong>shortest substring</strong> of <code>s</code> whose <strong>decimal value</strong>, <code>val</code>, yields <code>second<sub>i</sub></code> when <strong>bitwise XORed</strong> with <code>first<sub>i</sub></code>. In other words, <code>val ^ first<sub>i</sub> == second<sub>i</sub></code>.</p> <p>The answer to the <code>i<sup>th</sup></code> query is the endpoints (<strong>0-indexed</strong>) of the substring <code>[left<sub>i</sub>, right<sub>i</sub>]</code> or <code>[-1, -1]</code> if no such substring exists. If there are multiple answers, choose the one with the <strong>minimum</strong> <code>left<sub>i</sub></code>.</p> <p><em>Return an array</em> <code>ans</code> <em>where</em> <code>ans[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query.</em></p> <p>A <strong>substring</strong> is a contiguous non-empty sequence of characters within a string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;101101&quot;, queries = [[0,5],[1,2]] <strong>Output:</strong> [[0,2],[2,3]] <strong>Explanation:</strong> For the first query the substring in range <code>[0,2]</code> is <strong>&quot;101&quot;</strong> which has a decimal value of <strong><code>5</code></strong>, and <strong><code>5 ^ 0 = 5</code></strong>, hence the answer to the first query is <code>[0,2]</code>. In the second query, the substring in range <code>[2,3]</code> is <strong>&quot;11&quot;,</strong> and has a decimal value of <strong>3</strong>, and <strong>3<code> ^ 1 = 2</code></strong>.&nbsp;So, <code>[2,3]</code> is returned for the second query. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;0101&quot;, queries = [[12,8]] <strong>Output:</strong> [[-1,-1]] <strong>Explanation:</strong> In this example there is no substring that answers the query, hence <code>[-1,-1] is returned</code>. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;1&quot;, queries = [[4,5]] <strong>Output:</strong> [[0,0]] <strong>Explanation:</strong> For this example, the substring in range <code>[0,0]</code> has a decimal value of <strong><code>1</code></strong>, and <strong><code>1 ^ 4 = 5</code></strong>. So, the answer is <code>[0,0]</code>. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= first<sub>i</sub>, second<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Medium
14.6K
42.6K
14,619
42,633
34.3%
['string-matching-in-an-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[][] substringXorQueries(String s, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def substringXorQueries(self, s, queries):\n \"\"\"\n :type s: str\n :type queries: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def substringXorQueries(self, s: str, queries: 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** substringXorQueries(char* s, int** queries, int queriesSize, int* queriesColSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[][] SubstringXorQueries(string s, int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number[][]} queries\n * @return {number[][]}\n */\nvar substringXorQueries = function(s, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function substringXorQueries(s: string, queries: number[][]): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer[][] $queries\n * @return Integer[][]\n */\n function substringXorQueries($s, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func substringXorQueries(_ s: String, _ queries: [[Int]]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun substringXorQueries(s: String, queries: Array<IntArray>): Array<IntArray> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> substringXorQueries(String s, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func substringXorQueries(s string, queries [][]int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer[][]} queries\n# @return {Integer[][]}\ndef substring_xor_queries(s, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def substringXorQueries(s: String, queries: Array[Array[Int]]): Array[Array[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn substring_xor_queries(s: String, queries: Vec<Vec<i32>>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (substring-xor-queries s queries)\n (-> string? (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec substring_xor_queries(S :: unicode:unicode_binary(), Queries :: [[integer()]]) -> [[integer()]].\nsubstring_xor_queries(S, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec substring_xor_queries(s :: String.t, queries :: [[integer]]) :: [[integer]]\n def substring_xor_queries(s, queries) do\n \n end\nend"}]
"101101" [[0,5],[1,2]]
{ "name": "substringXorQueries", "params": [ { "name": "s", "type": "string" }, { "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', 'Hash Table', 'String', 'Bit Manipulation']
2,565
Subsequence With the Minimum Score
subsequence-with-the-minimum-score
<p>You are given two strings <code>s</code> and <code>t</code>.</p> <p>You are allowed to remove any number of characters from the string <code>t</code>.</p> <p>The score of the string is <code>0</code> if no characters are removed from the string <code>t</code>, otherwise:</p> <ul> <li>Let <code>left</code> be the minimum index among all removed characters.</li> <li>Let <code>right</code> be the maximum index among all removed characters.</li> </ul> <p>Then the score of the string is <code>right - left + 1</code>.</p> <p>Return <em>the minimum possible score to make </em><code>t</code><em>&nbsp;a subsequence of </em><code>s</code><em>.</em></p> <p>A <strong>subsequence</strong> of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., <code>&quot;ace&quot;</code> is a subsequence of <code>&quot;<u>a</u>b<u>c</u>d<u>e</u>&quot;</code> while <code>&quot;aec&quot;</code> is not).</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;abacaba&quot;, t = &quot;bzaa&quot; <strong>Output:</strong> 1 <strong>Explanation:</strong> In this example, we remove the character &quot;z&quot; at index 1 (0-indexed). The string t becomes &quot;baa&quot; which is a subsequence of the string &quot;abacaba&quot; and the score is 1 - 1 + 1 = 1. It can be proven that 1 is the minimum score that we can achieve. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;cde&quot;, t = &quot;xyz&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> In this example, we remove characters &quot;x&quot;, &quot;y&quot; and &quot;z&quot; at indices 0, 1, and 2 (0-indexed). The string t becomes &quot;&quot; which is a subsequence of the string &quot;cde&quot; and the score is 2 - 0 + 1 = 3. It can be proven that 3 is the minimum score that we can achieve. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> and <code>t</code> consist of only lowercase English letters.</li> </ul>
Hard
8.8K
27.3K
8,850
27,251
32.5%
['longest-common-subsequence']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumScore(string s, string t) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumScore(String s, String t) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumScore(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 minimumScore(self, s: str, t: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumScore(char* s, char* t) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumScore(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 minimumScore = function(s, t) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumScore(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 minimumScore($s, $t) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumScore(_ s: String, _ t: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumScore(s: String, t: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumScore(String s, String t) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumScore(s string, t string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} t\n# @return {Integer}\ndef minimum_score(s, t)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumScore(s: String, t: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_score(s: String, t: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-score s t)\n (-> string? string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_score(S :: unicode:unicode_binary(), T :: unicode:unicode_binary()) -> integer().\nminimum_score(S, T) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_score(s :: String.t, t :: String.t) :: integer\n def minimum_score(s, t) do\n \n end\nend"}]
"abacaba" "bzaa"
{ "name": "minimumScore", "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>"]}
['Two Pointers', 'String', 'Binary Search']
2,566
Maximum Difference by Remapping a Digit
maximum-difference-by-remapping-a-digit
<p>You are given an integer <code>num</code>. You know that Bob will sneakily <strong>remap</strong> one of the <code>10</code> possible digits (<code>0</code> to <code>9</code>) to another digit.</p> <p>Return <em>the difference between the maximum and minimum&nbsp;values Bob can make by remapping&nbsp;<strong>exactly</strong> <strong>one</strong> digit in </em><code>num</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>When Bob remaps a digit <font face="monospace">d1</font>&nbsp;to another digit <font face="monospace">d2</font>, Bob replaces all occurrences of <code>d1</code>&nbsp;in <code>num</code>&nbsp;with <code>d2</code>.</li> <li>Bob can remap a digit to itself, in which case <code>num</code>&nbsp;does not change.</li> <li>Bob can remap different digits for obtaining minimum and maximum values respectively.</li> <li>The resulting number after remapping can contain leading zeroes.</li> </ul> <p>&nbsp;</p> <p><strong>Example 1:</strong></p> <pre> <strong>Input:</strong> num = 11891 <strong>Output:</strong> 99009 <strong>Explanation:</strong> To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899. To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890. The difference between these two numbers is 99009. </pre> <p><strong>Example 2:</strong></p> <pre> <strong>Input:</strong> num = 90 <strong>Output:</strong> 99 <strong>Explanation:</strong> The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0). Thus, we return 99.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 10<sup>8</sup></code></li> </ul>
Easy
28.5K
47.2K
28,453
47,183
60.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minMaxDifference(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minMaxDifference(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMaxDifference(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minMaxDifference(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minMaxDifference(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinMaxDifference(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar minMaxDifference = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMaxDifference(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function minMaxDifference($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMaxDifference(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMaxDifference(num: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMaxDifference(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMaxDifference(num int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef min_max_difference(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMaxDifference(num: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_max_difference(num: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-max-difference num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_max_difference(Num :: integer()) -> integer().\nmin_max_difference(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_max_difference(num :: integer) :: integer\n def min_max_difference(num) do\n \n end\nend"}]
11891
{ "name": "minMaxDifference", "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']
2,567
Minimum Score by Changing Two Elements
minimum-score-by-changing-two-elements
<p>You are given an integer array <code>nums</code>.</p> <ul> <li>The <strong>low</strong> score of <code>nums</code> is the <strong>minimum</strong> absolute difference between any two integers.</li> <li>The <strong>high</strong> score of <code>nums</code> is the <strong>maximum</strong> absolute difference between any two integers.</li> <li>The <strong>score</strong> of <code>nums</code> is the sum of the <strong>high</strong> and <strong>low</strong> scores.</li> </ul> <p>Return the <strong>minimum score</strong> after <strong>changing two elements</strong> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,7,8,5]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Change <code>nums[0]</code> and <code>nums[1]</code> to be 6 so that <code>nums</code> becomes [6,6,7,8,5].</li> <li>The low score is the minimum absolute difference: |6 - 6| = 0.</li> <li>The high score is the maximum absolute difference: |8 - 5| = 3.</li> <li>The sum of high and low score is 3.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,4,3]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Change <code>nums[1]</code> and <code>nums[2]</code> to 1 so that <code>nums</code> becomes [1,1,1].</li> <li>The sum of maximum absolute difference and minimum absolute difference is 0.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
19.5K
40.1K
19,509
40,066
48.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimizeSum(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimizeSum(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimizeSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimizeSum(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimizeSum(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimizeSum(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minimizeSum = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimizeSum(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minimizeSum($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimizeSum(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimizeSum(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimizeSum(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimizeSum(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef minimize_sum(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimizeSum(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimize_sum(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimize-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimize_sum(Nums :: [integer()]) -> integer().\nminimize_sum(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimize_sum(nums :: [integer]) :: integer\n def minimize_sum(nums) do\n \n end\nend"}]
[1,4,7,8,5]
{ "name": "minimizeSum", "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', 'Greedy', 'Sorting']
2,568
Minimum Impossible OR
minimum-impossible-or
<p>You are given a <strong>0-indexed</strong>&nbsp;integer array <code>nums</code>.</p> <p>We say that an integer x is <strong>expressible</strong> from <code>nums</code> if there exist some integers <code>0 &lt;= index<sub>1</sub> &lt; index<sub>2</sub> &lt; ... &lt; index<sub>k</sub> &lt; nums.length</code> for which <code>nums[index<sub>1</sub>] | nums[index<sub>2</sub>] | ... | nums[index<sub>k</sub>] = x</code>. In other words, an integer is expressible if it can be written as the bitwise OR of some subsequence of <code>nums</code>.</p> <p>Return <em>the minimum <strong>positive non-zero integer</strong>&nbsp;that is not </em><em>expressible from </em><code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> 1 and 2 are already present in the array. We know that 3 is expressible, since nums[0] | nums[1] = 2 | 1 = 3. Since 4 is not expressible, we return 4. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,3,2] <strong>Output:</strong> 1 <strong>Explanation:</strong> We can show that 1 is the smallest number that is not expressible. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
17.6K
30.6K
17,613
30,602
57.6%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minImpossibleOR(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minImpossibleOR(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minImpossibleOR(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minImpossibleOR(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minImpossibleOR(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinImpossibleOR(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minImpossibleOR = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minImpossibleOR(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minImpossibleOR($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minImpossibleOR(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minImpossibleOR(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minImpossibleOR(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minImpossibleOR(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef min_impossible_or(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minImpossibleOR(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_impossible_or(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-impossible-or nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_impossible_or(Nums :: [integer()]) -> integer().\nmin_impossible_or(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_impossible_or(nums :: [integer]) :: integer\n def min_impossible_or(nums) do\n \n end\nend"}]
[2,1]
{ "name": "minImpossibleOR", "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', 'Bit Manipulation', 'Brainteaser']
2,569
Handling Sum Queries After Update
handling-sum-queries-after-update
<p>You are given two <strong>0-indexed</strong> arrays <code>nums1</code> and <code>nums2</code> and a 2D array <code>queries</code> of queries. There are three types of queries:</p> <ol> <li>For a query of type 1, <code>queries[i]&nbsp;= [1, l, r]</code>. Flip the values from <code>0</code> to <code>1</code> and from <code>1</code> to <code>0</code> in <code>nums1</code>&nbsp;from index <code>l</code> to index <code>r</code>. Both <code>l</code> and <code>r</code> are <strong>0-indexed</strong>.</li> <li>For a query of type 2, <code>queries[i]&nbsp;= [2, p, 0]</code>. For every index <code>0 &lt;= i &lt; n</code>, set&nbsp;<code>nums2[i] =&nbsp;nums2[i]&nbsp;+ nums1[i]&nbsp;* p</code>.</li> <li>For a query of type 3, <code>queries[i]&nbsp;= [3, 0, 0]</code>. Find the sum of the elements in <code>nums2</code>.</li> </ol> <p>Return <em>an array containing all the answers to the third type&nbsp;queries.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [1,0,1], nums2 = [0,0,0], queries = [[1,1,1],[2,1,0],[3,0,0]] <strong>Output:</strong> [3] <strong>Explanation:</strong> After the first query nums1 becomes [1,1,1]. After the second query, nums2 becomes [1,1,1], so the answer to the third query is 3. Thus, [3] is returned. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [1], nums2 = [5], queries = [[2,0,0],[3,0,0]] <strong>Output:</strong> [5] <strong>Explanation:</strong> After the first query, nums2 remains [5], so the answer to the second query is 5. Thus, [5] is returned. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length,nums2.length &lt;= 10<sup>5</sup></code></li> <li><code>nums1.length = nums2.length</code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code><font face="monospace">queries[i].length = 3</font></code></li> <li><code><font face="monospace">0 &lt;= l &lt;= r &lt;= nums1.length - 1</font></code></li> <li><code><font face="monospace">0 &lt;= p &lt;= 10<sup>6</sup></font></code></li> <li><code>0 &lt;= nums1[i] &lt;= 1</code></li> <li><code>0 &lt;= nums2[i] &lt;= 10<sup>9</sup></code></li> </ul>
Hard
6.6K
22.6K
6,556
22,561
29.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> handleQuery(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long[] handleQuery(int[] nums1, int[] nums2, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def handleQuery(self, nums1, nums2, queries):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def handleQuery(self, nums1: List[int], nums2: List[int], queries: List[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* handleQuery(int* nums1, int nums1Size, int* nums2, int nums2Size, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long[] HandleQuery(int[] nums1, int[] nums2, int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @param {number[][]} queries\n * @return {number[]}\n */\nvar handleQuery = function(nums1, nums2, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function handleQuery(nums1: number[], nums2: number[], queries: number[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums1\n * @param Integer[] $nums2\n * @param Integer[][] $queries\n * @return Integer[]\n */\n function handleQuery($nums1, $nums2, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func handleQuery(_ nums1: [Int], _ nums2: [Int], _ queries: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun handleQuery(nums1: IntArray, nums2: IntArray, queries: Array<IntArray>): LongArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> handleQuery(List<int> nums1, List<int> nums2, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func handleQuery(nums1 []int, nums2 []int, queries [][]int) []int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef handle_query(nums1, nums2, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def handleQuery(nums1: Array[Int], nums2: Array[Int], queries: Array[Array[Int]]): Array[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn handle_query(nums1: Vec<i32>, nums2: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (handle-query nums1 nums2 queries)\n (-> (listof exact-integer?) (listof exact-integer?) (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec handle_query(Nums1 :: [integer()], Nums2 :: [integer()], Queries :: [[integer()]]) -> [integer()].\nhandle_query(Nums1, Nums2, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec handle_query(nums1 :: [integer], nums2 :: [integer], queries :: [[integer]]) :: [integer]\n def handle_query(nums1, nums2, queries) do\n \n end\nend"}]
[1,0,1] [0,0,0] [[1,1,1],[2,1,0],[3,0,0]]
{ "name": "handleQuery", "params": [ { "name": "nums1", "type": "integer[]" }, { "type": "integer[]", "name": "nums2" }, { "type": "integer[][]", "name": "queries" } ], "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', 'Segment Tree']
2,570
Merge Two 2D Arrays by Summing Values
merge-two-2d-arrays-by-summing-values
<p>You are given two <strong>2D</strong> integer arrays <code>nums1</code> and <code>nums2.</code></p> <ul> <li><code>nums1[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li> <li><code>nums2[i] = [id<sub>i</sub>, val<sub>i</sub>]</code>&nbsp;indicate that the number with the id <code>id<sub>i</sub></code> has a value equal to <code>val<sub>i</sub></code>.</li> </ul> <p>Each array contains <strong>unique</strong> ids and is sorted in <strong>ascending</strong> order by id.</p> <p>Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:</p> <ul> <li>Only ids that appear in at least one of the two arrays should be included in the resulting array.</li> <li>Each id should be included <strong>only once</strong> and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be <code>0</code>.</li> </ul> <p>Return <em>the resulting array</em>. The returned array must be sorted in ascending order by id.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]] <strong>Output:</strong> [[1,6],[2,3],[3,2],[4,6]] <strong>Explanation:</strong> The resulting array contains the following: - id = 1, the value of this id is 2 + 4 = 6. - id = 2, the value of this id is 3. - id = 3, the value of this id is 2. - id = 4, the value of this id is 5 + 1 = 6. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]] <strong>Output:</strong> [[1,3],[2,4],[3,6],[4,3],[5,5]] <strong>Explanation:</strong> There are no common ids, so we just include each id with its value in the resulting list. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 200</code></li> <li><code>nums1[i].length == nums2[j].length == 2</code></li> <li><code>1 &lt;= id<sub>i</sub>, val<sub>i</sub> &lt;= 1000</code></li> <li>Both arrays contain unique ids.</li> <li>Both arrays are in&nbsp;strictly ascending order by id.</li> </ul>
Easy
190.6K
232.1K
190,566
232,123
82.1%
['merge-two-sorted-lists', 'meeting-scheduler', 'merge-similar-items']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[][] mergeArrays(int[][] nums1, int[][] nums2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def mergeArrays(self, nums1, nums2):\n \"\"\"\n :type nums1: List[List[int]]\n :type nums2: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def mergeArrays(self, nums1: List[List[int]], nums2: 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** mergeArrays(int** nums1, int nums1Size, int* nums1ColSize, int** nums2, int nums2Size, int* nums2ColSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[][] MergeArrays(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 mergeArrays = function(nums1, nums2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function mergeArrays(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 mergeArrays($nums1, $nums2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func mergeArrays(_ nums1: [[Int]], _ nums2: [[Int]]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> mergeArrays(List<List<int>> nums1, List<List<int>> nums2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func mergeArrays(nums1 [][]int, nums2 [][]int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} nums1\n# @param {Integer[][]} nums2\n# @return {Integer[][]}\ndef merge_arrays(nums1, nums2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def mergeArrays(nums1: Array[Array[Int]], nums2: Array[Array[Int]]): Array[Array[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn merge_arrays(nums1: Vec<Vec<i32>>, nums2: Vec<Vec<i32>>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (merge-arrays nums1 nums2)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec merge_arrays(Nums1 :: [[integer()]], Nums2 :: [[integer()]]) -> [[integer()]].\nmerge_arrays(Nums1, Nums2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec merge_arrays(nums1 :: [[integer]], nums2 :: [[integer]]) :: [[integer]]\n def merge_arrays(nums1, nums2) do\n \n end\nend"}]
[[1,2],[2,3],[4,5]] [[1,4],[3,2],[4,1]]
{ "name": "mergeArrays", "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', 'Hash Table', 'Two Pointers']
2,571
Minimum Operations to Reduce an Integer to 0
minimum-operations-to-reduce-an-integer-to-0
<p>You are given a positive integer <code>n</code>, you can do the following operation <strong>any</strong> number of times:</p> <ul> <li>Add or subtract a <strong>power</strong> of <code>2</code> from <code>n</code>.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>n</code><em> equal to </em><code>0</code>.</p> <p>A number <code>x</code> is power of <code>2</code> if <code>x == 2<sup>i</sup></code>&nbsp;where <code>i &gt;= 0</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 39 <strong>Output:</strong> 3 <strong>Explanation:</strong> We can do the following operations: - Add 2<sup>0</sup> = 1 to n, so now n = 40. - Subtract 2<sup>3</sup> = 8 from n, so now n = 32. - Subtract 2<sup>5</sup> = 32 from n, so now n = 0. It can be shown that 3 is the minimum number of operations we need to make n equal to 0. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 54 <strong>Output:</strong> 3 <strong>Explanation:</strong> We can do the following operations: - Add 2<sup>1</sup> = 2 to n, so now n = 56. - Add 2<sup>3</sup> = 8 to n, so now n = 64. - Subtract 2<sup>6</sup> = 64 from n, so now n = 0. So the minimum number of operations is 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Medium
34.6K
60.9K
34,620
60,882
56.9%
['plus-one']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minOperations(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minOperations(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minOperations(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minOperations(self, n: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minOperations(int n) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinOperations(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number}\n */\nvar minOperations = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minOperations(n: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function minOperations($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minOperations(_ n: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minOperations(n: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minOperations(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minOperations(n int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer}\ndef min_operations(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minOperations(n: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_operations(n: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-operations n)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_operations(N :: integer()) -> integer().\nmin_operations(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_operations(n :: integer) :: integer\n def min_operations(n) do\n \n end\nend"}]
39
{ "name": "minOperations", "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>"]}
['Dynamic Programming', 'Greedy', 'Bit Manipulation']
2,572
Count the Number of Square-Free Subsets
count-the-number-of-square-free-subsets
<p>You are given a positive integer <strong>0-indexed</strong>&nbsp;array <code>nums</code>.</p> <p>A subset of the array <code>nums</code> is <strong>square-free</strong> if the product of its elements is a <strong>square-free integer</strong>.</p> <p>A <strong>square-free integer</strong> is an integer that is divisible by no square number other than <code>1</code>.</p> <p>Return <em>the number of square-free non-empty subsets of the array</em> <strong>nums</strong>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>A <strong>non-empty</strong>&nbsp;<strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none but not all) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,4,4,5] <strong>Output:</strong> 3 <strong>Explanation:</strong> There are 3 square-free subsets in this example: - The subset consisting of the 0<sup>th</sup> element [3]. The product of its elements is 3, which is a square-free integer. - The subset consisting of the 3<sup>rd</sup> element [5]. The product of its elements is 5, which is a square-free integer. - The subset consisting of 0<sup>th</sup> and 3<sup>rd</sup> elements [3,5]. The product of its elements is 15, which is a square-free integer. It can be proven that there are no more than 3 square-free subsets in the given array.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1] <strong>Output:</strong> 1 <strong>Explanation:</strong> There is 1 square-free subset in this example: - The subset consisting of the 0<sup>th</sup> element [1]. The product of its elements is 1, which is a square-free integer. It can be proven that there is no more than 1 square-free subset in the given array. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length&nbsp;&lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 30</code></li> </ul>
Medium
11.5K
46.8K
11,545
46,813
24.7%
['distinct-prime-factors-of-product-of-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int squareFreeSubsets(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int squareFreeSubsets(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def squareFreeSubsets(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def squareFreeSubsets(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int squareFreeSubsets(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int SquareFreeSubsets(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar squareFreeSubsets = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function squareFreeSubsets(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function squareFreeSubsets($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func squareFreeSubsets(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun squareFreeSubsets(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int squareFreeSubsets(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func squareFreeSubsets(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef square_free_subsets(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def squareFreeSubsets(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn square_free_subsets(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (square-free-subsets nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec square_free_subsets(Nums :: [integer()]) -> integer().\nsquare_free_subsets(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec square_free_subsets(nums :: [integer]) :: integer\n def square_free_subsets(nums) do\n \n end\nend"}]
[3,4,4,5]
{ "name": "squareFreeSubsets", "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', 'Math', 'Dynamic Programming', 'Bit Manipulation', 'Bitmask']
2,573
Find the String with LCP
find-the-string-with-lcp
<p>We define the <code>lcp</code> matrix of any <strong>0-indexed</strong> string <code>word</code> of <code>n</code> lowercase English letters as an <code>n x n</code> grid such that:</p> <ul> <li><code>lcp[i][j]</code> is equal to the length of the <strong>longest common prefix</strong> between the substrings <code>word[i,n-1]</code> and <code>word[j,n-1]</code>.</li> </ul> <p>Given an&nbsp;<code>n x n</code> matrix <code>lcp</code>, return the alphabetically smallest string <code>word</code> that corresponds to <code>lcp</code>. If there is no such string, return an empty string.</p> <p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears earlier in the alphabet than the corresponding letter in <code>b</code>. For example, <code>&quot;aabd&quot;</code> is lexicographically smaller than <code>&quot;aaca&quot;</code> because the first position they differ is at the third letter, and <code>&#39;b&#39;</code> comes before <code>&#39;c&#39;</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> lcp = [[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]] <strong>Output:</strong> &quot;abab&quot; <strong>Explanation:</strong> lcp corresponds to any 4 letter string with two alternating letters. The lexicographically smallest of them is &quot;abab&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]] <strong>Output:</strong> &quot;aaaa&quot; <strong>Explanation:</strong> lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is &quot;aaaa&quot;. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,3]] <strong>Output:</strong> &quot;&quot; <strong>Explanation:</strong> lcp[3][3] cannot be equal to 3 since word[3,...,3] consists of only a single letter; Thus, no answer exists. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n ==&nbsp;</code><code>lcp.length == </code><code>lcp[i].length</code>&nbsp;<code>&lt;= 1000</code></li> <li><code><font face="monospace">0 &lt;= lcp[i][j] &lt;= n</font></code></li> </ul>
Hard
6K
18.9K
5,954
18,921
31.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string findTheString(vector<vector<int>>& lcp) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String findTheString(int[][] lcp) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findTheString(self, lcp):\n \"\"\"\n :type lcp: List[List[int]]\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findTheString(self, lcp: List[List[int]]) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* findTheString(int** lcp, int lcpSize, int* lcpColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string FindTheString(int[][] lcp) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} lcp\n * @return {string}\n */\nvar findTheString = function(lcp) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findTheString(lcp: number[][]): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $lcp\n * @return String\n */\n function findTheString($lcp) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findTheString(_ lcp: [[Int]]) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findTheString(lcp: Array<IntArray>): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String findTheString(List<List<int>> lcp) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findTheString(lcp [][]int) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} lcp\n# @return {String}\ndef find_the_string(lcp)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findTheString(lcp: Array[Array[Int]]): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_the_string(lcp: Vec<Vec<i32>>) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-the-string lcp)\n (-> (listof (listof exact-integer?)) string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_the_string(Lcp :: [[integer()]]) -> unicode:unicode_binary().\nfind_the_string(Lcp) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_the_string(lcp :: [[integer]]) :: String.t\n def find_the_string(lcp) do\n \n end\nend"}]
[[4,0,2,0],[0,3,0,1],[2,0,2,0],[0,1,0,1]]
{ "name": "findTheString", "params": [ { "name": "lcp", "type": "integer[][]" } ], "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', 'String', 'Dynamic Programming', 'Greedy', 'Union Find', 'Matrix']
2,574
Left and Right Sum Differences
left-and-right-sum-differences
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code>.</p> <p>Define two arrays <code>leftSum</code> and <code>rightSum</code> where:</p> <ul> <li><code>leftSum[i]</code> is the sum of elements to the left of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>leftSum[i] = 0</code>.</li> <li><code>rightSum[i]</code> is the sum of elements to the right of the index <code>i</code> in the array <code>nums</code>. If there is no such element, <code>rightSum[i] = 0</code>.</li> </ul> <p>Return an integer array <code>answer</code> of size <code>n</code> where <code>answer[i] = |leftSum[i] - rightSum[i]|</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [10,4,8,3] <strong>Output:</strong> [15,1,11,22] <strong>Explanation:</strong> The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0]. The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1] <strong>Output:</strong> [0] <strong>Explanation:</strong> The array leftSum is [0] and the array rightSum is [0]. The array answer is [|0 - 0|] = [0]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> </ul>
Easy
174.1K
199.3K
174,092
199,340
87.3%
['find-pivot-index', 'find-the-middle-index-in-array', 'find-the-distinct-difference-array', 'find-the-n-th-value-after-k-seconds']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> leftRightDifference(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] leftRightDifference(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def leftRightDifference(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def leftRightDifference(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* leftRightDifference(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] LeftRightDifference(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar leftRightDifference = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function leftRightDifference(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function leftRightDifference($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func leftRightDifference(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun leftRightDifference(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> leftRightDifference(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func leftRightDifference(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef left_right_difference(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def leftRightDifference(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn left_right_difference(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (left-right-difference nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec left_right_difference(Nums :: [integer()]) -> [integer()].\nleft_right_difference(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec left_right_difference(nums :: [integer]) :: [integer]\n def left_right_difference(nums) do\n \n end\nend"}]
[10,4,8,3]
{ "name": "leftRightDifference", "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', 'Prefix Sum']
2,575
Find the Divisibility Array of a String
find-the-divisibility-array-of-a-string
<p>You are given a <strong>0-indexed</strong> string <code>word</code> of length <code>n</code>&nbsp;consisting of digits, and a positive integer&nbsp;<code>m</code>.</p> <p>The <strong>divisibility array</strong> <code>div</code> of <code>word</code> is an integer array of length <code>n</code> such that:</p> <ul> <li><code>div[i] = 1</code> if the&nbsp;<strong>numeric value</strong>&nbsp;of&nbsp;<code>word[0,...,i]</code> is divisible by <code>m</code>, or</li> <li><code>div[i] = 0</code> otherwise.</li> </ul> <p>Return<em> the divisibility array of</em><em> </em><code>word</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word = &quot;998244353&quot;, m = 3 <strong>Output:</strong> [1,1,0,0,0,1,1,0,0] <strong>Explanation:</strong> There are only 4 prefixes that are divisible by 3: &quot;9&quot;, &quot;99&quot;, &quot;998244&quot;, and &quot;9982443&quot;. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word = &quot;1010&quot;, m = 10 <strong>Output:</strong> [0,1,0,1] <strong>Explanation:</strong> There are only 2 prefixes that are divisible by 10: &quot;10&quot;, and &quot;1010&quot;. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code><font face="monospace">word.length == n</font></code></li> <li><code><font face="monospace">word</font></code><font face="monospace"> consists of digits from <code>0</code>&nbsp;to <code>9</code></font></li> <li><code><font face="monospace">1 &lt;= m &lt;= 10<sup>9</sup></font></code></li> </ul>
Medium
33.3K
97.1K
33,322
97,124
34.3%
['subarray-sums-divisible-by-k', 'make-sum-divisible-by-p']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> divisibilityArray(string word, int m) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] divisibilityArray(String word, int m) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def divisibilityArray(self, word, m):\n \"\"\"\n :type word: str\n :type m: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def divisibilityArray(self, word: str, m: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* divisibilityArray(char* word, int m, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] DivisibilityArray(string word, int m) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} word\n * @param {number} m\n * @return {number[]}\n */\nvar divisibilityArray = function(word, m) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function divisibilityArray(word: string, m: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $word\n * @param Integer $m\n * @return Integer[]\n */\n function divisibilityArray($word, $m) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func divisibilityArray(_ word: String, _ m: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun divisibilityArray(word: String, m: Int): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> divisibilityArray(String word, int m) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func divisibilityArray(word string, m int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} word\n# @param {Integer} m\n# @return {Integer[]}\ndef divisibility_array(word, m)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def divisibilityArray(word: String, m: Int): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (divisibility-array word m)\n (-> string? exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec divisibility_array(Word :: unicode:unicode_binary(), M :: integer()) -> [integer()].\ndivisibility_array(Word, M) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec divisibility_array(word :: String.t, m :: integer) :: [integer]\n def divisibility_array(word, m) do\n \n end\nend"}]
"998244353" 3
{ "name": "divisibilityArray", "params": [ { "name": "word", "type": "string" }, { "type": "integer", "name": "m" } ], "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']
2,576
Find the Maximum Number of Marked Indices
find-the-maximum-number-of-marked-indices
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p> <p>Initially, all of the indices are unmarked. You are allowed to make this operation any number of times:</p> <ul> <li>Pick two <strong>different unmarked</strong> indices <code>i</code> and <code>j</code> such that <code>2 * nums[i] &lt;= nums[j]</code>, then mark <code>i</code> and <code>j</code>.</li> </ul> <p>Return <em>the maximum possible number of marked indices in <code>nums</code> using the above operation any number of times</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,5,2,4] <strong>Output:</strong> 2 <strong>Explanation: </strong>In the first operation: pick i = 2 and j = 1, the operation is allowed because 2 * nums[2] &lt;= nums[1]. Then mark index 2 and 1. It can be shown that there&#39;s no other valid operation so the answer is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [9,2,5,4] <strong>Output:</strong> 4 <strong>Explanation: </strong>In the first operation: pick i = 3 and j = 0, the operation is allowed because 2 * nums[3] &lt;= nums[0]. Then mark index 3 and 0. In the second operation: pick i = 1 and j = 2, the operation is allowed because 2 * nums[1] &lt;= nums[2]. Then mark index 1 and 2. Since there is no other operation, the answer is 4. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [7,6,8] <strong>Output:</strong> 0 <strong>Explanation: </strong>There is no valid operation to do, so the answer is 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Medium
23K
57.9K
22,979
57,933
39.7%
['minimum-array-length-after-pair-removals']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxNumOfMarkedIndices(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxNumOfMarkedIndices(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxNumOfMarkedIndices(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxNumOfMarkedIndices(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxNumOfMarkedIndices(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxNumOfMarkedIndices(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxNumOfMarkedIndices = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxNumOfMarkedIndices(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxNumOfMarkedIndices($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxNumOfMarkedIndices(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxNumOfMarkedIndices(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxNumOfMarkedIndices(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxNumOfMarkedIndices(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_num_of_marked_indices(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxNumOfMarkedIndices(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_num_of_marked_indices(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-num-of-marked-indices nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_num_of_marked_indices(Nums :: [integer()]) -> integer().\nmax_num_of_marked_indices(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_num_of_marked_indices(nums :: [integer]) :: integer\n def max_num_of_marked_indices(nums) do\n \n end\nend"}]
[3,5,2,4]
{ "name": "maxNumOfMarkedIndices", "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', 'Binary Search', 'Greedy', 'Sorting']
2,577
Minimum Time to Visit a Cell In a Grid
minimum-time-to-visit-a-cell-in-a-grid
<p>You are given a <code>m x n</code> matrix <code>grid</code> consisting of <b>non-negative</b> integers where <code>grid[row][col]</code> represents the <strong>minimum</strong> time required to be able to visit the cell <code>(row, col)</code>, which means you can visit the cell <code>(row, col)</code> only when the time you visit it is greater than or equal to <code>grid[row][col]</code>.</p> <p>You are standing in the <strong>top-left</strong> cell of the matrix in the <code>0<sup>th</sup></code> second, and you must move to <strong>any</strong> adjacent cell in the four directions: up, down, left, and right. Each move you make takes 1 second.</p> <p>Return <em>the <strong>minimum</strong> time required in which you can visit the bottom-right cell of the matrix</em>. If you cannot visit the bottom-right cell, then return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-8.png" /></p> <pre> <strong>Input:</strong> grid = [[0,1,3,2],[5,1,2,5],[4,3,8,6]] <strong>Output:</strong> 7 <strong>Explanation:</strong> One of the paths that we can take is the following: - at t = 0, we are on the cell (0,0). - at t = 1, we move to the cell (0,1). It is possible because grid[0][1] &lt;= 1. - at t = 2, we move to the cell (1,1). It is possible because grid[1][1] &lt;= 2. - at t = 3, we move to the cell (1,2). It is possible because grid[1][2] &lt;= 3. - at t = 4, we move to the cell (1,1). It is possible because grid[1][1] &lt;= 4. - at t = 5, we move to the cell (1,2). It is possible because grid[1][2] &lt;= 5. - at t = 6, we move to the cell (1,3). It is possible because grid[1][3] &lt;= 6. - at t = 7, we move to the cell (2,3). It is possible because grid[2][3] &lt;= 7. The final time is 7. It can be shown that it is the minimum time possible. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2023/02/14/yetgriddrawio-9.png" style="width: 151px; height: 151px;" /></p> <pre> <strong>Input:</strong> grid = [[0,2,4],[3,2,1],[1,0,4]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There is no path from the top left to the bottom-right cell. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == grid.length</code></li> <li><code>n == grid[i].length</code></li> <li><code>2 &lt;= m, n &lt;= 1000</code></li> <li><code>4 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li> <li><code>grid[0][0] == 0</code></li> </ul> <p>&nbsp;</p> <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0; } .spoiler {overflow:hidden;} .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;} .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;} .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;} </style>
Hard
81.8K
143.4K
81,755
143,372
57.0%
['find-minimum-time-to-reach-last-room-i', 'find-minimum-time-to-reach-last-room-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumTime(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumTime(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumTime(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumTime(self, grid: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumTime(int** grid, int gridSize, int* gridColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumTime(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar minimumTime = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumTime(grid: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer\n */\n function minimumTime($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumTime(_ grid: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumTime(grid: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumTime(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumTime(grid [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Integer}\ndef minimum_time(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumTime(grid: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_time(grid: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-time grid)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_time(Grid :: [[integer()]]) -> integer().\nminimum_time(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_time(grid :: [[integer]]) :: integer\n def minimum_time(grid) do\n \n end\nend"}]
[[0,1,3,2],[5,1,2,5],[4,3,8,6]]
{ "name": "minimumTime", "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', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path']
2,578
Split With Minimum Sum
split-with-minimum-sum
<p>Given a positive integer <code>num</code>, split it into two non-negative integers <code>num1</code> and <code>num2</code> such that:</p> <ul> <li>The concatenation of <code>num1</code> and <code>num2</code> is a permutation of <code>num</code>. <ul> <li>In other words, the sum of the number of occurrences of each digit in <code>num1</code> and <code>num2</code> is equal to the number of occurrences of that digit in <code>num</code>.</li> </ul> </li> <li><code>num1</code> and <code>num2</code> can contain leading zeros.</li> </ul> <p>Return <em>the <strong>minimum</strong> possible sum of</em> <code>num1</code> <em>and</em> <code>num2</code>.</p> <p><strong>Notes:</strong></p> <ul> <li>It is guaranteed that <code>num</code> does not contain any leading zeros.</li> <li>The order of occurrence of the digits in <code>num1</code> and <code>num2</code> may differ from the order of occurrence of <code>num</code>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> num = 4325 <strong>Output:</strong> 59 <strong>Explanation:</strong> We can split 4325 so that <code>num1</code> is 24 and <code>num2</code> is 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> num = 687 <strong>Output:</strong> 75 <strong>Explanation:</strong> We can split 687 so that <code>num1</code> is 68 and <code>num2</code> is 7, which would give an optimal sum of 75. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>10 &lt;= num &lt;= 10<sup>9</sup></code></li> </ul>
Easy
43.4K
60.3K
43,360
60,333
71.9%
['partition-equal-subset-sum', 'minimum-cost-to-move-chips-to-the-same-position', 'partition-array-into-two-arrays-to-minimize-sum-difference', 'minimum-sum-of-values-by-dividing-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int splitNum(int num) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int splitNum(int num) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def splitNum(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def splitNum(self, num: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int splitNum(int num) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int SplitNum(int num) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} num\n * @return {number}\n */\nvar splitNum = function(num) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function splitNum(num: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $num\n * @return Integer\n */\n function splitNum($num) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func splitNum(_ num: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun splitNum(num: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int splitNum(int num) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func splitNum(num int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num\n# @return {Integer}\ndef split_num(num)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def splitNum(num: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn split_num(num: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (split-num num)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec split_num(Num :: integer()) -> integer().\nsplit_num(Num) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec split_num(num :: integer) :: integer\n def split_num(num) do\n \n end\nend"}]
4325
{ "name": "splitNum", "params": [ { "name": "num", "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>"]}
['Math', 'Greedy', 'Sorting']
2,579
Count Total Number of Colored Cells
count-total-number-of-colored-cells
<p>There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer <code>n</code>, indicating that you must do the following routine for <code>n</code> minutes:</p> <ul> <li>At the first minute, color <strong>any</strong> arbitrary unit cell blue.</li> <li>Every minute thereafter, color blue <strong>every</strong> uncolored cell that touches a blue cell.</li> </ul> <p>Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.</p> <img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;" /> <p>Return <em>the number of <strong>colored cells</strong> at the end of </em><code>n</code> <em>minutes</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> After 1 minute, there is only 1 blue cell, so we return 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 2 <strong>Output:</strong> 5 <strong>Explanation:</strong> After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Medium
194.6K
294K
194,638
294,012
66.2%
['minimum-cuts-to-divide-a-circle']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long coloredCells(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long coloredCells(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def coloredCells(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def coloredCells(self, n: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long coloredCells(int n) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long ColoredCells(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number}\n */\nvar coloredCells = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function coloredCells(n: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer\n */\n function coloredCells($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func coloredCells(_ n: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun coloredCells(n: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int coloredCells(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func coloredCells(n int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer}\ndef colored_cells(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def coloredCells(n: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn colored_cells(n: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (colored-cells n)\n (-> exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec colored_cells(N :: integer()) -> integer().\ncolored_cells(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec colored_cells(n :: integer) :: integer\n def colored_cells(n) do\n \n end\nend"}]
1
{ "name": "coloredCells", "params": [ { "name": "n", "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,580
Count Ways to Group Overlapping Ranges
count-ways-to-group-overlapping-ranges
<p>You are given a 2D integer array <code>ranges</code> where <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> denotes that all integers between <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> (both <strong>inclusive</strong>) are contained in the <code>i<sup>th</sup></code> range.</p> <p>You are to split <code>ranges</code> into <strong>two</strong> (possibly empty) groups such that:</p> <ul> <li>Each range belongs to exactly one group.</li> <li>Any two <strong>overlapping</strong> ranges must belong to the <strong>same</strong> group.</li> </ul> <p>Two ranges are said to be <strong>overlapping</strong>&nbsp;if there exists at least <strong>one</strong> integer that is present in both ranges.</p> <ul> <li>For example, <code>[1, 3]</code> and <code>[2, 5]</code> are overlapping because <code>2</code> and <code>3</code> occur in both ranges.</li> </ul> <p>Return <em>the <strong>total number</strong> of ways to split</em> <code>ranges</code> <em>into two groups</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> ranges = [[6,10],[5,15]] <strong>Output:</strong> 2 <strong>Explanation:</strong> The two ranges are overlapping, so they must be in the same group. Thus, there are two possible ways: - Put both the ranges together in group 1. - Put both the ranges together in group 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> ranges = [[1,3],[10,20],[2,5],[4,8]] <strong>Output:</strong> 4 <strong>Explanation:</strong> Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group. Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group. Thus, there are four possible ways to group them: - All the ranges in group 1. - All the ranges in group 2. - Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2. - Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ranges.length &lt;= 10<sup>5</sup></code></li> <li><code>ranges[i].length == 2</code></li> <li><code>0 &lt;= start<sub>i</sub> &lt;= end<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Medium
19.9K
52.6K
19,871
52,629
37.8%
['merge-intervals']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countWays(vector<vector<int>>& ranges) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countWays(int[][] ranges) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countWays(self, ranges):\n \"\"\"\n :type ranges: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countWays(self, ranges: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countWays(int** ranges, int rangesSize, int* rangesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountWays(int[][] ranges) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} ranges\n * @return {number}\n */\nvar countWays = function(ranges) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countWays(ranges: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $ranges\n * @return Integer\n */\n function countWays($ranges) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countWays(_ ranges: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countWays(ranges: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countWays(List<List<int>> ranges) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countWays(ranges [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} ranges\n# @return {Integer}\ndef count_ways(ranges)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countWays(ranges: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_ways(ranges: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-ways ranges)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_ways(Ranges :: [[integer()]]) -> integer().\ncount_ways(Ranges) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_ways(ranges :: [[integer]]) :: integer\n def count_ways(ranges) do\n \n end\nend"}]
[[6,10],[5,15]]
{ "name": "countWays", "params": [ { "name": "ranges", "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']
2,581
Count Number of Possible Root Nodes
count-number-of-possible-root-nodes
<p>Alice has an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The tree is represented as a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p> <p>Alice wants Bob to find the root of the tree. She allows Bob to make several <strong>guesses</strong> about her tree. In one guess, he does the following:</p> <ul> <li>Chooses two <strong>distinct</strong> integers <code>u</code> and <code>v</code> such that there exists an edge <code>[u, v]</code> in the tree.</li> <li>He tells Alice that <code>u</code> is the <strong>parent</strong> of <code>v</code> in the tree.</li> </ul> <p>Bob&#39;s guesses are represented by a 2D integer array <code>guesses</code> where <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates Bob guessed <code>u<sub>j</sub></code> to be the parent of <code>v<sub>j</sub></code>.</p> <p>Alice being lazy, does not reply to each of Bob&#39;s guesses, but just says that <strong>at least</strong> <code>k</code> of his guesses are <code>true</code>.</p> <p>Given the 2D integer arrays <code>edges</code>, <code>guesses</code> and the integer <code>k</code>, return <em>the <strong>number of possible nodes</strong> that can be the root of Alice&#39;s tree</em>. If there is no such tree, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p> <pre> <strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3 <strong>Output:</strong> 3 <strong>Explanation:</strong> Root = 0, correct guesses = [1,3], [0,1], [2,4] Root = 1, correct guesses = [1,3], [1,0], [2,4] Root = 2, correct guesses = [1,3], [1,0], [2,4] Root = 3, correct guesses = [1,0], [2,4] Root = 4, correct guesses = [1,3], [1,0] Considering 0, 1, or 2 as root node leads to 3 correct guesses. </pre> <p><strong class="example">Example 2:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p> <pre> <strong>Input:</strong> edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1 <strong>Output:</strong> 5 <strong>Explanation:</strong> Root = 0, correct guesses = [3,4] Root = 1, correct guesses = [1,0], [3,4] Root = 2, correct guesses = [1,0], [2,1], [3,4] Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4] Root = 4, correct guesses = [1,0], [2,1], [3,2] Considering any node as root will give at least 1 correct guess. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>edges.length == n - 1</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= guesses.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> &lt;= n - 1</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>u<sub>j</sub> != v<sub>j</sub></code></li> <li><code>edges</code> represents a valid tree.</li> <li><code>guesses[j]</code> is an edge of the tree.</li> <li><code>guesses</code> is unique.</li> <li><code>0 &lt;= k &lt;= guesses.length</code></li> </ul>
Hard
7.5K
16.3K
7,504
16,313
46.0%
['closest-node-to-path-in-tree']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int rootCount(vector<vector<int>>& edges, vector<vector<int>>& guesses, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int rootCount(int[][] edges, int[][] guesses, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def rootCount(self, edges, guesses, k):\n \"\"\"\n :type edges: List[List[int]]\n :type guesses: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def rootCount(self, edges: List[List[int]], guesses: List[List[int]], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int rootCount(int** edges, int edgesSize, int* edgesColSize, int** guesses, int guessesSize, int* guessesColSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int RootCount(int[][] edges, int[][] guesses, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} edges\n * @param {number[][]} guesses\n * @param {number} k\n * @return {number}\n */\nvar rootCount = function(edges, guesses, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function rootCount(edges: number[][], guesses: number[][], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $edges\n * @param Integer[][] $guesses\n * @param Integer $k\n * @return Integer\n */\n function rootCount($edges, $guesses, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func rootCount(_ edges: [[Int]], _ guesses: [[Int]], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun rootCount(edges: Array<IntArray>, guesses: Array<IntArray>, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int rootCount(List<List<int>> edges, List<List<int>> guesses, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func rootCount(edges [][]int, guesses [][]int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} edges\n# @param {Integer[][]} guesses\n# @param {Integer} k\n# @return {Integer}\ndef root_count(edges, guesses, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def rootCount(edges: Array[Array[Int]], guesses: Array[Array[Int]], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn root_count(edges: Vec<Vec<i32>>, guesses: Vec<Vec<i32>>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (root-count edges guesses k)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec root_count(Edges :: [[integer()]], Guesses :: [[integer()]], K :: integer()) -> integer().\nroot_count(Edges, Guesses, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec root_count(edges :: [[integer]], guesses :: [[integer]], k :: integer) :: integer\n def root_count(edges, guesses, k) do\n \n end\nend"}]
[[0,1],[1,2],[1,3],[4,2]] [[1,3],[0,1],[1,0],[2,4]] 3
{ "name": "rootCount", "params": [ { "name": "edges", "type": "integer[][]" }, { "type": "integer[][]", "name": "guesses" }, { "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', 'Dynamic Programming', 'Tree', 'Depth-First Search']
2,582
Pass the Pillow
pass-the-pillow
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.</p> <ul> <li>For example, once the pillow reaches the <code>n<sup>th</sup></code> person they pass it to the <code>n - 1<sup>th</sup></code> person, then to the <code>n - 2<sup>th</sup></code> person and so on.</li> </ul> <p>Given the two positive integers <code>n</code> and <code>time</code>, return <em>the index of the person holding the pillow after </em><code>time</code><em> seconds</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 4, time = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 3 -&gt; 2. After five seconds, the 2<sup>nd</sup> person is holding the pillow. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> n = 3, time = 2 <strong>Output:</strong> 3 <strong>Explanation:</strong> People pass the pillow in the following way: 1 -&gt; 2 -&gt; 3. After two seconds, the 3<sup>r</sup><sup>d</sup> person is holding the pillow. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= time &lt;= 1000</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/find-the-child-who-has-the-ball-after-k-seconds/description/" target="_blank"> 3178: Find the Child Who Has the Ball After K Seconds.</a></p>
Easy
202.9K
358.7K
202,926
358,732
56.6%
['find-the-student-that-will-replace-the-chalk']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int passThePillow(int n, int time) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int passThePillow(int n, int time) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def passThePillow(self, n, time):\n \"\"\"\n :type n: int\n :type time: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def passThePillow(self, n: int, time: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int passThePillow(int n, int time) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int PassThePillow(int n, int time) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number} time\n * @return {number}\n */\nvar passThePillow = function(n, time) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function passThePillow(n: number, time: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $time\n * @return Integer\n */\n function passThePillow($n, $time) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func passThePillow(_ n: Int, _ time: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun passThePillow(n: Int, time: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int passThePillow(int n, int time) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func passThePillow(n int, time int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer} time\n# @return {Integer}\ndef pass_the_pillow(n, time)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def passThePillow(n: Int, time: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn pass_the_pillow(n: i32, time: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (pass-the-pillow n time)\n (-> exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec pass_the_pillow(N :: integer(), Time :: integer()) -> integer().\npass_the_pillow(N, Time) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec pass_the_pillow(n :: integer, time :: integer) :: integer\n def pass_the_pillow(n, time) do\n \n end\nend"}]
4 5
{ "name": "passThePillow", "params": [ { "name": "n", "type": "integer" }, { "type": "integer", "name": "time" } ], "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,583
Kth Largest Sum in a Binary Tree
kth-largest-sum-in-a-binary-tree
<p>You are given the <code>root</code> of a binary tree and a positive integer <code>k</code>.</p> <p>The <strong>level sum</strong> in the tree is the sum of the values of the nodes that are on the <strong>same</strong> level.</p> <p>Return<em> the </em><code>k<sup>th</sup></code><em> <strong>largest</strong> level sum in the tree (not necessarily distinct)</em>. If there are fewer than <code>k</code> levels in the tree, return <code>-1</code>.</p> <p><strong>Note</strong> that two nodes are on the same level if they have the same distance from the root.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" /> <pre> <strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], k = 2 <strong>Output:</strong> 13 <strong>Explanation:</strong> The level sums are the following: - Level 1: 5. - Level 2: 8 + 9 = 17. - Level 3: 2 + 1 + 3 + 7 = 13. - Level 4: 4 + 6 = 10. The 2<sup>nd</sup> largest level sum is 13. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" /> <pre> <strong>Input:</strong> root = [1,2,null,3], k = 1 <strong>Output:</strong> 3 <strong>Explanation:</strong> The largest level sum is 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is <code>n</code>.</li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= Node.val &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
Medium
163.8K
276.3K
163,839
276,295
59.3%
['binary-tree-preorder-traversal', 'maximum-level-sum-of-a-binary-tree', 'find-the-level-of-tree-with-minimum-sum']
[]
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 long long kthLargestLevelSum(TreeNode* root, int k) {\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 long kthLargestLevelSum(TreeNode root, int k) {\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 kthLargestLevelSum(self, root, k):\n \"\"\"\n :type root: Optional[TreeNode]\n :type k: int\n :rtype: int\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 kthLargestLevelSum(self, root: Optional[TreeNode], k: int) -> int:\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 */\nlong long kthLargestLevelSum(struct TreeNode* root, int k) {\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 long KthLargestLevelSum(TreeNode root, int k) {\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 {TreeNode} root\n * @param {number} k\n * @return {number}\n */\nvar kthLargestLevelSum = function(root, k) {\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 kthLargestLevelSum(root: TreeNode | null, k: number): number {\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 TreeNode $root\n * @param Integer $k\n * @return Integer\n */\n function kthLargestLevelSum($root, $k) {\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 kthLargestLevelSum(_ root: TreeNode?, _ k: Int) -> Int {\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 kthLargestLevelSum(root: TreeNode?, k: Int): Long {\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 int kthLargestLevelSum(TreeNode? root, int k) {\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 kthLargestLevelSum(root *TreeNode, k int) int64 {\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 {TreeNode} root\n# @param {Integer} k\n# @return {Integer}\ndef kth_largest_level_sum(root, k)\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 kthLargestLevelSum(root: TreeNode, k: Int): Long = {\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 kth_largest_level_sum(root: Option<Rc<RefCell<TreeNode>>>, k: i32) -> i64 {\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 (kth-largest-level-sum root k)\n (-> (or/c tree-node? #f) exact-integer? exact-integer?)\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 kth_largest_level_sum(Root :: #tree_node{} | null, K :: integer()) -> integer().\nkth_largest_level_sum(Root, K) ->\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 kth_largest_level_sum(root :: TreeNode.t | nil, k :: integer) :: integer\n def kth_largest_level_sum(root, k) do\n \n end\nend"}]
[5,8,9,2,1,3,7,4,6] 2
{ "name": "kthLargestLevelSum", "params": [ { "name": "root", "type": "TreeNode" }, { "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>"]}
['Tree', 'Breadth-First Search', 'Sorting', 'Binary Tree']
2,584
Split the Array to Make Coprime Products
split-the-array-to-make-coprime-products
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p> <p>A <strong>split</strong> at an index <code>i</code> where <code>0 &lt;= i &lt;= n - 2</code> is called <strong>valid</strong> if the product of the first <code>i + 1</code> elements and the product of the remaining elements are coprime.</p> <ul> <li>For example, if <code>nums = [2, 3, 3]</code>, then a split at the index <code>i = 0</code> is valid because <code>2</code> and <code>9</code> are coprime, while a split at the index <code>i = 1</code> is not valid because <code>6</code> and <code>3</code> are not coprime. A split at the index <code>i = 2</code> is not valid because <code>i == n - 1</code>.</li> </ul> <p>Return <em>the smallest index </em><code>i</code><em> at which the array can be split validly or </em><code>-1</code><em> if there is no such split</em>.</p> <p>Two values <code>val1</code> and <code>val2</code> are coprime if <code>gcd(val1, val2) == 1</code> where <code>gcd(val1, val2)</code> is the greatest common divisor of <code>val1</code> and <code>val2</code>.</p> <p>&nbsp;</p> <p><strong>Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/second.PNG" style="width: 450px; height: 211px;" /> <pre> <strong>Input:</strong> nums = [4,7,8,15,3,5] <strong>Output:</strong> 2 <strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. The only valid split is at index 2. </pre> <p><strong>Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/capture.PNG" style="width: 450px; height: 215px;" /> <pre> <strong>Input:</strong> nums = [4,7,15,8,3,5] <strong>Output:</strong> -1 <strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i. There is no valid split. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
Hard
12.9K
55.7K
12,937
55,691
23.2%
['replace-non-coprime-numbers-in-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findValidSplit(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findValidSplit(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findValidSplit(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findValidSplit(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findValidSplit(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindValidSplit(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findValidSplit = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findValidSplit(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function findValidSplit($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findValidSplit(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findValidSplit(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findValidSplit(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findValidSplit(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef find_valid_split(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findValidSplit(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_valid_split(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-valid-split nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_valid_split(Nums :: [integer()]) -> integer().\nfind_valid_split(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_valid_split(nums :: [integer]) :: integer\n def find_valid_split(nums) do\n \n end\nend"}]
[4,7,8,15,3,5]
{ "name": "findValidSplit", "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', 'Math', 'Number Theory']
2,585
Number of Ways to Earn Points
number-of-ways-to-earn-points
<p>There is a test that has <code>n</code> types of questions. You are given an integer <code>target</code> and a <strong>0-indexed</strong> 2D integer array <code>types</code> where <code>types[i] = [count<sub>i</sub>, marks<sub>i</sub>]</code> indicates that there are <code>count<sub>i</sub></code> questions of the <code>i<sup>th</sup></code> type, and each one of them is worth <code>marks<sub>i</sub></code> points.</p> <ul> </ul> <p>Return <em>the number of ways you can earn <strong>exactly</strong> </em><code>target</code><em> points in the exam</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that questions of the same type are indistinguishable.</p> <ul> <li>For example, if there are <code>3</code> questions of the same type, then solving the <code>1<sup>st</sup></code> and <code>2<sup>nd</sup></code> questions is the same as solving the <code>1<sup>st</sup></code> and <code>3<sup>rd</sup></code> questions, or the <code>2<sup>nd</sup></code> and <code>3<sup>rd</sup></code> questions.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> target = 6, types = [[6,1],[3,2],[2,3]] <strong>Output:</strong> 7 <strong>Explanation:</strong> You can earn 6 points in one of the seven ways: - Solve 6 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 + 1 = 6 - Solve 4 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 1 + 2 = 6 - Solve 2 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 1 + 2 + 2 = 6 - Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 1 + 1 + 3 = 6 - Solve 1 question of the 0<sup>th</sup> type, 1 question of the 1<sup>st</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 2 + 3 = 6 - Solve 3 questions of the 1<sup>st</sup> type: 2 + 2 + 2 = 6 - Solve 2 questions of the 2<sup>nd</sup> type: 3 + 3 = 6 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> target = 5, types = [[50,1],[50,2],[50,5]] <strong>Output:</strong> 4 <strong>Explanation:</strong> You can earn 5 points in one of the four ways: - Solve 5 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 = 5 - Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 2 = 5 - Solve 1 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 2 + 2 = 5 - Solve 1 question of the 2<sup>nd</sup> type: 5 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> target = 18, types = [[6,1],[3,2],[2,3]] <strong>Output:</strong> 1 <strong>Explanation:</strong> You can only earn 18 points by answering all questions. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= target &lt;= 1000</code></li> <li><code>n == types.length</code></li> <li><code>1 &lt;= n &lt;= 50</code></li> <li><code>types[i].length == 2</code></li> <li><code>1 &lt;= count<sub>i</sub>, marks<sub>i</sub> &lt;= 50</code></li> </ul>
Hard
20.1K
34.5K
20,144
34,526
58.3%
['coin-change-ii', 'minimum-total-distance-traveled']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int waysToReachTarget(int target, vector<vector<int>>& types) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int waysToReachTarget(int target, int[][] types) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def waysToReachTarget(self, target, types):\n \"\"\"\n :type target: int\n :type types: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def waysToReachTarget(self, target: int, types: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int waysToReachTarget(int target, int** types, int typesSize, int* typesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int WaysToReachTarget(int target, int[][] types) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} target\n * @param {number[][]} types\n * @return {number}\n */\nvar waysToReachTarget = function(target, types) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function waysToReachTarget(target: number, types: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $target\n * @param Integer[][] $types\n * @return Integer\n */\n function waysToReachTarget($target, $types) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func waysToReachTarget(_ target: Int, _ types: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun waysToReachTarget(target: Int, types: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int waysToReachTarget(int target, List<List<int>> types) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func waysToReachTarget(target int, types [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} target\n# @param {Integer[][]} types\n# @return {Integer}\ndef ways_to_reach_target(target, types)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def waysToReachTarget(target: Int, types: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn ways_to_reach_target(target: i32, types: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (ways-to-reach-target target types)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec ways_to_reach_target(Target :: integer(), Types :: [[integer()]]) -> integer().\nways_to_reach_target(Target, Types) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec ways_to_reach_target(target :: integer, types :: [[integer]]) :: integer\n def ways_to_reach_target(target, types) do\n \n end\nend"}]
6 [[6,1],[3,2],[2,3]]
{ "name": "waysToReachTarget", "params": [ { "name": "target", "type": "integer" }, { "type": "integer[][]", "name": "types" } ], "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,586
Count the Number of Vowel Strings in Range
count-the-number-of-vowel-strings-in-range
<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p> <p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>.</p> <p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> words = [&quot;are&quot;,&quot;amy&quot;,&quot;u&quot;], left = 0, right = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> - &quot;are&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;e&#39;. - &quot;amy&quot; is not a vowel string because it does not end with a vowel. - &quot;u&quot; is a vowel string because it starts with &#39;u&#39; and ends with &#39;u&#39;. The number of vowel strings in the mentioned range is 2. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> words = [&quot;hey&quot;,&quot;aeo&quot;,&quot;mu&quot;,&quot;ooo&quot;,&quot;artro&quot;], left = 1, right = 4 <strong>Output:</strong> 3 <strong>Explanation:</strong> - &quot;aeo&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;o&#39;. - &quot;mu&quot; is not a vowel string because it does not start with a vowel. - &quot;ooo&quot; is a vowel string because it starts with &#39;o&#39; and ends with &#39;o&#39;. - &quot;artro&quot; is a vowel string because it starts with &#39;a&#39; and ends with &#39;o&#39;. The number of vowel strings in the mentioned range is 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 1000</code></li> <li><code>1 &lt;= words[i].length &lt;= 10</code></li> <li><code>words[i]</code> consists of only lowercase English letters.</li> <li><code>0 &lt;= left &lt;= right &lt; words.length</code></li> </ul>
Easy
75.4K
102.8K
75,440
102,821
73.4%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int vowelStrings(vector<string>& words, int left, int right) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int vowelStrings(String[] words, int left, int right) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def vowelStrings(self, words, left, right):\n \"\"\"\n :type words: List[str]\n :type left: int\n :type right: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def vowelStrings(self, words: List[str], left: int, right: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int vowelStrings(char** words, int wordsSize, int left, int right) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int VowelStrings(string[] words, int left, int right) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @param {number} left\n * @param {number} right\n * @return {number}\n */\nvar vowelStrings = function(words, left, right) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function vowelStrings(words: string[], left: number, right: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @param Integer $left\n * @param Integer $right\n * @return Integer\n */\n function vowelStrings($words, $left, $right) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func vowelStrings(_ words: [String], _ left: Int, _ right: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun vowelStrings(words: Array<String>, left: Int, right: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int vowelStrings(List<String> words, int left, int right) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func vowelStrings(words []string, left int, right int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @param {Integer} left\n# @param {Integer} right\n# @return {Integer}\ndef vowel_strings(words, left, right)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def vowelStrings(words: Array[String], left: Int, right: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn vowel_strings(words: Vec<String>, left: i32, right: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (vowel-strings words left right)\n (-> (listof string?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec vowel_strings(Words :: [unicode:unicode_binary()], Left :: integer(), Right :: integer()) -> integer().\nvowel_strings(Words, Left, Right) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec vowel_strings(words :: [String.t], left :: integer, right :: integer) :: integer\n def vowel_strings(words, left, right) do\n \n end\nend"}]
["are","amy","u"] 0 2
{ "name": "vowelStrings", "params": [ { "name": "words", "type": "string[]" }, { "type": "integer", "name": "left" }, { "type": "integer", "name": "right" } ], "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', 'Counting']
2,587
Rearrange Array to Maximize Prefix Score
rearrange-array-to-maximize-prefix-score
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You can rearrange the elements of <code>nums</code> to <strong>any order</strong> (including the given order).</p> <p>Let <code>prefix</code> be the array containing the prefix sums of <code>nums</code> after rearranging it. In other words, <code>prefix[i]</code> is the sum of the elements from <code>0</code> to <code>i</code> in <code>nums</code> after rearranging it. The <strong>score</strong> of <code>nums</code> is the number of positive integers in the array <code>prefix</code>.</p> <p>Return <em>the maximum score you can achieve</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,-1,0,1,-3,3,-3] <strong>Output:</strong> 6 <strong>Explanation:</strong> We can rearrange the array into nums = [2,3,1,-1,-3,0,-3]. prefix = [2,5,6,5,2,2,-1], so the score is 6. It can be shown that 6 is the maximum score we can obtain. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [-2,-3,0] <strong>Output:</strong> 0 <strong>Explanation:</strong> Any rearrangement of the array will result in a score of 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
Medium
33.6K
81.5K
33,567
81,509
41.2%
['two-city-scheduling']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxScore(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxScore(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxScore(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxScore(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxScore(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxScore(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxScore = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxScore(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxScore($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxScore(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxScore(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxScore(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxScore(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_score(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxScore(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_score(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-score nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_score(Nums :: [integer()]) -> integer().\nmax_score(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_score(nums :: [integer]) :: integer\n def max_score(nums) do\n \n end\nend"}]
[2,-1,0,1,-3,3,-3]
{ "name": "maxScore", "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', 'Greedy', 'Sorting', 'Prefix Sum']
2,588
Count the Number of Beautiful Subarrays
count-the-number-of-beautiful-subarrays
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p> <ul> <li>Choose two different indices <code>i</code> and <code>j</code> such that <code>0 &lt;= i, j &lt; nums.length</code>.</li> <li>Choose a non-negative integer <code>k</code> such that the <code>k<sup>th</sup></code> bit (<strong>0-indexed</strong>) in the binary representation of <code>nums[i]</code> and <code>nums[j]</code> is <code>1</code>.</li> <li>Subtract <code>2<sup>k</sup></code> from <code>nums[i]</code> and <code>nums[j]</code>.</li> </ul> <p>A subarray is <strong>beautiful</strong> if it is possible to make all of its elements equal to <code>0</code> after applying the above operation any number of times.</p> <p>Return <em>the number of <strong>beautiful subarrays</strong> in the array</em> <code>nums</code>.</p> <p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,3,1,2,4] <strong>Output:</strong> 2 <strong>Explanation:</strong> There are 2 beautiful subarrays in nums: [4,<u>3,1,2</u>,4] and [<u>4,3,1,2,4</u>]. - We can make all elements in the subarray [3,1,2] equal to 0 in the following way: - Choose [<u>3</u>, 1, <u>2</u>] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [1, 1, 0]. - Choose [<u>1</u>, <u>1</u>, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 0, 0]. - We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way: - Choose [<u>4</u>, 3, 1, 2, <u>4</u>] and k = 2. Subtract 2<sup>2</sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0]. - Choose [0, <u>3</u>, <u>1</u>, 2, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0]. - Choose [0, <u>2</u>, 0, <u>2</u>, 0] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,10,4] <strong>Output:</strong> 0 <strong>Explanation:</strong> There are no beautiful subarrays in nums. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
Medium
21.2K
41.2K
21,180
41,205
51.4%
['maximum-xor-for-each-query', 'count-the-number-of-ideal-arrays']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long beautifulSubarrays(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long beautifulSubarrays(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def beautifulSubarrays(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def beautifulSubarrays(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long beautifulSubarrays(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long BeautifulSubarrays(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar beautifulSubarrays = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function beautifulSubarrays(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function beautifulSubarrays($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func beautifulSubarrays(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun beautifulSubarrays(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int beautifulSubarrays(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func beautifulSubarrays(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef beautiful_subarrays(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def beautifulSubarrays(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn beautiful_subarrays(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (beautiful-subarrays nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec beautiful_subarrays(Nums :: [integer()]) -> integer().\nbeautiful_subarrays(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec beautiful_subarrays(nums :: [integer]) :: integer\n def beautiful_subarrays(nums) do\n \n end\nend"}]
[4,3,1,2,4]
{ "name": "beautifulSubarrays", "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', 'Hash Table', 'Bit Manipulation', 'Prefix Sum']
2,589
Minimum Time to Complete All Tasks
minimum-time-to-complete-all-tasks
<p>There is a computer that can run an unlimited number of tasks <strong>at the same time</strong>. You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [start<sub>i</sub>, end<sub>i</sub>, duration<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> task should run for a total of <code>duration<sub>i</sub></code> seconds (not necessarily continuous) within the <strong>inclusive</strong> time range <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.</p> <p>You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.</p> <p>Return <em>the minimum time during which the computer should be turned on to complete all tasks</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[2,3,1],[4,5,1],[1,5,2]] <strong>Output:</strong> 2 <strong>Explanation:</strong> - The first task can be run in the inclusive time range [2, 2]. - The second task can be run in the inclusive time range [5, 5]. - The third task can be run in the two inclusive time ranges [2, 2] and [5, 5]. The computer will be on for a total of 2 seconds. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,3,2],[2,5,3],[5,6,2]] <strong>Output:</strong> 4 <strong>Explanation:</strong> - The first task can be run in the inclusive time range [2, 3]. - The second task can be run in the inclusive time ranges [2, 3] and [5, 5]. - The third task can be run in the two inclusive time range [5, 6]. The computer will be on for a total of 4 seconds. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= tasks.length &lt;= 2000</code></li> <li><code>tasks[i].length == 3</code></li> <li><code>1 &lt;= start<sub>i</sub>, end<sub>i</sub> &lt;= 2000</code></li> <li><code>1 &lt;= duration<sub>i</sub> &lt;= end<sub>i</sub> - start<sub>i</sub> + 1 </code></li> </ul>
Hard
11.8K
31.9K
11,784
31,888
37.0%
['single-threaded-cpu']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findMinimumTime(vector<vector<int>>& tasks) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findMinimumTime(int[][] tasks) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findMinimumTime(self, tasks):\n \"\"\"\n :type tasks: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findMinimumTime(self, tasks: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findMinimumTime(int** tasks, int tasksSize, int* tasksColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindMinimumTime(int[][] tasks) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} tasks\n * @return {number}\n */\nvar findMinimumTime = function(tasks) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findMinimumTime(tasks: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $tasks\n * @return Integer\n */\n function findMinimumTime($tasks) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findMinimumTime(_ tasks: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findMinimumTime(tasks: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findMinimumTime(List<List<int>> tasks) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findMinimumTime(tasks [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} tasks\n# @return {Integer}\ndef find_minimum_time(tasks)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findMinimumTime(tasks: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_minimum_time(tasks: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-minimum-time tasks)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_minimum_time(Tasks :: [[integer()]]) -> integer().\nfind_minimum_time(Tasks) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_minimum_time(tasks :: [[integer]]) :: integer\n def find_minimum_time(tasks) do\n \n end\nend"}]
[[2,3,1],[4,5,1],[1,5,2]]
{ "name": "findMinimumTime", "params": [ { "name": "tasks", "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', 'Binary Search', 'Stack', 'Greedy', 'Sorting']
2,590
Design a Todo List
design-a-todo-list
null
Medium
3.1K
5.3K
3,131
5,326
58.8%
[]
[]
Algorithms
null
["TodoList","addTask","addTask","getAllTasks","getAllTasks","addTask","getTasksForTag","completeTask","completeTask","getTasksForTag","getAllTasks"] [[],[1,"Task1",50,[]],[1,"Task2",100,["P1"]],[1],[5],[1,"Task3",30,["P1"]],[1,"P1"],[5,1],[1,2],[1,"P1"],[1]]
{ "classname": "TodoList", "constructor": { "params": [] }, "methods": [ { "params": [ { "type": "integer", "name": "userId" }, { "type": "string", "name": "taskDescription" }, { "type": "integer", "name": "dueDate" }, { "type": "list<string>", "name": "tags" } ], "name": "addTask", "return": { "type": "integer" } }, { "params": [ { "type": "integer", "name": "userId" } ], "name": "getAllTasks", "return": { "type": "list<string>" } }, { "params": [ { "type": "integer", "name": "userId" }, { "type": "string", "name": "tag" } ], "name": "getTasksForTag", "return": { "type": "list<string>" } }, { "params": [ { "type": "integer", "name": "userId" }, { "type": "integer", "name": "taskId" } ], "name": "completeTask", "return": { "type": "void" } } ], "return": { "type": "boolean" }, "systemdesign": true }
{"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', 'Sorting']
2,591
Distribute Money to Maximum Children
distribute-money-to-maximum-children
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p> <p>You have to distribute the money according to the following rules:</p> <ul> <li>All money must be distributed.</li> <li>Everyone must receive at least <code>1</code> dollar.</li> <li>Nobody receives <code>4</code> dollars.</li> </ul> <p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> money = 20, children = 3 <strong>Output:</strong> 1 <strong>Explanation:</strong> The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is: - 8 dollars to the first child. - 9 dollars to the second child. - 3 dollars to the third child. It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> money = 16, children = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> Each child can be given 8 dollars. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= money &lt;= 200</code></li> <li><code>2 &lt;= children &lt;= 30</code></li> </ul>
Easy
38.3K
197.9K
38,266
197,928
19.3%
['distribute-candies-to-people', 'fair-distribution-of-cookies', 'calculate-money-in-leetcode-bank']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int distMoney(int money, int children) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int distMoney(int money, int children) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def distMoney(self, money, children):\n \"\"\"\n :type money: int\n :type children: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def distMoney(self, money: int, children: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int distMoney(int money, int children) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DistMoney(int money, int children) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} money\n * @param {number} children\n * @return {number}\n */\nvar distMoney = function(money, children) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function distMoney(money: number, children: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $money\n * @param Integer $children\n * @return Integer\n */\n function distMoney($money, $children) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func distMoney(_ money: Int, _ children: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun distMoney(money: Int, children: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int distMoney(int money, int children) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func distMoney(money int, children int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} money\n# @param {Integer} children\n# @return {Integer}\ndef dist_money(money, children)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def distMoney(money: Int, children: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn dist_money(money: i32, children: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (dist-money money children)\n (-> exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec dist_money(Money :: integer(), Children :: integer()) -> integer().\ndist_money(Money, Children) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec dist_money(money :: integer, children :: integer) :: integer\n def dist_money(money, children) do\n \n end\nend"}]
20 3
{ "name": "distMoney", "params": [ { "name": "money", "type": "integer" }, { "type": "integer", "name": "children" } ], "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,592
Maximize Greatness of an Array
maximize-greatness-of-an-array
<p>You are given a 0-indexed integer array <code>nums</code>. You are allowed to permute <code>nums</code> into a new array <code>perm</code> of your choosing.</p> <p>We define the <strong>greatness</strong> of <code>nums</code> be the number of indices <code>0 &lt;= i &lt; nums.length</code> for which <code>perm[i] &gt; nums[i]</code>.</p> <p>Return <em>the <strong>maximum</strong> possible greatness you can achieve after permuting</em> <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,3,5,2,1,3,1] <strong>Output:</strong> 4 <strong>Explanation:</strong> One of the optimal rearrangements is perm = [2,5,1,3,3,1,1]. At indices = 0, 1, 3, and 4, perm[i] &gt; nums[i]. Hence, we return 4.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,3,4] <strong>Output:</strong> 3 <strong>Explanation:</strong> We can prove the optimal perm is [2,3,4,1]. At indices = 0, 1, and 2, perm[i] &gt; nums[i]. Hence, we return 3. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
30.6K
52.7K
30,569
52,699
58.0%
['3sum-smaller', 'maximum-matching-of-players-with-trainers']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximizeGreatness(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximizeGreatness(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximizeGreatness(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximizeGreatness(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximizeGreatness(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximizeGreatness = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximizeGreatness(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maximizeGreatness($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximizeGreatness(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximizeGreatness(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximizeGreatness(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximizeGreatness(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef maximize_greatness(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximizeGreatness(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximize_greatness(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximize-greatness nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximize_greatness(Nums :: [integer()]) -> integer().\nmaximize_greatness(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximize_greatness(nums :: [integer]) :: integer\n def maximize_greatness(nums) do\n \n end\nend"}]
[1,3,5,2,1,3,1]
{ "name": "maximizeGreatness", "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', 'Greedy', 'Sorting']
2,593
Find Score of an Array After Marking All Elements
find-score-of-an-array-after-marking-all-elements
<p>You are given an array <code>nums</code> consisting of positive integers.</p> <p>Starting with <code>score = 0</code>, apply the following algorithm:</p> <ul> <li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li> <li>Add the value of the chosen integer to <code>score</code>.</li> <li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li> <li>Repeat until all the array elements are marked.</li> </ul> <p>Return <em>the score you get after applying the above algorithm</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,1,3,4,5,2] <strong>Output:</strong> 7 <strong>Explanation:</strong> We mark the elements as follows: - 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<u>2</u>,<u>1</u>,<u>3</u>,4,5,2]. - 2 is the smallest unmarked element, so we mark it and its left adjacent element: [<u>2</u>,<u>1</u>,<u>3</u>,4,<u>5</u>,<u>2</u>]. - 4 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>1</u>,<u>3</u>,<u>4</u>,<u>5</u>,<u>2</u>]. Our score is 1 + 2 + 4 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,3,5,1,3,2] <strong>Output:</strong> 5 <strong>Explanation:</strong> We mark the elements as follows: - 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<u>5</u>,<u>1</u>,<u>3</u>,2]. - 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,2]. - 2 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,<u>2</u>]. Our score is 1 + 2 + 2 = 5. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
Medium
128.4K
198.5K
128,407
198,529
64.7%
['sort-integers-by-the-power-value']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long findScore(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long findScore(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findScore(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findScore(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long findScore(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long FindScore(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findScore = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findScore(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function findScore($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findScore(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findScore(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findScore(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findScore(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef find_score(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findScore(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_score(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-score nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_score(Nums :: [integer()]) -> integer().\nfind_score(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_score(nums :: [integer]) :: integer\n def find_score(nums) do\n \n end\nend"}]
[2,1,3,4,5,2]
{ "name": "findScore", "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', 'Hash Table', 'Sorting', 'Heap (Priority Queue)', 'Simulation']
2,594
Minimum Time to Repair Cars
minimum-time-to-repair-cars
<p>You are given an integer array <code>ranks</code> representing the <strong>ranks</strong> of some mechanics. <font face="monospace">ranks<sub>i</sub></font> is the rank of the <font face="monospace">i<sup>th</sup></font> mechanic<font face="monospace">.</font> A mechanic with a rank <code>r</code> can repair <font face="monospace">n</font> cars in <code>r * n<sup>2</sup></code> minutes.</p> <p>You are also given an integer <code>cars</code> representing the total number of cars waiting in the garage to be repaired.</p> <p>Return <em>the <strong>minimum</strong> time taken to repair all the cars.</em></p> <p><strong>Note:</strong> All the mechanics can repair the cars simultaneously.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> ranks = [4,2,3,1], cars = 10 <strong>Output:</strong> 16 <strong>Explanation:</strong> - The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes. - The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes. - The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes. - The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​ </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> ranks = [5,1,8], cars = 6 <strong>Output:</strong> 16 <strong>Explanation:</strong> - The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes. - The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes. - The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes. It can be proved that the cars cannot be repaired in less than 16 minutes.​​​​​ </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= ranks.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= ranks[i] &lt;= 100</code></li> <li><code>1 &lt;= cars &lt;= 10<sup>6</sup></code></li> </ul>
Medium
140.9K
234.9K
140,911
234,941
60.0%
['sort-transformed-array', 'koko-eating-bananas']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long repairCars(vector<int>& ranks, int cars) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long repairCars(int[] ranks, int cars) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def repairCars(self, ranks, cars):\n \"\"\"\n :type ranks: List[int]\n :type cars: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def repairCars(self, ranks: List[int], cars: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long repairCars(int* ranks, int ranksSize, int cars) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long RepairCars(int[] ranks, int cars) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} ranks\n * @param {number} cars\n * @return {number}\n */\nvar repairCars = function(ranks, cars) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function repairCars(ranks: number[], cars: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $ranks\n * @param Integer $cars\n * @return Integer\n */\n function repairCars($ranks, $cars) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func repairCars(_ ranks: [Int], _ cars: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun repairCars(ranks: IntArray, cars: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int repairCars(List<int> ranks, int cars) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func repairCars(ranks []int, cars int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} ranks\n# @param {Integer} cars\n# @return {Integer}\ndef repair_cars(ranks, cars)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def repairCars(ranks: Array[Int], cars: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn repair_cars(ranks: Vec<i32>, cars: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (repair-cars ranks cars)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec repair_cars(Ranks :: [integer()], Cars :: integer()) -> integer().\nrepair_cars(Ranks, Cars) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec repair_cars(ranks :: [integer], cars :: integer) :: integer\n def repair_cars(ranks, cars) do\n \n end\nend"}]
[4,2,3,1] 10
{ "name": "repairCars", "params": [ { "name": "ranks", "type": "integer[]" }, { "type": "integer", "name": "cars" } ], "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,595
Number of Even and Odd Bits
number-of-even-and-odd-bits
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p> <p>Let <code>even</code> denote the number of even indices in the binary representation of <code>n</code> with value 1.</p> <p>Let <code>odd</code> denote the number of odd indices in the binary representation of <code>n</code> with value 1.</p> <p>Note that bits are indexed from <strong>right to left</strong> in the binary representation of a number.</p> <p>Return the array <code>[even, odd]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 50</span></p> <p><strong>Output:</strong> <span class="example-io">[1,2]</span></p> <p><strong>Explanation:</strong></p> <p>The binary representation of 50 is <code>110010</code>.</p> <p>It contains 1 on indices 1, 4, and 5.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>The binary representation of 2 is <code>10</code>.</p> <p>It contains 1 only on index 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1000</code></li> </ul>
Easy
56.3K
77.8K
56,252
77,805
72.3%
['find-numbers-with-even-number-of-digits']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> evenOddBit(int n) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] evenOddBit(int n) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def evenOddBit(self, n):\n \"\"\"\n :type n: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def evenOddBit(self, n: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* evenOddBit(int n, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] EvenOddBit(int n) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @return {number[]}\n */\nvar evenOddBit = function(n) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function evenOddBit(n: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @return Integer[]\n */\n function evenOddBit($n) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func evenOddBit(_ n: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun evenOddBit(n: Int): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> evenOddBit(int n) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func evenOddBit(n int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @return {Integer[]}\ndef even_odd_bit(n)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def evenOddBit(n: Int): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn even_odd_bit(n: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (even-odd-bit n)\n (-> exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec even_odd_bit(N :: integer()) -> [integer()].\neven_odd_bit(N) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec even_odd_bit(n :: integer) :: [integer]\n def even_odd_bit(n) do\n \n end\nend"}]
50
{ "name": "evenOddBit", "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>"]}
['Bit Manipulation']
2,596
Check Knight Tour Configuration
check-knight-tour-configuration
<p>There is a knight on an <code>n x n</code> chessboard. In a valid configuration, the knight starts <strong>at the top-left cell</strong> of the board and visits every cell on the board <strong>exactly once</strong>.</p> <p>You are given an <code>n x n</code> integer matrix <code>grid</code> consisting of distinct integers from the range <code>[0, n * n - 1]</code> where <code>grid[row][col]</code> indicates that the cell <code>(row, col)</code> is the <code>grid[row][col]<sup>th</sup></code> cell that the knight visited. The moves are <strong>0-indexed</strong>.</p> <p>Return <code>true</code> <em>if</em> <code>grid</code> <em>represents a valid configuration of the knight&#39;s movements or</em> <code>false</code> <em>otherwise</em>.</p> <p><strong>Note</strong> that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.</p> <img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/knight.png" style="width: 300px; height: 300px;" /> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png" style="width: 251px; height: 251px;" /> <pre> <strong>Input:</strong> grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]] <strong>Output:</strong> true <strong>Explanation:</strong> The above diagram represents the grid. It can be shown that it is a valid configuration. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png" style="width: 151px; height: 151px;" /> <pre> <strong>Input:</strong> grid = [[0,3,6],[5,8,1],[2,7,4]] <strong>Output:</strong> false <strong>Explanation:</strong> The above diagram represents the grid. The 8<sup>th</sup> move of the knight is not valid considering its position after the 7<sup>th</sup> move. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == grid[i].length</code></li> <li><code>3 &lt;= n &lt;= 7</code></li> <li><code>0 &lt;= grid[row][col] &lt; n * n</code></li> <li>All integers in <code>grid</code> are <strong>unique</strong>.</li> </ul>
Medium
37.2K
64.9K
37,154
64,920
57.2%
['minimum-knight-moves', 'maximum-number-of-moves-to-kill-all-pawns']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool checkValidGrid(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean checkValidGrid(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def checkValidGrid(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def checkValidGrid(self, grid: List[List[int]]) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool checkValidGrid(int** grid, int gridSize, int* gridColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool CheckValidGrid(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {boolean}\n */\nvar checkValidGrid = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function checkValidGrid(grid: number[][]): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Boolean\n */\n function checkValidGrid($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func checkValidGrid(_ grid: [[Int]]) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun checkValidGrid(grid: Array<IntArray>): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool checkValidGrid(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func checkValidGrid(grid [][]int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Boolean}\ndef check_valid_grid(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def checkValidGrid(grid: Array[Array[Int]]): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn check_valid_grid(grid: Vec<Vec<i32>>) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (check-valid-grid grid)\n (-> (listof (listof exact-integer?)) boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec check_valid_grid(Grid :: [[integer()]]) -> boolean().\ncheck_valid_grid(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec check_valid_grid(grid :: [[integer]]) :: boolean\n def check_valid_grid(grid) do\n \n end\nend"}]
[[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
{ "name": "checkValidGrid", "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', 'Depth-First Search', 'Breadth-First Search', 'Matrix', 'Simulation']
2,597
The Number of Beautiful Subsets
the-number-of-beautiful-subsets
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p> <p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p> <p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p> <p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [2,4,6], k = 2 <strong>Output:</strong> 4 <strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6]. It can be proved that there are only 4 beautiful subsets in the array [2,4,6]. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1], k = 1 <strong>Output:</strong> 1 <strong>Explanation:</strong> The beautiful subset of the array nums is [1]. It can be proved that there is only 1 beautiful subset in the array [1]. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 18</code></li> <li><code>1 &lt;= nums[i], k &lt;= 1000</code></li> </ul>
Medium
122.9K
242.4K
122,902
242,387
50.7%
['construct-the-lexicographically-largest-valid-sequence']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int beautifulSubsets(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int beautifulSubsets(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def beautifulSubsets(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 beautifulSubsets(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int beautifulSubsets(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int BeautifulSubsets(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 beautifulSubsets = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function beautifulSubsets(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 beautifulSubsets($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func beautifulSubsets(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun beautifulSubsets(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int beautifulSubsets(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func beautifulSubsets(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef beautiful_subsets(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def beautifulSubsets(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn beautiful_subsets(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (beautiful-subsets nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec beautiful_subsets(Nums :: [integer()], K :: integer()) -> integer().\nbeautiful_subsets(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec beautiful_subsets(nums :: [integer], k :: integer) :: integer\n def beautiful_subsets(nums, k) do\n \n end\nend"}]
[2,4,6] 2
{ "name": "beautifulSubsets", "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', 'Hash Table', 'Math', 'Dynamic Programming', 'Backtracking', 'Sorting', 'Combinatorics']
2,598
Smallest Missing Non-negative Integer After Operations
smallest-missing-non-negative-integer-after-operations
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>value</code>.</p> <p>In one operation, you can add or subtract <code>value</code> from any element of <code>nums</code>.</p> <ul> <li>For example, if <code>nums = [1,2,3]</code> and <code>value = 2</code>, you can choose to subtract <code>value</code> from <code>nums[0]</code> to make <code>nums = [-1,2,3]</code>.</li> </ul> <p>The MEX (minimum excluded) of an array is the smallest missing <strong>non-negative</strong> integer in it.</p> <ul> <li>For example, the MEX of <code>[-1,2,3]</code> is <code>0</code> while the MEX of <code>[1,0,3]</code> is <code>2</code>.</li> </ul> <p>Return <em>the maximum MEX of </em><code>nums</code><em> after applying the mentioned operation <strong>any number of times</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 5 <strong>Output:</strong> 4 <strong>Explanation:</strong> One can achieve this result by applying the following operations: - Add value to nums[1] twice to make nums = [1,<strong><u>0</u></strong>,7,13,6,8] - Subtract value from nums[2] once to make nums = [1,0,<strong><u>2</u></strong>,13,6,8] - Subtract value from nums[3] twice to make nums = [1,0,2,<strong><u>3</u></strong>,6,8] The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 7 <strong>Output:</strong> 2 <strong>Explanation:</strong> One can achieve this result by applying the following operation: - subtract value from nums[2] once to make nums = [1,-10,<u><strong>0</strong></u>,13,6,8] The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length, value &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
18.7K
47.2K
18,668
47,173
39.6%
['first-missing-positive']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findSmallestInteger(vector<int>& nums, int value) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findSmallestInteger(int[] nums, int value) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findSmallestInteger(self, nums, value):\n \"\"\"\n :type nums: List[int]\n :type value: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findSmallestInteger(self, nums: List[int], value: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findSmallestInteger(int* nums, int numsSize, int value) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindSmallestInteger(int[] nums, int value) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} value\n * @return {number}\n */\nvar findSmallestInteger = function(nums, value) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findSmallestInteger(nums: number[], value: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $value\n * @return Integer\n */\n function findSmallestInteger($nums, $value) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findSmallestInteger(_ nums: [Int], _ value: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findSmallestInteger(nums: IntArray, value: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findSmallestInteger(List<int> nums, int value) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findSmallestInteger(nums []int, value int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} value\n# @return {Integer}\ndef find_smallest_integer(nums, value)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findSmallestInteger(nums: Array[Int], value: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_smallest_integer(nums: Vec<i32>, value: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-smallest-integer nums value)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_smallest_integer(Nums :: [integer()], Value :: integer()) -> integer().\nfind_smallest_integer(Nums, Value) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_smallest_integer(nums :: [integer], value :: integer) :: integer\n def find_smallest_integer(nums, value) do\n \n end\nend"}]
[1,-10,7,13,6,8] 5
{ "name": "findSmallestInteger", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "value" } ], "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', 'Greedy']
2,599
Make the Prefix Sum Non-negative
make-the-prefix-sum-non-negative
null
Medium
7.8K
15.2K
7,840
15,180
51.6%
['furthest-building-you-can-reach']
[]
Algorithms
null
[2,3,-5,4]
{ "name": "makePrefSumNonNegative", "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', 'Greedy', 'Heap (Priority Queue)']
2,600
K Items With the Maximum Sum
k-items-with-the-maximum-sum
<p>There is a bag that consists of items, each item&nbsp;has a number <code>1</code>, <code>0</code>, or <code>-1</code> written on it.</p> <p>You are given four <strong>non-negative </strong>integers <code>numOnes</code>, <code>numZeros</code>, <code>numNegOnes</code>, and <code>k</code>.</p> <p>The bag initially contains:</p> <ul> <li><code>numOnes</code> items with <code>1</code>s written on them.</li> <li><code>numZeroes</code> items with <code>0</code>s written on them.</li> <li><code>numNegOnes</code> items with <code>-1</code>s written on them.</li> </ul> <p>We want to pick exactly <code>k</code> items among the available items. Return <em>the <strong>maximum</strong> possible sum of numbers written on the items</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2 <strong>Output:</strong> 2 <strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2. It can be proven that 2 is the maximum possible sum. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4 <strong>Output:</strong> 3 <strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3. It can be proven that 3 is the maximum possible sum. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= numOnes, numZeros, numNegOnes &lt;= 50</code></li> <li><code>0 &lt;= k &lt;= numOnes + numZeros + numNegOnes</code></li> </ul>
Easy
50.1K
82.4K
50,079
82,446
60.7%
['maximum-subarray']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def kItemsWithMaximumSum(self, numOnes, numZeros, numNegOnes, k):\n \"\"\"\n :type numOnes: int\n :type numZeros: int\n :type numNegOnes: int\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def kItemsWithMaximumSum(self, numOnes: int, numZeros: int, numNegOnes: int, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int KItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} numOnes\n * @param {number} numZeros\n * @param {number} numNegOnes\n * @param {number} k\n * @return {number}\n */\nvar kItemsWithMaximumSum = function(numOnes, numZeros, numNegOnes, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function kItemsWithMaximumSum(numOnes: number, numZeros: number, numNegOnes: number, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $numOnes\n * @param Integer $numZeros\n * @param Integer $numNegOnes\n * @param Integer $k\n * @return Integer\n */\n function kItemsWithMaximumSum($numOnes, $numZeros, $numNegOnes, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func kItemsWithMaximumSum(_ numOnes: Int, _ numZeros: Int, _ numNegOnes: Int, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun kItemsWithMaximumSum(numOnes: Int, numZeros: Int, numNegOnes: Int, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func kItemsWithMaximumSum(numOnes int, numZeros int, numNegOnes int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} num_ones\n# @param {Integer} num_zeros\n# @param {Integer} num_neg_ones\n# @param {Integer} k\n# @return {Integer}\ndef k_items_with_maximum_sum(num_ones, num_zeros, num_neg_ones, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def kItemsWithMaximumSum(numOnes: Int, numZeros: Int, numNegOnes: Int, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn k_items_with_maximum_sum(num_ones: i32, num_zeros: i32, num_neg_ones: i32, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (k-items-with-maximum-sum numOnes numZeros numNegOnes k)\n (-> exact-integer? exact-integer? exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec k_items_with_maximum_sum(NumOnes :: integer(), NumZeros :: integer(), NumNegOnes :: integer(), K :: integer()) -> integer().\nk_items_with_maximum_sum(NumOnes, NumZeros, NumNegOnes, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec k_items_with_maximum_sum(num_ones :: integer, num_zeros :: integer, num_neg_ones :: integer, k :: integer) :: integer\n def k_items_with_maximum_sum(num_ones, num_zeros, num_neg_ones, k) do\n \n end\nend"}]
3 2 0 2
{ "name": "kItemsWithMaximumSum", "params": [ { "name": "numOnes", "type": "integer" }, { "type": "integer", "name": "numZeros" }, { "type": "integer", "name": "numNegOnes" }, { "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>"]}
['Math', 'Greedy']
2,601
Prime Subtraction Operation
prime-subtraction-operation
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p> <p>You can perform the following operation as many times as you want:</p> <ul> <li>Pick an index <code>i</code> that you haven&rsquo;t picked before, and pick a prime <code>p</code> <strong>strictly less than</strong> <code>nums[i]</code>, then subtract <code>p</code> from <code>nums[i]</code>.</li> </ul> <p>Return <em>true if you can make <code>nums</code> a strictly increasing array using the above operation and false otherwise.</em></p> <p>A <strong>strictly increasing array</strong> is an array whose each element is strictly greater than its preceding element.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [4,9,6,10] <strong>Output:</strong> true <strong>Explanation:</strong> In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10]. In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10]. After the second operation, nums is sorted in strictly increasing order, so the answer is true.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [6,8,11,12] <strong>Output:</strong> true <strong>Explanation: </strong>Initially nums is sorted in strictly increasing order, so we don&#39;t need to make any operations.</pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [5,8,3] <strong>Output:</strong> false <strong>Explanation:</strong> It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>1 &lt;= nums[i] &lt;= 1000</code></li> <li><code><font face="monospace">nums.length == n</font></code></li> </ul>
Medium
116.3K
209.3K
116,329
209,320
55.6%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool primeSubOperation(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean primeSubOperation(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def primeSubOperation(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def primeSubOperation(self, nums: List[int]) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool primeSubOperation(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool PrimeSubOperation(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {boolean}\n */\nvar primeSubOperation = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function primeSubOperation(nums: number[]): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Boolean\n */\n function primeSubOperation($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func primeSubOperation(_ nums: [Int]) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun primeSubOperation(nums: IntArray): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool primeSubOperation(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func primeSubOperation(nums []int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Boolean}\ndef prime_sub_operation(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def primeSubOperation(nums: Array[Int]): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn prime_sub_operation(nums: Vec<i32>) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (prime-sub-operation nums)\n (-> (listof exact-integer?) boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec prime_sub_operation(Nums :: [integer()]) -> boolean().\nprime_sub_operation(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec prime_sub_operation(nums :: [integer]) :: boolean\n def prime_sub_operation(nums) do\n \n end\nend"}]
[4,9,6,10]
{ "name": "primeSubOperation", "params": [ { "name": "nums", "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', 'Binary Search', 'Greedy', 'Number Theory']
2,602
Minimum Operations to Make All Array Elements Equal
minimum-operations-to-make-all-array-elements-equal
<p>You are given an array <code>nums</code> consisting of positive integers.</p> <p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to<code> queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p> <ul> <li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li> </ul> <p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>queries[i]</code>.</p> <p><strong>Note</strong> that after each query the array is reset to its original state.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,1,6,8], queries = [1,5] <strong>Output:</strong> [14,10] <strong>Explanation:</strong> For the first query we can do the following operations: - Decrease nums[0] 2 times, so that nums = [1,1,6,8]. - Decrease nums[2] 5 times, so that nums = [1,1,1,8]. - Decrease nums[3] 7 times, so that nums = [1,1,1,1]. So the total number of operations for the first query is 2 + 5 + 7 = 14. For the second query we can do the following operations: - Increase nums[0] 2 times, so that nums = [5,1,6,8]. - Increase nums[1] 4 times, so that nums = [5,5,6,8]. - Decrease nums[2] 1 time, so that nums = [5,5,5,8]. - Decrease nums[3] 3 times, so that nums = [5,5,5,5]. So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [2,9,6,3], queries = [10] <strong>Output:</strong> [20] <strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>m == queries.length</code></li> <li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i], queries[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
24.3K
66.5K
24,310
66,486
36.6%
['minimum-moves-to-equal-array-elements-ii', 'minimum-cost-to-make-array-equal', 'sum-of-distances']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> minOperations(vector<int>& nums, vector<int>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Long> minOperations(int[] nums, int[] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minOperations(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minOperations(self, nums: List[int], queries: 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* minOperations(int* nums, int numsSize, int* queries, int queriesSize, int* returnSize){\n\n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<long> MinOperations(int[] nums, int[] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number[]} queries\n * @return {number[]}\n */\nvar minOperations = function(nums, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minOperations(nums: number[], queries: number[]): number[] {\n\n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[] $queries\n * @return Integer[]\n */\n function minOperations($nums, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minOperations(_ nums: [Int], _ queries: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minOperations(nums: IntArray, queries: IntArray): List<Long> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> minOperations(List<int> nums, List<int> queries) {\n\n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minOperations(nums []int, queries []int) []int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer[]} queries\n# @return {Integer[]}\ndef min_operations(nums, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minOperations(nums: Array[Int], queries: Array[Int]): List[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_operations(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-operations nums queries)\n (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?))\n\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_operations(Nums :: [integer()], Queries :: [integer()]) -> [integer()].\nmin_operations(Nums, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_operations(nums :: [integer], queries :: [integer]) :: [integer]\n def min_operations(nums, queries) do\n\n end\nend"}]
[3,1,6,8] [1,5]
{ "name": "minOperations", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[]", "name": "queries" } ], "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>"]}
['Array', 'Binary Search', 'Sorting', 'Prefix Sum']
2,603
Collect Coins in a Tree
collect-coins-in-a-tree
<p>There exists an undirected and unrooted tree with <code>n</code> nodes indexed from <code>0</code> to <code>n - 1</code>. You are given an integer <code>n</code> and a 2D integer array edges of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree. You are also given&nbsp;an array <code>coins</code> of size <code>n</code> where <code>coins[i]</code> can be either <code>0</code> or <code>1</code>, where <code>1</code> indicates the presence of a coin in the vertex <code>i</code>.</p> <p>Initially, you choose to start at any vertex in&nbsp;the tree.&nbsp;Then, you can perform&nbsp;the following operations any number of times:&nbsp;</p> <ul> <li>Collect all the coins that are at a distance of at most <code>2</code> from the current vertex, or</li> <li>Move to any adjacent vertex in the tree.</li> </ul> <p>Find <em>the minimum number of edges you need to go through to collect all the coins and go back to the initial vertex</em>.</p> <p>Note that if you pass an edge several times, you need to count it into the answer several times.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/03/01/graph-2.png" style="width: 522px; height: 522px;" /> <pre> <strong>Input:</strong> coins = [1,0,0,0,0,1], edges = [[0,1],[1,2],[2,3],[3,4],[4,5]] <strong>Output:</strong> 2 <strong>Explanation:</strong> Start at vertex 2, collect the coin at vertex 0, move to vertex 3, collect the coin at vertex 5 then move back to vertex 2. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/03/02/graph-4.png" style="width: 522px; height: 522px;" /> <pre> <strong>Input:</strong> coins = [0,0,0,1,1,0,0,1], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[5,6],[5,7]] <strong>Output:</strong> 2 <strong>Explanation:</strong> Start at vertex 0, collect the coins at vertices 4 and 3, move to vertex 2, collect the coin at vertex 7, then move back to vertex 0. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == coins.length</code></li> <li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li> <li><code>0 &lt;= coins[i] &lt;= 1</code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li> <li><code>a<sub>i</sub> != b<sub>i</sub></code></li> <li><code>edges</code> represents a valid tree.</li> </ul>
Hard
9.1K
25.5K
9,074
25,544
35.5%
['minimum-height-trees', 'sum-of-distances-in-tree', 'maximum-score-after-applying-operations-on-a-tree', 'find-number-of-coins-to-place-in-tree-nodes']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int collectTheCoins(vector<int>& coins, vector<vector<int>>& edges) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int collectTheCoins(int[] coins, int[][] edges) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def collectTheCoins(self, coins, edges):\n \"\"\"\n :type coins: List[int]\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def collectTheCoins(self, coins: List[int], edges: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int collectTheCoins(int* coins, int coinsSize, int** edges, int edgesSize, int* edgesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CollectTheCoins(int[] coins, int[][] edges) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} coins\n * @param {number[][]} edges\n * @return {number}\n */\nvar collectTheCoins = function(coins, edges) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function collectTheCoins(coins: number[], edges: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $coins\n * @param Integer[][] $edges\n * @return Integer\n */\n function collectTheCoins($coins, $edges) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func collectTheCoins(_ coins: [Int], _ edges: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun collectTheCoins(coins: IntArray, edges: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int collectTheCoins(List<int> coins, List<List<int>> edges) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func collectTheCoins(coins []int, edges [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} coins\n# @param {Integer[][]} edges\n# @return {Integer}\ndef collect_the_coins(coins, edges)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def collectTheCoins(coins: Array[Int], edges: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn collect_the_coins(coins: Vec<i32>, edges: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (collect-the-coins coins edges)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec collect_the_coins(Coins :: [integer()], Edges :: [[integer()]]) -> integer().\ncollect_the_coins(Coins, Edges) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec collect_the_coins(coins :: [integer], edges :: [[integer]]) :: integer\n def collect_the_coins(coins, edges) do\n \n end\nend"}]
[1,0,0,0,0,1] [[0,1],[1,2],[2,3],[3,4],[4,5]]
{ "name": "collectTheCoins", "params": [ { "name": "coins", "type": "integer[]" }, { "type": "integer[][]", "name": "edges" } ], "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', 'Tree', 'Graph', 'Topological Sort']
2,604
Minimum Time to Eat All Grains
minimum-time-to-eat-all-grains
null
Hard
1.7K
4.2K
1,657
4,214
39.3%
['kth-smallest-number-in-multiplication-table']
[]
Algorithms
null
[3,6,7] [2,4,7,9]
{ "name": "minimumTime", "params": [ { "name": "hens", "type": "integer[]" }, { "type": "integer[]", "name": "grains" } ], "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', 'Binary Search', 'Sorting']
2,605
Form Smallest Number From Two Digit Arrays
form-smallest-number-from-two-digit-arrays
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7] <strong>Output:</strong> 15 <strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7] <strong>Output:</strong> 3 <strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums1.length, nums2.length &lt;= 9</code></li> <li><code>1 &lt;= nums1[i], nums2[i] &lt;= 9</code></li> <li>All digits in each array are <strong>unique</strong>.</li> </ul>
Easy
44.1K
81.4K
44,072
81,402
54.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minNumber(vector<int>& nums1, vector<int>& nums2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minNumber(int[] nums1, int[] nums2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minNumber(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 minNumber(self, nums1: List[int], nums2: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minNumber(int* nums1, int nums1Size, int* nums2, int nums2Size) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinNumber(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 minNumber = function(nums1, nums2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minNumber(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 minNumber($nums1, $nums2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minNumber(_ nums1: [Int], _ nums2: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minNumber(nums1: IntArray, nums2: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minNumber(List<int> nums1, List<int> nums2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minNumber(nums1 []int, nums2 []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums1\n# @param {Integer[]} nums2\n# @return {Integer}\ndef min_number(nums1, nums2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minNumber(nums1: Array[Int], nums2: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-number nums1 nums2)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_number(Nums1 :: [integer()], Nums2 :: [integer()]) -> integer().\nmin_number(Nums1, Nums2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_number(nums1 :: [integer], nums2 :: [integer]) :: integer\n def min_number(nums1, nums2) do\n \n end\nend"}]
[4,1,3] [5,7]
{ "name": "minNumber", "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', 'Hash Table', 'Enumeration']
2,606
Find the Substring With Maximum Cost
find-the-substring-with-maximum-cost
<p>You are given a string <code>s</code>, a string <code>chars</code> of <strong>distinct</strong> characters and an integer array <code>vals</code> of the same length as <code>chars</code>.</p> <p>The <strong>cost of the substring </strong>is the sum of the values of each character in the substring. The cost of an empty string is considered <code>0</code>.</p> <p>The <strong>value of the character </strong>is defined in the following way:</p> <ul> <li>If the character is not in the string <code>chars</code>, then its value is its corresponding position <strong>(1-indexed)</strong> in the alphabet. <ul> <li>For example, the value of <code>&#39;a&#39;</code> is <code>1</code>, the value of <code>&#39;b&#39;</code> is <code>2</code>, and so on. The value of <code>&#39;z&#39;</code> is <code>26</code>.</li> </ul> </li> <li>Otherwise, assuming <code>i</code> is the index where the character occurs in the string <code>chars</code>, then its value is <code>vals[i]</code>.</li> </ul> <p>Return <em>the maximum cost among all substrings of the string</em> <code>s</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;adaa&quot;, chars = &quot;d&quot;, vals = [-1000] <strong>Output:</strong> 2 <strong>Explanation:</strong> The value of the characters &quot;a&quot; and &quot;d&quot; is 1 and -1000 respectively. The substring with the maximum cost is &quot;aa&quot; and its cost is 1 + 1 = 2. It can be proven that 2 is the maximum cost. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;abc&quot;, chars = &quot;abc&quot;, vals = [-1,-1,-1] <strong>Output:</strong> 0 <strong>Explanation:</strong> The value of the characters &quot;a&quot;, &quot;b&quot; and &quot;c&quot; is -1, -1, and -1 respectively. The substring with the maximum cost is the empty substring &quot;&quot; and its cost is 0. It can be proven that 0 is the maximum cost. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consist of lowercase English letters.</li> <li><code>1 &lt;= chars.length &lt;= 26</code></li> <li><code>chars</code> consist of <strong>distinct</strong> lowercase English letters.</li> <li><code>vals.length == chars.length</code></li> <li><code>-1000 &lt;= vals[i] &lt;= 1000</code></li> </ul>
Medium
28.1K
49.7K
28,082
49,685
56.5%
['maximum-subarray']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumCostSubstring(string s, string chars, vector<int>& vals) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumCostSubstring(String s, String chars, int[] vals) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumCostSubstring(self, s, chars, vals):\n \"\"\"\n :type s: str\n :type chars: str\n :type vals: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumCostSubstring(self, s: str, chars: str, vals: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumCostSubstring(char* s, char* chars, int* vals, int valsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumCostSubstring(string s, string chars, int[] vals) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {string} chars\n * @param {number[]} vals\n * @return {number}\n */\nvar maximumCostSubstring = function(s, chars, vals) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumCostSubstring(s: string, chars: string, vals: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param String $chars\n * @param Integer[] $vals\n * @return Integer\n */\n function maximumCostSubstring($s, $chars, $vals) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumCostSubstring(_ s: String, _ chars: String, _ vals: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumCostSubstring(s: String, chars: String, vals: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumCostSubstring(String s, String chars, List<int> vals) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumCostSubstring(s string, chars string, vals []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} chars\n# @param {Integer[]} vals\n# @return {Integer}\ndef maximum_cost_substring(s, chars, vals)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumCostSubstring(s: String, chars: String, vals: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_cost_substring(s: String, chars: String, vals: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-cost-substring s chars vals)\n (-> string? string? (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_cost_substring(S :: unicode:unicode_binary(), Chars :: unicode:unicode_binary(), Vals :: [integer()]) -> integer().\nmaximum_cost_substring(S, Chars, Vals) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_cost_substring(s :: String.t, chars :: String.t, vals :: [integer]) :: integer\n def maximum_cost_substring(s, chars, vals) do\n \n end\nend"}]
"adaa" "d" [-1000]
{ "name": "maximumCostSubstring", "params": [ { "name": "s", "type": "string" }, { "type": "string", "name": "chars" }, { "type": "integer[]", "name": "vals" } ], "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', 'Dynamic Programming']
2,607
Make K-Subarray Sums Equal
make-k-subarray-sums-equal
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code> and an integer <code>k</code>. The array <code>arr</code> is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.</p> <p>You can do the following operation any number of times:</p> <ul> <li>Pick any element from <code>arr</code> and increase or decrease it by <code>1</code>.</li> </ul> <p>Return <em>the minimum number of operations such that the sum of each <strong>subarray</strong> of length </em><code>k</code><em> is equal</em>.</p> <p>A <strong>subarray</strong> is a contiguous part of the array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> arr = [1,4,1,3], k = 2 <strong>Output:</strong> 1 <strong>Explanation:</strong> we can do one operation on index 1 to make its value equal to 3. The array after the operation is [1,3,1,3] - Subarray starts at index 0 is [1, 3], and its sum is 4 - Subarray starts at index 1 is [3, 1], and its sum is 4 - Subarray starts at index 2 is [1, 3], and its sum is 4 - Subarray starts at index 3 is [3, 1], and its sum is 4 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> arr = [2,5,5,7], k = 3 <strong>Output:</strong> 5 <strong>Explanation:</strong> we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5. The array after the operations is [5,5,5,5] - Subarray starts at index 0 is [5, 5, 5], and its sum is 15 - Subarray starts at index 1 is [5, 5, 5], and its sum is 15 - Subarray starts at index 2 is [5, 5, 5], and its sum is 15 - Subarray starts at index 3 is [5, 5, 5], and its sum is 15 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= arr.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
12K
33.2K
11,996
33,156
36.2%
['rotate-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long makeSubKSumEqual(vector<int>& arr, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long makeSubKSumEqual(int[] arr, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def makeSubKSumEqual(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 makeSubKSumEqual(self, arr: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long makeSubKSumEqual(int* arr, int arrSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MakeSubKSumEqual(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 makeSubKSumEqual = function(arr, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function makeSubKSumEqual(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 makeSubKSumEqual($arr, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func makeSubKSumEqual(_ arr: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun makeSubKSumEqual(arr: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int makeSubKSumEqual(List<int> arr, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func makeSubKSumEqual(arr []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} arr\n# @param {Integer} k\n# @return {Integer}\ndef make_sub_k_sum_equal(arr, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def makeSubKSumEqual(arr: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn make_sub_k_sum_equal(arr: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (make-sub-k-sum-equal arr k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec make_sub_k_sum_equal(Arr :: [integer()], K :: integer()) -> integer().\nmake_sub_k_sum_equal(Arr, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec make_sub_k_sum_equal(arr :: [integer], k :: integer) :: integer\n def make_sub_k_sum_equal(arr, k) do\n \n end\nend"}]
[1,4,1,3] 2
{ "name": "makeSubKSumEqual", "params": [ { "name": "arr", "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', 'Number Theory']
2,608
Shortest Cycle in a Graph
shortest-cycle-in-a-graph
<p>There is a <strong>bi-directional </strong>graph with <code>n</code> vertices, where each vertex is labeled from <code>0</code> to <code>n - 1</code>. The edges in the graph are represented by a given 2D integer array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between vertex <code>u<sub>i</sub></code> and vertex <code>v<sub>i</sub></code>. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.</p> <p>Return <em>the length of the <strong>shortest </strong>cycle in the graph</em>. If no cycle exists, return <code>-1</code>.</p> <p>A cycle is a path that starts and ends at the same node, and each edge in the path is used only once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/cropped.png" style="width: 387px; height: 331px;" /> <pre> <strong>Input:</strong> n = 7, edges = [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]] <strong>Output:</strong> 3 <strong>Explanation:</strong> The cycle with the smallest length is : 0 -&gt; 1 -&gt; 2 -&gt; 0 </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2023/01/04/croppedagin.png" style="width: 307px; height: 307px;" /> <pre> <strong>Input:</strong> n = 4, edges = [[0,1],[0,2]] <strong>Output:</strong> -1 <strong>Explanation:</strong> There are no cycles in this graph. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= edges.length &lt;= 1000</code></li> <li><code>edges[i].length == 2</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li> <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> <li>There are no repeated edges.</li> </ul>
Hard
22.9K
61.9K
22,950
61,871
37.1%
['redundant-connection', 'longest-cycle-in-a-graph', 'divide-nodes-into-the-maximum-number-of-groups']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int findShortestCycle(int n, vector<vector<int>>& edges) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int findShortestCycle(int n, int[][] edges) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findShortestCycle(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findShortestCycle(self, n: int, edges: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int findShortestCycle(int n, int** edges, int edgesSize, int* edgesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int FindShortestCycle(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 findShortestCycle = function(n, edges) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findShortestCycle(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 findShortestCycle($n, $edges) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findShortestCycle(_ n: Int, _ edges: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findShortestCycle(n: Int, edges: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int findShortestCycle(int n, List<List<int>> edges) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findShortestCycle(n int, edges [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} edges\n# @return {Integer}\ndef find_shortest_cycle(n, edges)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findShortestCycle(n: Int, edges: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_shortest_cycle(n: i32, edges: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-shortest-cycle n edges)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_shortest_cycle(N :: integer(), Edges :: [[integer()]]) -> integer().\nfind_shortest_cycle(N, Edges) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_shortest_cycle(n :: integer, edges :: [[integer]]) :: integer\n def find_shortest_cycle(n, edges) do\n \n end\nend"}]
7 [[0,1],[1,2],[2,0],[3,4],[4,5],[5,6],[6,3]]
{ "name": "findShortestCycle", "params": [ { "type": "integer", "name": "n" }, { "type": "integer[][]", "name": "edges" } ], "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>"]}
['Breadth-First Search', 'Graph']