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
3,410
Maximize Subarray Sum After Removing All Occurrences of One Element
maximize-subarray-sum-after-removing-all-occurrences-of-one-element
<p>You are given an integer array <code>nums</code>.</p> <p>You can do the following operation on the array <strong>at most</strong> once:</p> <ul> <li>Choose <strong>any</strong> integer <code>x</code> such that <code>nums</code> remains <strong>non-empty</strong> on removing all occurrences of <code>x</code>.</li> <li>Remove&nbsp;<strong>all</strong> occurrences of <code>x</code> from the array.</li> </ul> <p>Return the <strong>maximum</strong> <span data-keyword="subarray-nonempty">subarray</span> sum across <strong>all</strong> possible resulting arrays.</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 = [-3,2,-2,-1,3,-2,3]</span></p> <p><strong>Output:</strong> <span class="example-io">7</span></p> <p><strong>Explanation:</strong></p> <p>We can have the following arrays after at most one operation:</p> <ul> <li>The original array is <code>nums = [<span class="example-io">-3, 2, -2, -1, <u><strong>3, -2, 3</strong></u></span>]</code>. The maximum subarray sum is <code>3 + (-2) + 3 = 4</code>.</li> <li>Deleting all occurences of <code>x = -3</code> results in <code>nums = [2, -2, -1, <strong><u><span class="example-io">3, -2, 3</span></u></strong>]</code>. The maximum subarray sum is <code>3 + (-2) + 3 = 4</code>.</li> <li>Deleting all occurences of <code>x = -2</code> results in <code>nums = [<span class="example-io">-3, <strong><u>2, -1, 3, 3</u></strong></span>]</code>. The maximum subarray sum is <code>2 + (-1) + 3 + 3 = 7</code>.</li> <li>Deleting all occurences of <code>x = -1</code> results in <code>nums = [<span class="example-io">-3, 2, -2, <strong><u>3, -2, 3</u></strong></span>]</code>. The maximum subarray sum is <code>3 + (-2) + 3 = 4</code>.</li> <li>Deleting all occurences of <code>x = 3</code> results in <code>nums = [<span class="example-io">-3, <u><strong>2</strong></u>, -2, -1, -2</span>]</code>. The maximum subarray sum is 2.</li> </ul> <p>The output is <code>max(4, 4, 7, 4, 2) = 7</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>It is optimal to not perform any operations.</p> </div> <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>
Hard
2.6K
12.9K
2,625
12,942
20.3%
['maximum-subarray', 'maximum-subarray-sum-with-one-deletion']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxSubarraySum(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxSubarraySum(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSubarraySum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSubarraySum(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxSubarraySum(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxSubarraySum(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxSubarraySum = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSubarraySum(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxSubarraySum($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSubarraySum(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSubarraySum(nums: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxSubarraySum(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSubarraySum(nums []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_subarray_sum(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSubarraySum(nums: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_subarray_sum(nums: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-subarray-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_subarray_sum(Nums :: [integer()]) -> integer().\nmax_subarray_sum(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_subarray_sum(nums :: [integer]) :: integer\n def max_subarray_sum(nums) do\n \n end\nend"}]
[-3,2,-2,-1,3,-2,3]
{ "name": "maxSubarraySum", "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', 'Segment Tree']
3,411
Maximum Subarray With Equal Products
maximum-subarray-with-equal-products
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p> <p>An array <code>arr</code> is called <strong>product equivalent</strong> if <code>prod(arr) == lcm(arr) * gcd(arr)</code>, where:</p> <ul> <li><code>prod(arr)</code> is the product of all elements of <code>arr</code>.</li> <li><code>gcd(arr)</code> is the <span data-keyword="gcd-function">GCD</span> of all elements of <code>arr</code>.</li> <li><code>lcm(arr)</code> is the <span data-keyword="lcm-function">LCM</span> of all elements of <code>arr</code>.</li> </ul> <p>Return the length of the <strong>longest</strong> <strong>product equivalent</strong> <span data-keyword="subarray-nonempty">subarray</span> 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,2,1,2,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong>&nbsp;</p> <p>The longest product equivalent subarray is <code>[1, 2, 1, 1, 1]</code>, where&nbsp;<code>prod([1, 2, 1, 1, 1]) = 2</code>,&nbsp;<code>gcd([1, 2, 1, 1, 1]) = 1</code>, and&nbsp;<code>lcm([1, 2, 1, 1, 1]) = 2</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,4,5,6]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong>&nbsp;</p> <p>The longest product equivalent subarray is <code>[3, 4, 5].</code></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,4,5,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 10</code></li> </ul>
Easy
23.4K
50.8K
23,383
50,826
46.0%
['find-greatest-common-divisor-of-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxLength(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxLength(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxLength(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxLength(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxLength(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxLength = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxLength(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxLength($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxLength(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxLength(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxLength(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxLength(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_length(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxLength(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_length(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-length nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_length(Nums :: [integer()]) -> integer().\nmax_length(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_length(nums :: [integer]) :: integer\n def max_length(nums) do\n \n end\nend"}]
[1,2,1,2,1,1,1]
{ "name": "maxLength", "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', 'Sliding Window', 'Enumeration', 'Number Theory']
3,412
Find Mirror Score of a String
find-mirror-score-of-a-string
<p>You are given a string <code>s</code>.</p> <p>We define the <strong>mirror</strong> of a letter in the English alphabet as its corresponding letter when the alphabet is reversed. For example, the mirror of <code>&#39;a&#39;</code> is <code>&#39;z&#39;</code>, and the mirror of <code>&#39;y&#39;</code> is <code>&#39;b&#39;</code>.</p> <p>Initially, all characters in the string <code>s</code> are <strong>unmarked</strong>.</p> <p>You start with a score of 0, and you perform the following process on the string <code>s</code>:</p> <ul> <li>Iterate through the string from left to right.</li> <li>At each index <code>i</code>, find the closest <strong>unmarked</strong> index <code>j</code> such that <code>j &lt; i</code> and <code>s[j]</code> is the mirror of <code>s[i]</code>. Then, <strong>mark</strong> both indices <code>i</code> and <code>j</code>, and add the value <code>i - j</code> to the total score.</li> <li>If no such index <code>j</code> exists for the index <code>i</code>, move on to the next index without making any changes.</li> </ul> <p>Return the total score at the end of the process.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aczzx&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>i = 0</code>. There is no index <code>j</code> that satisfies the conditions, so we skip.</li> <li><code>i = 1</code>. There is no index <code>j</code> that satisfies the conditions, so we skip.</li> <li><code>i = 2</code>. The closest index <code>j</code> that satisfies the conditions is <code>j = 0</code>, so we mark both indices 0 and 2, and then add <code>2 - 0 = 2</code> to the score.</li> <li><code>i = 3</code>. There is no index <code>j</code> that satisfies the conditions, so we skip.</li> <li><code>i = 4</code>. The closest index <code>j</code> that satisfies the conditions is <code>j = 1</code>, so we mark both indices 1 and 4, and then add <code>4 - 1 = 3</code> to the score.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abcdef&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>For each index <code>i</code>, there is no index <code>j</code> that satisfies the conditions.</p> </div> <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 only of lowercase English letters.</li> </ul>
Medium
20.8K
57.7K
20,834
57,659
36.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long calculateScore(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long calculateScore(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def calculateScore(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def calculateScore(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long calculateScore(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CalculateScore(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar calculateScore = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function calculateScore(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function calculateScore($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func calculateScore(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun calculateScore(s: String): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int calculateScore(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func calculateScore(s string) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef calculate_score(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def calculateScore(s: String): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn calculate_score(s: String) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (calculate-score s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec calculate_score(S :: unicode:unicode_binary()) -> integer().\ncalculate_score(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec calculate_score(s :: String.t) :: integer\n def calculate_score(s) do\n \n end\nend"}]
"aczzx"
{ "name": "calculateScore", "params": [ { "name": "s", "type": "string" } ], "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>"]}
['Hash Table', 'String', 'Stack', 'Simulation']
3,413
Maximum Coins From K Consecutive Bags
maximum-coins-from-k-consecutive-bags
<p>There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins.</p> <p>You are given a 2D array <code>coins</code>, where <code>coins[i] = [l<sub>i</sub>, r<sub>i</sub>, c<sub>i</sub>]</code> denotes that every bag from <code>l<sub>i</sub></code> to <code>r<sub>i</sub></code> contains <code>c<sub>i</sub></code> coins.</p> <p>The segments that <code>coins</code> contain are non-overlapping.</p> <p>You are also given an integer <code>k</code>.</p> <p>Return the <strong>maximum</strong> amount of coins you can obtain by collecting <code>k</code> consecutive bags.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coins = [[8,10,1],[1,3,2],[5,6,4]], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>Selecting bags at positions <code>[3, 4, 5, 6]</code> gives the maximum number of coins:&nbsp;<code>2 + 0 + 4 + 4 = 10</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coins = [[1,10,3]], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>Selecting bags at positions <code>[1, 2]</code> gives the maximum number of coins:&nbsp;<code>3 + 3 = 6</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= coins.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>coins[i] == [l<sub>i</sub>, r<sub>i</sub>, c<sub>i</sub>]</code></li> <li><code>1 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= c<sub>i</sub> &lt;= 1000</code></li> <li>The given segments are non-overlapping.</li> </ul>
Medium
6.7K
28.5K
6,714
28,511
23.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maximumCoins(vector<vector<int>>& coins, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maximumCoins(int[][] coins, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumCoins(self, coins, k):\n \"\"\"\n :type coins: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumCoins(self, coins: List[List[int]], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maximumCoins(int** coins, int coinsSize, int* coinsColSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaximumCoins(int[][] coins, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} coins\n * @param {number} k\n * @return {number}\n */\nvar maximumCoins = function(coins, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumCoins(coins: number[][], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $coins\n * @param Integer $k\n * @return Integer\n */\n function maximumCoins($coins, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumCoins(_ coins: [[Int]], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumCoins(coins: Array<IntArray>, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumCoins(List<List<int>> coins, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumCoins(coins [][]int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} coins\n# @param {Integer} k\n# @return {Integer}\ndef maximum_coins(coins, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumCoins(coins: Array[Array[Int]], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_coins(coins: Vec<Vec<i32>>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-coins coins k)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_coins(Coins :: [[integer()]], K :: integer()) -> integer().\nmaximum_coins(Coins, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_coins(coins :: [[integer]], k :: integer) :: integer\n def maximum_coins(coins, k) do\n \n end\nend"}]
[[8,10,1],[1,3,2],[5,6,4]] 4
{ "name": "maximumCoins", "params": [ { "name": "coins", "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', 'Binary Search', 'Greedy', 'Sliding Window', 'Sorting', 'Prefix Sum']
3,414
Maximum Score of Non-overlapping Intervals
maximum-score-of-non-overlapping-intervals
<p>You are given a 2D integer array <code>intervals</code>, where <code>intervals[i] = [l<sub>i</sub>, r<sub>i</sub>, weight<sub>i</sub>]</code>. Interval <code>i</code> starts at position <code>l<sub>i</sub></code> and ends at <code>r<sub>i</sub></code>, and has a weight of <code>weight<sub>i</sub></code>. You can choose <em>up to</em> 4 <strong>non-overlapping</strong> intervals. The <strong>score</strong> of the chosen intervals is defined as the total sum of their weights.</p> <p>Return the <span data-keyword="lexicographically-smaller-array">lexicographically smallest</span> array of at most 4 indices from <code>intervals</code> with <strong>maximum</strong> score, representing your choice of non-overlapping intervals.</p> <p>Two intervals are said to be <strong>non-overlapping</strong> if they do not share any points. In particular, intervals sharing a left or right boundary are considered overlapping.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">intervals = [[1,3,2],[4,5,2],[1,5,5],[6,9,3],[6,7,1],[8,9,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,3]</span></p> <p><strong>Explanation:</strong></p> <p>You can choose the intervals with indices 2, and 3 with respective weights of 5, and 3.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">intervals = [[5,8,1],[6,7,7],[4,7,3],[9,10,6],[7,8,2],[11,14,3],[3,5,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,3,5,6]</span></p> <p><strong>Explanation:</strong></p> <p>You can choose the intervals with indices 1, 3, 5, and 6 with respective weights of 7, 6, 3, and 5.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= intevals.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>intervals[i].length == 3</code></li> <li><code>intervals[i] = [l<sub>i</sub>, r<sub>i</sub>, weight<sub>i</sub>]</code></li> <li><code>1 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= weight<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
Hard
3.1K
9.8K
3,142
9,754
32.2%
['two-best-non-overlapping-events']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> maximumWeight(vector<vector<int>>& intervals) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] maximumWeight(List<List<Integer>> intervals) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumWeight(self, intervals):\n \"\"\"\n :type intervals: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumWeight(self, intervals: 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* maximumWeight(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] MaximumWeight(IList<IList<int>> intervals) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} intervals\n * @return {number[]}\n */\nvar maximumWeight = function(intervals) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumWeight(intervals: number[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $intervals\n * @return Integer[]\n */\n function maximumWeight($intervals) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumWeight(_ intervals: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumWeight(intervals: List<List<Int>>): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> maximumWeight(List<List<int>> intervals) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumWeight(intervals [][]int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} intervals\n# @return {Integer[]}\ndef maximum_weight(intervals)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumWeight(intervals: List[List[Int]]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_weight(intervals: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-weight intervals)\n (-> (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_weight(Intervals :: [[integer()]]) -> [integer()].\nmaximum_weight(Intervals) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_weight(intervals :: [[integer]]) :: [integer]\n def maximum_weight(intervals) do\n \n end\nend"}]
[[1,3,2],[4,5,2],[1,5,5],[6,9,3],[6,7,1],[8,9,1]]
{ "name": "maximumWeight", "params": [ { "name": "intervals", "type": "list<list<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', 'Dynamic Programming', 'Sorting']
3,415
Find Products with Three Consecutive Digits
find-products-with-three-consecutive-digits
null
Easy
851
1K
851
1,031
82.5%
[]
['CREATE TABLE if not exists products (\n product_id INT,\n name VARCHAR(255)\n)\n', 'Truncate table Products', "insert into Products (product_id, name) values ('1', 'ABC123XYZ')", "insert into Products (product_id, name) values ('2', 'A12B34C')", "insert into Products (product_id, name) values ('3', 'Product56789')", "insert into Products (product_id, name) values ('4', 'NoDigitsHere')", "insert into Products (product_id, name) values ('5', '789Product')", "insert into Products (product_id, name) values ('6', 'Item003Description')", "insert into Products (product_id, name) values ('7', 'Product12X34')"]
Database
null
{"headers":{"Products":["product_id","name"]},"rows":{"Products":[[1,"ABC123XYZ"],[2,"A12B34C"],[3,"Product56789"],[4,"NoDigitsHere"],[5,"789Product"],[6,"Item003Description"],[7,"Product12X34"]]}}
{"mysql": ["CREATE TABLE if not exists products (\n product_id INT,\n name VARCHAR(255)\n)\n"], "mssql": ["CREATE TABLE products (\n product_id INT,\n name VARCHAR(255)\n)\n"], "oraclesql": ["CREATE TABLE products (\n product_id NUMBER,\n name VARCHAR2(255)\n)\n"], "postgresql": ["CREATE TABLE if not exists products (\n product_id INT,\n name VARCHAR(255)\n)\n"], "database": true, "name": "find_products", "pythondata": ["Products = pd.DataFrame(columns=['product_id', 'name']).astype({'product_id': 'int', 'name': 'string'})"], "database_schema": {"products": {"product_id": "INT", "name": "VARCHAR(255)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,416
Subsequences with a Unique Middle Mode II
subsequences-with-a-unique-middle-mode-ii
null
Hard
124
285
124
285
43.5%
['subsequences-with-a-unique-middle-mode-i']
[]
Algorithms
null
[1,1,1,1,1,1]
{ "name": "subsequencesWithMiddleMode", "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', 'Combinatorics']
3,417
Zigzag Grid Traversal With Skip
zigzag-grid-traversal-with-skip
<p>You are given an <code>m x n</code> 2D array <code>grid</code> of <strong>positive</strong> integers.</p> <p>Your task is to traverse <code>grid</code> in a <strong>zigzag</strong> pattern while skipping every <strong>alternate</strong> cell.</p> <p>Zigzag pattern traversal is defined as following the below actions:</p> <ul> <li>Start at the top-left cell <code>(0, 0)</code>.</li> <li>Move <em>right</em> within a row until the end of the row is reached.</li> <li>Drop down to the next row, then traverse <em>left</em> until the beginning of the row is reached.</li> <li>Continue <strong>alternating</strong> between right and left traversal until every row has been traversed.</li> </ul> <p><strong>Note </strong>that you <strong>must skip</strong> every <em>alternate</em> cell during the traversal.</p> <p>Return an array of integers <code>result</code> containing, <strong>in order</strong>, the value of the cells visited during the zigzag traversal with skips.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2],[3,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,4]</span></p> <p><strong>Explanation:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/11/23/4012_example0.png" style="width: 200px; height: 200px;" /></strong></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[2,1],[2,1],[2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,1,2]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/23/4012_example1.png" style="width: 200px; height: 240px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,3],[4,5,6],[7,8,9]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,3,5,7,9]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/23/4012_example2.png" style="width: 260px; height: 250px;" /></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == grid.length &lt;= 50</code></li> <li><code>2 &lt;= m == grid[i].length &lt;= 50</code></li> <li><code>1 &lt;= grid[i][j] &lt;= 2500</code></li> </ul>
Easy
31.7K
49.4K
31,700
49,420
64.1%
['binary-tree-zigzag-level-order-traversal', 'longest-zigzag-path-in-a-binary-tree']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> zigzagTraversal(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> zigzagTraversal(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def zigzagTraversal(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def zigzagTraversal(self, grid: 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* zigzagTraversal(int** grid, int gridSize, int* gridColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> ZigzagTraversal(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {number[]}\n */\nvar zigzagTraversal = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function zigzagTraversal(grid: number[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer[]\n */\n function zigzagTraversal($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func zigzagTraversal(_ grid: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun zigzagTraversal(grid: Array<IntArray>): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> zigzagTraversal(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func zigzagTraversal(grid [][]int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Integer[]}\ndef zigzag_traversal(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def zigzagTraversal(grid: Array[Array[Int]]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn zigzag_traversal(grid: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (zigzag-traversal grid)\n (-> (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec zigzag_traversal(Grid :: [[integer()]]) -> [integer()].\nzigzag_traversal(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec zigzag_traversal(grid :: [[integer]]) :: [integer]\n def zigzag_traversal(grid) do\n \n end\nend"}]
[[1,2],[3,4]]
{ "name": "zigzagTraversal", "params": [ { "name": "grid", "type": "integer[][]" } ], "return": { "type": "list<integer>" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Matrix', 'Simulation']
3,418
Maximum Amount of Money Robot Can Earn
maximum-amount-of-money-robot-can-earn
<p>You are given an <code>m x n</code> grid. A robot starts at the top-left corner of the grid <code>(0, 0)</code> and wants to reach the bottom-right corner <code>(m - 1, n - 1)</code>. The robot can move either right or down at any point in time.</p> <p>The grid contains a value <code>coins[i][j]</code> in each cell:</p> <ul> <li>If <code>coins[i][j] &gt;= 0</code>, the robot gains that many coins.</li> <li>If <code>coins[i][j] &lt; 0</code>, the robot encounters a robber, and the robber steals the <strong>absolute</strong> value of <code>coins[i][j]</code> coins.</li> </ul> <p>The robot has a special ability to <strong>neutralize robbers</strong> in at most <strong>2 cells</strong> on its path, preventing them from stealing coins in those cells.</p> <p><strong>Note:</strong> The robot&#39;s total coins can be negative.</p> <p>Return the <strong>maximum</strong> profit the robot can gain on the route.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coins = [[0,1,-1],[1,-2,3],[2,-3,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p>An optimal path for maximum coins is:</p> <ol> <li>Start at <code>(0, 0)</code> with <code>0</code> coins (total coins = <code>0</code>).</li> <li>Move to <code>(0, 1)</code>, gaining <code>1</code> coin (total coins = <code>0 + 1 = 1</code>).</li> <li>Move to <code>(1, 1)</code>, where there&#39;s a robber stealing <code>2</code> coins. The robot uses one neutralization here, avoiding the robbery (total coins = <code>1</code>).</li> <li>Move to <code>(1, 2)</code>, gaining <code>3</code> coins (total coins = <code>1 + 3 = 4</code>).</li> <li>Move to <code>(2, 2)</code>, gaining <code>4</code> coins (total coins = <code>4 + 4 = 8</code>).</li> </ol> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">coins = [[10,10,10],[10,10,10]]</span></p> <p><strong>Output:</strong> <span class="example-io">40</span></p> <p><strong>Explanation:</strong></p> <p>An optimal path for maximum coins is:</p> <ol> <li>Start at <code>(0, 0)</code> with <code>10</code> coins (total coins = <code>10</code>).</li> <li>Move to <code>(0, 1)</code>, gaining <code>10</code> coins (total coins = <code>10 + 10 = 20</code>).</li> <li>Move to <code>(0, 2)</code>, gaining another <code>10</code> coins (total coins = <code>20 + 10 = 30</code>).</li> <li>Move to <code>(1, 2)</code>, gaining the final <code>10</code> coins (total coins = <code>30 + 10 = 40</code>).</li> </ol> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == coins.length</code></li> <li><code>n == coins[i].length</code></li> <li><code>1 &lt;= m, n &lt;= 500</code></li> <li><code>-1000 &lt;= coins[i][j] &lt;= 1000</code></li> </ul>
Medium
20.2K
67.9K
20,226
67,875
29.8%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maximumAmount(vector<vector<int>>& coins) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maximumAmount(int[][] coins) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maximumAmount(self, coins):\n \"\"\"\n :type coins: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maximumAmount(self, coins: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maximumAmount(int** coins, int coinsSize, int* coinsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaximumAmount(int[][] coins) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} coins\n * @return {number}\n */\nvar maximumAmount = function(coins) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maximumAmount(coins: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $coins\n * @return Integer\n */\n function maximumAmount($coins) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maximumAmount(_ coins: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maximumAmount(coins: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maximumAmount(List<List<int>> coins) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maximumAmount(coins [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} coins\n# @return {Integer}\ndef maximum_amount(coins)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maximumAmount(coins: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn maximum_amount(coins: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (maximum-amount coins)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec maximum_amount(Coins :: [[integer()]]) -> integer().\nmaximum_amount(Coins) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec maximum_amount(coins :: [[integer]]) :: integer\n def maximum_amount(coins) do\n \n end\nend"}]
[[0,1,-1],[1,-2,3],[2,-3,4]]
{ "name": "maximumAmount", "params": [ { "name": "coins", "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', 'Dynamic Programming', 'Matrix']
3,419
Minimize the Maximum Edge Weight of Graph
minimize-the-maximum-edge-weight-of-graph
<p>You are given two integers, <code>n</code> and <code>threshold</code>, as well as a <strong>directed</strong> weighted graph of <code>n</code> nodes numbered from 0 to <code>n - 1</code>. The graph is represented by a <strong>2D</strong> integer array <code>edges</code>, where <code>edges[i] = [A<sub>i</sub>, B<sub>i</sub>, W<sub>i</sub>]</code> indicates that there is an edge going from node <code>A<sub>i</sub></code> to node <code>B<sub>i</sub></code> with weight <code>W<sub>i</sub></code>.</p> <p>You have to remove some edges from this graph (possibly <strong>none</strong>), so that it satisfies the following conditions:</p> <ul> <li>Node 0 must be reachable from all other nodes.</li> <li>The <strong>maximum</strong> edge weight in the resulting graph is <strong>minimized</strong>.</li> <li>Each node has <strong>at most</strong> <code>threshold</code> outgoing edges.</li> </ul> <p>Return the <strong>minimum</strong> possible value of the <strong>maximum</strong> edge weight after removing the necessary edges. If it is impossible for all conditions to be satisfied, return -1.</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 = 5, edges = [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]], threshold = 2</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/s-1.png" style="width: 300px; height: 233px;" /></p> <p>Remove the edge <code>2 -&gt; 0</code>. The maximum weight among the remaining edges is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[0,1,1],[0,2,2],[0,3,1],[0,4,1],[1,2,1],[1,4,1]], threshold = 1</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong>&nbsp;</p> <p>It is impossible to reach node 0 from node 2.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[3,4,2],[4,0,1]], threshold = 1</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong>&nbsp;</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/s2-1.png" style="width: 300px; height: 267px;" /></p> <p>Remove the edges <code>1 -&gt; 3</code> and <code>1 -&gt; 4</code>. The maximum weight among the remaining edges is 2.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, edges = [[1,2,1],[1,3,3],[1,4,5],[2,3,2],[4,0,1]], threshold = 1</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= threshold &lt;= n - 1</code></li> <li><code>1 &lt;= edges.length &lt;= min(10<sup>5</sup>, n * (n - 1) / 2).</code></li> <li><code>edges[i].length == 3</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>1 &lt;= W<sub>i</sub> &lt;= 10<sup>6</sup></code></li> <li>There <strong>may be</strong> multiple edges between a pair of nodes, but they must have unique weights.</li> </ul>
Medium
10.8K
25.1K
10,838
25,068
43.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minMaxWeight(int n, int[][] edges, int threshold) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMaxWeight(self, n, edges, threshold):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type threshold: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minMaxWeight(self, n: int, edges: List[List[int]], threshold: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minMaxWeight(int n, int** edges, int edgesSize, int* edgesColSize, int threshold) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinMaxWeight(int n, int[][] edges, int threshold) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} edges\n * @param {number} threshold\n * @return {number}\n */\nvar minMaxWeight = function(n, edges, threshold) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMaxWeight(n: number, edges: number[][], threshold: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $edges\n * @param Integer $threshold\n * @return Integer\n */\n function minMaxWeight($n, $edges, $threshold) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMaxWeight(_ n: Int, _ edges: [[Int]], _ threshold: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMaxWeight(n: Int, edges: Array<IntArray>, threshold: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMaxWeight(int n, List<List<int>> edges, int threshold) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMaxWeight(n int, edges [][]int, threshold int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} edges\n# @param {Integer} threshold\n# @return {Integer}\ndef min_max_weight(n, edges, threshold)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMaxWeight(n: Int, edges: Array[Array[Int]], threshold: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_max_weight(n: i32, edges: Vec<Vec<i32>>, threshold: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-max-weight n edges threshold)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_max_weight(N :: integer(), Edges :: [[integer()]], Threshold :: integer()) -> integer().\nmin_max_weight(N, Edges, Threshold) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_max_weight(n :: integer, edges :: [[integer]], threshold :: integer) :: integer\n def min_max_weight(n, edges, threshold) do\n \n end\nend"}]
5 [[1,0,1],[2,0,2],[3,0,1],[4,3,1],[2,1,1]] 2
{ "name": "minMaxWeight", "params": [ { "name": "n", "type": "integer" }, { "type": "integer[][]", "name": "edges" }, { "type": "integer", "name": "threshold" } ], "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>"]}
['Binary Search', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Shortest Path']
3,420
Count Non-Decreasing Subarrays After K Operations
count-non-decreasing-subarrays-after-k-operations
<p>You are given an array <code>nums</code> of <code>n</code> integers and an integer <code>k</code>.</p> <p>For each subarray of <code>nums</code>, you can apply <strong>up to</strong> <code>k</code> operations on it. In each operation, you increment any element of the subarray by 1.</p> <p><strong>Note</strong> that each subarray is considered independently, meaning changes made to one subarray do not persist to another.</p> <p>Return the number of subarrays that you can make <strong>non-decreasing</strong> ​​​​​after performing at most <code>k</code> operations.</p> <p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous element, if it exists.</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 = [6,3,1,2,4,4], k = 7</span></p> <p><strong>Output:</strong> <span class="example-io">17</span></p> <p><strong>Explanation:</strong></p> <p>Out of all 21 possible subarrays of <code>nums</code>, only the subarrays <code>[6, 3, 1]</code>, <code>[6, 3, 1, 2]</code>, <code>[6, 3, 1, 2, 4]</code> and <code>[6, 3, 1, 2, 4, 4]</code> cannot be made non-decreasing after applying up to k = 7 operations. Thus, the number of non-decreasing subarrays is <code>21 - 4 = 17</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [6,3,1,3,6], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The subarray <code>[3, 1, 3, 6]</code> along with all subarrays of <code>nums</code> with three or fewer elements, except <code>[6, 3, 1]</code>, can be made non-decreasing after <code>k</code> operations. There are 5 subarrays of a single element, 4 subarrays of two elements, and 2 subarrays of three elements except <code>[6, 3, 1]</code>, so there are <code>1 + 5 + 4 + 2 = 12</code> subarrays that can be made non-decreasing.</p> </div> <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;= 10<sup>9</sup></code></li> </ul>
Hard
2.8K
12.9K
2,817
12,878
21.9%
['non-decreasing-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countNonDecreasingSubarrays(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countNonDecreasingSubarrays(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countNonDecreasingSubarrays(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 countNonDecreasingSubarrays(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countNonDecreasingSubarrays(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountNonDecreasingSubarrays(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 countNonDecreasingSubarrays = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countNonDecreasingSubarrays(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 countNonDecreasingSubarrays($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countNonDecreasingSubarrays(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countNonDecreasingSubarrays(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countNonDecreasingSubarrays(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countNonDecreasingSubarrays(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef count_non_decreasing_subarrays(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countNonDecreasingSubarrays(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_non_decreasing_subarrays(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-non-decreasing-subarrays nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_non_decreasing_subarrays(Nums :: [integer()], K :: integer()) -> integer().\ncount_non_decreasing_subarrays(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_non_decreasing_subarrays(nums :: [integer], k :: integer) :: integer\n def count_non_decreasing_subarrays(nums, k) do\n \n end\nend"}]
[6,3,1,2,4,4] 7
{ "name": "countNonDecreasingSubarrays", "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', 'Stack', 'Segment Tree', 'Queue', 'Sliding Window', 'Monotonic Stack', 'Monotonic Queue']
3,421
Find Students Who Improved
find-students-who-improved
<p>Table: <code>Scores</code></p> <pre> +-------------+---------+ | Column Name | Type | +-------------+---------+ | student_id | int | | subject | varchar | | score | int | | exam_date | varchar | +-------------+---------+ (student_id, subject, exam_date) is the primary key for this table. Each row contains information about a student&#39;s score in a specific subject on a particular exam date. score is between 0 and 100 (inclusive). </pre> <p>Write a solution to find the <strong>students who have shown improvement</strong>. A student is considered to have shown improvement if they meet <strong>both</strong> of these conditions:</p> <ul> <li>Have taken exams in the <strong>same subject</strong> on at least two different dates</li> <li>Their <strong>latest score</strong> in that subject is <strong>higher</strong> than their <strong>first score</strong></li> </ul> <p>Return <em>the result table</em>&nbsp;<em>ordered by</em> <code>student_id,</code> <code>subject</code> <em>in <strong>ascending</strong> order</em>.</p> <p>The result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>Scores table:</p> <pre class="example-io"> +------------+----------+-------+------------+ | student_id | subject | score | exam_date | +------------+----------+-------+------------+ | 101 | Math | 70 | 2023-01-15 | | 101 | Math | 85 | 2023-02-15 | | 101 | Physics | 65 | 2023-01-15 | | 101 | Physics | 60 | 2023-02-15 | | 102 | Math | 80 | 2023-01-15 | | 102 | Math | 85 | 2023-02-15 | | 103 | Math | 90 | 2023-01-15 | | 104 | Physics | 75 | 2023-01-15 | | 104 | Physics | 85 | 2023-02-15 | +------------+----------+-------+------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +------------+----------+-------------+--------------+ | student_id | subject | first_score | latest_score | +------------+----------+-------------+--------------+ | 101 | Math | 70 | 85 | | 102 | Math | 80 | 85 | | 104 | Physics | 75 | 85 | +------------+----------+-------------+--------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Student 101 in Math: Improved from 70 to 85</li> <li>Student 101 in Physics: No improvement (dropped from 65 to 60)</li> <li>Student 102 in Math: Improved from 80 to 85</li> <li>Student 103 in Math: Only one exam, not eligible</li> <li>Student 104 in Physics: Improved from 75 to 85</li> </ul> <p>Result table is ordered by student_id, subject.</p> </div>
Medium
5.1K
10.5K
5,076
10,475
48.5%
[]
['CREATE TABLE Scores (\n student_id INT,\n subject VARCHAR(50),\n score INT,\n exam_date VARCHAR(10)\n)', 'Truncate table Scores', "insert into Scores (student_id, subject, score, exam_date) values ('101', 'Math', '70', '2023-01-15')", "insert into Scores (student_id, subject, score, exam_date) values ('101', 'Math', '85', '2023-02-15')", "insert into Scores (student_id, subject, score, exam_date) values ('101', 'Physics', '65', '2023-01-15')", "insert into Scores (student_id, subject, score, exam_date) values ('101', 'Physics', '60', '2023-02-15')", "insert into Scores (student_id, subject, score, exam_date) values ('102', 'Math', '80', '2023-01-15')", "insert into Scores (student_id, subject, score, exam_date) values ('102', 'Math', '85', '2023-02-15')", "insert into Scores (student_id, subject, score, exam_date) values ('103', 'Math', '90', '2023-01-15')", "insert into Scores (student_id, subject, score, exam_date) values ('104', 'Physics', '75', '2023-01-15')", "insert into Scores (student_id, subject, score, exam_date) values ('104', 'Physics', '85', '2023-02-15')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef find_students_who_improved(scores: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"Scores":["student_id","subject","score","exam_date"]},"rows":{"Scores":[[101,"Math",70,"2023-01-15"],[101,"Math",85,"2023-02-15"],[101,"Physics",65,"2023-01-15"],[101,"Physics",60,"2023-02-15"],[102,"Math",80,"2023-01-15"],[102,"Math",85,"2023-02-15"],[103,"Math",90,"2023-01-15"],[104,"Physics",75,"2023-01-15"],[104,"Physics",85,"2023-02-15"]]}}
{"mysql": ["CREATE TABLE Scores (\n student_id INT,\n subject VARCHAR(50),\n score INT,\n exam_date VARCHAR(10)\n)"], "mssql": ["CREATE TABLE Scores (\n student_id INT,\n subject VARCHAR(50),\n score INT,\n exam_date VARCHAR(10)\n)"], "oraclesql": ["CREATE TABLE Scores (\n student_id NUMBER,\n subject VARCHAR2(50),\n score NUMBER,\n exam_date VARCHAR2(10)\n)"], "database": true, "name": "find_students_who_improved", "postgresql": ["CREATE TABLE Scores (\n student_id INT,\n subject VARCHAR(50),\n score INT,\n exam_date VARCHAR(10)\n);\n"], "pythondata": ["Scores = pd.DataFrame({\"student_id\": pd.Series(dtype=\"int\"),\n \"subject\": pd.Series(dtype=\"str\"),\n \"score\": pd.Series(dtype=\"int\"),\n \"exam_date\": pd.Series(dtype=\"str\")})"], "database_schema": {"Scores": {"student_id": "INT", "subject": "VARCHAR(50)", "score": "INT", "exam_date": "VARCHAR(10)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,422
Minimum Operations to Make Subarray Elements Equal
minimum-operations-to-make-subarray-elements-equal
null
Medium
418
836
418
836
50.0%
['find-median-from-data-stream', 'minimum-moves-to-equal-array-elements-ii']
[]
Algorithms
null
[4,-3,2,1,-4,6] 3
{ "name": "minOperations", "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', 'Math', 'Sliding Window', 'Heap (Priority Queue)']
3,423
Maximum Difference Between Adjacent Elements in a Circular Array
maximum-difference-between-adjacent-elements-in-a-circular-array
<p>Given a <strong>circular</strong> array <code>nums</code>, find the <b>maximum</b> absolute difference between adjacent elements.</p> <p><strong>Note</strong>: In a circular array, the first and last elements are adjacent.</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,2,4]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>Because <code>nums</code> is circular, <code>nums[0]</code> and <code>nums[2]</code> are adjacent. They have the maximum absolute difference of <code>|4 - 1| = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-5,-10,-5]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>The adjacent elements <code>nums[0]</code> and <code>nums[1]</code> have the maximum absolute difference of <code>|-5 - (-10)| = 5</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Easy
37K
50.2K
37,042
50,231
73.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxAdjacentDistance(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxAdjacentDistance(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxAdjacentDistance(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxAdjacentDistance(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxAdjacentDistance(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxAdjacentDistance(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxAdjacentDistance = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxAdjacentDistance(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxAdjacentDistance($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxAdjacentDistance(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxAdjacentDistance(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxAdjacentDistance(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxAdjacentDistance(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_adjacent_distance(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxAdjacentDistance(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_adjacent_distance(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-adjacent-distance nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_adjacent_distance(Nums :: [integer()]) -> integer().\nmax_adjacent_distance(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_adjacent_distance(nums :: [integer]) :: integer\n def max_adjacent_distance(nums) do\n \n end\nend"}]
[1,2,4]
{ "name": "maxAdjacentDistance", "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']
3,424
Minimum Cost to Make Arrays Identical
minimum-cost-to-make-arrays-identical
<p>You are given two integer arrays <code>arr</code> and <code>brr</code> of length <code>n</code>, and an integer <code>k</code>. You can perform the following operations on <code>arr</code> <em>any</em> number of times:</p> <ul> <li>Split <code>arr</code> into <em>any</em> number of <strong>contiguous</strong> <span data-keyword="subarray-nonempty">subarrays</span> and rearrange these subarrays in <em>any order</em>. This operation has a fixed cost of <code>k</code>.</li> <li> <p>Choose any element in <code>arr</code> and add or subtract a positive integer <code>x</code> to it. The cost of this operation is <code>x</code>.</p> </li> </ul> <p>Return the <strong>minimum </strong>total cost to make <code>arr</code> <strong>equal</strong> to <code>brr</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">arr = [-7,9,5], brr = [7,-2,-5], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Split <code>arr</code> into two contiguous subarrays: <code>[-7]</code> and <code>[9, 5]</code> and rearrange them as <code>[9, 5, -7]</code>, with a cost of 2.</li> <li>Subtract 2 from element <code>arr[0]</code>. The array becomes <code>[7, 5, -7]</code>. The cost of this operation is 2.</li> <li>Subtract 7 from element <code>arr[1]</code>. The array becomes <code>[7, -2, -7]</code>. The cost of this operation is 7.</li> <li>Add 2 to element <code>arr[2]</code>. The array becomes <code>[7, -2, -5]</code>. The cost of this operation is 2.</li> </ul> <p>The total cost to make the arrays equal is <code>2 + 2 + 7 + 2 = 13</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">arr = [2,1], brr = [2,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Since the arrays are already equal, no operations are needed, and the total cost is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= arr.length == brr.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= 2 * 10<sup>10</sup></code></li> <li><code>-10<sup>5</sup> &lt;= arr[i] &lt;= 10<sup>5</sup></code></li> <li><code>-10<sup>5</sup> &lt;= brr[i] &lt;= 10<sup>5</sup></code></li> </ul>
Medium
22.5K
61.2K
22,469
61,230
36.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minCost(vector<int>& arr, vector<int>& brr, long long k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minCost(int[] arr, int[] brr, long k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCost(self, arr, brr, k):\n \"\"\"\n :type arr: List[int]\n :type brr: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCost(self, arr: List[int], brr: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minCost(int* arr, int arrSize, int* brr, int brrSize, long long k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinCost(int[] arr, int[] brr, long k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} arr\n * @param {number[]} brr\n * @param {number} k\n * @return {number}\n */\nvar minCost = function(arr, brr, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCost(arr: number[], brr: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $arr\n * @param Integer[] $brr\n * @param Integer $k\n * @return Integer\n */\n function minCost($arr, $brr, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCost(_ arr: [Int], _ brr: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCost(arr: IntArray, brr: IntArray, k: Long): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCost(List<int> arr, List<int> brr, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCost(arr []int, brr []int, k int64) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} arr\n# @param {Integer[]} brr\n# @param {Integer} k\n# @return {Integer}\ndef min_cost(arr, brr, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCost(arr: Array[Int], brr: Array[Int], k: Long): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost(arr: Vec<i32>, brr: Vec<i32>, k: i64) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost arr brr k)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost(Arr :: [integer()], Brr :: [integer()], K :: integer()) -> integer().\nmin_cost(Arr, Brr, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost(arr :: [integer], brr :: [integer], k :: integer) :: integer\n def min_cost(arr, brr, k) do\n \n end\nend"}]
[-7,9,5] [7,-2,-5] 2
{ "name": "minCost", "params": [ { "name": "arr", "type": "integer[]" }, { "type": "integer[]", "name": "brr" }, { "type": "long", "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']
3,425
Longest Special Path
longest-special-path
<p>You are given an undirected tree rooted at node <code>0</code> with <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>, represented by a 2D array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> indicates an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>length<sub>i</sub></code>. You are also given an integer array <code>nums</code>, where <code>nums[i]</code> represents the value at node <code>i</code>.</p> <p>A <b data-stringify-type="bold">special path</b> is defined as a <b data-stringify-type="bold">downward</b> path from an ancestor node to a descendant node such that all the values of the nodes in that path are <b data-stringify-type="bold">unique</b>.</p> <p><strong>Note</strong> that a path may start and end at the same node.</p> <p>Return an array <code data-stringify-type="code">result</code> of size 2, where <code>result[0]</code> is the <b data-stringify-type="bold">length</b> of the <strong>longest</strong> special path, and <code>result[1]</code> is the <b data-stringify-type="bold">minimum</b> number of nodes in all <i data-stringify-type="italic">possible</i> <strong>longest</strong> special paths.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">edges = [[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]], nums = [2,1,2,1,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[6,2]</span></p> <p><strong>Explanation:</strong></p> <h4>In the image below, nodes are colored by their corresponding values in <code>nums</code></h4> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/02/tree3.jpeg" style="width: 250px; height: 350px;" /></p> <p>The longest special paths are <code>2 -&gt; 5</code> and <code>0 -&gt; 1 -&gt; 4</code>, both having a length of 6. The minimum number of nodes across all longest special paths is 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">edges = [[1,0,8]], nums = [2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/02/tree4.jpeg" style="width: 190px; height: 75px;" /></p> <p>The longest special paths are <code>0</code> and <code>1</code>, both having a length of 0. The minimum number of nodes across all longest special paths is 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>3</sup></code></li> <li><code>nums.length == n</code></li> <li><code>0 &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li> <li>The input is generated such that <code>edges</code> represents a valid tree.</li> </ul>
Hard
4.4K
21.8K
4,387
21,828
20.1%
['frog-position-after-t-seconds', 'longest-special-path-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> longestSpecialPath(vector<vector<int>>& edges, vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] longestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestSpecialPath(self, edges, nums):\n \"\"\"\n :type edges: List[List[int]]\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def longestSpecialPath(self, edges: List[List[int]], nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* longestSpecialPath(int** edges, int edgesSize, int* edgesColSize, int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] LongestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} edges\n * @param {number[]} nums\n * @return {number[]}\n */\nvar longestSpecialPath = function(edges, nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestSpecialPath(edges: number[][], nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $edges\n * @param Integer[] $nums\n * @return Integer[]\n */\n function longestSpecialPath($edges, $nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestSpecialPath(_ edges: [[Int]], _ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestSpecialPath(edges: Array<IntArray>, nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> longestSpecialPath(List<List<int>> edges, List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestSpecialPath(edges [][]int, nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} edges\n# @param {Integer[]} nums\n# @return {Integer[]}\ndef longest_special_path(edges, nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestSpecialPath(edges: Array[Array[Int]], nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_special_path(edges: Vec<Vec<i32>>, nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-special-path edges nums)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_special_path(Edges :: [[integer()]], Nums :: [integer()]) -> [integer()].\nlongest_special_path(Edges, Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_special_path(edges :: [[integer]], nums :: [integer]) :: [integer]\n def longest_special_path(edges, nums) do\n \n end\nend"}]
[[0,1,2],[1,2,3],[1,3,5],[1,4,4],[2,5,6]] [2,1,2,1,3,1]
{ "name": "longestSpecialPath", "params": [ { "name": "edges", "type": "integer[][]" }, { "type": "integer[]", "name": "nums" } ], "return": { "type": "integer[]" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'Prefix Sum']
3,426
Manhattan Distances of All Arrangements of Pieces
manhattan-distances-of-all-arrangements-of-pieces
<p>You are given three integers <code><font face="monospace">m</font></code>, <code><font face="monospace">n</font></code>, and <code>k</code>.</p> <p>There is a rectangular grid of size <code>m &times; n</code> containing <code>k</code> identical pieces. Return the sum of Manhattan distances between every pair of pieces over all <strong>valid arrangements</strong> of pieces.</p> <p>A <strong>valid arrangement</strong> is a placement of all <code>k</code> pieces on the grid with <strong>at most</strong> one piece per cell.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p>The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</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">m = 2, n = 2, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p>The valid arrangements of pieces on the board are:</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/25/4040example1.drawio" /><img alt="" src="https://assets.leetcode.com/uploads/2024/12/25/untitled-diagramdrawio.png" style="width: 441px; height: 204px;" /></p> <ul> <li>In the first 4 arrangements, the Manhattan distance between the two pieces is 1.</li> <li>In the last 2 arrangements, the Manhattan distance between the two pieces is 2.</li> </ul> <p>Thus, the total Manhattan distance across all valid arrangements is <code>1 + 1 + 1 + 1 + 2 + 2 = 8</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">m = 1, n = 4, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <p>The valid arrangements of pieces on the board are:</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/25/4040example2drawio.png" style="width: 762px; height: 41px;" /></p> <ul> <li>The first and last arrangements have a total Manhattan distance of <code>1 + 1 + 2 = 4</code>.</li> <li>The middle two arrangements have a total Manhattan distance of <code>1 + 2 + 3 = 6</code>.</li> </ul> <p>The total Manhattan distance between all pairs of pieces across all arrangements is <code>4 + 6 + 6 + 4 = 20</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li> <li><code>2 &lt;= m * n &lt;= 10<sup>5</sup></code></li> <li><code><font face="monospace">2 &lt;= k &lt;= m * n</font></code></li> </ul>
Hard
3.1K
8.6K
3,097
8,644
35.8%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int distanceSum(int m, int n, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int distanceSum(int m, int n, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def distanceSum(self, m, n, k):\n \"\"\"\n :type m: int\n :type n: int\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def distanceSum(self, m: int, n: int, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int distanceSum(int m, int n, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int DistanceSum(int m, int n, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} m\n * @param {number} n\n * @param {number} k\n * @return {number}\n */\nvar distanceSum = function(m, n, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function distanceSum(m: number, n: number, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $m\n * @param Integer $n\n * @param Integer $k\n * @return Integer\n */\n function distanceSum($m, $n, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func distanceSum(_ m: Int, _ n: Int, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun distanceSum(m: Int, n: Int, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int distanceSum(int m, int n, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func distanceSum(m int, n int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} m\n# @param {Integer} n\n# @param {Integer} k\n# @return {Integer}\ndef distance_sum(m, n, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def distanceSum(m: Int, n: Int, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn distance_sum(m: i32, n: i32, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (distance-sum m n k)\n (-> exact-integer? exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec distance_sum(M :: integer(), N :: integer(), K :: integer()) -> integer().\ndistance_sum(M, N, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec distance_sum(m :: integer, n :: integer, k :: integer) :: integer\n def distance_sum(m, n, k) do\n \n end\nend"}]
2 2 2
{ "name": "distanceSum", "params": [ { "name": "m", "type": "integer" }, { "type": "integer", "name": "n" }, { "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', 'Combinatorics']
3,427
Sum of Variable Length Subarrays
sum-of-variable-length-subarrays
<p>You are given an integer array <code>nums</code> of size <code>n</code>. For <strong>each</strong> index <code>i</code> where <code>0 &lt;= i &lt; n</code>, define a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[start ... i]</code> where <code>start = max(0, i - nums[i])</code>.</p> <p>Return the total sum of all elements from the subarray defined for each index in the array.</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 = [2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;">i</th> <th style="border: 1px solid black;">Subarray</th> <th style="border: 1px solid black;">Sum</th> </tr> <tr> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>nums[0] = [2]</code></td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>nums[0 ... 1] = [2, 3]</code></td> <td style="border: 1px solid black;">5</td> </tr> <tr> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;"><code>nums[1 ... 2] = [3, 1]</code></td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;"><strong>Total Sum</strong></td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">11</td> </tr> </tbody> </table> <p>The total sum is 11. Hence, 11 is the output.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,1,1,2]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;">i</th> <th style="border: 1px solid black;">Subarray</th> <th style="border: 1px solid black;">Sum</th> </tr> <tr> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>nums[0] = [3]</code></td> <td style="border: 1px solid black;">3</td> </tr> <tr> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>nums[0 ... 1] = [3, 1]</code></td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;"><code>nums[1 ... 2] = [1, 1]</code></td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;"><code>nums[1 ... 3] = [1, 1, 2]</code></td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;"><strong>Total Sum</strong></td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">13</td> </tr> </tbody> </table> <p>The total sum is 13. Hence, 13 is the output.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 1000</code></li> </ul>
Easy
40.1K
46.6K
40,062
46,586
86.0%
['range-sum-query-immutable', 'maximum-sum-of-3-non-overlapping-subarrays']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int subarraySum(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int subarraySum(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def subarraySum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def subarraySum(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int subarraySum(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int SubarraySum(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar subarraySum = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function subarraySum(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function subarraySum($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func subarraySum(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun subarraySum(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int subarraySum(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func subarraySum(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef subarray_sum(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def subarraySum(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn subarray_sum(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (subarray-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec subarray_sum(Nums :: [integer()]) -> integer().\nsubarray_sum(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec subarray_sum(nums :: [integer]) :: integer\n def subarray_sum(nums) do\n \n end\nend"}]
[2,3,1]
{ "name": "subarraySum", "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']
3,428
Maximum and Minimum Sums of at Most Size K Subsequences
maximum-and-minimum-sums-of-at-most-size-k-subsequences
<p>You are given an integer array <code>nums</code> and a positive integer <code>k</code>. Return the sum of the <strong>maximum</strong> and <strong>minimum</strong> elements of all <strong><span data-keyword="subsequence-sequence-nonempty">subsequences</span></strong> of <code>nums</code> with <strong>at most</strong> <code>k</code> elements.</p> <p>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> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 2</span></p> <p><strong>Output:</strong> 24</p> <p><strong>Explanation:</strong></p> <p>The subsequences of <code>nums</code> with at most 2 elements are:</p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;"><b>Subsequence </b></th> <th style="border: 1px solid black;">Minimum</th> <th style="border: 1px solid black;">Maximum</th> <th style="border: 1px solid black;">Sum</th> </tr> <tr> <td style="border: 1px solid black;"><code>[1]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;"><code>[2]</code></td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;"><code>[3]</code></td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">6</td> </tr> <tr> <td style="border: 1px solid black;"><code>[1, 2]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">3</td> </tr> <tr> <td style="border: 1px solid black;"><code>[1, 3]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;"><code>[2, 3]</code></td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">5</td> </tr> <tr> <td style="border: 1px solid black;"><strong>Final Total</strong></td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">24</td> </tr> </tbody> </table> <p>The output would be 24.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [5,0,6], k = 1</span></p> <p><strong>Output:</strong> 2<span class="example-io">2</span></p> <p><strong>Explanation: </strong></p> <p>For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is <code>5 + 5 + 0 + 0 + 6 + 6 = 22</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,1], k = 2</span></p> <p><strong>Output:</strong> 12</p> <p><strong>Explanation:</strong></p> <p>The subsequences <code>[1, 1]</code> and <code>[1]</code> each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12.</p> </div> <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> <li><code><font face="monospace">1 &lt;= k &lt;= min(70, nums.length)</font></code></li> </ul>
Medium
10.5K
50.1K
10,535
50,054
21.0%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minMaxSums(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minMaxSums(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMaxSums(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 minMaxSums(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minMaxSums(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinMaxSums(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 minMaxSums = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMaxSums(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 minMaxSums($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMaxSums(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMaxSums(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMaxSums(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMaxSums(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef min_max_sums(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMaxSums(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_max_sums(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-max-sums nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_max_sums(Nums :: [integer()], K :: integer()) -> integer().\nmin_max_sums(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_max_sums(nums :: [integer], k :: integer) :: integer\n def min_max_sums(nums, k) do\n \n end\nend"}]
[1,2,3] 2
{ "name": "minMaxSums", "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', 'Math', 'Dynamic Programming', 'Sorting', 'Combinatorics']
3,429
Paint House IV
paint-house-iv
<p>You are given an <strong>even</strong> integer <code>n</code> representing the number of houses arranged in a straight line, and a 2D array <code>cost</code> of size <code>n x 3</code>, where <code>cost[i][j]</code> represents the cost of painting house <code>i</code> with color <code>j + 1</code>.</p> <p>The houses will look <strong>beautiful</strong> if they satisfy the following conditions:</p> <ul> <li>No <strong>two</strong> adjacent houses are painted the same color.</li> <li>Houses <strong>equidistant</strong> from the ends of the row are <strong>not</strong> painted the same color. For example, if <code>n = 6</code>, houses at positions <code>(0, 5)</code>, <code>(1, 4)</code>, and <code>(2, 3)</code> are considered equidistant.</li> </ul> <p>Return the <strong>minimum</strong> cost to paint the houses such that they look <strong>beautiful</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 = 4, cost = [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>The optimal painting sequence is <code>[1, 2, 3, 2]</code> with corresponding costs <code>[3, 2, 1, 3]</code>. This satisfies the following conditions:</p> <ul> <li>No adjacent houses have the same color.</li> <li>Houses at positions 0 and 3 (equidistant from the ends) are not painted the same color <code>(1 != 2)</code>.</li> <li>Houses at positions 1 and 2 (equidistant from the ends) are not painted the same color <code>(2 != 3)</code>.</li> </ul> <p>The minimum cost to paint the houses so that they look beautiful is <code>3 + 2 + 1 + 3 = 9</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 6, cost = [[2,4,6],[5,3,8],[7,1,9],[4,6,2],[3,5,7],[8,2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">18</span></p> <p><strong>Explanation:</strong></p> <p>The optimal painting sequence is <code>[1, 3, 2, 3, 1, 2]</code> with corresponding costs <code>[2, 8, 1, 2, 3, 2]</code>. This satisfies the following conditions:</p> <ul> <li>No adjacent houses have the same color.</li> <li>Houses at positions 0 and 5 (equidistant from the ends) are not painted the same color <code>(1 != 2)</code>.</li> <li>Houses at positions 1 and 4 (equidistant from the ends) are not painted the same color <code>(3 != 1)</code>.</li> <li>Houses at positions 2 and 3 (equidistant from the ends) are not painted the same color <code>(2 != 3)</code>.</li> </ul> <p>The minimum cost to paint the houses so that they look beautiful is <code>2 + 8 + 1 + 2 + 3 + 2 = 18</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is even.</li> <li><code>cost.length == n</code></li> <li><code>cost[i].length == 3</code></li> <li><code>0 &lt;= cost[i][j] &lt;= 10<sup>5</sup></code></li> </ul>
Medium
8.4K
18.4K
8,367
18,438
45.4%
['paint-house-iii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minCost(int n, vector<vector<int>>& cost) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minCost(int n, int[][] cost) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCost(self, n, cost):\n \"\"\"\n :type n: int\n :type cost: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCost(self, n: int, cost: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minCost(int n, int** cost, int costSize, int* costColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinCost(int n, int[][] cost) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} cost\n * @return {number}\n */\nvar minCost = function(n, cost) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCost(n: number, cost: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $cost\n * @return Integer\n */\n function minCost($n, $cost) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCost(_ n: Int, _ cost: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCost(n: Int, cost: Array<IntArray>): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCost(int n, List<List<int>> cost) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCost(n int, cost [][]int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} cost\n# @return {Integer}\ndef min_cost(n, cost)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCost(n: Int, cost: Array[Array[Int]]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost(n: i32, cost: Vec<Vec<i32>>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost n cost)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost(N :: integer(), Cost :: [[integer()]]) -> integer().\nmin_cost(N, Cost) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost(n :: integer, cost :: [[integer]]) :: integer\n def min_cost(n, cost) do\n \n end\nend"}]
4 [[3,5,7],[6,2,9],[4,8,1],[7,3,5]]
{ "name": "minCost", "params": [ { "name": "n", "type": "integer" }, { "type": "integer[][]", "name": "cost" } ], "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']
3,430
Maximum and Minimum Sums of at Most Size K Subarrays
maximum-and-minimum-sums-of-at-most-size-k-subarrays
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. Return the sum of the <strong>maximum</strong> and <strong>minimum</strong> elements of all <span data-keyword="subarray-nonempty">subarrays</span> with <strong>at most</strong> <code>k</code> elements.</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,2,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">20</span></p> <p><strong>Explanation:</strong></p> <p>The subarrays of <code>nums</code> with at most 2 elements are:</p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;"><b>Subarray</b></th> <th style="border: 1px solid black;">Minimum</th> <th style="border: 1px solid black;">Maximum</th> <th style="border: 1px solid black;">Sum</th> </tr> <tr> <td style="border: 1px solid black;"><code>[1]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;"><code>[2]</code></td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">4</td> </tr> <tr> <td style="border: 1px solid black;"><code>[3]</code></td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">6</td> </tr> <tr> <td style="border: 1px solid black;"><code>[1, 2]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">3</td> </tr> <tr> <td style="border: 1px solid black;"><code>[2, 3]</code></td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">5</td> </tr> <tr> <td style="border: 1px solid black;"><strong>Final Total</strong></td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">20</td> </tr> </tbody> </table> <p>The output would be 20.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-3,1], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">-6</span></p> <p><strong>Explanation:</strong></p> <p>The subarrays of <code>nums</code> with at most 2 elements are:</p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;"><b>Subarray</b></th> <th style="border: 1px solid black;">Minimum</th> <th style="border: 1px solid black;">Maximum</th> <th style="border: 1px solid black;">Sum</th> </tr> <tr> <td style="border: 1px solid black;"><code>[1]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;"><code>[-3]</code></td> <td style="border: 1px solid black;">-3</td> <td style="border: 1px solid black;">-3</td> <td style="border: 1px solid black;">-6</td> </tr> <tr> <td style="border: 1px solid black;"><code>[1]</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;"><code>[1, -3]</code></td> <td style="border: 1px solid black;">-3</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">-2</td> </tr> <tr> <td style="border: 1px solid black;"><code>[-3, 1]</code></td> <td style="border: 1px solid black;">-3</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">-2</td> </tr> <tr> <td style="border: 1px solid black;"><strong>Final Total</strong></td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">&nbsp;</td> <td style="border: 1px solid black;">-6</td> </tr> </tbody> </table> <p>The output would be -6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 80000</code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> <li><code>-10<sup>6</sup> &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> </ul>
Hard
3K
12.6K
2,955
12,577
23.5%
['next-greater-element-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minMaxSubarraySum(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minMaxSubarraySum(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minMaxSubarraySum(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 minMaxSubarraySum(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minMaxSubarraySum(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinMaxSubarraySum(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 minMaxSubarraySum = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minMaxSubarraySum(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 minMaxSubarraySum($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minMaxSubarraySum(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minMaxSubarraySum(nums: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minMaxSubarraySum(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minMaxSubarraySum(nums []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef min_max_subarray_sum(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minMaxSubarraySum(nums: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_max_subarray_sum(nums: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-max-subarray-sum nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_max_subarray_sum(Nums :: [integer()], K :: integer()) -> integer().\nmin_max_subarray_sum(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_max_subarray_sum(nums :: [integer], k :: integer) :: integer\n def min_max_subarray_sum(nums, k) do\n \n end\nend"}]
[1,2,3] 2
{ "name": "minMaxSubarraySum", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "k" } ], "return": { "type": "long" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Math', 'Stack', 'Monotonic Stack']
3,431
Minimum Unlocked Indices to Sort Nums
minimum-unlocked-indices-to-sort-nums
null
Medium
325
546
325
546
59.5%
[]
[]
Algorithms
null
[1,2,1,2,3,2] [1,0,1,1,0,1]
{ "name": "minUnlockedIndices", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[]", "name": "locked" } ], "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']
3,432
Count Partitions with Even Sum Difference
count-partitions-with-even-sum-difference
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p> <p>A <strong>partition</strong> is defined as an index <code>i</code> where <code>0 &lt;= i &lt; n - 1</code>, splitting the array into two <strong>non-empty</strong> subarrays such that:</p> <ul> <li>Left subarray contains indices <code>[0, i]</code>.</li> <li>Right subarray contains indices <code>[i + 1, n - 1]</code>.</li> </ul> <p>Return the number of <strong>partitions</strong> where the <strong>difference</strong> between the <strong>sum</strong> of the left and right subarrays is <strong>even</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">nums = [10,10,3,7,6]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The 4 partitions are:</p> <ul> <li><code>[10]</code>, <code>[10, 3, 7, 6]</code> with a sum difference of <code>10 - 26 = -16</code>, which is even.</li> <li><code>[10, 10]</code>, <code>[3, 7, 6]</code> with a sum difference of <code>20 - 16 = 4</code>, which is even.</li> <li><code>[10, 10, 3]</code>, <code>[7, 6]</code> with a sum difference of <code>23 - 13 = 10</code>, which is even.</li> <li><code>[10, 10, 3, 7]</code>, <code>[6]</code> with a sum difference of <code>30 - 6 = 24</code>, which is even.</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,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No partition results in an even sum difference.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,4,6,8]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All partitions result in an even sum difference.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 100</code></li> </ul>
Easy
43.3K
53.7K
43,328
53,694
80.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countPartitions(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countPartitions(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countPartitions(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countPartitions(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countPartitions(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountPartitions(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar countPartitions = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countPartitions(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function countPartitions($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countPartitions(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countPartitions(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countPartitions(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countPartitions(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef count_partitions(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countPartitions(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_partitions(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-partitions nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_partitions(Nums :: [integer()]) -> integer().\ncount_partitions(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_partitions(nums :: [integer]) :: integer\n def count_partitions(nums) do\n \n end\nend"}]
[10,10,3,7,6]
{ "name": "countPartitions", "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', 'Prefix Sum']
3,433
Count Mentions Per User
count-mentions-per-user
<p>You are given an integer <code>numberOfUsers</code> representing the total number of users and an array <code>events</code> of size <code>n x 3</code>.</p> <p>Each <code inline="">events[i]</code> can be either of the following two types:</p> <ol> <li><strong>Message Event:</strong> <code>[&quot;MESSAGE&quot;, &quot;timestamp<sub>i</sub>&quot;, &quot;mentions_string<sub>i</sub>&quot;]</code> <ul> <li>This event indicates that a set of users was mentioned in a message at <code>timestamp<sub>i</sub></code>.</li> <li>The <code>mentions_string<sub>i</sub></code> string can contain one of the following tokens: <ul> <li><code>id&lt;number&gt;</code>: where <code>&lt;number&gt;</code> is an integer in range <code>[0,numberOfUsers - 1]</code>. There can be <strong>multiple</strong> ids separated by a single whitespace and may contain duplicates. This can mention even the offline users.</li> <li><code>ALL</code>: mentions <strong>all</strong> users.</li> <li><code>HERE</code>: mentions all <strong>online</strong> users.</li> </ul> </li> </ul> </li> <li><strong>Offline Event:</strong> <code>[&quot;OFFLINE&quot;, &quot;timestamp<sub>i</sub>&quot;, &quot;id<sub>i</sub>&quot;]</code> <ul> <li>This event indicates that the user <code>id<sub>i</sub></code> had become offline at <code>timestamp<sub>i</sub></code> for <strong>60 time units</strong>. The user will automatically be online again at time <code>timestamp<sub>i</sub> + 60</code>.</li> </ul> </li> </ol> <p>Return an array <code>mentions</code> where <code>mentions[i]</code> represents the number of mentions the user with id <code>i</code> has across all <code>MESSAGE</code> events.</p> <p>All users are initially online, and if a user goes offline or comes back online, their status change is processed <em>before</em> handling any message event that occurs at the same timestamp.</p> <p><strong>Note </strong>that a user can be mentioned <strong>multiple</strong> times in a <strong>single</strong> message event, and each mention should be counted <strong>separately</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">numberOfUsers = 2, events = [[&quot;MESSAGE&quot;,&quot;10&quot;,&quot;id1 id0&quot;],[&quot;OFFLINE&quot;,&quot;11&quot;,&quot;0&quot;],[&quot;MESSAGE&quot;,&quot;71&quot;,&quot;HERE&quot;]]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2]</span></p> <p><strong>Explanation:</strong></p> <p>Initially, all users are online.</p> <p>At timestamp 10, <code>id1</code> and <code>id0</code> are mentioned. <code>mentions = [1,1]</code></p> <p>At timestamp 11, <code>id0</code> goes <strong>offline.</strong></p> <p>At timestamp 71, <code>id0</code> comes back <strong>online</strong> and <code>&quot;HERE&quot;</code> is mentioned. <code>mentions = [2,2]</code></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">numberOfUsers = 2, events = [[&quot;MESSAGE&quot;,&quot;10&quot;,&quot;id1 id0&quot;],[&quot;OFFLINE&quot;,&quot;11&quot;,&quot;0&quot;],[&quot;MESSAGE&quot;,&quot;12&quot;,&quot;ALL&quot;]]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2]</span></p> <p><strong>Explanation:</strong></p> <p>Initially, all users are online.</p> <p>At timestamp 10, <code>id1</code> and <code>id0</code> are mentioned. <code>mentions = [1,1]</code></p> <p>At timestamp 11, <code>id0</code> goes <strong>offline.</strong></p> <p>At timestamp 12, <code>&quot;ALL&quot;</code> is mentioned. This includes offline users, so both <code>id0</code> and <code>id1</code> are mentioned. <code>mentions = [2,2]</code></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">numberOfUsers = 2, events = [[&quot;OFFLINE&quot;,&quot;10&quot;,&quot;0&quot;],[&quot;MESSAGE&quot;,&quot;12&quot;,&quot;HERE&quot;]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>Initially, all users are online.</p> <p>At timestamp 10, <code>id0</code> goes <strong>offline.</strong></p> <p>At timestamp 12, <code>&quot;HERE&quot;</code> is mentioned. Because <code>id0</code> is still offline, they will not be mentioned. <code>mentions = [0,1]</code></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= numberOfUsers &lt;= 100</code></li> <li><code>1 &lt;= events.length &lt;= 100</code></li> <li><code>events[i].length == 3</code></li> <li><code>events[i][0]</code> will be one of <code>MESSAGE</code> or <code>OFFLINE</code>.</li> <li><code>1 &lt;= int(events[i][1]) &lt;= 10<sup>5</sup></code></li> <li>The number of <code>id&lt;number&gt;</code> mentions in any <code>&quot;MESSAGE&quot;</code> event is between <code>1</code> and <code>100</code>.</li> <li><code>0 &lt;= &lt;number&gt; &lt;= numberOfUsers - 1</code></li> <li>It is <strong>guaranteed</strong> that the user id referenced in the <code>OFFLINE</code> event is <strong>online</strong> at the time the event occurs.</li> </ul>
Medium
14.8K
49.2K
14,845
49,237
30.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> countMentions(int numberOfUsers, vector<vector<string>>& events) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] countMentions(int numberOfUsers, List<List<String>> events) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countMentions(self, numberOfUsers, events):\n \"\"\"\n :type numberOfUsers: int\n :type events: List[List[str]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* countMentions(int numberOfUsers, char*** events, int eventsSize, int* eventsColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] CountMentions(int numberOfUsers, IList<IList<string>> events) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} numberOfUsers\n * @param {string[][]} events\n * @return {number[]}\n */\nvar countMentions = function(numberOfUsers, events) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countMentions(numberOfUsers: number, events: string[][]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $numberOfUsers\n * @param String[][] $events\n * @return Integer[]\n */\n function countMentions($numberOfUsers, $events) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countMentions(_ numberOfUsers: Int, _ events: [[String]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countMentions(numberOfUsers: Int, events: List<List<String>>): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> countMentions(int numberOfUsers, List<List<String>> events) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countMentions(numberOfUsers int, events [][]string) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} number_of_users\n# @param {String[][]} events\n# @return {Integer[]}\ndef count_mentions(number_of_users, events)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countMentions(numberOfUsers: Int, events: List[List[String]]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_mentions(number_of_users: i32, events: Vec<Vec<String>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-mentions numberOfUsers events)\n (-> exact-integer? (listof (listof string?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_mentions(NumberOfUsers :: integer(), Events :: [[unicode:unicode_binary()]]) -> [integer()].\ncount_mentions(NumberOfUsers, Events) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_mentions(number_of_users :: integer, events :: [[String.t]]) :: [integer]\n def count_mentions(number_of_users, events) do\n \n end\nend"}]
2 [["MESSAGE","10","id1 id0"],["OFFLINE","11","0"],["MESSAGE","71","HERE"]]
{ "name": "countMentions", "params": [ { "name": "numberOfUsers", "type": "integer" }, { "type": "list<list<string>>", "name": "events" } ], "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', 'Sorting', 'Simulation']
3,434
Maximum Frequency After Subarray Operation
maximum-frequency-after-subarray-operation
<p>You are given an array <code>nums</code> of length <code>n</code>. You are also given an integer <code>k</code>.</p> <p>You perform the following operation on <code>nums</code> <strong>once</strong>:</p> <ul> <li>Select a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[i..j]</code> where <code>0 &lt;= i &lt;= j &lt;= n - 1</code>.</li> <li>Select an integer <code>x</code> and add <code>x</code> to <strong>all</strong> the elements in <code>nums[i..j]</code>.</li> </ul> <p>Find the <strong>maximum</strong> frequency of the value <code>k</code> after the operation.</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,2,3,4,5,6], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>After adding -5 to <code>nums[2..5]</code>, 1 has a frequency of 2 in <code>[1, 2, -2, -1, 0, 1]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [10,2,3,4,5,5,4,3,2,2], k = 10</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>After adding 8 to <code>nums[1..9]</code>, 10 has a frequency of 4 in <code>[10, 10, 11, 12, 13, 13, 12, 11, 10, 10]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul>
Medium
12.2K
51.8K
12,180
51,770
23.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxFrequency(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxFrequency(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 maxFrequency(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxFrequency(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxFrequency(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 maxFrequency = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxFrequency(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 maxFrequency($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxFrequency(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxFrequency(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxFrequency(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxFrequency(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef max_frequency(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxFrequency(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_frequency(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-frequency nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_frequency(Nums :: [integer()], K :: integer()) -> integer().\nmax_frequency(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_frequency(nums :: [integer], k :: integer) :: integer\n def max_frequency(nums, k) do\n \n end\nend"}]
[1,2,3,4,5,6] 1
{ "name": "maxFrequency", "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', 'Greedy', 'Enumeration', 'Prefix Sum']
3,435
Frequencies of Shortest Supersequences
frequencies-of-shortest-supersequences
<p>You are given an array of strings <code>words</code>. Find all <strong>shortest common supersequences (SCS)</strong> of <code><font face="monospace">words</font></code> that are not <span data-keyword="permutation-string">permutations</span> of each other.</p> <p>A <strong>shortest common supersequence</strong> is a string of <strong>minimum</strong> length that contains each string in <code>words</code> as a <span data-keyword="subsequence-string-nonempty">subsequence</span>.</p> <p>Return a 2D array of integers <code>freqs</code> that represent all the SCSs. Each <code>freqs[i]</code> is an array of size 26, representing the frequency of each letter in the lowercase English alphabet for a single SCS. You may return the frequency arrays in any order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;ab&quot;,&quot;ba&quot;]</span></p> <p><strong>Output: </strong>[[1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],[2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]</p> <p><strong>Explanation:</strong></p> <p>The two SCSs are <code>&quot;aba&quot;</code> and <code>&quot;bab&quot;</code>. The output is the letter frequencies for each one.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;aa&quot;,&quot;ac&quot;]</span></p> <p><strong>Output: </strong>[[2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]</p> <p><strong>Explanation:</strong></p> <p>The two SCSs are <code>&quot;aac&quot;</code> and <code>&quot;aca&quot;</code>. Since they are permutations of each other, keep only <code>&quot;aac&quot;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = </span>[&quot;aa&quot;,&quot;bb&quot;,&quot;cc&quot;]</p> <p><strong>Output: </strong>[[2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]</p> <p><strong>Explanation:</strong></p> <p><code>&quot;aabbcc&quot;</code> and all its permutations are SCSs.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= words.length &lt;= 256</code></li> <li><code>words[i].length == 2</code></li> <li>All strings in <code>words</code> will altogether be composed of no more than 16 unique lowercase letters.</li> <li>All strings in <code>words</code> are unique.</li> </ul>
Hard
1.6K
6.4K
1,576
6,407
24.6%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> supersequences(vector<string>& words) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<List<Integer>> supersequences(String[] words) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def supersequences(self, words):\n \"\"\"\n :type words: List[str]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def supersequences(self, words: List[str]) -> 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** supersequences(char** words, int wordsSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<IList<int>> Supersequences(string[] words) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @return {number[][]}\n */\nvar supersequences = function(words) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function supersequences(words: string[]): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @return Integer[][]\n */\n function supersequences($words) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func supersequences(_ words: [String]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun supersequences(words: Array<String>): List<List<Int>> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> supersequences(List<String> words) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func supersequences(words []string) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @return {Integer[][]}\ndef supersequences(words)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def supersequences(words: Array[String]): List[List[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn supersequences(words: Vec<String>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (supersequences words)\n (-> (listof string?) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec supersequences(Words :: [unicode:unicode_binary()]) -> [[integer()]].\nsupersequences(Words) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec supersequences(words :: [String.t]) :: [[integer]]\n def supersequences(words) do\n \n end\nend"}]
["ab","ba"]
{ "name": "supersequences", "params": [ { "name": "words", "type": "string[]" } ], "return": { "type": "list<list<integer>>" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'String', 'Bit Manipulation', 'Graph', 'Topological Sort', 'Enumeration']
3,436
Find Valid Emails
find-valid-emails
<p>Table: <code>Users</code></p> <pre> +-----------------+---------+ | Column Name | Type | +-----------------+---------+ | user_id | int | | email | varchar | +-----------------+---------+ (user_id) is the unique key for this table. Each row contains a user&#39;s unique ID and email address. </pre> <p>Write a solution to find all the <strong>valid email addresses</strong>. A valid email address meets the following criteria:</p> <ul> <li>It contains exactly one <code>@</code> symbol.</li> <li>It ends with <code>.com</code>.</li> <li>The part before the <code>@</code> symbol contains only <strong>alphanumeric</strong> characters and <strong>underscores</strong>.</li> <li>The part after the <code>@</code> symbol and before <code>.com</code> contains a domain name <strong>that contains only letters</strong>.</li> </ul> <p>Return<em> the result table ordered by</em> <code>user_id</code> <em>in</em> <strong>ascending </strong><em>order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>Users table:</p> <pre class="example-io"> +---------+---------------------+ | user_id | email | +---------+---------------------+ | 1 | [email protected] | | 2 | bob_at_example.com | | 3 | [email protected] | | 4 | [email protected] | | 5 | eve@invalid | +---------+---------------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +---------+-------------------+ | user_id | email | +---------+-------------------+ | 1 | [email protected] | | 4 | [email protected] | +---------+-------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>[email protected]</strong> is valid because it contains one <code>@</code>, alice&nbsp;is alphanumeric, and example.com&nbsp;starts with a letter and ends with .com.</li> <li><strong>bob_at_example.com</strong> is invalid because it contains an underscore instead of an <code>@</code>.</li> <li><strong>[email protected]</strong> is invalid because the domain does not end with <code>.com</code>.</li> <li><strong>[email protected]</strong> is valid because it meets all criteria.</li> <li><strong>eve@invalid</strong> is invalid because the domain does not end with <code>.com</code>.</li> </ul> <p>Result table is ordered by user_id in ascending order.</p> </div>
Easy
5.9K
10.2K
5,928
10,182
58.2%
[]
['CREATE TABLE If not Exists Users (\n user_id INT,\n email VARCHAR(255)\n)', 'Truncate table Users', "insert into Users (user_id, email) values ('1', '[email protected]')", "insert into Users (user_id, email) values ('2', 'bob_at_example.com')", "insert into Users (user_id, email) values ('3', '[email protected]')", "insert into Users (user_id, email) values ('4', '[email protected]')", "insert into Users (user_id, email) values ('5', 'eve@invalid')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef find_valid_emails(users: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"Users":["user_id","email"]},"rows":{"Users":[[1,"[email protected]"],[2,"bob_at_example.com"],[3,"[email protected]"],[4,"[email protected]"],[5,"eve@invalid"]]}}
{"mysql": ["CREATE TABLE If not Exists Users (\n user_id INT,\n email VARCHAR(255)\n)"], "mssql": ["CREATE TABLE Users (\n user_id INT,\n email VARCHAR(255)\n)"], "oraclesql": ["CREATE TABLE Users (\n user_id NUMBER,\n email VARCHAR2(255)\n)"], "database": true, "name": "find_valid_emails", "postgresql": ["CREATE TABLE IF NOT EXISTS users (\n user_id INT,\n email VARCHAR(255)\n);\n"], "pythondata": ["Users = pd.DataFrame(columns=[\"user_id\", \"email\"]).astype({\"user_id\": \"int32\", \"email\": \"string\"})\n"], "database_schema": {"Users": {"user_id": "INT", "email": "VARCHAR(255)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,437
Permutations III
permutations-iii
null
Medium
1.5K
1.7K
1,504
1,728
87.0%
['permutations-iv']
[]
Algorithms
null
4
{ "name": "permute", "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', 'Backtracking']
3,438
Find Valid Pair of Adjacent Digits in String
find-valid-pair-of-adjacent-digits-in-string
<p>You are given a string <code>s</code> consisting only of digits. A <strong>valid pair</strong> is defined as two <strong>adjacent</strong> digits in <code>s</code> such that:</p> <ul> <li>The first digit is <strong>not equal</strong> to the second.</li> <li>Each digit in the pair appears in <code>s</code> <strong>exactly</strong> as many times as its numeric value.</li> </ul> <p>Return the first <strong>valid pair</strong> found in the string <code>s</code> when traversing from left to right. If no valid pair exists, return an empty string.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;2523533&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;23&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Digit <code>&#39;2&#39;</code> appears 2 times and digit <code>&#39;3&#39;</code> appears 3 times. Each digit in the pair <code>&quot;23&quot;</code> appears in <code>s</code> exactly as many times as its numeric value. Hence, the output is <code>&quot;23&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;221&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;21&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Digit <code>&#39;2&#39;</code> appears 2 times and digit <code>&#39;1&#39;</code> appears 1 time. Hence, the output is <code>&quot;21&quot;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;22&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p> <p><strong>Explanation:</strong></p> <p>There are no valid adjacent pairs.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> only consists of digits from <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li> </ul>
Easy
36.7K
59.5K
36,667
59,505
61.6%
['majority-element', 'contains-duplicate']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string findValidPair(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String findValidPair(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findValidPair(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findValidPair(self, s: str) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* findValidPair(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string FindValidPair(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {string}\n */\nvar findValidPair = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findValidPair(s: string): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return String\n */\n function findValidPair($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findValidPair(_ s: String) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findValidPair(s: String): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String findValidPair(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findValidPair(s string) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {String}\ndef find_valid_pair(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findValidPair(s: String): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_valid_pair(s: String) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-valid-pair s)\n (-> string? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_valid_pair(S :: unicode:unicode_binary()) -> unicode:unicode_binary().\nfind_valid_pair(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_valid_pair(s :: String.t) :: String.t\n def find_valid_pair(s) do\n \n end\nend"}]
"2523533"
{ "name": "findValidPair", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "string" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Hash Table', 'String', 'Counting']
3,439
Reschedule Meetings for Maximum Free Time I
reschedule-meetings-for-maximum-free-time-i
<p>You are given an integer <code>eventTime</code> denoting the duration of an event, where the event occurs from time <code>t = 0</code> to time <code>t = eventTime</code>.</p> <p>You are also given two integer arrays <code>startTime</code> and <code>endTime</code>, each of length <code>n</code>. These represent the start and end time of <code>n</code> <strong>non-overlapping</strong> meetings, where the <code>i<sup>th</sup></code> meeting occurs during the time <code>[startTime[i], endTime[i]]</code>.</p> <p>You can reschedule <strong>at most</strong> <code>k</code> meetings by moving their start time while maintaining the <strong>same duration</strong>, to <strong>maximize</strong> the <strong>longest</strong> <em>continuous period of free time</em> during the event.</p> <p>The <strong>relative</strong> order of all the meetings should stay the<em> same</em> and they should remain non-overlapping.</p> <p>Return the <strong>maximum</strong> amount of free time possible after rearranging the meetings.</p> <p><strong>Note</strong> that the meetings can <strong>not</strong> be rescheduled to a time outside the event.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 5, k = 1, startTime = [1,3], endTime = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/21/example0_rescheduled.png" style="width: 375px; height: 123px;" /></p> <p>Reschedule the meeting at <code>[1, 2]</code> to <code>[2, 3]</code>, leaving no meetings during the time <code>[0, 2]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 10, k = 1, startTime = [0,2,9], endTime = [1,4,10]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/21/example1_rescheduled.png" style="width: 375px; height: 125px;" /></p> <p>Reschedule the meeting at <code>[2, 4]</code> to <code>[1, 3]</code>, leaving no meetings during the time <code>[3, 9]</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 5, k = 2, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>There is no time during the event not occupied by meetings.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= eventTime &lt;= 10<sup>9</sup></code></li> <li><code>n == startTime.length == endTime.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> <li><code>0 &lt;= startTime[i] &lt; endTime[i] &lt;= eventTime</code></li> <li><code>endTime[i] &lt;= startTime[i + 1]</code> where <code>i</code> lies in the range <code>[0, n - 2]</code>.</li> </ul>
Medium
17.5K
55.7K
17,536
55,740
31.5%
['meeting-scheduler']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxFreeTime(int eventTime, int k, vector<int>& startTime, vector<int>& endTime) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxFreeTime(self, eventTime, k, startTime, endTime):\n \"\"\"\n :type eventTime: int\n :type k: int\n :type startTime: List[int]\n :type endTime: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxFreeTime(self, eventTime: int, k: int, startTime: List[int], endTime: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxFreeTime(int eventTime, int k, int* startTime, int startTimeSize, int* endTime, int endTimeSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxFreeTime(int eventTime, int k, int[] startTime, int[] endTime) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} eventTime\n * @param {number} k\n * @param {number[]} startTime\n * @param {number[]} endTime\n * @return {number}\n */\nvar maxFreeTime = function(eventTime, k, startTime, endTime) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxFreeTime(eventTime: number, k: number, startTime: number[], endTime: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $eventTime\n * @param Integer $k\n * @param Integer[] $startTime\n * @param Integer[] $endTime\n * @return Integer\n */\n function maxFreeTime($eventTime, $k, $startTime, $endTime) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxFreeTime(_ eventTime: Int, _ k: Int, _ startTime: [Int], _ endTime: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxFreeTime(eventTime: Int, k: Int, startTime: IntArray, endTime: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxFreeTime(int eventTime, int k, List<int> startTime, List<int> endTime) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxFreeTime(eventTime int, k int, startTime []int, endTime []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} event_time\n# @param {Integer} k\n# @param {Integer[]} start_time\n# @param {Integer[]} end_time\n# @return {Integer}\ndef max_free_time(event_time, k, start_time, end_time)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxFreeTime(eventTime: Int, k: Int, startTime: Array[Int], endTime: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_free_time(event_time: i32, k: i32, start_time: Vec<i32>, end_time: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-free-time eventTime k startTime endTime)\n (-> exact-integer? exact-integer? (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_free_time(EventTime :: integer(), K :: integer(), StartTime :: [integer()], EndTime :: [integer()]) -> integer().\nmax_free_time(EventTime, K, StartTime, EndTime) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_free_time(event_time :: integer, k :: integer, start_time :: [integer], end_time :: [integer]) :: integer\n def max_free_time(event_time, k, start_time, end_time) do\n \n end\nend"}]
5 1 [1,3] [2,5]
{ "name": "maxFreeTime", "params": [ { "name": "eventTime", "type": "integer" }, { "type": "integer", "name": "k" }, { "type": "integer[]", "name": "startTime" }, { "type": "integer[]", "name": "endTime" } ], "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', 'Sliding Window']
3,440
Reschedule Meetings for Maximum Free Time II
reschedule-meetings-for-maximum-free-time-ii
<p>You are given an integer <code>eventTime</code> denoting the duration of an event. You are also given two integer arrays <code>startTime</code> and <code>endTime</code>, each of length <code>n</code>.</p> <p>These represent the start and end times of <code>n</code> <strong>non-overlapping</strong> meetings that occur during the event between time <code>t = 0</code> and time <code>t = eventTime</code>, where the <code>i<sup>th</sup></code> meeting occurs during the time <code>[startTime[i], endTime[i]].</code></p> <p>You can reschedule <strong>at most </strong>one meeting by moving its start time while maintaining the <strong>same duration</strong>, such that the meetings remain non-overlapping, to <strong>maximize</strong> the <strong>longest</strong> <em>continuous period of free time</em> during the event.</p> <p>Return the <strong>maximum</strong> amount of free time possible after rearranging the meetings.</p> <p><strong>Note</strong> that the meetings can <strong>not</strong> be rescheduled to a time outside the event and they should remain non-overlapping.</p> <p><strong>Note:</strong> <em>In this version</em>, it is <strong>valid</strong> for the relative ordering of the meetings to change after rescheduling one meeting.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 5, startTime = [1,3], endTime = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/22/example0_rescheduled.png" style="width: 375px; height: 123px;" /></p> <p>Reschedule the meeting at <code>[1, 2]</code> to <code>[2, 3]</code>, leaving no meetings during the time <code>[0, 2]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 10, startTime = [0,7,9], endTime = [1,8,10]</span></p> <p><strong>Output:</strong> <span class="example-io">7</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/22/rescheduled_example0.png" style="width: 375px; height: 125px;" /></p> <p>Reschedule the meeting at <code>[0, 1]</code> to <code>[8, 9]</code>, leaving no meetings during the time <code>[0, 7]</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 10, startTime = [0,3,7,9], endTime = [1,4,8,10]</span></p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/image3.png" style="width: 375px; height: 125px;" /></strong></p> <p>Reschedule the meeting at <code>[3, 4]</code> to <code>[8, 9]</code>, leaving no meetings during the time <code>[1, 7]</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">eventTime = 5, startTime = [0,1,2,3,4], endTime = [1,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>There is no time during the event not occupied by meetings.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= eventTime &lt;= 10<sup>9</sup></code></li> <li><code>n == startTime.length == endTime.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= startTime[i] &lt; endTime[i] &lt;= eventTime</code></li> <li><code>endTime[i] &lt;= startTime[i + 1]</code> where <code>i</code> lies in the range <code>[0, n - 2]</code>.</li> </ul>
Medium
10K
24.8K
9,958
24,779
40.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxFreeTime(int eventTime, vector<int>& startTime, vector<int>& endTime) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxFreeTime(int eventTime, int[] startTime, int[] endTime) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxFreeTime(self, eventTime, startTime, endTime):\n \"\"\"\n :type eventTime: int\n :type startTime: List[int]\n :type endTime: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxFreeTime(self, eventTime: int, startTime: List[int], endTime: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxFreeTime(int eventTime, int* startTime, int startTimeSize, int* endTime, int endTimeSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxFreeTime(int eventTime, int[] startTime, int[] endTime) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} eventTime\n * @param {number[]} startTime\n * @param {number[]} endTime\n * @return {number}\n */\nvar maxFreeTime = function(eventTime, startTime, endTime) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxFreeTime(eventTime: number, startTime: number[], endTime: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $eventTime\n * @param Integer[] $startTime\n * @param Integer[] $endTime\n * @return Integer\n */\n function maxFreeTime($eventTime, $startTime, $endTime) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxFreeTime(_ eventTime: Int, _ startTime: [Int], _ endTime: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxFreeTime(eventTime: Int, startTime: IntArray, endTime: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxFreeTime(int eventTime, List<int> startTime, List<int> endTime) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxFreeTime(eventTime int, startTime []int, endTime []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} event_time\n# @param {Integer[]} start_time\n# @param {Integer[]} end_time\n# @return {Integer}\ndef max_free_time(event_time, start_time, end_time)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxFreeTime(eventTime: Int, startTime: Array[Int], endTime: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_free_time(event_time: i32, start_time: Vec<i32>, end_time: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-free-time eventTime startTime endTime)\n (-> exact-integer? (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_free_time(EventTime :: integer(), StartTime :: [integer()], EndTime :: [integer()]) -> integer().\nmax_free_time(EventTime, StartTime, EndTime) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_free_time(event_time :: integer, start_time :: [integer], end_time :: [integer]) :: integer\n def max_free_time(event_time, start_time, end_time) do\n \n end\nend"}]
5 [1,3] [2,5]
{ "name": "maxFreeTime", "params": [ { "name": "eventTime", "type": "integer" }, { "type": "integer[]", "name": "startTime" }, { "type": "integer[]", "name": "endTime" } ], "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', 'Enumeration']
3,441
Minimum Cost Good Caption
minimum-cost-good-caption
<p>You are given a string <code>caption</code> of length <code>n</code>. A <strong>good</strong> caption is a string where <strong>every</strong> character appears in groups of <strong>at least 3</strong> consecutive occurrences.</p> <p>For example:</p> <ul> <li><code>&quot;aaabbb&quot;</code> and <code>&quot;aaaaccc&quot;</code> are <strong>good</strong> captions.</li> <li><code>&quot;aabbb&quot;</code> and <code>&quot;ccccd&quot;</code> are <strong>not</strong> good captions.</li> </ul> <p>You can perform the following operation <strong>any</strong> number of times:</p> <p>Choose an index <code>i</code> (where <code>0 &lt;= i &lt; n</code>) and change the character at that index to either:</p> <ul> <li>The character immediately <strong>before</strong> it in the alphabet (if <code>caption[i] != &#39;a&#39;</code>).</li> <li>The character immediately <strong>after</strong> it in the alphabet (if <code>caption[i] != &#39;z&#39;</code>).</li> </ul> <p>Your task is to convert the given <code>caption</code> into a <strong>good</strong> caption using the <strong>minimum</strong> number of operations, and return it. If there are <strong>multiple</strong> possible good captions, return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> one among them. If it is <strong>impossible</strong> to create a good caption, return an empty string <code>&quot;&quot;</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">caption = &quot;cdcd&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;cccc&quot;</span></p> <p><strong>Explanation:</strong></p> <p>It can be shown that the given caption cannot be transformed into a good caption with fewer than 2 operations. The possible good captions that can be created using exactly 2 operations are:</p> <ul> <li><code>&quot;dddd&quot;</code>: Change <code>caption[0]</code> and <code>caption[2]</code> to their next character <code>&#39;d&#39;</code>.</li> <li><code>&quot;cccc&quot;</code>: Change <code>caption[1]</code> and <code>caption[3]</code> to their previous character <code>&#39;c&#39;</code>.</li> </ul> <p>Since <code>&quot;cccc&quot;</code> is lexicographically smaller than <code>&quot;dddd&quot;</code>, return <code>&quot;cccc&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">caption = &quot;aca&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;aaa&quot;</span></p> <p><strong>Explanation:</strong></p> <p>It can be proven that the given caption requires at least 2 operations to be transformed into a good caption. The only good caption that can be obtained with exactly 2 operations is as follows:</p> <ul> <li>Operation 1: Change <code>caption[1]</code> to <code>&#39;b&#39;</code>. <code>caption = &quot;aba&quot;</code>.</li> <li>Operation 2: Change <code>caption[1]</code> to <code>&#39;a&#39;</code>. <code>caption = &quot;aaa&quot;</code>.</li> </ul> <p>Thus, return <code>&quot;aaa&quot;</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">caption = &quot;bc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p> <p><strong>Explanation:</strong></p> <p>It can be shown that the given caption cannot be converted to a good caption by using any number of operations.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= caption.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>caption</code> consists only of lowercase English letters.</li> </ul>
Hard
2.3K
12.2K
2,343
12,182
19.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string minCostGoodCaption(string caption) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String minCostGoodCaption(String caption) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCostGoodCaption(self, caption):\n \"\"\"\n :type caption: str\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCostGoodCaption(self, caption: str) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* minCostGoodCaption(char* caption) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string MinCostGoodCaption(string caption) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} caption\n * @return {string}\n */\nvar minCostGoodCaption = function(caption) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCostGoodCaption(caption: string): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $caption\n * @return String\n */\n function minCostGoodCaption($caption) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCostGoodCaption(_ caption: String) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCostGoodCaption(caption: String): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String minCostGoodCaption(String caption) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCostGoodCaption(caption string) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} caption\n# @return {String}\ndef min_cost_good_caption(caption)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCostGoodCaption(caption: String): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost_good_caption(caption: String) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost-good-caption caption)\n (-> string? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost_good_caption(Caption :: unicode:unicode_binary()) -> unicode:unicode_binary().\nmin_cost_good_caption(Caption) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost_good_caption(caption :: String.t) :: String.t\n def min_cost_good_caption(caption) do\n \n end\nend"}]
"cdcd"
{ "name": "minCostGoodCaption", "params": [ { "name": "caption", "type": "string" } ], "return": { "type": "string" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['String', 'Dynamic Programming']
3,442
Maximum Difference Between Even and Odd Frequency I
maximum-difference-between-even-and-odd-frequency-i
<p>You are given a string <code>s</code> consisting of lowercase English letters. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters in the string such that:</p> <ul> <li>One of the characters has an <strong>even frequency</strong> in the string.</li> <li>The other character has an <strong>odd frequency</strong> in the string.</li> </ul> <p>Return the <strong>maximum</strong> difference, calculated as the frequency of the character with an <b>odd</b> frequency <strong>minus</strong> the frequency of the character with an <b>even</b> frequency.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aaaaabbc&quot;</span></p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <ul> <li>The character <code>&#39;a&#39;</code> has an <strong>odd frequency</strong> of <code><font face="monospace">5</font></code><font face="monospace">,</font> and <code>&#39;b&#39;</code> has an <strong>even frequency</strong> of <code><font face="monospace">2</font></code>.</li> <li>The maximum difference is <code>5 - 2 = 3</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abcabcab&quot;</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <ul> <li>The character <code>&#39;a&#39;</code> has an <strong>odd frequency</strong> of <code><font face="monospace">3</font></code><font face="monospace">,</font> and <code>&#39;c&#39;</code> has an <strong>even frequency</strong> of <font face="monospace">2</font>.</li> <li>The maximum difference is <code>3 - 2 = 1</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists only of lowercase English letters.</li> <li><code>s</code> contains at least one character with an odd frequency and one with an even frequency.</li> </ul>
Easy
39.8K
81K
39,779
80,977
49.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxDifference(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxDifference(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxDifference(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxDifference(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxDifference(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxDifference(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar maxDifference = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxDifference(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function maxDifference($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxDifference(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxDifference(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxDifference(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxDifference(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef max_difference(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxDifference(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_difference(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-difference s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_difference(S :: unicode:unicode_binary()) -> integer().\nmax_difference(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_difference(s :: String.t) :: integer\n def max_difference(s) do\n \n end\nend"}]
"aaaaabbc"
{ "name": "maxDifference", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "integer" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Hash Table', 'String', 'Counting']
3,443
Maximum Manhattan Distance After K Changes
maximum-manhattan-distance-after-k-changes
<p>You are given a string <code>s</code> consisting of the characters <code>&#39;N&#39;</code>, <code>&#39;S&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;W&#39;</code>, where <code>s[i]</code> indicates movements in an infinite grid:</p> <ul> <li><code>&#39;N&#39;</code> : Move north by 1 unit.</li> <li><code>&#39;S&#39;</code> : Move south by 1 unit.</li> <li><code>&#39;E&#39;</code> : Move east by 1 unit.</li> <li><code>&#39;W&#39;</code> : Move west by 1 unit.</li> </ul> <p>Initially, you are at the origin <code>(0, 0)</code>. You can change <strong>at most</strong> <code>k</code> characters to any of the four directions.</p> <p>Find the <strong>maximum</strong> <strong>Manhattan distance</strong> from the origin that can be achieved <strong>at any time</strong> while performing the movements <strong>in order</strong>.</p> The <strong>Manhattan Distance</strong> between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;NWSE&quot;, k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>Change <code>s[2]</code> from <code>&#39;S&#39;</code> to <code>&#39;N&#39;</code>. The string <code>s</code> becomes <code>&quot;NWNE&quot;</code>.</p> <table style="border: 1px solid black;"> <thead> <tr> <th style="border: 1px solid black;">Movement</th> <th style="border: 1px solid black;">Position (x, y)</th> <th style="border: 1px solid black;">Manhattan Distance</th> <th style="border: 1px solid black;">Maximum</th> </tr> </thead> <tbody> <tr> <td style="border: 1px solid black;">s[0] == &#39;N&#39;</td> <td style="border: 1px solid black;">(0, 1)</td> <td style="border: 1px solid black;">0 + 1 = 1</td> <td style="border: 1px solid black;">1</td> </tr> <tr> <td style="border: 1px solid black;">s[1] == &#39;W&#39;</td> <td style="border: 1px solid black;">(-1, 1)</td> <td style="border: 1px solid black;">1 + 1 = 2</td> <td style="border: 1px solid black;">2</td> </tr> <tr> <td style="border: 1px solid black;">s[2] == &#39;N&#39;</td> <td style="border: 1px solid black;">(-1, 2)</td> <td style="border: 1px solid black;">1 + 2 = 3</td> <td style="border: 1px solid black;">3</td> </tr> <tr> <td style="border: 1px solid black;">s[3] == &#39;E&#39;</td> <td style="border: 1px solid black;">(0, 2)</td> <td style="border: 1px solid black;">0 + 2 = 2</td> <td style="border: 1px solid black;">3</td> </tr> </tbody> </table> <p>The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;NSWWEW&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>Change <code>s[1]</code> from <code>&#39;S&#39;</code> to <code>&#39;N&#39;</code>, and <code>s[4]</code> from <code>&#39;E&#39;</code> to <code>&#39;W&#39;</code>. The string <code>s</code> becomes <code>&quot;NNWWWW&quot;</code>.</p> <p>The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= k &lt;= s.length</code></li> <li><code>s</code> consists of only <code>&#39;N&#39;</code>, <code>&#39;S&#39;</code>, <code>&#39;E&#39;</code>, and <code>&#39;W&#39;</code>.</li> </ul>
Medium
15.6K
50.3K
15,616
50,345
31.0%
['as-far-from-land-as-possible']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxDistance(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxDistance(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxDistance(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 maxDistance(self, s: str, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxDistance(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxDistance(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 maxDistance = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxDistance(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 maxDistance($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxDistance(_ s: String, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxDistance(s: String, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxDistance(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxDistance(s string, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef max_distance(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxDistance(s: String, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_distance(s: String, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-distance s k)\n (-> string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_distance(S :: unicode:unicode_binary(), K :: integer()) -> integer().\nmax_distance(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_distance(s :: String.t, k :: integer) :: integer\n def max_distance(s, k) do\n \n end\nend"}]
"NWSE" 1
{ "name": "maxDistance", "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', 'Math', 'String', 'Counting']
3,444
Minimum Increments for Target Multiples in an Array
minimum-increments-for-target-multiples-in-an-array
<p>You are given two arrays, <code>nums</code> and <code>target</code>.</p> <p>In a single operation, you may increment any element of <code>nums</code> by 1.</p> <p>Return <strong>the minimum number</strong> of operations required so that each element in <code>target</code> has <strong>at least</strong> one multiple in <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,2,3], target = [4]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The minimum number of operations required to satisfy the condition is 1.</p> <ul> <li>Increment 3 to 4 with just one operation, making 4 a multiple of itself.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [8,4], target = [10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The minimum number of operations required to satisfy the condition is 2.</p> <ul> <li>Increment 8 to 10 with 2 operations, making 10 a multiple of both 5 and 10.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [7,9,10], target = [7]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Target 7 already has a multiple in nums, so no additional operations are needed.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= target.length &lt;= 4</code></li> <li><code>target.length &lt;= nums.length</code></li> <li><code>1 &lt;= nums[i], target[i] &lt;= 10<sup>4</sup></code></li> </ul>
Hard
5.3K
20.5K
5,275
20,549
25.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumIncrements(vector<int>& nums, vector<int>& target) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumIncrements(int[] nums, int[] target) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumIncrements(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumIncrements(self, nums: List[int], target: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumIncrements(int* nums, int numsSize, int* target, int targetSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumIncrements(int[] nums, int[] target) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number[]} target\n * @return {number}\n */\nvar minimumIncrements = function(nums, target) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumIncrements(nums: number[], target: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[] $target\n * @return Integer\n */\n function minimumIncrements($nums, $target) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumIncrements(_ nums: [Int], _ target: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumIncrements(nums: IntArray, target: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumIncrements(List<int> nums, List<int> target) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumIncrements(nums []int, target []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer[]} target\n# @return {Integer}\ndef minimum_increments(nums, target)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumIncrements(nums: Array[Int], target: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_increments(nums: Vec<i32>, target: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-increments nums target)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_increments(Nums :: [integer()], Target :: [integer()]) -> integer().\nminimum_increments(Nums, Target) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_increments(nums :: [integer], target :: [integer]) :: integer\n def minimum_increments(nums, target) do\n \n end\nend"}]
[1,2,3] [4]
{ "name": "minimumIncrements", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[]", "name": "target" } ], "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', 'Number Theory', 'Bitmask']
3,445
Maximum Difference Between Even and Odd Frequency II
maximum-difference-between-even-and-odd-frequency-ii
<p>You are given a string <code>s</code> and an integer <code>k</code>. Your task is to find the <strong>maximum</strong> difference between the frequency of <strong>two</strong> characters, <code>freq[a] - freq[b]</code>, in a <span data-keyword="substring">substring</span> <code>subs</code> of <code>s</code>, such that:</p> <ul> <li><code>subs</code> has a size of <strong>at least</strong> <code>k</code>.</li> <li>Character <code>a</code> has an <em>odd frequency</em> in <code>subs</code>.</li> <li>Character <code>b</code> has an <em>even frequency</em> in <code>subs</code>.</li> </ul> <p>Return the <strong>maximum</strong> difference.</p> <p><strong>Note</strong> that <code>subs</code> can contain more than 2 <strong>distinct</strong> characters.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;12233&quot;, k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>For the substring <code>&quot;12233&quot;</code>, the frequency of <code>&#39;1&#39;</code> is 1 and the frequency of <code>&#39;3&#39;</code> is 2. The difference is <code>1 - 2 = -1</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1122211&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>For the substring <code>&quot;11222&quot;</code>, the frequency of <code>&#39;2&#39;</code> is 3 and the frequency of <code>&#39;1&#39;</code> is 2. The difference is <code>3 - 2 = 1</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;110&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= s.length &lt;= 3 * 10<sup>4</sup></code></li> <li><code>s</code> consists only of digits <code>&#39;0&#39;</code> to <code>&#39;4&#39;</code>.</li> <li>The input is generated that at least one substring has a character with an even frequency and a character with an odd frequency.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
Hard
2.1K
10.8K
2,089
10,797
19.3%
['frequency-of-the-most-frequent-element', 'count-elements-with-maximum-frequency']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxDifference(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxDifference(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxDifference(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 maxDifference(self, s: str, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxDifference(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxDifference(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 maxDifference = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxDifference(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 maxDifference($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxDifference(_ s: String, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxDifference(s: String, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxDifference(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxDifference(s string, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef max_difference(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxDifference(s: String, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_difference(s: String, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-difference s k)\n (-> string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_difference(S :: unicode:unicode_binary(), K :: integer()) -> integer().\nmax_difference(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_difference(s :: String.t, k :: integer) :: integer\n def max_difference(s, k) do\n \n end\nend"}]
"12233" 4
{ "name": "maxDifference", "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', 'Sliding Window', 'Enumeration', 'Prefix Sum']
3,446
Sort Matrix by Diagonals
sort-matrix-by-diagonals
<p>You are given an <code>n x n</code> square matrix of integers <code>grid</code>. Return the matrix such that:</p> <ul> <li>The diagonals in the <strong>bottom-left triangle</strong> (including the middle diagonal) are sorted in <strong>non-increasing order</strong>.</li> <li>The diagonals in the <strong>top-right triangle</strong> are sorted in <strong>non-decreasing order</strong>.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,7,3],[9,8,2],[4,5,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">[[8,2,3],[9,6,7],[4,5,1]]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/29/4052example1drawio.png" style="width: 461px; height: 181px;" /></p> <p>The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:</p> <ul> <li><code>[1, 8, 6]</code> becomes <code>[8, 6, 1]</code>.</li> <li><code>[9, 5]</code> and <code>[4]</code> remain unchanged.</li> </ul> <p>The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:</p> <ul> <li><code>[7, 2]</code> becomes <code>[2, 7]</code>.</li> <li><code>[3]</code> remains unchanged.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[0,1],[1,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">[[2,1],[1,0]]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/29/4052example2adrawio.png" style="width: 383px; height: 141px;" /></p> <p>The diagonals with a black arrow must be non-increasing, so <code>[0, 2]</code> is changed to <code>[2, 0]</code>. The other diagonals are already in the correct order.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">[[1]]</span></p> <p><strong>Explanation:</strong></p> <p>Diagonals with exactly one element are already in order, so no changes are needed.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>grid.length == grid[i].length == n</code></li> <li><code>1 &lt;= n &lt;= 10</code></li> <li><code>-10<sup>5</sup> &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li> </ul>
Medium
28K
38.6K
27,989
38,561
72.6%
['sort-the-matrix-diagonally']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<vector<int>> sortMatrix(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[][] sortMatrix(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sortMatrix(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def sortMatrix(self, grid: 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** sortMatrix(int** grid, int gridSize, int* gridColSize, int* returnSize, int** returnColumnSizes) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[][] SortMatrix(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar sortMatrix = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sortMatrix(grid: number[][]): number[][] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer[][]\n */\n function sortMatrix($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sortMatrix(_ grid: [[Int]]) -> [[Int]] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sortMatrix(grid: Array<IntArray>): Array<IntArray> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<List<int>> sortMatrix(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sortMatrix(grid [][]int) [][]int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Integer[][]}\ndef sort_matrix(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sortMatrix(grid: Array[Array[Int]]): Array[Array[Int]] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sort_matrix(grid: Vec<Vec<i32>>) -> Vec<Vec<i32>> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sort-matrix grid)\n (-> (listof (listof exact-integer?)) (listof (listof exact-integer?)))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sort_matrix(Grid :: [[integer()]]) -> [[integer()]].\nsort_matrix(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sort_matrix(grid :: [[integer]]) :: [[integer]]\n def sort_matrix(grid) do\n \n end\nend"}]
[[1,7,3],[9,8,2],[4,5,6]]
{ "name": "sortMatrix", "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', 'Sorting', 'Matrix']
3,447
Assign Elements to Groups with Constraints
assign-elements-to-groups-with-constraints
<p>You are given an integer array <code>groups</code>, where <code>groups[i]</code> represents the size of the <code>i<sup>th</sup></code> group. You are also given an integer array <code>elements</code>.</p> <p>Your task is to assign <strong>one</strong> element to each group based on the following rules:</p> <ul> <li>An element at index <code>j</code> can be assigned to a group <code>i</code> if <code>groups[i]</code> is <strong>divisible</strong> by <code>elements[j]</code>.</li> <li>If there are multiple elements that can be assigned, assign the element with the <strong>smallest index</strong> <code>j</code>.</li> <li>If no element satisfies the condition for a group, assign -1 to that group.</li> </ul> <p>Return an integer array <code>assigned</code>, where <code>assigned[i]</code> is the index of the element chosen for group <code>i</code>, or -1 if no suitable element exists.</p> <p><strong>Note</strong>: An element may be assigned to more than one group.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">groups = [8,4,3,2,4], elements = [4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,0,-1,1,0]</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>elements[0] = 4</code> is assigned to groups 0, 1, and 4.</li> <li><code>elements[1] = 2</code> is assigned to group 3.</li> <li>Group 2 cannot be assigned any element.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">groups = [2,3,5,7], elements = [5,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,0,-1]</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>elements[1] = 3</code> is assigned to group 1.</li> <li><code>elements[0] = 5</code> is assigned to group 2.</li> <li>Groups 0 and 3 cannot be assigned any element.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">groups = [10,21,30,41], elements = [2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1,0,1]</span></p> <p><strong>Explanation:</strong></p> <p><code>elements[0] = 2</code> is assigned to the groups with even values, and <code>elements[1] = 1</code> is assigned to the groups with odd values.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= groups.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= elements.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= groups[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= elements[i] &lt;= 10<sup>5</sup></code></li> </ul>
Medium
18.1K
67K
18,127
67,011
27.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> assignElements(vector<int>& groups, vector<int>& elements) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] assignElements(int[] groups, int[] elements) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def assignElements(self, groups, elements):\n \"\"\"\n :type groups: List[int]\n :type elements: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def assignElements(self, groups: List[int], elements: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* assignElements(int* groups, int groupsSize, int* elements, int elementsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] AssignElements(int[] groups, int[] elements) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} groups\n * @param {number[]} elements\n * @return {number[]}\n */\nvar assignElements = function(groups, elements) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function assignElements(groups: number[], elements: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $groups\n * @param Integer[] $elements\n * @return Integer[]\n */\n function assignElements($groups, $elements) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func assignElements(_ groups: [Int], _ elements: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun assignElements(groups: IntArray, elements: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> assignElements(List<int> groups, List<int> elements) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func assignElements(groups []int, elements []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} groups\n# @param {Integer[]} elements\n# @return {Integer[]}\ndef assign_elements(groups, elements)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def assignElements(groups: Array[Int], elements: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn assign_elements(groups: Vec<i32>, elements: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (assign-elements groups elements)\n (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec assign_elements(Groups :: [integer()], Elements :: [integer()]) -> [integer()].\nassign_elements(Groups, Elements) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec assign_elements(groups :: [integer], elements :: [integer]) :: [integer]\n def assign_elements(groups, elements) do\n \n end\nend"}]
[8,4,3,2,4] [4,2]
{ "name": "assignElements", "params": [ { "name": "groups", "type": "integer[]" }, { "type": "integer[]", "name": "elements" } ], "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']
3,448
Count Substrings Divisible By Last Digit
count-substrings-divisible-by-last-digit
<p>You are given a string <code>s</code> consisting of digits.</p> <p>Return the <strong>number</strong> of <span data-keyword="substring-nonempty">substrings</span> of <code>s</code> <strong>divisible</strong> by their <strong>non-zero</strong> last digit.</p> <p><strong>Note</strong>: A substring may contain leading zeros.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;12936&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Substrings <code>&quot;29&quot;</code>, <code>&quot;129&quot;</code>, <code>&quot;293&quot;</code> and <code>&quot;2936&quot;</code> are not divisible by their last digit. There are 15 substrings in total, so the answer is <code>15 - 4 = 11</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;5701283&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">18</span></p> <p><strong>Explanation:</strong></p> <p>Substrings <code>&quot;01&quot;</code>, <code>&quot;12&quot;</code>, <code>&quot;701&quot;</code>, <code>&quot;012&quot;</code>, <code>&quot;128&quot;</code>, <code>&quot;5701&quot;</code>, <code>&quot;7012&quot;</code>, <code>&quot;0128&quot;</code>, <code>&quot;57012&quot;</code>, <code>&quot;70128&quot;</code>, <code>&quot;570128&quot;</code>, and <code>&quot;701283&quot;</code> are all divisible by their last digit. Additionally, all substrings that are just 1 non-zero digit are divisible by themselves. Since there are 6 such digits, the answer is <code>12 + 6 = 18</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1010101010&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">25</span></p> <p><strong>Explanation:</strong></p> <p>Only substrings that end with digit <code>&#39;1&#39;</code> are divisible by their last digit. There are 25 such substrings.</p> </div> <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 digits only.</li> </ul>
Hard
4.8K
22.8K
4,774
22,830
20.9%
['number-of-divisible-substrings']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long countSubstrings(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long countSubstrings(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countSubstrings(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countSubstrings(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long countSubstrings(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long CountSubstrings(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar countSubstrings = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countSubstrings(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function countSubstrings($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countSubstrings(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countSubstrings(s: String): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countSubstrings(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countSubstrings(s string) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef count_substrings(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countSubstrings(s: String): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_substrings(s: String) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-substrings s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_substrings(S :: unicode:unicode_binary()) -> integer().\ncount_substrings(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_substrings(s :: String.t) :: integer\n def count_substrings(s) do\n \n end\nend"}]
"12936"
{ "name": "countSubstrings", "params": [ { "name": "s", "type": "string" } ], "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>"]}
['String', 'Dynamic Programming']
3,449
Maximize the Minimum Game Score
maximize-the-minimum-game-score
<p>You are given an array <code>points</code> of size <code>n</code> and an integer <code>m</code>. There is another array <code>gameScore</code> of size <code>n</code>, where <code>gameScore[i]</code> represents the score achieved at the <code>i<sup>th</sup></code> game. Initially, <code>gameScore[i] == 0</code> for all <code>i</code>.</p> <p>You start at index -1, which is outside the array (before the first position at index 0). You can make <strong>at most</strong> <code>m</code> moves. In each move, you can either:</p> <ul> <li>Increase the index by 1 and add <code>points[i]</code> to <code>gameScore[i]</code>.</li> <li>Decrease the index by 1 and add <code>points[i]</code> to <code>gameScore[i]</code>.</li> </ul> <p><strong>Note</strong> that the index must always remain within the bounds of the array after the first move.</p> <p>Return the <strong>maximum possible minimum</strong> value in <code>gameScore</code> after <strong>at most</strong> <code>m</code> moves.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">points = [2,4], m = 3</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Initially, index <code>i = -1</code> and <code>gameScore = [0, 0]</code>.</p> <table style="border: 1px solid black;"> <thead> <tr> <th style="border: 1px solid black;">Move</th> <th style="border: 1px solid black;">Index</th> <th style="border: 1px solid black;">gameScore</th> </tr> </thead> <tbody> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>[2, 0]</code></td> </tr> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>[2, 4]</code></td> </tr> <tr> <td style="border: 1px solid black;">Decrease <code>i</code></td> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>[4, 4]</code></td> </tr> </tbody> </table> <p>The minimum value in <code>gameScore</code> is 4, and this is the maximum possible minimum among all configurations. Hence, 4 is the output.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">points = [1,2,3], m = 5</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Initially, index <code>i = -1</code> and <code>gameScore = [0, 0, 0]</code>.</p> <table style="border: 1px solid black;"> <thead> <tr> <th style="border: 1px solid black;">Move</th> <th style="border: 1px solid black;">Index</th> <th style="border: 1px solid black;">gameScore</th> </tr> </thead> <tbody> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>[1, 0, 0]</code></td> </tr> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>[1, 2, 0]</code></td> </tr> <tr> <td style="border: 1px solid black;">Decrease <code>i</code></td> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>[2, 2, 0]</code></td> </tr> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>[2, 4, 0]</code></td> </tr> <tr> <td style="border: 1px solid black;">Increase <code>i</code></td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;"><code>[2, 4, 3]</code></td> </tr> </tbody> </table> <p>The minimum value in <code>gameScore</code> is 2, and this is the maximum possible minimum among all configurations. Hence, 2 is the output.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == points.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= points[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= m &lt;= 10<sup>9</sup></code></li> </ul>
Hard
2.9K
11.7K
2,876
11,749
24.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxScore(vector<int>& points, int m) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxScore(int[] points, int m) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxScore(self, points, m):\n \"\"\"\n :type points: List[int]\n :type m: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxScore(self, points: List[int], m: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxScore(int* points, int pointsSize, int m) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxScore(int[] points, int m) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} points\n * @param {number} m\n * @return {number}\n */\nvar maxScore = function(points, m) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxScore(points: number[], m: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $points\n * @param Integer $m\n * @return Integer\n */\n function maxScore($points, $m) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxScore(_ points: [Int], _ m: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxScore(points: IntArray, m: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxScore(List<int> points, int m) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxScore(points []int, m int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} points\n# @param {Integer} m\n# @return {Integer}\ndef max_score(points, m)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxScore(points: Array[Int], m: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_score(points: Vec<i32>, m: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-score points m)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_score(Points :: [integer()], M :: integer()) -> integer().\nmax_score(Points, M) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_score(points :: [integer], m :: integer) :: integer\n def max_score(points, m) do\n \n end\nend"}]
[2,4] 3
{ "name": "maxScore", "params": [ { "name": "points", "type": "integer[]" }, { "type": "integer", "name": "m" } ], "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']
3,450
Maximum Students on a Single Bench
maximum-students-on-a-single-bench
null
Easy
1.1K
1.3K
1,085
1,256
86.4%
[]
[]
Algorithms
null
[[1,2],[2,2],[3,3],[1,3],[2,3]]
{ "name": "maxStudentsOnBench", "params": [ { "name": "students", "type": "integer[][]" } ], "return": { "type": "integer" }, "manual": false }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Hash Table']
3,451
Find Invalid IP Addresses
find-invalid-ip-addresses
<p>Table: <code> logs</code></p> <pre> +-------------+---------+ | Column Name | Type | +-------------+---------+ | log_id | int | | ip | varchar | | status_code | int | +-------------+---------+ log_id is the unique key for this table. Each row contains server access log information including IP address and HTTP status code. </pre> <p>Write a solution to find <strong>invalid IP addresses</strong>. An IPv4 address is invalid if it meets any of these conditions:</p> <ul> <li>Contains numbers <strong>greater than</strong> <code>255</code> in any octet</li> <li>Has <strong>leading zeros</strong> in any octet (like <code>01.02.03.04</code>)</li> <li>Has <strong>less or more</strong> than <code>4</code> octets</li> </ul> <p>Return <em>the result table </em><em>ordered by</em> <code>invalid_count</code>,&nbsp;<code>ip</code>&nbsp;<em>in <strong>descending</strong> order respectively</em>.&nbsp;</p> <p>The result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>logs table:</p> <pre class="example-io"> +--------+---------------+-------------+ | log_id | ip | status_code | +--------+---------------+-------------+ | 1 | 192.168.1.1 | 200 | | 2 | 256.1.2.3 | 404 | | 3 | 192.168.001.1 | 200 | | 4 | 192.168.1.1 | 200 | | 5 | 192.168.1 | 500 | | 6 | 256.1.2.3 | 404 | | 7 | 192.168.001.1 | 200 | +--------+---------------+-------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +---------------+--------------+ | ip | invalid_count| +---------------+--------------+ | 256.1.2.3 | 2 | | 192.168.001.1 | 2 | | 192.168.1 | 1 | +---------------+--------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>256.1.2.3&nbsp;is invalid because 256 &gt; 255</li> <li>192.168.001.1&nbsp;is invalid because of leading zeros</li> <li>192.168.1&nbsp;is invalid because it has only 3 octets</li> </ul> <p>The output table is ordered by invalid_count, ip in descending order respectively.</p> </div>
Hard
2.3K
3.9K
2,282
3,942
57.9%
[]
['CREATE TABLE logs (\n log_id INT,\n ip VARCHAR(255),\n status_code INT\n)\n', 'Truncate table logs', "insert into logs (log_id, ip, status_code) values ('1', '192.168.1.1', '200')", "insert into logs (log_id, ip, status_code) values ('2', '256.1.2.3', '404')", "insert into logs (log_id, ip, status_code) values ('3', '192.168.001.1', '200')", "insert into logs (log_id, ip, status_code) values ('4', '192.168.1.1', '200')", "insert into logs (log_id, ip, status_code) values ('5', '192.168.1', '500')", "insert into logs (log_id, ip, status_code) values ('6', '256.1.2.3', '404')", "insert into logs (log_id, ip, status_code) values ('7', '192.168.001.1', '200')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"logs":["log_id","ip","status_code"]},"rows":{"logs":[[1,"192.168.1.1",200],[2,"256.1.2.3",404],[3,"192.168.001.1",200],[4,"192.168.1.1",200],[5,"192.168.1",500],[6,"256.1.2.3",404],[7,"192.168.001.1",200]]}}
{"mysql": ["CREATE TABLE logs (\n log_id INT,\n ip VARCHAR(255),\n status_code INT\n)\n"], "mssql": ["CREATE TABLE logs (\n log_id INT,\n ip VARCHAR(255),\n status_code INT\n)\n"], "oraclesql": ["CREATE TABLE logs (\n log_id NUMBER,\n ip VARCHAR2(255),\n status_code NUMBER\n)\n"], "database": true, "name": "find_invalid_ips", "postgresql": ["CREATE TABLE logs (\n log_id INTEGER,\n ip VARCHAR(255), \n status_code INTEGER \n);"], "pythondata": ["logs = pd.DataFrame(columns=[\"log_id\", \"ip\", \"status_code\"]).astype({\"log_id\": \"Int64\", \"ip\": \"string\", \"status_code\": \"Int64\"})\n"], "database_schema": {"logs": {"log_id": "INT", "ip": "VARCHAR(255)", "status_code": "INT"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,452
Sum of Good Numbers
sum-of-good-numbers
<p>Given an array of integers <code>nums</code> and an integer <code>k</code>, an element <code>nums[i]</code> is considered <strong>good</strong> if it is <strong>strictly</strong> greater than the elements at indices <code>i - k</code> and <code>i + k</code> (if those indices exist). If neither of these indices <em>exists</em>, <code>nums[i]</code> is still considered <strong>good</strong>.</p> <p>Return the <strong>sum</strong> of all the <strong>good</strong> elements in the array.</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,3,2,1,5,4], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>The good numbers are <code>nums[1] = 3</code>, <code>nums[4] = 5</code>, and <code>nums[5] = 4</code> because they are strictly greater than the numbers at indices <code>i - k</code> and <code>i + k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The only good number is <code>nums[0] = 2</code> because it is strictly greater than <code>nums[1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 1000</code></li> <li><code>1 &lt;= k &lt;= floor(nums.length / 2)</code></li> </ul>
Easy
41.2K
59K
41,239
58,984
69.9%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int sumOfGoodNumbers(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int sumOfGoodNumbers(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def sumOfGoodNumbers(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 sumOfGoodNumbers(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int sumOfGoodNumbers(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int SumOfGoodNumbers(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 sumOfGoodNumbers = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function sumOfGoodNumbers(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 sumOfGoodNumbers($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func sumOfGoodNumbers(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun sumOfGoodNumbers(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int sumOfGoodNumbers(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func sumOfGoodNumbers(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef sum_of_good_numbers(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def sumOfGoodNumbers(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn sum_of_good_numbers(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (sum-of-good-numbers nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec sum_of_good_numbers(Nums :: [integer()], K :: integer()) -> integer().\nsum_of_good_numbers(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec sum_of_good_numbers(nums :: [integer], k :: integer) :: integer\n def sum_of_good_numbers(nums, k) do\n \n end\nend"}]
[1,3,2,1,5,4] 2
{ "name": "sumOfGoodNumbers", "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']
3,453
Separate Squares I
separate-squares-i
<p>You are given a 2D integer array <code>squares</code>. Each <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.</p> <p>Find the <strong>minimum</strong> y-coordinate value of a horizontal line such that the total area of the squares above the line <em>equals</em> the total area of the squares below the line.</p> <p>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p> <p><strong>Note</strong>: Squares <strong>may</strong> overlap. Overlapping areas should be counted <strong>multiple times</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">squares = [[0,0,1],[2,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">1.00000</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/06/4062example1drawio.png" style="width: 378px; height: 352px;" /></p> <p>Any horizontal line between <code>y = 1</code> and <code>y = 2</code> will have 1 square unit above it and 1 square unit below it. The lowest option is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">squares = [[0,0,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">1.16667</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/15/4062example2drawio.png" style="width: 378px; height: 352px;" /></p> <p>The areas are:</p> <ul> <li>Below the line: <code>7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5</code>.</li> <li>Above the line: <code>5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5</code>.</li> </ul> <p>Since the areas above and below the line are equal, the output is <code>7/6 = 1.16667</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= squares.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code></li> <li><code>squares[i].length == 3</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= l<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li>The total area of all the squares will not exceed <code>10<sup>12</sup></code>.</li> </ul>
Medium
20.3K
55.4K
20,297
55,366
36.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n double separateSquares(vector<vector<int>>& squares) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public double separateSquares(int[][] squares) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def separateSquares(self, squares):\n \"\"\"\n :type squares: List[List[int]]\n :rtype: float\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def separateSquares(self, squares: List[List[int]]) -> float:\n "}, {"value": "c", "text": "C", "defaultCode": "double separateSquares(int** squares, int squaresSize, int* squaresColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public double SeparateSquares(int[][] squares) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} squares\n * @return {number}\n */\nvar separateSquares = function(squares) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function separateSquares(squares: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $squares\n * @return Float\n */\n function separateSquares($squares) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func separateSquares(_ squares: [[Int]]) -> Double {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun separateSquares(squares: Array<IntArray>): Double {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n double separateSquares(List<List<int>> squares) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func separateSquares(squares [][]int) float64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} squares\n# @return {Float}\ndef separate_squares(squares)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def separateSquares(squares: Array[Array[Int]]): Double = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn separate_squares(squares: Vec<Vec<i32>>) -> f64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (separate-squares squares)\n (-> (listof (listof exact-integer?)) flonum?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec separate_squares(Squares :: [[integer()]]) -> float().\nseparate_squares(Squares) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec separate_squares(squares :: [[integer]]) :: float\n def separate_squares(squares) do\n \n end\nend"}]
[[0,0,1],[2,2,1]]
{ "name": "separateSquares", "params": [ { "name": "squares", "type": "integer[][]" } ], "return": { "type": "double" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Binary Search']
3,454
Separate Squares II
separate-squares-ii
<p>You are given a 2D integer array <code>squares</code>. Each <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.</p> <p>Find the <strong>minimum</strong> y-coordinate value of a horizontal line such that the total area covered by squares above the line <em>equals</em> the total area covered by squares below the line.</p> <p>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p> <p><strong>Note</strong>: Squares <strong>may</strong> overlap. Overlapping areas should be counted <strong>only once</strong> in this version.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">squares = [[0,0,1],[2,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">1.00000</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/15/4065example1drawio.png" style="width: 269px; height: 203px;" /></p> <p>Any horizontal line between <code>y = 1</code> and <code>y = 2</code> results in an equal split, with 1 square unit above and 1 square unit below. The minimum y-value is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">squares = [[0,0,2],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">1.00000</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/15/4065example2drawio.png" style="width: 269px; height: 203px;" /></p> <p>Since the blue square overlaps with the red square, it will not be counted again. Thus, the line <code>y = 1</code> splits the squares into two equal parts.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= squares.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code></li> <li><code>squares[i].length == 3</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= l<sub>i</sub> &lt;= 10<sup>9</sup></code></li> <li>The total area of all the squares will not exceed <code>10<sup>15</sup></code>.</li> </ul>
Hard
2K
10.4K
2,035
10,445
19.5%
['rectangle-area-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n double separateSquares(vector<vector<int>>& squares) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public double separateSquares(int[][] squares) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def separateSquares(self, squares):\n \"\"\"\n :type squares: List[List[int]]\n :rtype: float\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def separateSquares(self, squares: List[List[int]]) -> float:\n "}, {"value": "c", "text": "C", "defaultCode": "double separateSquares(int** squares, int squaresSize, int* squaresColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public double SeparateSquares(int[][] squares) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} squares\n * @return {number}\n */\nvar separateSquares = function(squares) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function separateSquares(squares: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $squares\n * @return Float\n */\n function separateSquares($squares) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func separateSquares(_ squares: [[Int]]) -> Double {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun separateSquares(squares: Array<IntArray>): Double {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n double separateSquares(List<List<int>> squares) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func separateSquares(squares [][]int) float64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} squares\n# @return {Float}\ndef separate_squares(squares)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def separateSquares(squares: Array[Array[Int]]): Double = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn separate_squares(squares: Vec<Vec<i32>>) -> f64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (separate-squares squares)\n (-> (listof (listof exact-integer?)) flonum?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec separate_squares(Squares :: [[integer()]]) -> float().\nseparate_squares(Squares) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec separate_squares(squares :: [[integer]]) :: float\n def separate_squares(squares) do\n \n end\nend"}]
[[0,0,1],[2,2,1]]
{ "name": "separateSquares", "params": [ { "name": "squares", "type": "integer[][]" } ], "return": { "type": "double" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Binary Search', 'Segment Tree', 'Line Sweep']
3,455
Shortest Matching Substring
shortest-matching-substring
<p>You are given a string <code>s</code> and a pattern string <code>p</code>, where <code>p</code> contains <strong>exactly two</strong> <code>&#39;*&#39;</code> characters.</p> <p>The <code>&#39;*&#39;</code> in <code>p</code> matches any sequence of zero or more characters.</p> <p>Return the length of the <strong>shortest</strong> <span data-keyword="substring">substring</span> in <code>s</code> that matches <code>p</code>. If there is no such substring, return -1.</p> <strong>Note:</strong> The empty substring is considered valid. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abaacbaecebce&quot;, p = &quot;ba*c*ce&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <p>The shortest matching substring of <code>p</code> in <code>s</code> is <code>&quot;<u><strong>ba</strong></u>e<u><strong>c</strong></u>eb<u><strong>ce</strong></u>&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;baccbaadbc&quot;, p = &quot;cc*baa*adb&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>There is no matching substring in <code>s</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;a&quot;, p = &quot;**&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The empty substring is the shortest matching substring.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;madlogic&quot;, p = &quot;*adlogi*&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The shortest matching substring of <code>p</code> in <code>s</code> is <code>&quot;<strong><u>adlogi</u></strong>&quot;</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>2 &lt;= p.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> contains only lowercase English letters.</li> <li><code>p</code> contains only lowercase English letters and exactly two <code>&#39;*&#39;</code>.</li> </ul>
Hard
5K
20.5K
5,018
20,485
24.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int shortestMatchingSubstring(string s, string p) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int shortestMatchingSubstring(String s, String p) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def shortestMatchingSubstring(self, s, p):\n \"\"\"\n :type s: str\n :type p: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def shortestMatchingSubstring(self, s: str, p: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int shortestMatchingSubstring(char* s, char* p) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int ShortestMatchingSubstring(string s, string p) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {string} p\n * @return {number}\n */\nvar shortestMatchingSubstring = function(s, p) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function shortestMatchingSubstring(s: string, p: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param String $p\n * @return Integer\n */\n function shortestMatchingSubstring($s, $p) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func shortestMatchingSubstring(_ s: String, _ p: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun shortestMatchingSubstring(s: String, p: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int shortestMatchingSubstring(String s, String p) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func shortestMatchingSubstring(s string, p string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} p\n# @return {Integer}\ndef shortest_matching_substring(s, p)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def shortestMatchingSubstring(s: String, p: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn shortest_matching_substring(s: String, p: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (shortest-matching-substring s p)\n (-> string? string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec shortest_matching_substring(S :: unicode:unicode_binary(), P :: unicode:unicode_binary()) -> integer().\nshortest_matching_substring(S, P) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec shortest_matching_substring(s :: String.t, p :: String.t) :: integer\n def shortest_matching_substring(s, p) do\n \n end\nend"}]
"abaacbaecebce" "ba*c*ce"
{ "name": "shortestMatchingSubstring", "params": [ { "name": "s", "type": "string" }, { "type": "string", "name": "p" } ], "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', 'String Matching']
3,456
Find Special Substring of Length K
find-special-substring-of-length-k
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>Determine if there exists a <span data-keyword="substring-nonempty">substring</span> of length <strong>exactly</strong> <code>k</code> in <code>s</code> that satisfies the following conditions:</p> <ol> <li>The substring consists of <strong>only one distinct character</strong> (e.g., <code>&quot;aaa&quot;</code> or <code>&quot;bbb&quot;</code>).</li> <li>If there is a character <strong>immediately before</strong> the substring, it must be different from the character in the substring.</li> <li>If there is a character <strong>immediately after</strong> the substring, it must also be different from the character in the substring.</li> </ol> <p>Return <code>true</code> if such a substring exists. Otherwise, return <code>false</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">s = &quot;aaabaaa&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <p>The substring <code>s[4..6] == &quot;aaa&quot;</code> satisfies the conditions.</p> <ul> <li>It has a length of 3.</li> <li>All characters are the same.</li> <li>The character before <code>&quot;aaa&quot;</code> is <code>&#39;b&#39;</code>, which is different from <code>&#39;a&#39;</code>.</li> <li>There is no character after <code>&quot;aaa&quot;</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>There is no substring of length 2 that consists of one distinct character and satisfies the conditions.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters only.</li> </ul>
Easy
36.1K
102.1K
36,055
102,077
35.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool hasSpecialSubstring(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean hasSpecialSubstring(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def hasSpecialSubstring(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def hasSpecialSubstring(self, s: str, k: int) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool hasSpecialSubstring(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool HasSpecialSubstring(string s, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} k\n * @return {boolean}\n */\nvar hasSpecialSubstring = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function hasSpecialSubstring(s: string, k: number): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @return Boolean\n */\n function hasSpecialSubstring($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func hasSpecialSubstring(_ s: String, _ k: Int) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun hasSpecialSubstring(s: String, k: Int): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool hasSpecialSubstring(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func hasSpecialSubstring(s string, k int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Boolean}\ndef has_special_substring(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def hasSpecialSubstring(s: String, k: Int): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn has_special_substring(s: String, k: i32) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (has-special-substring s k)\n (-> string? exact-integer? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec has_special_substring(S :: unicode:unicode_binary(), K :: integer()) -> boolean().\nhas_special_substring(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec has_special_substring(s :: String.t, k :: integer) :: boolean\n def has_special_substring(s, k) do\n \n end\nend"}]
"aaabaaa" 3
{ "name": "hasSpecialSubstring", "params": [ { "name": "s", "type": "string" }, { "type": "integer", "name": "k" } ], "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']
3,457
Eat Pizzas!
eat-pizzas
<p>You are given an integer array <code>pizzas</code> of size <code>n</code>, where <code>pizzas[i]</code> represents the weight of the <code>i<sup>th</sup></code> pizza. Every day, you eat <strong>exactly</strong> 4 pizzas. Due to your incredible metabolism, when you eat pizzas of weights <code>W</code>, <code>X</code>, <code>Y</code>, and <code>Z</code>, where <code>W &lt;= X &lt;= Y &lt;= Z</code>, you gain the weight of only 1 pizza!</p> <ul> <li>On <strong><span style="box-sizing: border-box; margin: 0px; padding: 0px;">odd-numbered</span></strong> days <strong>(1-indexed)</strong>, you gain a weight of <code>Z</code>.</li> <li>On <strong>even-numbered</strong> days, you gain a weight of <code>Y</code>.</li> </ul> <p>Find the <strong>maximum</strong> total weight you can gain by eating <strong>all</strong> pizzas optimally.</p> <p><strong>Note</strong>: It is guaranteed that <code>n</code> is a multiple of 4, and each pizza can be eaten only once.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pizzas = [1,2,3,4,5,6,7,8]</span></p> <p><strong>Output:</strong> <span class="example-io">14</span></p> <p><strong>Explanation:</strong></p> <ul> <li>On day 1, you eat pizzas at indices <code>[1, 2, 4, 7] = [2, 3, 5, 8]</code>. You gain a weight of 8.</li> <li>On day 2, you eat pizzas at indices <code>[0, 3, 5, 6] = [1, 4, 6, 7]</code>. You gain a weight of 6.</li> </ul> <p>The total weight gained after eating all the pizzas is <code>8 + 6 = 14</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">pizzas = [2,1,1,1,1,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li>On day 1, you eat pizzas at indices <code>[4, 5, 6, 0] = [1, 1, 1, 2]</code>. You gain a weight of 2.</li> <li>On day 2, you eat pizzas at indices <code>[1, 2, 3, 7] = [1, 1, 1, 1]</code>. You gain a weight of 1.</li> </ul> <p>The total weight gained after eating all the pizzas is <code>2 + 1 = 3.</code></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>4 &lt;= n == pizzas.length &lt;= 2 * 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li><code>1 &lt;= pizzas[i] &lt;= 10<sup>5</sup></code></li> <li><code>n</code> is a multiple of 4.</li> </ul>
Medium
20.9K
63.9K
20,878
63,939
32.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxWeight(vector<int>& pizzas) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxWeight(int[] pizzas) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxWeight(self, pizzas):\n \"\"\"\n :type pizzas: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxWeight(self, pizzas: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxWeight(int* pizzas, int pizzasSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxWeight(int[] pizzas) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} pizzas\n * @return {number}\n */\nvar maxWeight = function(pizzas) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxWeight(pizzas: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $pizzas\n * @return Integer\n */\n function maxWeight($pizzas) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxWeight(_ pizzas: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxWeight(pizzas: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxWeight(List<int> pizzas) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxWeight(pizzas []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} pizzas\n# @return {Integer}\ndef max_weight(pizzas)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxWeight(pizzas: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_weight(pizzas: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-weight pizzas)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_weight(Pizzas :: [integer()]) -> integer().\nmax_weight(Pizzas) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_weight(pizzas :: [integer]) :: integer\n def max_weight(pizzas) do\n \n end\nend"}]
[1,2,3,4,5,6,7,8]
{ "name": "maxWeight", "params": [ { "name": "pizzas", "type": "integer[]" } ], "return": { "type": "long" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Greedy', 'Sorting']
3,458
Select K Disjoint Special Substrings
select-k-disjoint-special-substrings
<p>Given a string <code>s</code> of length <code>n</code> and an integer <code>k</code>, determine whether it is possible to select <code>k</code> disjoint <strong>special substrings</strong>.</p> <p>A <strong>special substring</strong> is a <span data-keyword="substring-nonempty">substring</span> where:</p> <ul> <li>Any character present inside the substring should not appear outside it in the string.</li> <li>The substring is not the entire string <code>s</code>.</li> </ul> <p><strong>Note</strong> that all <code>k</code> substrings must be disjoint, meaning they cannot overlap.</p> <p>Return <code>true</code> if it is possible to select <code>k</code> such disjoint special substrings; otherwise, return <code>false</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">s = &quot;abcdbaefab&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <ul> <li>We can select two disjoint special substrings: <code>&quot;cd&quot;</code> and <code>&quot;ef&quot;</code>.</li> <li><code>&quot;cd&quot;</code> contains the characters <code>&#39;c&#39;</code> and <code>&#39;d&#39;</code>, which do not appear elsewhere in <code>s</code>.</li> <li><code>&quot;ef&quot;</code> contains the characters <code>&#39;e&#39;</code> and <code>&#39;f&#39;</code>, which do not appear elsewhere in <code>s</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;cdefdc&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <p>There can be at most 2 disjoint special substrings: <code>&quot;e&quot;</code> and <code>&quot;f&quot;</code>. Since <code>k = 3</code>, the output is <code>false</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abeabe&quot;, k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == s.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>0 &lt;= k &lt;= 26</code></li> <li><code>s</code> consists only of lowercase English letters.</li> </ul>
Medium
8.8K
49.3K
8,792
49,252
17.9%
['find-longest-self-contained-substring']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool maxSubstringLength(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean maxSubstringLength(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSubstringLength(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSubstringLength(self, s: str, k: int) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool maxSubstringLength(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool MaxSubstringLength(string s, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @param {number} k\n * @return {boolean}\n */\nvar maxSubstringLength = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSubstringLength(s: string, k: number): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @param Integer $k\n * @return Boolean\n */\n function maxSubstringLength($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSubstringLength(_ s: String, _ k: Int) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSubstringLength(s: String, k: Int): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool maxSubstringLength(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSubstringLength(s string, k int) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Boolean}\ndef max_substring_length(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSubstringLength(s: String, k: Int): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_substring_length(s: String, k: i32) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-substring-length s k)\n (-> string? exact-integer? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_substring_length(S :: unicode:unicode_binary(), K :: integer()) -> boolean().\nmax_substring_length(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_substring_length(s :: String.t, k :: integer) :: boolean\n def max_substring_length(s, k) do\n \n end\nend"}]
"abcdbaefab" 2
{ "name": "maxSubstringLength", "params": [ { "name": "s", "type": "string" }, { "type": "integer", "name": "k" } ], "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', 'Dynamic Programming', 'Greedy', 'Sorting']
3,459
Length of Longest V-Shaped Diagonal Segment
length-of-longest-v-shaped-diagonal-segment
<p>You are given a 2D integer matrix <code>grid</code> of size <code>n x m</code>, where each element is either <code>0</code>, <code>1</code>, or <code>2</code>.</p> <p>A <strong>V-shaped diagonal segment</strong> is defined as:</p> <ul> <li>The segment starts with <code>1</code>.</li> <li>The subsequent elements follow this infinite sequence: <code>2, 0, 2, 0, ...</code>.</li> <li>The segment: <ul> <li>Starts <strong>along</strong> a diagonal direction (top-left to bottom-right, bottom-right to top-left, top-right to bottom-left, or bottom-left to top-right).</li> <li>Continues the<strong> sequence</strong> in the same diagonal direction.</li> <li>Makes<strong> at most one clockwise 90-degree</strong><strong> turn</strong> to another diagonal direction while <strong>maintaining</strong> the sequence.</li> </ul> </li> </ul> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/11/length_of_longest3.jpg" style="width: 481px; height: 202px;" /></p> <p>Return the <strong>length</strong> of the <strong>longest</strong> <strong>V-shaped diagonal segment</strong>. If no valid segment <em>exists</em>, return 0.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_1-2.jpg" style="width: 201px; height: 192px;" /></p> <p>The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: <code>(0,2) &rarr; (1,3) &rarr; (2,4)</code>, takes a <strong>90-degree clockwise turn</strong> at <code>(2,4)</code>, and continues as <code>(3,3) &rarr; (4,2)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[2,2,2,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_2.jpg" style="width: 201px; height: 201px;" /></strong></p> <p>The longest V-shaped diagonal segment has a length of 4 and follows these coordinates: <code>(2,3) &rarr; (3,2)</code>, takes a <strong>90-degree clockwise turn</strong> at <code>(3,2)</code>, and continues as <code>(2,1) &rarr; (1,0)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,2,2,2,2],[2,2,2,2,0],[2,0,0,0,0],[0,0,2,2,2],[2,0,0,2,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><strong><img alt="" src="https://assets.leetcode.com/uploads/2024/12/09/matrix_3.jpg" style="width: 201px; height: 201px;" /></strong></p> <p>The longest V-shaped diagonal segment has a length of 5 and follows these coordinates: <code>(0,0) &rarr; (1,1) &rarr; (2,2) &rarr; (3,3) &rarr; (4,4)</code>.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The longest V-shaped diagonal segment has a length of 1 and follows these coordinates: <code>(0,0)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length</code></li> <li><code>m == grid[i].length</code></li> <li><code>1 &lt;= n, m &lt;= 500</code></li> <li><code>grid[i][j]</code> is either <code>0</code>, <code>1</code> or <code>2</code>.</li> </ul>
Hard
4.7K
11.8K
4,724
11,842
39.9%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int lenOfVDiagonal(vector<vector<int>>& grid) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int lenOfVDiagonal(int[][] grid) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def lenOfVDiagonal(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def lenOfVDiagonal(self, grid: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int lenOfVDiagonal(int** grid, int gridSize, int* gridColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LenOfVDiagonal(int[][] grid) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar lenOfVDiagonal = function(grid) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function lenOfVDiagonal(grid: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @return Integer\n */\n function lenOfVDiagonal($grid) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func lenOfVDiagonal(_ grid: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun lenOfVDiagonal(grid: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int lenOfVDiagonal(List<List<int>> grid) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func lenOfVDiagonal(grid [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @return {Integer}\ndef len_of_v_diagonal(grid)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def lenOfVDiagonal(grid: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn len_of_v_diagonal(grid: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (len-of-v-diagonal grid)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec len_of_v_diagonal(Grid :: [[integer()]]) -> integer().\nlen_of_v_diagonal(Grid) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec len_of_v_diagonal(grid :: [[integer]]) :: integer\n def len_of_v_diagonal(grid) do\n \n end\nend"}]
[[2,2,1,2,2],[2,0,2,2,0],[2,0,1,1,0],[1,0,2,2,2],[2,0,0,2,2]]
{ "name": "lenOfVDiagonal", "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', 'Dynamic Programming', 'Memoization', 'Matrix']
3,460
Longest Common Prefix After at Most One Removal
longest-common-prefix-after-at-most-one-removal
null
Medium
600
872
600
872
68.8%
['longest-common-prefix']
[]
Algorithms
null
"madxa" "madam"
{ "name": "longestCommonPrefix", "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']
3,461
Check If Digits Are Equal in String After Operations I
check-if-digits-are-equal-in-string-after-operations-i
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p> <ul> <li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li> <li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li> </ul> <p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; otherwise, return <code>false</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">s = &quot;3902&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Initially, <code>s = &quot;3902&quot;</code></li> <li>First operation: <ul> <li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li> <li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li> <li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li> <li><code>s</code> becomes <code>&quot;292&quot;</code></li> </ul> </li> <li>Second operation: <ul> <li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li> <li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li> <li><code>s</code> becomes <code>&quot;11&quot;</code></li> </ul> </li> <li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Initially, <code>s = &quot;34789&quot;</code>.</li> <li>After the first operation, <code>s = &quot;7157&quot;</code>.</li> <li>After the second operation, <code>s = &quot;862&quot;</code>.</li> <li>After the third operation, <code>s = &quot;48&quot;</code>.</li> <li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of only digits.</li> </ul>
Easy
44.1K
55K
44,062
55,017
80.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean hasSameDigits(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def hasSameDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def hasSameDigits(self, s: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool hasSameDigits(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool HasSameDigits(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar hasSameDigits = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function hasSameDigits(s: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Boolean\n */\n function hasSameDigits($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func hasSameDigits(_ s: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun hasSameDigits(s: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool hasSameDigits(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func hasSameDigits(s string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Boolean}\ndef has_same_digits(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def hasSameDigits(s: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn has_same_digits(s: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (has-same-digits s)\n (-> string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec has_same_digits(S :: unicode:unicode_binary()) -> boolean().\nhas_same_digits(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec has_same_digits(s :: String.t) :: boolean\n def has_same_digits(s) do\n \n end\nend"}]
"3902"
{ "name": "hasSameDigits", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "boolean" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Math', 'String', 'Simulation', 'Combinatorics', 'Number Theory']
3,462
Maximum Sum With at Most K Elements
maximum-sum-with-at-most-k-elements
<p data-pm-slice="1 3 []">You are given a 2D integer matrix <code>grid</code> of size <code>n x m</code>, an integer array <code>limits</code> of length <code>n</code>, and an integer <code>k</code>. The task is to find the <strong>maximum sum</strong> of <strong>at most</strong> <code>k</code> elements from the matrix <code>grid</code> such that:</p> <ul data-spread="false"> <li> <p>The number of elements taken from the <code>i<sup>th</sup></code> row of <code>grid</code> does not exceed <code>limits[i]</code>.</p> </li> </ul> <p data-pm-slice="1 1 []">Return the <strong>maximum sum</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">grid = [[1,2],[3,4]], limits = [1,2], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">7</span></p> <p><strong>Explanation:</strong></p> <ul> <li>From the second row, we can take at most 2 elements. The elements taken are 4 and 3.</li> <li>The maximum possible sum of at most 2 selected elements is <code>4 + 3 = 7</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[5,3,7],[8,2,6]], limits = [2,2], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">21</span></p> <p><strong>Explanation:</strong></p> <ul> <li>From the first row, we can take at most 2 elements. The element taken is 7.</li> <li>From the second row, we can take at most 2 elements. The elements taken are 8 and 6.</li> <li>The maximum possible sum of at most 3 selected elements is <code>7 + 8 + 6 = 21</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == grid.length == limits.length</code></li> <li><code>m == grid[i].length</code></li> <li><code>1 &lt;= n, m &lt;= 500</code></li> <li><code>0 &lt;= grid[i][j] &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= limits[i] &lt;= m</code></li> <li><code>0 &lt;= k &lt;= min(n * m, sum(limits))</code></li> </ul>
Medium
33.5K
55.5K
33,472
55,489
60.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxSum(vector<vector<int>>& grid, vector<int>& limits, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxSum(int[][] grid, int[] limits, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSum(self, grid, limits, k):\n \"\"\"\n :type grid: List[List[int]]\n :type limits: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSum(self, grid: List[List[int]], limits: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxSum(int** grid, int gridSize, int* gridColSize, int* limits, int limitsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxSum(int[][] grid, int[] limits, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} grid\n * @param {number[]} limits\n * @param {number} k\n * @return {number}\n */\nvar maxSum = function(grid, limits, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSum(grid: number[][], limits: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $grid\n * @param Integer[] $limits\n * @param Integer $k\n * @return Integer\n */\n function maxSum($grid, $limits, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSum(_ grid: [[Int]], _ limits: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSum(grid: Array<IntArray>, limits: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxSum(List<List<int>> grid, List<int> limits, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSum(grid [][]int, limits []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} grid\n# @param {Integer[]} limits\n# @param {Integer} k\n# @return {Integer}\ndef max_sum(grid, limits, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSum(grid: Array[Array[Int]], limits: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_sum(grid: Vec<Vec<i32>>, limits: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-sum grid limits k)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_sum(Grid :: [[integer()]], Limits :: [integer()], K :: integer()) -> integer().\nmax_sum(Grid, Limits, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_sum(grid :: [[integer]], limits :: [integer], k :: integer) :: integer\n def max_sum(grid, limits, k) do\n \n end\nend"}]
[[1,2],[3,4]] [1,2] 2
{ "name": "maxSum", "params": [ { "name": "grid", "type": "integer[][]" }, { "type": "integer[]", "name": "limits" }, { "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)', 'Matrix']
3,463
Check If Digits Are Equal in String After Operations II
check-if-digits-are-equal-in-string-after-operations-ii
<p>You are given a string <code>s</code> consisting of digits. Perform the following operation repeatedly until the string has <strong>exactly</strong> two digits:</p> <ul> <li>For each pair of consecutive digits in <code>s</code>, starting from the first digit, calculate a new digit as the sum of the two digits <strong>modulo</strong> 10.</li> <li>Replace <code>s</code> with the sequence of newly calculated digits, <em>maintaining the order</em> in which they are computed.</li> </ul> <p>Return <code>true</code> if the final two digits in <code>s</code> are the <strong>same</strong>; otherwise, return <code>false</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">s = &quot;3902&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">true</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Initially, <code>s = &quot;3902&quot;</code></li> <li>First operation: <ul> <li><code>(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2</code></li> <li><code>(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9</code></li> <li><code>(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2</code></li> <li><code>s</code> becomes <code>&quot;292&quot;</code></li> </ul> </li> <li>Second operation: <ul> <li><code>(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1</code></li> <li><code>(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1</code></li> <li><code>s</code> becomes <code>&quot;11&quot;</code></li> </ul> </li> <li>Since the digits in <code>&quot;11&quot;</code> are the same, the output is <code>true</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;34789&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">false</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Initially, <code>s = &quot;34789&quot;</code>.</li> <li>After the first operation, <code>s = &quot;7157&quot;</code>.</li> <li>After the second operation, <code>s = &quot;862&quot;</code>.</li> <li>After the third operation, <code>s = &quot;48&quot;</code>.</li> <li>Since <code>&#39;4&#39; != &#39;8&#39;</code>, the output is <code>false</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s</code> consists of only digits.</li> </ul>
Hard
6.5K
65.3K
6,482
65,287
9.9%
['pascals-triangle']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n bool hasSameDigits(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public boolean hasSameDigits(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def hasSameDigits(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def hasSameDigits(self, s: str) -> bool:\n "}, {"value": "c", "text": "C", "defaultCode": "bool hasSameDigits(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public bool HasSameDigits(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {boolean}\n */\nvar hasSameDigits = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function hasSameDigits(s: string): boolean {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Boolean\n */\n function hasSameDigits($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func hasSameDigits(_ s: String) -> Bool {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun hasSameDigits(s: String): Boolean {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n bool hasSameDigits(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func hasSameDigits(s string) bool {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Boolean}\ndef has_same_digits(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def hasSameDigits(s: String): Boolean = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn has_same_digits(s: String) -> bool {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (has-same-digits s)\n (-> string? boolean?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec has_same_digits(S :: unicode:unicode_binary()) -> boolean().\nhas_same_digits(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec has_same_digits(s :: String.t) :: boolean\n def has_same_digits(s) do\n \n end\nend"}]
"3902"
{ "name": "hasSameDigits", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "boolean" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Math', 'String', 'Combinatorics', 'Number Theory']
3,464
Maximize the Distance Between Points on a Square
maximize-the-distance-between-points-on-a-square
<p>You are given an integer <code><font face="monospace">side</font></code>, representing the edge length of a square with corners at <code>(0, 0)</code>, <code>(0, side)</code>, <code>(side, 0)</code>, and <code>(side, side)</code> on a Cartesian plane.</p> <p>You are also given a <strong>positive</strong> integer <code>k</code> and a 2D integer array <code>points</code>, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the coordinate of a point lying on the <strong>boundary</strong> of the square.</p> <p>You need to select <code>k</code> elements among <code>points</code> such that the <strong>minimum</strong> Manhattan distance between any two points is <strong>maximized</strong>.</p> <p>Return the <strong>maximum</strong> possible <strong>minimum</strong> Manhattan distance between the selected <code>k</code> points.</p> <p>The Manhattan Distance between two cells <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and <code>(x<sub>j</sub>, y<sub>j</sub>)</code> is <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</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">side = 2, points = [[0,2],[2,0],[2,2],[0,0]], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example0_revised.png" style="width: 200px; height: 200px;" /></p> <p>Select all four points.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">side = 2, points = [[0,0],[1,2],[2,0],[2,2],[2,1]], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example1_revised.png" style="width: 211px; height: 200px;" /></p> <p>Select the points <code>(0, 0)</code>, <code>(2, 0)</code>, <code>(2, 2)</code>, and <code>(2, 1)</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">side = 2, points = [[0,0],[0,1],[0,2],[1,2],[2,0],[2,2],[2,1]], k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/01/28/4080_example2_revised.png" style="width: 200px; height: 200px;" /></p> <p>Select the points <code>(0, 0)</code>, <code>(0, 1)</code>, <code>(0, 2)</code>, <code>(1, 2)</code>, and <code>(2, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= side &lt;= 10<sup>9</sup></code></li> <li><code>4 &lt;= points.length &lt;= min(4 * side, 15 * 10<sup>3</sup>)</code></li> <li><code>points[i] == [xi, yi]</code></li> <li>The input is generated such that: <ul> <li><code>points[i]</code> lies on the boundary of the square.</li> <li>All <code>points[i]</code> are <strong>unique</strong>.</li> </ul> </li> <li><code>4 &lt;= k &lt;= min(25, points.length)</code></li> </ul>
Hard
2.5K
13.4K
2,505
13,387
18.7%
['maximum-number-of-integers-to-choose-from-a-range-ii', 'maximum-points-inside-the-square']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxDistance(int side, vector<vector<int>>& points, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxDistance(int side, int[][] points, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxDistance(self, side, points, k):\n \"\"\"\n :type side: int\n :type points: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxDistance(self, side: int, points: List[List[int]], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxDistance(int side, int** points, int pointsSize, int* pointsColSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxDistance(int side, int[][] points, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} side\n * @param {number[][]} points\n * @param {number} k\n * @return {number}\n */\nvar maxDistance = function(side, points, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxDistance(side: number, points: number[][], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $side\n * @param Integer[][] $points\n * @param Integer $k\n * @return Integer\n */\n function maxDistance($side, $points, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxDistance(_ side: Int, _ points: [[Int]], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxDistance(side: Int, points: Array<IntArray>, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxDistance(int side, List<List<int>> points, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxDistance(side int, points [][]int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} side\n# @param {Integer[][]} points\n# @param {Integer} k\n# @return {Integer}\ndef max_distance(side, points, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxDistance(side: Int, points: Array[Array[Int]], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_distance(side: i32, points: Vec<Vec<i32>>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-distance side points k)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_distance(Side :: integer(), Points :: [[integer()]], K :: integer()) -> integer().\nmax_distance(Side, Points, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_distance(side :: integer, points :: [[integer]], k :: integer) :: integer\n def max_distance(side, points, k) do\n \n end\nend"}]
2 [[0,2],[2,0],[2,2],[0,0]] 4
{ "name": "maxDistance", "params": [ { "name": "side", "type": "integer" }, { "type": "integer[][]", "name": "points" }, { "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']
3,465
Find Products with Valid Serial Numbers
find-products-with-valid-serial-numbers
<p>Table: <code>products</code></p> <pre> +--------------+------------+ | Column Name | Type | +--------------+------------+ | product_id | int | | product_name | varchar | | description | varchar | +--------------+------------+ (product_id) is the unique key for this table. Each row in the table represents a product with its unique ID, name, and description. </pre> <p>Write a solution to find all products whose description <strong>contains a valid serial number</strong> pattern. A valid serial number follows these rules:</p> <ul> <li>It starts with the letters <strong>SN</strong>&nbsp;(case-sensitive).</li> <li>Followed by exactly <code>4</code> digits.</li> <li>It must have a hyphen (-) <strong>followed by exactly</strong> <code>4</code> digits.</li> <li>The serial number must be within the description (it may not necessarily start at the beginning).</li> </ul> <p>Return <em>the result table&nbsp;ordered by</em> <code>product_id</code> <em>in <strong>ascending</strong> order</em>.</p> <p>The result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>products table:</p> <pre class="example-io"> +------------+--------------+------------------------------------------------------+ | product_id | product_name | description | +------------+--------------+------------------------------------------------------+ | 1 | Widget A | This is a sample product with SN1234-5678 | | 2 | Widget B | A product with serial SN9876-1234 in the description | | 3 | Widget C | Product SN1234-56789 is available now | | 4 | Widget D | No serial number here | | 5 | Widget E | Check out SN4321-8765 in this description | +------------+--------------+------------------------------------------------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +------------+--------------+------------------------------------------------------+ | product_id | product_name | description | +------------+--------------+------------------------------------------------------+ | 1 | Widget A | This is a sample product with SN1234-5678 | | 2 | Widget B | A product with serial SN9876-1234 in the description | | 5 | Widget E | Check out SN4321-8765 in this description | +------------+--------------+------------------------------------------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>Product 1:</strong> Valid serial number SN1234-5678</li> <li><strong>Product 2:</strong> Valid serial number SN9876-1234</li> <li><strong>Product 3:</strong> Invalid serial number SN1234-56789 (contains 5 digits after the hyphen)</li> <li><strong>Product 4:</strong> No serial number in the description</li> <li><strong>Product 5:</strong> Valid serial number SN4321-8765</li> </ul> <p>The result table is ordered by product_id in ascending order.</p> </div>
Easy
4.1K
4.9K
4,124
4,888
84.4%
[]
['CREATE TABLE If not exists products (\n product_id INT,\n product_name VARCHAR(255),\n description VARCHAR(255)\n)\n', 'Truncate table products', "insert into products (product_id, product_name, description) values ('1', 'Widget A', 'This is a sample product with SN1234-5678')", "insert into products (product_id, product_name, description) values ('2', 'Widget B', 'A product with serial SN9876-1234 in the description')", "insert into products (product_id, product_name, description) values ('3', 'Widget C', 'Product SN1234-56789 is available now')", "insert into products (product_id, product_name, description) values ('4', 'Widget D', 'No serial number here')", "insert into products (product_id, product_name, description) values ('5', 'Widget E', 'Check out SN4321-8765 in this description')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef find_valid_serial_products(products: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"products":["product_id","product_name","description"]},"rows":{"products":[[1,"Widget A","This is a sample product with SN1234-5678"],[2,"Widget B","A product with serial SN9876-1234 in the description"],[3,"Widget C","Product SN1234-56789 is available now"],[4,"Widget D","No serial number here"],[5,"Widget E","Check out SN4321-8765 in this description"]]}}
{"mysql": ["CREATE TABLE If not exists products (\n product_id INT,\n product_name VARCHAR(255),\n description VARCHAR(255)\n)\n"], "mssql": ["CREATE TABLE products (\n product_id INT,\n product_name VARCHAR(255),\n description VARCHAR(255)\n)"], "oraclesql": ["CREATE TABLE products (\n product_id NUMBER,\n product_name VARCHAR2(255),\n description VARCHAR2(255)\n)\n"], "database": true, "name": "find_valid_serial_products", "postgresql": ["CREATE TABLE IF NOT EXISTS products (\n product_id SERIAL PRIMARY KEY,\n product_name VARCHAR(255) NOT NULL,\n description TEXT\n);\n"], "pythondata": ["products = pd.DataFrame(columns=['product_id', 'product_name', 'description']).astype({'product_id': 'int32', 'product_name': 'string', 'description': 'string'})\n"], "database_schema": {"products": {"product_id": "INT", "product_name": "VARCHAR(255)", "description": "VARCHAR(255)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,466
Maximum Coin Collection
maximum-coin-collection
null
Medium
1.4K
2.6K
1,404
2,606
53.9%
[]
[]
Algorithms
null
[1,-2,-10,3] [-5,10,0,1]
{ "name": "maxCoins", "params": [ { "name": "lane1", "type": "integer[]" }, { "type": "integer[]", "name": "lane2" } ], "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']
3,467
Transform Array by Parity
transform-array-by-parity
<p>You are given an integer array <code>nums</code>. Transform <code>nums</code> by performing the following operations in the <strong>exact</strong> order specified:</p> <ol> <li>Replace each even number with 0.</li> <li>Replace each odd numbers with 1.</li> <li>Sort the modified array in <strong>non-decreasing</strong> order.</li> </ol> <p>Return the resulting array after performing these operations.</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 = [4,3,2,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,0,1,1]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Replace the even numbers (4 and 2) with 0 and the odd numbers (3 and 1) with 1. Now, <code>nums = [0, 1, 0, 1]</code>.</li> <li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1]</code>.</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,5,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,0,1,1,1]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Replace the even numbers (4 and 2) with 0 and the odd numbers (1, 5 and 1) with 1. Now, <code>nums = [1, 1, 1, 0, 0]</code>.</li> <li>After sorting <code>nums</code> in non-descending order, <code>nums = [0, 0, 1, 1, 1]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>1 &lt;= nums[i] &lt;= 1000</code></li> </ul>
Easy
43.2K
48.5K
43,184
48,488
89.1%
['odd-even-linked-list']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> transformArray(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] transformArray(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def transformArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def transformArray(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* transformArray(int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] TransformArray(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar transformArray = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function transformArray(nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer[]\n */\n function transformArray($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func transformArray(_ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun transformArray(nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> transformArray(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func transformArray(nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer[]}\ndef transform_array(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def transformArray(nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn transform_array(nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (transform-array nums)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec transform_array(Nums :: [integer()]) -> [integer()].\ntransform_array(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec transform_array(nums :: [integer]) :: [integer]\n def transform_array(nums) do\n \n end\nend"}]
[4,3,2,1]
{ "name": "transformArray", "params": [ { "name": "nums", "type": "integer[]" } ], "return": { "type": "integer[]" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Sorting', 'Counting']
3,468
Find the Number of Copy Arrays
find-the-number-of-copy-arrays
<p>You are given an array <code>original</code> of length <code>n</code> and a 2D array <code>bounds</code> of length <code>n x 2</code>, where <code>bounds[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>.</p> <p>You need to find the number of <strong>possible</strong> arrays <code>copy</code> of length <code>n</code> such that:</p> <ol> <li><code>(copy[i] - copy[i - 1]) == (original[i] - original[i - 1])</code> for <code>1 &lt;= i &lt;= n - 1</code>.</li> <li><code>u<sub>i</sub> &lt;= copy[i] &lt;= v<sub>i</sub></code> for <code>0 &lt;= i &lt;= n - 1</code>.</li> </ol> <p>Return the number of such arrays.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The possible arrays are:</p> <ul> <li><code>[1, 2, 3, 4]</code></li> <li><code>[2, 3, 4, 5]</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The possible arrays are:</p> <ul> <li><code>[1, 2, 3, 4]</code></li> <li><code>[2, 3, 4, 5]</code></li> <li><code>[3, 4, 5, 6]</code></li> <li><code>[4, 5, 6, 7]</code></li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>No array is possible.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == original.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= original[i] &lt;= 10<sup>9</sup></code></li> <li><code>bounds.length == n</code></li> <li><code>bounds[i].length == 2</code></li> <li><code>1 &lt;= bounds[i][0] &lt;= bounds[i][1] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
18.7K
41K
18,671
41,015
45.5%
['count-of-range-sum']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int countArrays(vector<int>& original, vector<vector<int>>& bounds) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int countArrays(int[] original, int[][] bounds) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def countArrays(self, original, bounds):\n \"\"\"\n :type original: List[int]\n :type bounds: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def countArrays(self, original: List[int], bounds: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int countArrays(int* original, int originalSize, int** bounds, int boundsSize, int* boundsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int CountArrays(int[] original, int[][] bounds) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} original\n * @param {number[][]} bounds\n * @return {number}\n */\nvar countArrays = function(original, bounds) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function countArrays(original: number[], bounds: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $original\n * @param Integer[][] $bounds\n * @return Integer\n */\n function countArrays($original, $bounds) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func countArrays(_ original: [Int], _ bounds: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun countArrays(original: IntArray, bounds: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int countArrays(List<int> original, List<List<int>> bounds) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func countArrays(original []int, bounds [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} original\n# @param {Integer[][]} bounds\n# @return {Integer}\ndef count_arrays(original, bounds)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def countArrays(original: Array[Int], bounds: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn count_arrays(original: Vec<i32>, bounds: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (count-arrays original bounds)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec count_arrays(Original :: [integer()], Bounds :: [[integer()]]) -> integer().\ncount_arrays(Original, Bounds) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec count_arrays(original :: [integer], bounds :: [[integer]]) :: integer\n def count_arrays(original, bounds) do\n \n end\nend"}]
[1,2,3,4] [[1,2],[2,3],[3,4],[4,5]]
{ "name": "countArrays", "params": [ { "name": "original", "type": "integer[]" }, { "type": "integer[][]", "name": "bounds" } ], "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']
3,469
Find Minimum Cost to Remove Array Elements
find-minimum-cost-to-remove-array-elements
<p>You are given an integer array <code>nums</code>. Your task is to remove <strong>all elements</strong> from the array by performing one of the following operations at each step until <code>nums</code> is empty:</p> <ul> <li>Choose any two elements from the first three elements of <code>nums</code> and remove them. The cost of this operation is the <strong>maximum</strong> of the two elements removed.</li> <li>If fewer than three elements remain in <code>nums</code>, remove all the remaining elements in a single operation. The cost of this operation is the <strong>maximum</strong> of the remaining elements.</li> </ul> <p>Return the <strong>minimum</strong> cost required to remove all the elements.</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 = [6,2,8,4]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <p>Initially, <code>nums = [6, 2, 8, 4]</code>.</p> <ul> <li>In the first operation, remove <code>nums[0] = 6</code> and <code>nums[2] = 8</code> with a cost of <code>max(6, 8) = 8</code>. Now, <code>nums = [2, 4]</code>.</li> <li>In the second operation, remove the remaining elements with a cost of <code>max(2, 4) = 4</code>.</li> </ul> <p>The cost to remove all elements is <code>8 + 4 = 12</code>. This is the minimum cost to remove all elements in <code>nums</code>. Hence, the output is 12.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>Initially, <code>nums = [2, 1, 3, 3]</code>.</p> <ul> <li>In the first operation, remove <code>nums[0] = 2</code> and <code>nums[1] = 1</code> with a cost of <code>max(2, 1) = 2</code>. Now, <code>nums = [3, 3]</code>.</li> <li>In the second operation remove the remaining elements with a cost of <code>max(3, 3) = 3</code>.</li> </ul> <p>The cost to remove all elements is <code>2 + 3 = 5</code>. This is the minimum cost to remove all elements in <code>nums</code>. Hence, the output is 5.</p> </div> <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>6</sup></code></li> </ul>
Medium
8.3K
47.4K
8,320
47,392
17.6%
['minimum-difference-in-sums-after-removal-of-elements']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minCost(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minCost(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCost(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCost(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minCost(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinCost(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minCost = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCost(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minCost($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCost(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCost(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minCost(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCost(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef min_cost(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCost(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_cost(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-cost nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_cost(Nums :: [integer()]) -> integer().\nmin_cost(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_cost(nums :: [integer]) :: integer\n def min_cost(nums) do\n \n end\nend"}]
[6,2,8,4]
{ "name": "minCost", "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', 'Dynamic Programming']
3,470
Permutations IV
permutations-iv
<p>Given two integers, <code>n</code> and <code>k</code>, an <strong>alternating permutation</strong> is a permutation of the first <code>n</code> positive integers such that no <strong>two</strong> adjacent elements are both odd or both even.</p> <p>Return the <strong>k-th</strong> <strong>alternating permutation</strong> sorted in <em>lexicographical order</em>. If there are fewer than <code>k</code> valid <strong>alternating permutations</strong>, return an empty list.</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 = 4, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">[3,4,1,2]</span></p> <p><strong>Explanation:</strong></p> <p>The lexicographically-sorted alternating permutations of <code>[1, 2, 3, 4]</code> are:</p> <ol> <li><code>[1, 2, 3, 4]</code></li> <li><code>[1, 4, 3, 2]</code></li> <li><code>[2, 1, 4, 3]</code></li> <li><code>[2, 3, 4, 1]</code></li> <li><code>[3, 2, 1, 4]</code></li> <li><code>[3, 4, 1, 2]</code> &larr; 6th permutation</li> <li><code>[4, 1, 2, 3]</code></li> <li><code>[4, 3, 2, 1]</code></li> </ol> <p>Since <code>k = 6</code>, we return <code>[3, 4, 1, 2]</code>.</p> </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</span></p> <p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p> <p><strong>Explanation:</strong></p> <p>The lexicographically-sorted alternating permutations of <code>[1, 2, 3]</code> are:</p> <ol> <li><code>[1, 2, 3]</code></li> <li><code>[3, 2, 1]</code> &larr; 2nd permutation</li> </ol> <p>Since <code>k = 2</code>, we return <code>[3, 2, 1]</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[]</span></p> <p><strong>Explanation:</strong></p> <p>The lexicographically-sorted alternating permutations of <code>[1, 2]</code> are:</p> <ol> <li><code>[1, 2]</code></li> <li><code>[2, 1]</code></li> </ol> <p>There are only 2 alternating permutations, but <code>k = 3</code>, which is out of range. Thus, we return an empty list <code>[]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= 10<sup>15</sup></code></li> </ul>
Hard
2.3K
8.7K
2,314
8,735
26.5%
['permutations-iii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> permute(int n, long long k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] permute(int n, long k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def permute(self, n, k):\n \"\"\"\n :type n: int\n :type k: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def permute(self, n: 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* permute(int n, long long k, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] Permute(int n, long k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number} k\n * @return {number[]}\n */\nvar permute = function(n, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function permute(n: number, k: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $k\n * @return Integer[]\n */\n function permute($n, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func permute(_ n: Int, _ k: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun permute(n: Int, k: Long): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> permute(int n, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func permute(n int, k int64) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer} k\n# @return {Integer[]}\ndef permute(n, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def permute(n: Int, k: Long): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn permute(n: i32, k: i64) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (permute n k)\n (-> exact-integer? exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec permute(N :: integer(), K :: integer()) -> [integer()].\npermute(N, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec permute(n :: integer, k :: integer) :: [integer]\n def permute(n, k) do\n \n end\nend"}]
4 6
{ "name": "permute", "params": [ { "name": "n", "type": "integer" }, { "type": "long", "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', 'Math', 'Combinatorics', 'Enumeration']
3,471
Find the Largest Almost Missing Integer
find-the-largest-almost-missing-integer
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> <p>An integer <code>x</code> is <strong>almost missing</strong> from <code>nums</code> if <code>x</code> appears in <em>exactly</em> one subarray of size <code>k</code> within <code>nums</code>.</p> <p>Return the <b>largest</b> <strong>almost missing</strong> integer from <code>nums</code>. If no such integer exists, return <code>-1</code>.</p> A <strong>subarray</strong> is a contiguous sequence of elements within an array. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,9,2,1,7], k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">7</span></p> <p><strong>Explanation:</strong></p> <ul> <li>1 appears in 2 subarrays of size 3: <code>[9, 2, 1]</code> and <code>[2, 1, 7]</code>.</li> <li>2 appears in 3 subarrays of size 3: <code>[3, 9, 2]</code>, <code>[9, 2, 1]</code>, <code>[2, 1, 7]</code>.</li> <li index="2">3 appears in 1 subarray of size 3: <code>[3, 9, 2]</code>.</li> <li index="3">7 appears in 1 subarray of size 3: <code>[2, 1, 7]</code>.</li> <li index="4">9 appears in 2 subarrays of size 3: <code>[3, 9, 2]</code>, and <code>[9, 2, 1]</code>.</li> </ul> <p>We return 7 since it is the largest integer that appears in exactly one subarray of size <code>k</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,9,7,2,1,7], k = 4</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li>1 appears in 2 subarrays of size 4: <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li> <li>2 appears in 3 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li> <li>3 appears in 1 subarray of size 4: <code>[3, 9, 7, 2]</code>.</li> <li>7 appears in 3 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>, <code>[7, 2, 1, 7]</code>.</li> <li>9 appears in 2 subarrays of size 4: <code>[3, 9, 7, 2]</code>, <code>[9, 7, 2, 1]</code>.</li> </ul> <p>We return 3 since it is the largest and only integer that appears in exactly one subarray of size <code>k</code>.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,0], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>There is no integer that appears in only one subarray of size 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>0 &lt;= nums[i] &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= nums.length</code></li> </ul>
Easy
30K
82.2K
30,008
82,249
36.5%
['missing-number']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int largestInteger(vector<int>& nums, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int largestInteger(int[] nums, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def largestInteger(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 largestInteger(self, nums: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int largestInteger(int* nums, int numsSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LargestInteger(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 largestInteger = function(nums, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function largestInteger(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 largestInteger($nums, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func largestInteger(_ nums: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun largestInteger(nums: IntArray, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int largestInteger(List<int> nums, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func largestInteger(nums []int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @return {Integer}\ndef largest_integer(nums, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def largestInteger(nums: Array[Int], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn largest_integer(nums: Vec<i32>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (largest-integer nums k)\n (-> (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec largest_integer(Nums :: [integer()], K :: integer()) -> integer().\nlargest_integer(Nums, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec largest_integer(nums :: [integer], k :: integer) :: integer\n def largest_integer(nums, k) do\n \n end\nend"}]
[3,9,2,1,7] 3
{ "name": "largestInteger", "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']
3,472
Longest Palindromic Subsequence After at Most K Operations
longest-palindromic-subsequence-after-at-most-k-operations
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>In one operation, you can replace the character at any position with the next or previous letter in the alphabet (wrapping around so that <code>&#39;a&#39;</code> is after <code>&#39;z&#39;</code>). For example, replacing <code>&#39;a&#39;</code> with the next letter results in <code>&#39;b&#39;</code>, and replacing <code>&#39;a&#39;</code> with the previous letter results in <code>&#39;z&#39;</code>. Similarly, replacing <code>&#39;z&#39;</code> with the next letter results in <code>&#39;a&#39;</code>, and replacing <code>&#39;z&#39;</code> with the previous letter results in <code>&#39;y&#39;</code>.</p> <p>Return the length of the <strong>longest <span data-keyword="palindrome-string">palindromic</span> <span data-keyword="subsequence-string-nonempty">subsequence</span></strong> of <code>s</code> that can be obtained after performing <strong>at most</strong> <code>k</code> operations.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abced&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Replace <code>s[1]</code> with the next letter, and <code>s</code> becomes <code>&quot;acced&quot;</code>.</li> <li>Replace <code>s[4]</code> with the previous letter, and <code>s</code> becomes <code>&quot;accec&quot;</code>.</li> </ul> <p>The subsequence <code>&quot;ccc&quot;</code> forms a palindrome of length 3, which is the maximum.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;</span>aaazzz<span class="example-io">&quot;, k = 4</span></p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong></p> <ul> <li>Replace <code>s[0]</code> with the previous letter, and <code>s</code> becomes <code>&quot;zaazzz&quot;</code>.</li> <li>Replace <code>s[4]</code> with the next letter, and <code>s</code> becomes <code>&quot;zaazaz&quot;</code>.</li> <li>Replace <code>s[3]</code> with the next letter, and <code>s</code> becomes <code>&quot;zaaaaz&quot;</code>.</li> </ul> <p>The entire string forms a palindrome of length 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 200</code></li> <li><code>1 &lt;= k &lt;= 200</code></li> <li><code>s</code> consists of only lowercase English letters.</li> </ul>
Medium
12.5K
35.1K
12,543
35,063
35.8%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int longestPalindromicSubsequence(string s, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int longestPalindromicSubsequence(String s, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestPalindromicSubsequence(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 longestPalindromicSubsequence(self, s: str, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int longestPalindromicSubsequence(char* s, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LongestPalindromicSubsequence(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 longestPalindromicSubsequence = function(s, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestPalindromicSubsequence(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 longestPalindromicSubsequence($s, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestPalindromicSubsequence(_ s: String, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestPalindromicSubsequence(s: String, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int longestPalindromicSubsequence(String s, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestPalindromicSubsequence(s string, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer} k\n# @return {Integer}\ndef longest_palindromic_subsequence(s, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestPalindromicSubsequence(s: String, k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_palindromic_subsequence(s: String, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-palindromic-subsequence s k)\n (-> string? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_palindromic_subsequence(S :: unicode:unicode_binary(), K :: integer()) -> integer().\nlongest_palindromic_subsequence(S, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_palindromic_subsequence(s :: String.t, k :: integer) :: integer\n def longest_palindromic_subsequence(s, k) do\n \n end\nend"}]
"abced" 2
{ "name": "longestPalindromicSubsequence", "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']
3,473
Sum of K Subarrays With Length at Least M
sum-of-k-subarrays-with-length-at-least-m
<p>You are given an integer array <code>nums</code> and two integers, <code>k</code> and <code>m</code>.</p> <p>Return the <strong>maximum</strong> sum of <code>k</code> non-overlapping <span data-keyword="subarray">subarrays</span> of <code>nums</code>, where each subarray has a length of <strong>at least</strong> <code>m</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,2,-1,3,3,4], k = 2, m = 2</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The optimal choice is:</p> <ul> <li>Subarray <code>nums[3..5]</code> with sum <code>3 + 3 + 4 = 10</code> (length is <code>3 &gt;= m</code>).</li> <li>Subarray <code>nums[0..1]</code> with sum <code>1 + 2 = 3</code> (length is <code>2 &gt;= m</code>).</li> </ul> <p>The total sum is <code>10 + 3 = 13</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-10,3,-1,-2], k = 4, m = 1</span></p> <p><strong>Output:</strong> <span class="example-io">-10</span></p> <p><strong>Explanation:</strong></p> <p>The optimal choice is choosing each element as a subarray. The output is <code>(-10) + 3 + (-1) + (-2) = -10</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 2000</code></li> <li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= k &lt;= floor(nums.length / m)</code></li> <li><code>1 &lt;= m &lt;= 3</code></li> </ul>
Medium
6.1K
25.1K
6,099
25,063
24.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxSum(vector<int>& nums, int k, int m) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxSum(int[] nums, int k, int m) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSum(self, nums, k, m):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type m: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSum(self, nums: List[int], k: int, m: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxSum(int* nums, int numsSize, int k, int m) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxSum(int[] nums, int k, int m) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} m\n * @return {number}\n */\nvar maxSum = function(nums, k, m) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSum(nums: number[], k: number, m: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @param Integer $m\n * @return Integer\n */\n function maxSum($nums, $k, $m) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSum(_ nums: [Int], _ k: Int, _ m: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSum(nums: IntArray, k: Int, m: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxSum(List<int> nums, int k, int m) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSum(nums []int, k int, m int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @param {Integer} m\n# @return {Integer}\ndef max_sum(nums, k, m)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSum(nums: Array[Int], k: Int, m: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_sum(nums: Vec<i32>, k: i32, m: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-sum nums k m)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_sum(Nums :: [integer()], K :: integer(), M :: integer()) -> integer().\nmax_sum(Nums, K, M) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_sum(nums :: [integer], k :: integer, m :: integer) :: integer\n def max_sum(nums, k, m) do\n \n end\nend"}]
[1,2,-1,3,3,4] 2 2
{ "name": "maxSum", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "k" }, { "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', 'Dynamic Programming', 'Prefix Sum']
3,474
Lexicographically Smallest Generated String
lexicographically-smallest-generated-string
<p>You are given two strings, <code>str1</code> and <code>str2</code>, of lengths <code>n</code> and <code>m</code>, respectively.</p> <p>A string <code>word</code> of length <code>n + m - 1</code> is defined to be <strong>generated</strong> by <code>str1</code> and <code>str2</code> if it satisfies the following conditions for <strong>each</strong> index <code>0 &lt;= i &lt;= n - 1</code>:</p> <ul> <li>If <code>str1[i] == &#39;T&#39;</code>, the <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>word</code> with size <code>m</code> starting at index <code>i</code> is <strong>equal</strong> to <code>str2</code>, i.e., <code>word[i..(i + m - 1)] == str2</code>.</li> <li>If <code>str1[i] == &#39;F&#39;</code>, the <strong><span data-keyword="substring-nonempty">substring</span></strong> of <code>word</code> with size <code>m</code> starting at index <code>i</code> is <strong>not equal</strong> to <code>str2</code>, i.e., <code>word[i..(i + m - 1)] != str2</code>.</li> </ul> <p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> possible string that can be <strong>generated</strong> by <code>str1</code> and <code>str2</code>. If no string can be generated, return an empty string <code>&quot;&quot;</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">str1 = &quot;TFTF&quot;, str2 = &quot;ab&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;ababa&quot;</span></p> <p><strong>Explanation:</strong></p> <h4>The table below represents the string <code>&quot;ababa&quot;</code></h4> <table> <tbody> <tr> <th style="border: 1px solid black;">Index</th> <th style="border: 1px solid black;">T/F</th> <th style="border: 1px solid black;">Substring of length <code>m</code></th> </tr> <tr> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;"><code>&#39;T&#39;</code></td> <td style="border: 1px solid black;">&quot;ab&quot;</td> </tr> <tr> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;"><code>&#39;F&#39;</code></td> <td style="border: 1px solid black;">&quot;ba&quot;</td> </tr> <tr> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;"><code>&#39;T&#39;</code></td> <td style="border: 1px solid black;">&quot;ab&quot;</td> </tr> <tr> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;"><code>&#39;F&#39;</code></td> <td style="border: 1px solid black;">&quot;ba&quot;</td> </tr> </tbody> </table> <p>The strings <code>&quot;ababa&quot;</code> and <code>&quot;ababb&quot;</code> can be generated by <code>str1</code> and <code>str2</code>.</p> <p>Return <code>&quot;ababa&quot;</code> since it is the lexicographically smaller string.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">str1 = &quot;TFTF&quot;, str2 = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p> <p><strong>Explanation:</strong></p> <p>No string that satisfies the conditions can be generated.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">str1 = &quot;F&quot;, str2 = &quot;d&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;a&quot;</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == str1.length &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= m == str2.length &lt;= 500</code></li> <li><code>str1</code> consists only of <code>&#39;T&#39;</code> or <code>&#39;F&#39;</code>.</li> <li><code>str2</code> consists only of lowercase English characters.</li> </ul>
Hard
2.9K
10.2K
2,938
10,212
28.8%
['lexicographically-smallest-equivalent-string']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n string generateString(string str1, string str2) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public String generateString(String str1, String str2) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def generateString(self, str1, str2):\n \"\"\"\n :type str1: str\n :type str2: str\n :rtype: str\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def generateString(self, str1: str, str2: str) -> str:\n "}, {"value": "c", "text": "C", "defaultCode": "char* generateString(char* str1, char* str2) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public string GenerateString(string str1, string str2) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} str1\n * @param {string} str2\n * @return {string}\n */\nvar generateString = function(str1, str2) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function generateString(str1: string, str2: string): string {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $str1\n * @param String $str2\n * @return String\n */\n function generateString($str1, $str2) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func generateString(_ str1: String, _ str2: String) -> String {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun generateString(str1: String, str2: String): String {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n String generateString(String str1, String str2) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func generateString(str1 string, str2 string) string {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} str1\n# @param {String} str2\n# @return {String}\ndef generate_string(str1, str2)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def generateString(str1: String, str2: String): String = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn generate_string(str1: String, str2: String) -> String {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (generate-string str1 str2)\n (-> string? string? string?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec generate_string(Str1 :: unicode:unicode_binary(), Str2 :: unicode:unicode_binary()) -> unicode:unicode_binary().\ngenerate_string(Str1, Str2) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec generate_string(str1 :: String.t, str2 :: String.t) :: String.t\n def generate_string(str1, str2) do\n \n end\nend"}]
"TFTF" "ab"
{ "name": "generateString", "params": [ { "name": "str1", "type": "string" }, { "type": "string", "name": "str2" } ], "return": { "type": "string" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['String', 'Greedy', 'String Matching']
3,475
DNA Pattern Recognition
dna-pattern-recognition
<p>Table: <code>Samples</code></p> <pre> +----------------+---------+ | Column Name | Type | +----------------+---------+ | sample_id | int | | dna_sequence | varchar | | species | varchar | +----------------+---------+ sample_id is the unique key for this table. Each row contains a DNA sequence represented as a string of characters (A, T, G, C) and the species it was collected from. </pre> <p>Biologists are studying basic patterns in DNA sequences. Write a solution to identify <code>sample_id</code> with the following patterns:</p> <ul> <li>Sequences that <strong>start</strong> with <strong>ATG</strong>&nbsp;(a common <strong>start codon</strong>)</li> <li>Sequences that <strong>end</strong> with either <strong>TAA</strong>, <strong>TAG</strong>, or <strong>TGA</strong>&nbsp;(<strong>stop codons</strong>)</li> <li>Sequences containing the motif <strong>ATAT</strong>&nbsp;(a simple repeated pattern)</li> <li>Sequences that have <strong>at least</strong> <code>3</code> <strong>consecutive</strong> <strong>G</strong>&nbsp;(like <strong>GGG</strong>&nbsp;or <strong>GGGG</strong>)</li> </ul> <p>Return <em>the result table ordered by&nbsp;</em><em>sample_id in <strong>ascending</strong> order</em>.</p> <p>The result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>Samples table:</p> <pre class="example-io"> +-----------+------------------+-----------+ | sample_id | dna_sequence | species | +-----------+------------------+-----------+ | 1 | ATGCTAGCTAGCTAA | Human | | 2 | GGGTCAATCATC | Human | | 3 | ATATATCGTAGCTA | Human | | 4 | ATGGGGTCATCATAA | Mouse | | 5 | TCAGTCAGTCAG | Mouse | | 6 | ATATCGCGCTAG | Zebrafish | | 7 | CGTATGCGTCGTA | Zebrafish | +-----------+------------------+-----------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +-----------+------------------+-------------+-------------+------------+------------+------------+ | sample_id | dna_sequence | species | has_start | has_stop | has_atat | has_ggg | +-----------+------------------+-------------+-------------+------------+------------+------------+ | 1 | ATGCTAGCTAGCTAA | Human | 1 | 1 | 0 | 0 | | 2 | GGGTCAATCATC | Human | 0 | 0 | 0 | 1 | | 3 | ATATATCGTAGCTA | Human | 0 | 0 | 1 | 0 | | 4 | ATGGGGTCATCATAA | Mouse | 1 | 1 | 0 | 1 | | 5 | TCAGTCAGTCAG | Mouse | 0 | 0 | 0 | 0 | | 6 | ATATCGCGCTAG | Zebrafish | 0 | 1 | 1 | 0 | | 7 | CGTATGCGTCGTA | Zebrafish | 0 | 0 | 0 | 0 | +-----------+------------------+-------------+-------------+------------+------------+------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Sample 1 (ATGCTAGCTAGCTAA): <ul> <li>Starts with ATG&nbsp;(has_start = 1)</li> <li>Ends with TAA&nbsp;(has_stop = 1)</li> <li>Does not contain ATAT&nbsp;(has_atat = 0)</li> <li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li> </ul> </li> <li>Sample 2 (GGGTCAATCATC): <ul> <li>Does not start with ATG&nbsp;(has_start = 0)</li> <li>Does not end with TAA, TAG, or TGA&nbsp;(has_stop = 0)</li> <li>Does not contain ATAT&nbsp;(has_atat = 0)</li> <li>Contains GGG&nbsp;(has_ggg = 1)</li> </ul> </li> <li>Sample 3 (ATATATCGTAGCTA): <ul> <li>Does not start with ATG&nbsp;(has_start = 0)</li> <li>Does not end with TAA, TAG, or TGA&nbsp;(has_stop = 0)</li> <li>Contains ATAT&nbsp;(has_atat = 1)</li> <li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li> </ul> </li> <li>Sample 4 (ATGGGGTCATCATAA): <ul> <li>Starts with ATG&nbsp;(has_start = 1)</li> <li>Ends with TAA&nbsp;(has_stop = 1)</li> <li>Does not contain ATAT&nbsp;(has_atat = 0)</li> <li>Contains GGGG&nbsp;(has_ggg = 1)</li> </ul> </li> <li>Sample 5 (TCAGTCAGTCAG): <ul> <li>Does not match any patterns (all fields = 0)</li> </ul> </li> <li>Sample 6 (ATATCGCGCTAG): <ul> <li>Does not start with ATG&nbsp;(has_start = 0)</li> <li>Ends with TAG&nbsp;(has_stop = 1)</li> <li>Starts with ATAT&nbsp;(has_atat = 1)</li> <li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li> </ul> </li> <li>Sample 7 (CGTATGCGTCGTA): <ul> <li>Does not start with ATG&nbsp;(has_start = 0)</li> <li>Does not end with TAA, &quot;TAG&quot;, or &quot;TGA&quot; (has_stop = 0)</li> <li>Does not contain ATAT&nbsp;(has_atat = 0)</li> <li>Does not contain at least 3 consecutive &#39;G&#39;s (has_ggg = 0)</li> </ul> </li> </ul> <p><strong>Note:</strong></p> <ul> <li>The result is ordered by sample_id in ascending order</li> <li>For each pattern, 1 indicates the pattern is present and 0 indicates it is not present</li> </ul> </div>
Medium
3.1K
3.5K
3,068
3,505
87.5%
[]
['CREATE TABLE if not exists Samples (\n sample_id INT,\n dna_sequence VARCHAR(255),\n species VARCHAR(100)\n)', 'Truncate table Samples', "insert into Samples (sample_id, dna_sequence, species) values ('1', 'ATGCTAGCTAGCTAA', 'Human')", "insert into Samples (sample_id, dna_sequence, species) values ('2', 'GGGTCAATCATC', 'Human')", "insert into Samples (sample_id, dna_sequence, species) values ('3', 'ATATATCGTAGCTA', 'Human')", "insert into Samples (sample_id, dna_sequence, species) values ('4', 'ATGGGGTCATCATAA', 'Mouse')", "insert into Samples (sample_id, dna_sequence, species) values ('5', 'TCAGTCAGTCAG', 'Mouse')", "insert into Samples (sample_id, dna_sequence, species) values ('6', 'ATATCGCGCTAG', 'Zebrafish')", "insert into Samples (sample_id, dna_sequence, species) values ('7', 'CGTATGCGTCGTA', 'Zebrafish')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef analyze_dna_patterns(samples: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"Samples":["sample_id","dna_sequence","species"]},"rows":{"Samples":[[1,"ATGCTAGCTAGCTAA","Human"],[2,"GGGTCAATCATC","Human"],[3,"ATATATCGTAGCTA","Human"],[4,"ATGGGGTCATCATAA","Mouse"],[5,"TCAGTCAGTCAG","Mouse"],[6,"ATATCGCGCTAG","Zebrafish"],[7,"CGTATGCGTCGTA","Zebrafish"]]}}
{"mysql": ["CREATE TABLE if not exists Samples (\n sample_id INT,\n dna_sequence VARCHAR(255),\n species VARCHAR(100)\n)"], "mssql": ["CREATE TABLE Samples (\n sample_id INT,\n dna_sequence VARCHAR(255),\n species VARCHAR(100)\n)"], "oraclesql": ["CREATE TABLE Samples (\n sample_id NUMBER,\n dna_sequence VARCHAR2(255),\n species VARCHAR2(100)\n)"], "database": true, "name": "analyze_dna_patterns", "postgresql": ["CREATE TABLE IF NOT EXISTS Samples (\n sample_id INT,\n dna_sequence TEXT NOT NULL,\n species VARCHAR(100) NOT NULL\n);\n"], "pythondata": ["Samples = pd.DataFrame({\n 'sample_id': pd.Series(dtype='int'), # Equivalent to SERIAL/INTEGER\n 'dna_sequence': pd.Series(dtype='string'), # Equivalent to TEXT/VARCHAR\n 'species': pd.Series(dtype='string') # Equivalent to VARCHAR(100)\n})"], "database_schema": {"Samples": {"sample_id": "INT", "dna_sequence": "VARCHAR(255)", "species": "VARCHAR(100)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,476
Maximize Profit from Task Assignment
maximize-profit-from-task-assignment
null
Medium
424
671
424
671
63.2%
[]
[]
Algorithms
null
[1,2,3,4,5] [[1,100],[2,400],[3,100],[3,400]]
{ "name": "maxProfit", "params": [ { "name": "workers", "type": "integer[]" }, { "type": "integer[][]", "name": "tasks" } ], "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)']
3,477
Fruits Into Baskets II
fruits-into-baskets-ii
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p> <p>From left to right, place the fruits according to these rules:</p> <ul> <li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li> <li>Each basket can hold <b>only one</b> type of fruit.</li> <li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li> </ul> <p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li> <li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li> <li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li> </ul> <p>Since one fruit type remains unplaced, we return 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li> <li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li> <li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li> </ul> <p>Since all fruits are successfully placed, we return 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == fruits.length == baskets.length</code></li> <li><code>1 &lt;= n &lt;= 100</code></li> <li><code>1 &lt;= fruits[i], baskets[i] &lt;= 1000</code></li> </ul>
Easy
33.8K
65.9K
33,831
65,918
51.3%
['fruit-into-baskets']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numOfUnplacedFruits(self, fruits, baskets):\n \"\"\"\n :type fruits: List[int]\n :type baskets: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numOfUnplacedFruits(int* fruits, int fruitsSize, int* baskets, int basketsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} fruits\n * @param {number[]} baskets\n * @return {number}\n */\nvar numOfUnplacedFruits = function(fruits, baskets) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $fruits\n * @param Integer[] $baskets\n * @return Integer\n */\n function numOfUnplacedFruits($fruits, $baskets) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numOfUnplacedFruits(_ fruits: [Int], _ baskets: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numOfUnplacedFruits(List<int> fruits, List<int> baskets) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numOfUnplacedFruits(fruits []int, baskets []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} fruits\n# @param {Integer[]} baskets\n# @return {Integer}\ndef num_of_unplaced_fruits(fruits, baskets)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numOfUnplacedFruits(fruits: Array[Int], baskets: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec<i32>, baskets: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (num-of-unplaced-fruits fruits baskets)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec num_of_unplaced_fruits(Fruits :: [integer()], Baskets :: [integer()]) -> integer().\nnum_of_unplaced_fruits(Fruits, Baskets) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec num_of_unplaced_fruits(fruits :: [integer], baskets :: [integer]) :: integer\n def num_of_unplaced_fruits(fruits, baskets) do\n \n end\nend"}]
[4,2,5] [3,5,4]
{ "name": "numOfUnplacedFruits", "params": [ { "name": "fruits", "type": "integer[]" }, { "type": "integer[]", "name": "baskets" } ], "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', 'Segment Tree', 'Simulation']
3,478
Choose K Elements With Maximum Sum
choose-k-elements-with-maximum-sum
<p>You are given two integer arrays, <code>nums1</code> and <code>nums2</code>, both of length <code>n</code>, along with a positive integer <code>k</code>.</p> <p>For each index <code>i</code> from <code>0</code> to <code>n - 1</code>, perform the following:</p> <ul> <li>Find <strong>all</strong> indices <code>j</code> where <code>nums1[j]</code> is less than <code>nums1[i]</code>.</li> <li>Choose <strong>at most</strong> <code>k</code> values of <code>nums2[j]</code> at these indices to <strong>maximize</strong> the total sum.</li> </ul> <p>Return an array <code>answer</code> of size <code>n</code>, where <code>answer[i]</code> represents the result for the corresponding index <code>i</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">nums1 = [4,2,1,5,3], nums2 = [10,20,30,40,50], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[80,30,0,80,50]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[1, 2, 4]</code> where <code>nums1[j] &lt; nums1[0]</code>, resulting in <code>50 + 30 = 80</code>.</li> <li>For <code>i = 1</code>: Select the 2 largest values from <code>nums2</code> at index <code>[2]</code> where <code>nums1[j] &lt; nums1[1]</code>, resulting in 30.</li> <li>For <code>i = 2</code>: No indices satisfy <code>nums1[j] &lt; nums1[2]</code>, resulting in 0.</li> <li>For <code>i = 3</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[0, 1, 2, 4]</code> where <code>nums1[j] &lt; nums1[3]</code>, resulting in <code>50 + 30 = 80</code>.</li> <li>For <code>i = 4</code>: Select the 2 largest values from <code>nums2</code> at indices <code>[1, 2]</code> where <code>nums1[j] &lt; nums1[4]</code>, resulting in <code>30 + 20 = 50</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums1 = [2,2,2,2], nums2 = [3,1,2,3], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">[0,0,0,0]</span></p> <p><strong>Explanation:</strong></p> <p>Since all elements in <code>nums1</code> are equal, no indices satisfy the condition <code>nums1[j] &lt; nums1[i]</code> for any <code>i</code>, resulting in 0 for all positions.</p> </div> <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>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>6</sup></code></li> <li><code>1 &lt;= k &lt;= n</code></li> </ul>
Medium
15.5K
50.7K
15,530
50,672
30.6%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<long long> findMaxSum(vector<int>& nums1, vector<int>& nums2, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long[] findMaxSum(int[] nums1, int[] nums2, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def findMaxSum(self, nums1, nums2, k):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def findMaxSum(self, nums1: List[int], nums2: 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 */\nlong long* findMaxSum(int* nums1, int nums1Size, int* nums2, int nums2Size, int k, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long[] FindMaxSum(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 findMaxSum = function(nums1, nums2, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function findMaxSum(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 findMaxSum($nums1, $nums2, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func findMaxSum(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun findMaxSum(nums1: IntArray, nums2: IntArray, k: Int): LongArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> findMaxSum(List<int> nums1, List<int> nums2, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func findMaxSum(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 find_max_sum(nums1, nums2, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def findMaxSum(nums1: Array[Int], nums2: Array[Int], k: Int): Array[Long] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn find_max_sum(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> Vec<i64> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (find-max-sum nums1 nums2 k)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec find_max_sum(Nums1 :: [integer()], Nums2 :: [integer()], K :: integer()) -> [integer()].\nfind_max_sum(Nums1, Nums2, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec find_max_sum(nums1 :: [integer], nums2 :: [integer], k :: integer) :: [integer]\n def find_max_sum(nums1, nums2, k) do\n \n end\nend"}]
[4,2,1,5,3] [10,20,30,40,50] 2
{ "name": "findMaxSum", "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', 'Sorting', 'Heap (Priority Queue)']
3,479
Fruits Into Baskets III
fruits-into-baskets-iii
<p>You are given two arrays of integers, <code>fruits</code> and <code>baskets</code>, each of length <code>n</code>, where <code>fruits[i]</code> represents the <strong>quantity</strong> of the <code>i<sup>th</sup></code> type of fruit, and <code>baskets[j]</code> represents the <strong>capacity</strong> of the <code>j<sup>th</sup></code> basket.</p> <p>From left to right, place the fruits according to these rules:</p> <ul> <li>Each fruit type must be placed in the <strong>leftmost available basket</strong> with a capacity <strong>greater than or equal</strong> to the quantity of that fruit type.</li> <li>Each basket can hold <b>only one</b> type of fruit.</li> <li>If a fruit type <b>cannot be placed</b> in any basket, it remains <b>unplaced</b>.</li> </ul> <p>Return the number of fruit types that remain unplaced after all possible allocations are made.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">fruits = [4,2,5], baskets = [3,5,4]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>fruits[0] = 4</code> is placed in <code>baskets[1] = 5</code>.</li> <li><code>fruits[1] = 2</code> is placed in <code>baskets[0] = 3</code>.</li> <li><code>fruits[2] = 5</code> cannot be placed in <code>baskets[2] = 4</code>.</li> </ul> <p>Since one fruit type remains unplaced, we return 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">fruits = [3,6,1], baskets = [6,4,7]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>fruits[0] = 3</code> is placed in <code>baskets[0] = 6</code>.</li> <li><code>fruits[1] = 6</code> cannot be placed in <code>baskets[1] = 4</code> (insufficient capacity) but can be placed in the next available basket, <code>baskets[2] = 7</code>.</li> <li><code>fruits[2] = 1</code> is placed in <code>baskets[1] = 4</code>.</li> </ul> <p>Since all fruits are successfully placed, we return 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == fruits.length == baskets.length</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= fruits[i], baskets[i] &lt;= 10<sup>9</sup></code></li> </ul>
Medium
6.9K
32.8K
6,926
32,779
21.1%
['block-placement-queries']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numOfUnplacedFruits(vector<int>& fruits, vector<int>& baskets) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numOfUnplacedFruits(self, fruits, baskets):\n \"\"\"\n :type fruits: List[int]\n :type baskets: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numOfUnplacedFruits(self, fruits: List[int], baskets: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numOfUnplacedFruits(int* fruits, int fruitsSize, int* baskets, int basketsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumOfUnplacedFruits(int[] fruits, int[] baskets) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} fruits\n * @param {number[]} baskets\n * @return {number}\n */\nvar numOfUnplacedFruits = function(fruits, baskets) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numOfUnplacedFruits(fruits: number[], baskets: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $fruits\n * @param Integer[] $baskets\n * @return Integer\n */\n function numOfUnplacedFruits($fruits, $baskets) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numOfUnplacedFruits(_ fruits: [Int], _ baskets: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numOfUnplacedFruits(fruits: IntArray, baskets: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numOfUnplacedFruits(List<int> fruits, List<int> baskets) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numOfUnplacedFruits(fruits []int, baskets []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} fruits\n# @param {Integer[]} baskets\n# @return {Integer}\ndef num_of_unplaced_fruits(fruits, baskets)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numOfUnplacedFruits(fruits: Array[Int], baskets: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn num_of_unplaced_fruits(fruits: Vec<i32>, baskets: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (num-of-unplaced-fruits fruits baskets)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec num_of_unplaced_fruits(Fruits :: [integer()], Baskets :: [integer()]) -> integer().\nnum_of_unplaced_fruits(Fruits, Baskets) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec num_of_unplaced_fruits(fruits :: [integer], baskets :: [integer]) :: integer\n def num_of_unplaced_fruits(fruits, baskets) do\n \n end\nend"}]
[4,2,5] [3,5,4]
{ "name": "numOfUnplacedFruits", "params": [ { "name": "fruits", "type": "integer[]" }, { "type": "integer[]", "name": "baskets" } ], "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', 'Segment Tree', 'Ordered Set']
3,480
Maximize Subarrays After Removing One Conflicting Pair
maximize-subarrays-after-removing-one-conflicting-pair
<p>You are given an integer <code>n</code> which represents an array <code>nums</code> containing the numbers from 1 to <code>n</code> in order. Additionally, you are given a 2D array <code>conflictingPairs</code>, where <code>conflictingPairs[i] = [a, b]</code> indicates that <code>a</code> and <code>b</code> form a conflicting pair.</p> <p>Remove <strong>exactly</strong> one element from <code>conflictingPairs</code>. Afterward, count the number of <span data-keyword="subarray-nonempty">non-empty subarrays</span> of <code>nums</code> which do not contain both <code>a</code> and <code>b</code> for any remaining conflicting pair <code>[a, b]</code>.</p> <p>Return the <strong>maximum</strong> number of subarrays possible after removing <strong>exactly</strong> one conflicting pair.</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 = 4, conflictingPairs = [[2,3],[1,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Remove <code>[2, 3]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[1, 4]]</code>.</li> <li>There are 9 subarrays in <code>nums</code> where <code>[1, 4]</code> do not appear together. They are <code>[1]</code>, <code>[2]</code>, <code>[3]</code>, <code>[4]</code>, <code>[1, 2]</code>, <code>[2, 3]</code>, <code>[3, 4]</code>, <code>[1, 2, 3]</code> and <code>[2, 3, 4]</code>.</li> <li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 9.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, conflictingPairs = [[1,2],[2,5],[3,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Remove <code>[1, 2]</code> from <code>conflictingPairs</code>. Now, <code>conflictingPairs = [[2, 5], [3, 5]]</code>.</li> <li>There are 12 subarrays in <code>nums</code> where <code>[2, 5]</code> and <code>[3, 5]</code> do not appear together.</li> <li>The maximum number of subarrays we can achieve after removing one element from <code>conflictingPairs</code> is 12.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= conflictingPairs.length &lt;= 2 * n</code></li> <li><code>conflictingPairs[i].length == 2</code></li> <li><code>1 &lt;= conflictingPairs[i][j] &lt;= n</code></li> <li><code>conflictingPairs[i][0] != conflictingPairs[i][1]</code></li> </ul>
Hard
2.2K
7K
2,226
7,033
31.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long maxSubarrays(int n, vector<vector<int>>& conflictingPairs) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long maxSubarrays(int n, int[][] conflictingPairs) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSubarrays(self, n, conflictingPairs):\n \"\"\"\n :type n: int\n :type conflictingPairs: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSubarrays(self, n: int, conflictingPairs: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long maxSubarrays(int n, int** conflictingPairs, int conflictingPairsSize, int* conflictingPairsColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MaxSubarrays(int n, int[][] conflictingPairs) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number[][]} conflictingPairs\n * @return {number}\n */\nvar maxSubarrays = function(n, conflictingPairs) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSubarrays(n: number, conflictingPairs: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[][] $conflictingPairs\n * @return Integer\n */\n function maxSubarrays($n, $conflictingPairs) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSubarrays(_ n: Int, _ conflictingPairs: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSubarrays(n: Int, conflictingPairs: Array<IntArray>): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxSubarrays(int n, List<List<int>> conflictingPairs) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSubarrays(n int, conflictingPairs [][]int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer[][]} conflicting_pairs\n# @return {Integer}\ndef max_subarrays(n, conflicting_pairs)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSubarrays(n: Int, conflictingPairs: Array[Array[Int]]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_subarrays(n: i32, conflicting_pairs: Vec<Vec<i32>>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-subarrays n conflictingPairs)\n (-> exact-integer? (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_subarrays(N :: integer(), ConflictingPairs :: [[integer()]]) -> integer().\nmax_subarrays(N, ConflictingPairs) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_subarrays(n :: integer, conflicting_pairs :: [[integer]]) :: integer\n def max_subarrays(n, conflicting_pairs) do\n \n end\nend"}]
4 [[2,3],[1,4]]
{ "name": "maxSubarrays", "params": [ { "name": "n", "type": "integer" }, { "type": "integer[][]", "name": "conflictingPairs" } ], "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', 'Enumeration', 'Prefix Sum']
3,481
Apply Substitutions
apply-substitutions
null
Medium
2.6K
3.4K
2,594
3,359
77.2%
['evaluate-the-bracket-pairs-of-a-string']
[]
Algorithms
null
[["A","abc"],["B","def"]] "%A%_%B%"
{ "name": "applySubstitutions", "params": [ { "name": "replacements", "type": "list<list<string>>" }, { "type": "string", "name": "text" } ], "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', 'Hash Table', 'String', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Topological Sort']
3,482
Analyze Organization Hierarchy
analyze-organization-hierarchy
<p>Table: <code>Employees</code></p> <pre> +----------------+---------+ | Column Name | Type | +----------------+---------+ | employee_id | int | | employee_name | varchar | | manager_id | int | | salary | int | | department | varchar | +----------------+----------+ employee_id is the unique key for this table. Each row contains information about an employee, including their ID, name, their manager&#39;s ID, salary, and department. manager_id is null for the top-level manager (CEO). </pre> <p>Write a solution to analyze the organizational hierarchy and answer the following:</p> <ol> <li><strong>Hierarchy Levels:</strong> For each employee, determine their level in the organization (CEO is level <code>1</code>, employees reporting directly to the CEO are level <code>2</code>, and so on).</li> <li><strong>Team Size:</strong> For each employee who is a manager, count the total number of employees under them (direct and indirect reports).</li> <li><strong>Salary Budget:</strong> For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary).</li> </ol> <p>Return <em>the result table ordered by&nbsp;<em>the result ordered by <strong>level</strong> in <strong>ascending</strong> order, then by <strong>budget</strong> in <strong>descending</strong> order, and finally by <strong>employee_name</strong> in <strong>ascending</strong> order</em>.</em></p> <p><em>The result format is in the following example.</em></p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>Employees table:</p> <pre class="example-io"> +-------------+---------------+------------+--------+-------------+ | employee_id | employee_name | manager_id | salary | department | +-------------+---------------+------------+--------+-------------+ | 1 | Alice | null | 12000 | Executive | | 2 | Bob | 1 | 10000 | Sales | | 3 | Charlie | 1 | 10000 | Engineering | | 4 | David | 2 | 7500 | Sales | | 5 | Eva | 2 | 7500 | Sales | | 6 | Frank | 3 | 9000 | Engineering | | 7 | Grace | 3 | 8500 | Engineering | | 8 | Hank | 4 | 6000 | Sales | | 9 | Ivy | 6 | 7000 | Engineering | | 10 | Judy | 6 | 7000 | Engineering | +-------------+---------------+------------+--------+-------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +-------------+---------------+-------+-----------+--------+ | employee_id | employee_name | level | team_size | budget | +-------------+---------------+-------+-----------+--------+ | 1 | Alice | 1 | 9 | 84500 | | 3 | Charlie | 2 | 4 | 41500 | | 2 | Bob | 2 | 3 | 31000 | | 6 | Frank | 3 | 2 | 23000 | | 4 | David | 3 | 1 | 13500 | | 7 | Grace | 3 | 0 | 8500 | | 5 | Eva | 3 | 0 | 7500 | | 9 | Ivy | 4 | 0 | 7000 | | 10 | Judy | 4 | 0 | 7000 | | 8 | Hank | 4 | 0 | 6000 | +-------------+---------------+-------+-----------+--------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>Organization Structure:</strong> <ul> <li>Alice (ID: 1) is the CEO (level 1) with no manager</li> <li>Bob (ID: 2) and Charlie (ID: 3) report directly to Alice (level 2)</li> <li>David (ID: 4), Eva (ID: 5) report to Bob, while Frank (ID: 6) and Grace (ID: 7) report to Charlie (level 3)</li> <li>Hank (ID: 8) reports to David, and Ivy (ID: 9) and Judy (ID: 10) report to Frank (level 4)</li> </ul> </li> <li><strong>Level Calculation:</strong> <ul> <li>The CEO (Alice) is at level 1</li> <li>Each subsequent level of management adds 1 to the level</li> </ul> </li> <li><strong>Team Size Calculation:</strong> <ul> <li>Alice has 9 employees under her (the entire company except herself)</li> <li>Bob has 3 employees (David, Eva, and Hank)</li> <li>Charlie has 4 employees (Frank, Grace, Ivy, and Judy)</li> <li>David has 1 employee (Hank)</li> <li>Frank has 2 employees (Ivy and Judy)</li> <li>Eva, Grace, Hank, Ivy, and Judy have no direct reports (team_size = 0)</li> </ul> </li> <li><strong>Budget Calculation:</strong> <ul> <li>Alice&#39;s budget: Her salary (12000) + all employees&#39; salaries (72500) = 84500</li> <li>Charlie&#39;s budget: His salary (10000) + Frank&#39;s budget (23000) + Grace&#39;s salary (8500) = 41500</li> <li>Bob&#39;s budget: His salary (10000) + David&#39;s budget (13500) + Eva&#39;s salary (7500) = 31000</li> <li>Frank&#39;s budget: His salary (9000) + Ivy&#39;s salary (7000) + Judy&#39;s salary (7000) = 23000</li> <li>David&#39;s budget: His salary (7500) + Hank&#39;s salary (6000) = 13500</li> <li>Employees with no direct reports have budgets equal to their own salary</li> </ul> </li> </ul> <p><strong>Note:</strong></p> <ul> <li>The result is ordered first by level in ascending order</li> <li>Within the same level, employees are ordered by budget in descending order then by name in ascending order</li> </ul> </div>
Hard
1.1K
1.8K
1,068
1,778
60.1%
[]
['CREATE TABLE if not exists Employees (\n employee_id INT,\n employee_name VARCHAR(100),\n manager_id INT,\n salary INT,\n department VARCHAR(50)\n)', 'Truncate table Employees', "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('1', 'Alice', NULL, '12000', 'Executive')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('2', 'Bob', '1', '10000', 'Sales')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('3', 'Charlie', '1', '10000', 'Engineering')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('4', 'David', '2', '7500', 'Sales')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('5', 'Eva', '2', '7500', 'Sales')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('6', 'Frank', '3', '9000', 'Engineering')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('7', 'Grace', '3', '8500', 'Engineering')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('8', 'Hank', '4', '6000', 'Sales')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('9', 'Ivy', '6', '7000', 'Engineering')", "insert into Employees (employee_id, employee_name, manager_id, salary, department) values ('10', 'Judy', '6', '7000', 'Engineering')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef analyze_organization_hierarchy(employees: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"Employees":["employee_id","employee_name","manager_id","salary","department"]},"rows":{"Employees":[[1,"Alice",null,12000,"Executive"],[2,"Bob",1,10000,"Sales"],[3,"Charlie",1,10000,"Engineering"],[4,"David",2,7500,"Sales"],[5,"Eva",2,7500,"Sales"],[6,"Frank",3,9000,"Engineering"],[7,"Grace",3,8500,"Engineering"],[8,"Hank",4,6000,"Sales"],[9,"Ivy",6,7000,"Engineering"],[10,"Judy",6,7000,"Engineering"]]}}
{"mysql": ["CREATE TABLE if not exists Employees (\n employee_id INT,\n employee_name VARCHAR(100),\n manager_id INT,\n salary INT,\n department VARCHAR(50)\n)"], "mssql": ["CREATE TABLE Employees (\n employee_id INT,\n employee_name VARCHAR(100),\n manager_id INT,\n salary INT,\n department VARCHAR(50)\n)"], "oraclesql": ["CREATE TABLE Employees (\n employee_id NUMBER,\n employee_name VARCHAR2(100),\n manager_id NUMBER,\n salary NUMBER,\n department VARCHAR2(50)\n)"], "database": true, "name": "analyze_organization_hierarchy", "postgresql": ["CREATE TABLE IF NOT EXISTS Employees (\n employee_id INT,\n employee_name VARCHAR(100),\n manager_id INT,\n salary INT,\n department VARCHAR(50)\n);\n"], "pythondata": ["Employees = pd.DataFrame(columns=[\"employee_id\", \"employee_name\", \"manager_id\", \"salary\", \"department\"]).astype({\"employee_id\": \"int\", \"employee_name\": \"string\", \"manager_id\": \"Int64\", \"salary\": \"int\", \"department\": \"string\"})\n"], "database_schema": {"Employees": {"employee_id": "INT", "employee_name": "VARCHAR(100)", "manager_id": "INT", "salary": "INT", "department": "VARCHAR(50)"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,483
Unique 3-Digit Even Numbers
unique-3-digit-even-numbers
<p>You are given an array of digits called <code>digits</code>. Your task is to determine the number of <strong>distinct</strong> three-digit even numbers that can be formed using these digits.</p> <p><strong>Note</strong>: Each <em>copy</em> of a digit can only be used <strong>once per number</strong>, and there may <strong>not</strong> be leading zeros.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">digits = [1,2,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">12</span></p> <p><strong>Explanation:</strong> The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">digits = [0,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">digits = [6,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong> Only 666 can be formed.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">digits = [1,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong> No even 3-digit numbers can be formed.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= digits.length &lt;= 10</code></li> <li><code>0 &lt;= digits[i] &lt;= 9</code></li> </ul>
Easy
27K
41.1K
27,006
41,078
65.7%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int totalNumbers(vector<int>& digits) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int totalNumbers(int[] digits) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def totalNumbers(self, digits):\n \"\"\"\n :type digits: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def totalNumbers(self, digits: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int totalNumbers(int* digits, int digitsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int TotalNumbers(int[] digits) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} digits\n * @return {number}\n */\nvar totalNumbers = function(digits) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function totalNumbers(digits: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $digits\n * @return Integer\n */\n function totalNumbers($digits) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func totalNumbers(_ digits: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun totalNumbers(digits: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int totalNumbers(List<int> digits) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func totalNumbers(digits []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} digits\n# @return {Integer}\ndef total_numbers(digits)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def totalNumbers(digits: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn total_numbers(digits: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (total-numbers digits)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec total_numbers(Digits :: [integer()]) -> integer().\ntotal_numbers(Digits) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec total_numbers(digits :: [integer]) :: integer\n def total_numbers(digits) do\n \n end\nend"}]
[1,2,3,4]
{ "name": "totalNumbers", "params": [ { "name": "digits", "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', 'Recursion', 'Enumeration']
3,484
Design Spreadsheet
design-spreadsheet
<p>A spreadsheet is a grid with 26 columns (labeled from <code>&#39;A&#39;</code> to <code>&#39;Z&#39;</code>) and a given number of <code>rows</code>. Each cell in the spreadsheet can hold an integer value between 0 and 10<sup>5</sup>.</p> <p>Implement the <code>Spreadsheet</code> class:</p> <ul> <li><code>Spreadsheet(int rows)</code> Initializes a spreadsheet with 26 columns (labeled <code>&#39;A&#39;</code> to <code>&#39;Z&#39;</code>) and the specified number of rows. All cells are initially set to 0.</li> <li><code>void setCell(String cell, int value)</code> Sets the value of the specified <code>cell</code>. The cell reference is provided in the format <code>&quot;AX&quot;</code> (e.g., <code>&quot;A1&quot;</code>, <code>&quot;B10&quot;</code>), where the letter represents the column (from <code>&#39;A&#39;</code> to <code>&#39;Z&#39;</code>) and the number represents a <strong>1-indexed</strong> row.</li> <li><code>void resetCell(String cell)</code> Resets the specified cell to 0.</li> <li><code>int getValue(String formula)</code> Evaluates a formula of the form <code>&quot;=X+Y&quot;</code>, where <code>X</code> and <code>Y</code> are <strong>either</strong> cell references or non-negative integers, and returns the computed sum.</li> </ul> <p><strong>Note:</strong> If <code>getValue</code> references a cell that has not been explicitly set using <code>setCell</code>, its value is considered 0.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong><br /> <span class="example-io">[&quot;Spreadsheet&quot;, &quot;getValue&quot;, &quot;setCell&quot;, &quot;getValue&quot;, &quot;setCell&quot;, &quot;getValue&quot;, &quot;resetCell&quot;, &quot;getValue&quot;]<br /> [[3], [&quot;=5+7&quot;], [&quot;A1&quot;, 10], [&quot;=A1+6&quot;], [&quot;B2&quot;, 15], [&quot;=A1+B2&quot;], [&quot;A1&quot;], [&quot;=A1+B2&quot;]]</span></p> <p><strong>Output:</strong><br /> <span class="example-io">[null, 12, null, 16, null, 25, null, 15] </span></p> <p><strong>Explanation</strong></p> Spreadsheet spreadsheet = new Spreadsheet(3); // Initializes a spreadsheet with 3 rows and 26 columns<br data-end="321" data-start="318" /> spreadsheet.getValue(&quot;=5+7&quot;); // returns 12 (5+7)<br data-end="373" data-start="370" /> spreadsheet.setCell(&quot;A1&quot;, 10); // sets A1 to 10<br data-end="423" data-start="420" /> spreadsheet.getValue(&quot;=A1+6&quot;); // returns 16 (10+6)<br data-end="477" data-start="474" /> spreadsheet.setCell(&quot;B2&quot;, 15); // sets B2 to 15<br data-end="527" data-start="524" /> spreadsheet.getValue(&quot;=A1+B2&quot;); // returns 25 (10+15)<br data-end="583" data-start="580" /> spreadsheet.resetCell(&quot;A1&quot;); // resets A1 to 0<br data-end="634" data-start="631" /> spreadsheet.getValue(&quot;=A1+B2&quot;); // returns 15 (0+15)</div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rows &lt;= 10<sup>3</sup></code></li> <li><code>0 &lt;= value &lt;= 10<sup>5</sup></code></li> <li>The formula is always in the format <code>&quot;=X+Y&quot;</code>, where <code>X</code> and <code>Y</code> are either valid cell references or <strong>non-negative</strong> integers with values less than or equal to <code>10<sup>5</sup></code>.</li> <li>Each cell reference consists of a capital letter from <code>&#39;A&#39;</code> to <code>&#39;Z&#39;</code> followed by a row number between <code>1</code> and <code>rows</code>.</li> <li>At most <code>10<sup>4</sup></code> calls will be made in <strong>total</strong> to <code>setCell</code>, <code>resetCell</code>, and <code>getValue</code>.</li> </ul>
Medium
18.5K
27.3K
18,540
27,274
68.0%
['excel-sheet-column-title']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Spreadsheet {\npublic:\n Spreadsheet(int rows) {\n \n }\n \n void setCell(string cell, int value) {\n \n }\n \n void resetCell(string cell) {\n \n }\n \n int getValue(string formula) {\n \n }\n};\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * Spreadsheet* obj = new Spreadsheet(rows);\n * obj->setCell(cell,value);\n * obj->resetCell(cell);\n * int param_3 = obj->getValue(formula);\n */"}, {"value": "java", "text": "Java", "defaultCode": "class Spreadsheet {\n\n public Spreadsheet(int rows) {\n \n }\n \n public void setCell(String cell, int value) {\n \n }\n \n public void resetCell(String cell) {\n \n }\n \n public int getValue(String formula) {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * Spreadsheet obj = new Spreadsheet(rows);\n * obj.setCell(cell,value);\n * obj.resetCell(cell);\n * int param_3 = obj.getValue(formula);\n */"}, {"value": "python", "text": "Python", "defaultCode": "class Spreadsheet(object):\n\n def __init__(self, rows):\n \"\"\"\n :type rows: int\n \"\"\"\n \n\n def setCell(self, cell, value):\n \"\"\"\n :type cell: str\n :type value: int\n :rtype: None\n \"\"\"\n \n\n def resetCell(self, cell):\n \"\"\"\n :type cell: str\n :rtype: None\n \"\"\"\n \n\n def getValue(self, formula):\n \"\"\"\n :type formula: str\n :rtype: int\n \"\"\"\n \n\n\n# Your Spreadsheet object will be instantiated and called as such:\n# obj = Spreadsheet(rows)\n# obj.setCell(cell,value)\n# obj.resetCell(cell)\n# param_3 = obj.getValue(formula)"}, {"value": "python3", "text": "Python3", "defaultCode": "class Spreadsheet:\n\n def __init__(self, rows: int):\n \n\n def setCell(self, cell: str, value: int) -> None:\n \n\n def resetCell(self, cell: str) -> None:\n \n\n def getValue(self, formula: str) -> int:\n \n\n\n# Your Spreadsheet object will be instantiated and called as such:\n# obj = Spreadsheet(rows)\n# obj.setCell(cell,value)\n# obj.resetCell(cell)\n# param_3 = obj.getValue(formula)"}, {"value": "c", "text": "C", "defaultCode": "\n\n\ntypedef struct {\n \n} Spreadsheet;\n\n\nSpreadsheet* spreadsheetCreate(int rows) {\n \n}\n\nvoid spreadsheetSetCell(Spreadsheet* obj, char* cell, int value) {\n \n}\n\nvoid spreadsheetResetCell(Spreadsheet* obj, char* cell) {\n \n}\n\nint spreadsheetGetValue(Spreadsheet* obj, char* formula) {\n \n}\n\nvoid spreadsheetFree(Spreadsheet* obj) {\n \n}\n\n/**\n * Your Spreadsheet struct will be instantiated and called as such:\n * Spreadsheet* obj = spreadsheetCreate(rows);\n * spreadsheetSetCell(obj, cell, value);\n \n * spreadsheetResetCell(obj, cell);\n \n * int param_3 = spreadsheetGetValue(obj, formula);\n \n * spreadsheetFree(obj);\n*/"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Spreadsheet {\n\n public Spreadsheet(int rows) {\n \n }\n \n public void SetCell(string cell, int value) {\n \n }\n \n public void ResetCell(string cell) {\n \n }\n \n public int GetValue(string formula) {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * Spreadsheet obj = new Spreadsheet(rows);\n * obj.SetCell(cell,value);\n * obj.ResetCell(cell);\n * int param_3 = obj.GetValue(formula);\n */"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} rows\n */\nvar Spreadsheet = function(rows) {\n \n};\n\n/** \n * @param {string} cell \n * @param {number} value\n * @return {void}\n */\nSpreadsheet.prototype.setCell = function(cell, value) {\n \n};\n\n/** \n * @param {string} cell\n * @return {void}\n */\nSpreadsheet.prototype.resetCell = function(cell) {\n \n};\n\n/** \n * @param {string} formula\n * @return {number}\n */\nSpreadsheet.prototype.getValue = function(formula) {\n \n};\n\n/** \n * Your Spreadsheet object will be instantiated and called as such:\n * var obj = new Spreadsheet(rows)\n * obj.setCell(cell,value)\n * obj.resetCell(cell)\n * var param_3 = obj.getValue(formula)\n */"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "class Spreadsheet {\n constructor(rows: number) {\n \n }\n\n setCell(cell: string, value: number): void {\n \n }\n\n resetCell(cell: string): void {\n \n }\n\n getValue(formula: string): number {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * var obj = new Spreadsheet(rows)\n * obj.setCell(cell,value)\n * obj.resetCell(cell)\n * var param_3 = obj.getValue(formula)\n */"}, {"value": "php", "text": "PHP", "defaultCode": "class Spreadsheet {\n /**\n * @param Integer $rows\n */\n function __construct($rows) {\n \n }\n \n /**\n * @param String $cell\n * @param Integer $value\n * @return NULL\n */\n function setCell($cell, $value) {\n \n }\n \n /**\n * @param String $cell\n * @return NULL\n */\n function resetCell($cell) {\n \n }\n \n /**\n * @param String $formula\n * @return Integer\n */\n function getValue($formula) {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * $obj = Spreadsheet($rows);\n * $obj->setCell($cell, $value);\n * $obj->resetCell($cell);\n * $ret_3 = $obj->getValue($formula);\n */"}, {"value": "swift", "text": "Swift", "defaultCode": "\nclass Spreadsheet {\n\n init(_ rows: Int) {\n \n }\n \n func setCell(_ cell: String, _ value: Int) {\n \n }\n \n func resetCell(_ cell: String) {\n \n }\n \n func getValue(_ formula: String) -> Int {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * let obj = Spreadsheet(rows)\n * obj.setCell(cell, value)\n * obj.resetCell(cell)\n * let ret_3: Int = obj.getValue(formula)\n */"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Spreadsheet(rows: Int) {\n\n fun setCell(cell: String, value: Int) {\n \n }\n\n fun resetCell(cell: String) {\n \n }\n\n fun getValue(formula: String): Int {\n \n }\n\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * var obj = Spreadsheet(rows)\n * obj.setCell(cell,value)\n * obj.resetCell(cell)\n * var param_3 = obj.getValue(formula)\n */"}, {"value": "dart", "text": "Dart", "defaultCode": "class Spreadsheet {\n\n Spreadsheet(int rows) {\n \n }\n \n void setCell(String cell, int value) {\n \n }\n \n void resetCell(String cell) {\n \n }\n \n int getValue(String formula) {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * Spreadsheet obj = Spreadsheet(rows);\n * obj.setCell(cell,value);\n * obj.resetCell(cell);\n * int param3 = obj.getValue(formula);\n */"}, {"value": "golang", "text": "Go", "defaultCode": "type Spreadsheet struct {\n \n}\n\n\nfunc Constructor(rows int) Spreadsheet {\n \n}\n\n\nfunc (this *Spreadsheet) SetCell(cell string, value int) {\n \n}\n\n\nfunc (this *Spreadsheet) ResetCell(cell string) {\n \n}\n\n\nfunc (this *Spreadsheet) GetValue(formula string) int {\n \n}\n\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * obj := Constructor(rows);\n * obj.SetCell(cell,value);\n * obj.ResetCell(cell);\n * param_3 := obj.GetValue(formula);\n */"}, {"value": "ruby", "text": "Ruby", "defaultCode": "class Spreadsheet\n\n=begin\n :type rows: Integer\n=end\n def initialize(rows)\n \n end\n\n\n=begin\n :type cell: String\n :type value: Integer\n :rtype: Void\n=end\n def set_cell(cell, value)\n \n end\n\n\n=begin\n :type cell: String\n :rtype: Void\n=end\n def reset_cell(cell)\n \n end\n\n\n=begin\n :type formula: String\n :rtype: Integer\n=end\n def get_value(formula)\n \n end\n\n\nend\n\n# Your Spreadsheet object will be instantiated and called as such:\n# obj = Spreadsheet.new(rows)\n# obj.set_cell(cell, value)\n# obj.reset_cell(cell)\n# param_3 = obj.get_value(formula)"}, {"value": "scala", "text": "Scala", "defaultCode": "class Spreadsheet(_rows: Int) {\n\n def setCell(cell: String, value: Int): Unit = {\n \n }\n\n def resetCell(cell: String): Unit = {\n \n }\n\n def getValue(formula: String): Int = {\n \n }\n\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * val obj = new Spreadsheet(rows)\n * obj.setCell(cell,value)\n * obj.resetCell(cell)\n * val param_3 = obj.getValue(formula)\n */"}, {"value": "rust", "text": "Rust", "defaultCode": "struct Spreadsheet {\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 Spreadsheet {\n\n fn new(rows: i32) -> Self {\n \n }\n \n fn set_cell(&self, cell: String, value: i32) {\n \n }\n \n fn reset_cell(&self, cell: String) {\n \n }\n \n fn get_value(&self, formula: String) -> i32 {\n \n }\n}\n\n/**\n * Your Spreadsheet object will be instantiated and called as such:\n * let obj = Spreadsheet::new(rows);\n * obj.set_cell(cell, value);\n * obj.reset_cell(cell);\n * let ret_3: i32 = obj.get_value(formula);\n */"}, {"value": "racket", "text": "Racket", "defaultCode": "(define spreadsheet%\n (class object%\n (super-new)\n \n ; rows : exact-integer?\n (init-field\n rows)\n \n ; set-cell : string? exact-integer? -> void?\n (define/public (set-cell cell value)\n )\n ; reset-cell : string? -> void?\n (define/public (reset-cell cell)\n )\n ; get-value : string? -> exact-integer?\n (define/public (get-value formula)\n )))\n\n;; Your spreadsheet% object will be instantiated and called as such:\n;; (define obj (new spreadsheet% [rows rows]))\n;; (send obj set-cell cell value)\n;; (send obj reset-cell cell)\n;; (define param_3 (send obj get-value formula))"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec spreadsheet_init_(Rows :: integer()) -> any().\nspreadsheet_init_(Rows) ->\n .\n\n-spec spreadsheet_set_cell(Cell :: unicode:unicode_binary(), Value :: integer()) -> any().\nspreadsheet_set_cell(Cell, Value) ->\n .\n\n-spec spreadsheet_reset_cell(Cell :: unicode:unicode_binary()) -> any().\nspreadsheet_reset_cell(Cell) ->\n .\n\n-spec spreadsheet_get_value(Formula :: unicode:unicode_binary()) -> integer().\nspreadsheet_get_value(Formula) ->\n .\n\n\n%% Your functions will be called as such:\n%% spreadsheet_init_(Rows),\n%% spreadsheet_set_cell(Cell, Value),\n%% spreadsheet_reset_cell(Cell),\n%% Param_3 = spreadsheet_get_value(Formula),\n\n%% spreadsheet_init_ will be called before every test case, in which you can do some necessary initializations."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Spreadsheet do\n @spec init_(rows :: integer) :: any\n def init_(rows) do\n \n end\n\n @spec set_cell(cell :: String.t, value :: integer) :: any\n def set_cell(cell, value) do\n \n end\n\n @spec reset_cell(cell :: String.t) :: any\n def reset_cell(cell) do\n \n end\n\n @spec get_value(formula :: String.t) :: integer\n def get_value(formula) do\n \n end\nend\n\n# Your functions will be called as such:\n# Spreadsheet.init_(rows)\n# Spreadsheet.set_cell(cell, value)\n# Spreadsheet.reset_cell(cell)\n# param_3 = Spreadsheet.get_value(formula)\n\n# Spreadsheet.init_ will be called before every test case, in which you can do some necessary initializations."}]
["Spreadsheet","getValue","setCell","getValue","setCell","getValue","resetCell","getValue"] [[3],["=5+7"],["A1",10],["=A1+6"],["B2",15],["=A1+B2"],["A1"],["=A1+B2"]]
{ "classname": "Spreadsheet", "constructor": { "params": [ { "type": "integer", "name": "rows" } ] }, "methods": [ { "params": [ { "type": "string", "name": "cell" }, { "type": "integer", "name": "value" } ], "name": "setCell", "return": { "type": "void" } }, { "params": [ { "type": "string", "name": "cell" } ], "name": "resetCell", "return": { "type": "void" } }, { "params": [ { "type": "string", "name": "formula" } ], "name": "getValue", "return": { "type": "integer" } } ], "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', 'Matrix']
3,485
Longest Common Prefix of K Strings After Removal
longest-common-prefix-of-k-strings-after-removal
<p>You are given an array of strings <code>words</code> and an integer <code>k</code>.</p> <p>For each index <code>i</code> in the range <code>[0, words.length - 1]</code>, find the <strong>length</strong> of the <strong>longest common <span data-keyword="string-prefix">prefix</span></strong> among any <code>k</code> strings (selected at <strong>distinct indices</strong>) from the remaining array after removing the <code>i<sup>th</sup></code> element.</p> <p>Return an array <code>answer</code>, where <code>answer[i]</code> is the answer for <code>i<sup>th</sup></code> element. If removing the <code>i<sup>th</sup></code> element leaves the array with fewer than <code>k</code> strings, <code>answer[i]</code> is 0.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;jump&quot;,&quot;run&quot;,&quot;run&quot;,&quot;jump&quot;,&quot;run&quot;], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[3,4,4,3,4]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Removing index 0 (<code>&quot;jump&quot;</code>): <ul> <li><code>words</code> becomes: <code>[&quot;run&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code>. <code>&quot;run&quot;</code> occurs 3 times. Choosing any two gives the longest common prefix <code>&quot;run&quot;</code> (length 3).</li> </ul> </li> <li>Removing index 1 (<code>&quot;run&quot;</code>): <ul> <li><code>words</code> becomes: <code>[&quot;jump&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code>. <code>&quot;jump&quot;</code> occurs twice. Choosing these two gives the longest common prefix <code>&quot;jump&quot;</code> (length 4).</li> </ul> </li> <li>Removing index 2 (<code>&quot;run&quot;</code>): <ul> <li><code>words</code> becomes: <code>[&quot;jump&quot;, &quot;run&quot;, &quot;jump&quot;, &quot;run&quot;]</code>. <code>&quot;jump&quot;</code> occurs twice. Choosing these two gives the longest common prefix <code>&quot;jump&quot;</code> (length 4).</li> </ul> </li> <li>Removing index 3 (<code>&quot;jump&quot;</code>): <ul> <li><code>words</code> becomes: <code>[&quot;jump&quot;, &quot;run&quot;, &quot;run&quot;, &quot;run&quot;]</code>. <code>&quot;run&quot;</code> occurs 3 times. Choosing any two gives the longest common prefix <code>&quot;run&quot;</code> (length 3).</li> </ul> </li> <li>Removing index 4 (&quot;run&quot;): <ul> <li><code>words</code> becomes: <code>[&quot;jump&quot;, &quot;run&quot;, &quot;run&quot;, &quot;jump&quot;]</code>. <code>&quot;jump&quot;</code> occurs twice. Choosing these two gives the longest common prefix <code>&quot;jump&quot;</code> (length 4).</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">words = [&quot;dog&quot;,&quot;racer&quot;,&quot;car&quot;], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[0,0,0]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Removing any index results in an answer of 0.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= k &lt;= words.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= words[i].length &lt;= 10<sup>4</sup></code></li> <li><code>words[i]</code> consists of lowercase English letters.</li> <li>The sum of <code>words[i].length</code> is smaller than or equal <code>10<sup>5</sup></code>.</li> </ul>
Hard
4.3K
22.1K
4,319
22,106
19.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> longestCommonPrefix(vector<string>& words, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] longestCommonPrefix(String[] words, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestCommonPrefix(self, words, k):\n \"\"\"\n :type words: List[str]\n :type k: int\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def longestCommonPrefix(self, words: List[str], k: int) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* longestCommonPrefix(char** words, int wordsSize, int k, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] LongestCommonPrefix(string[] words, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string[]} words\n * @param {number} k\n * @return {number[]}\n */\nvar longestCommonPrefix = function(words, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestCommonPrefix(words: string[], k: number): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String[] $words\n * @param Integer $k\n * @return Integer[]\n */\n function longestCommonPrefix($words, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestCommonPrefix(_ words: [String], _ k: Int) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestCommonPrefix(words: Array<String>, k: Int): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> longestCommonPrefix(List<String> words, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestCommonPrefix(words []string, k int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String[]} words\n# @param {Integer} k\n# @return {Integer[]}\ndef longest_common_prefix(words, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestCommonPrefix(words: Array[String], k: Int): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_common_prefix(words: Vec<String>, k: i32) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-common-prefix words k)\n (-> (listof string?) exact-integer? (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_common_prefix(Words :: [unicode:unicode_binary()], K :: integer()) -> [integer()].\nlongest_common_prefix(Words, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_common_prefix(words :: [String.t], k :: integer) :: [integer]\n def longest_common_prefix(words, k) do\n \n end\nend"}]
["jump","run","run","jump","run"] 2
{ "name": "longestCommonPrefix", "params": [ { "name": "words", "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>"]}
['Array', 'String', 'Trie']
3,486
Longest Special Path II
longest-special-path-ii
<p>You are given an undirected tree rooted at node <code>0</code>, with <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. This is represented by a 2D array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, length<sub>i</sub>]</code> indicates an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with length <code>length<sub>i</sub></code>. You are also given an integer array <code>nums</code>, where <code>nums[i]</code> represents the value at node <code>i</code>.</p> <p>A <strong>special path</strong> is defined as a <strong>downward</strong> path from an ancestor node to a descendant node in which all node values are <strong>distinct</strong>, except for <strong>at most</strong> one value that may appear twice.</p> <p>Return an array <code data-stringify-type="code">result</code> of size 2, where <code>result[0]</code> is the <b data-stringify-type="bold">length</b> of the <strong>longest</strong> special path, and <code>result[1]</code> is the <b data-stringify-type="bold">minimum</b> number of nodes in all <i data-stringify-type="italic">possible</i> <strong>longest</strong> special paths.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">edges = [[0,1,1],[1,2,3],[1,3,1],[2,4,6],[4,7,2],[3,5,2],[3,6,5],[6,8,3]], nums = [1,1,0,3,1,2,1,1,0]</span></p> <p><strong>Output:</strong> <span class="example-io">[9,3]</span></p> <p><strong>Explanation:</strong></p> <p>In the image below, nodes are colored by their corresponding values in <code>nums</code>.</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/02/18/e1.png" style="width: 190px; height: 270px;" /></p> <p>The longest special paths are <code>1 -&gt; 2 -&gt; 4</code> and <code>1 -&gt; 3 -&gt; 6 -&gt; 8</code>, both having a length of 9. The minimum number of nodes across all longest special paths is 3.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">edges = [[1,0,3],[0,2,4],[0,3,5]], nums = [1,1,0,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/02/18/e2.png" style="width: 150px; height: 110px;" /></p> <p>The longest path is <code>0 -&gt; 3</code> consisting of 2 nodes with a length of 5.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li> <li><code>edges.length == n - 1</code></li> <li><code>edges[i].length == 3</code></li> <li><code>0 &lt;= u<sub>i</sub>, v<sub>i</sub> &lt; n</code></li> <li><code>1 &lt;= length<sub>i</sub> &lt;= 10<sup>3</sup></code></li> <li><code>nums.length == n</code></li> <li><code>0 &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li> <li>The input is generated such that <code>edges</code> represents a valid tree.</li> </ul>
Hard
1.2K
8K
1,248
8,017
15.6%
['longest-special-path']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> longestSpecialPath(vector<vector<int>>& edges, vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] longestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestSpecialPath(self, edges, nums):\n \"\"\"\n :type edges: List[List[int]]\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def longestSpecialPath(self, edges: List[List[int]], nums: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* longestSpecialPath(int** edges, int edgesSize, int* edgesColSize, int* nums, int numsSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] LongestSpecialPath(int[][] edges, int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} edges\n * @param {number[]} nums\n * @return {number[]}\n */\nvar longestSpecialPath = function(edges, nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestSpecialPath(edges: number[][], nums: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $edges\n * @param Integer[] $nums\n * @return Integer[]\n */\n function longestSpecialPath($edges, $nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestSpecialPath(_ edges: [[Int]], _ nums: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestSpecialPath(edges: Array<IntArray>, nums: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> longestSpecialPath(List<List<int>> edges, List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestSpecialPath(edges [][]int, nums []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} edges\n# @param {Integer[]} nums\n# @return {Integer[]}\ndef longest_special_path(edges, nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestSpecialPath(edges: Array[Array[Int]], nums: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_special_path(edges: Vec<Vec<i32>>, nums: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-special-path edges nums)\n (-> (listof (listof exact-integer?)) (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_special_path(Edges :: [[integer()]], Nums :: [integer()]) -> [integer()].\nlongest_special_path(Edges, Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_special_path(edges :: [[integer]], nums :: [integer]) :: [integer]\n def longest_special_path(edges, nums) do\n \n end\nend"}]
[[0,1,1],[1,2,3],[1,3,1],[2,4,6],[4,7,2],[3,5,2],[3,6,5],[6,8,3]] [1,1,0,3,1,2,1,1,0]
{ "name": "longestSpecialPath", "params": [ { "name": "edges", "type": "integer[][]" }, { "type": "integer[]", "name": "nums" } ], "return": { "type": "integer[]" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Hash Table', 'Tree', 'Depth-First Search', 'Prefix Sum']
3,487
Maximum Unique Subarray Sum After Deletion
maximum-unique-subarray-sum-after-deletion
<p>You are given an integer array <code>nums</code>.</p> <p>You are allowed to delete any number of elements from <code>nums</code> without making it <strong>empty</strong>. After performing the deletions, select a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that:</p> <ol> <li>All elements in the subarray are <strong>unique</strong>.</li> <li>The sum of the elements in the subarray is <strong>maximized</strong>.</li> </ol> <p>Return the <strong>maximum sum</strong> of such a subarray.</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,2,3,4,5]</span></p> <p><strong>Output:</strong> <span class="example-io">15</span></p> <p><strong>Explanation:</strong></p> <p>Select the entire array without deleting any element to obtain the maximum sum.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,1,0,1,1]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>Delete the element <code>nums[0] == 1</code>, <code>nums[1] == 1</code>, <code>nums[2] == 0</code>, and <code>nums[3] == 1</code>. Select the entire array <code>[1]</code> to obtain the maximum sum.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,-1,-2,1,0,-1]</span></p> <p><strong>Output:</strong> 3</p> <p><strong>Explanation:</strong></p> <p>Delete the elements <code>nums[2] == -1</code> and <code>nums[3] == -2</code>, and select the subarray <code>[2, 1]</code> from <code>[1, 2, 1, 0, -1]</code> to obtain the maximum sum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Easy
28.6K
106.4K
28,579
106,439
26.9%
['maximum-subarray-sum-with-one-deletion']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxSum(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxSum(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxSum(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxSum(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxSum(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxSum = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxSum(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function maxSum($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxSum(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxSum(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxSum(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxSum(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef max_sum(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxSum(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_sum(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-sum nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_sum(Nums :: [integer()]) -> integer().\nmax_sum(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_sum(nums :: [integer]) :: integer\n def max_sum(nums) do\n \n end\nend"}]
[1,2,3,4,5]
{ "name": "maxSum", "params": [ { "name": "nums", "type": "integer[]" } ], "return": { "type": "integer" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Hash Table', 'Greedy']
3,488
Closest Equal Element Queries
closest-equal-element-queries
<p>You are given a <strong>circular</strong> array <code>nums</code> and an array <code>queries</code>.</p> <p>For each query <code>i</code>, you have to find the following:</p> <ul> <li>The <strong>minimum</strong> distance between the element at index <code>queries[i]</code> and <strong>any</strong> other index <code>j</code> in the <strong>circular</strong> array, where <code>nums[j] == nums[queries[i]]</code>. If no such index exists, the answer for that query should be -1.</li> </ul> <p>Return an array <code>answer</code> of the <strong>same</strong> size as <code>queries</code>, where <code>answer[i]</code> represents the result for query <code>i</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,3,1,4,1,3,2], queries = [0,3,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,-1,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Query 0: The element at <code>queries[0] = 0</code> is <code>nums[0] = 1</code>. The nearest index with the same value is 2, and the distance between them is 2.</li> <li>Query 1: The element at <code>queries[1] = 3</code> is <code>nums[3] = 4</code>. No other index contains 4, so the result is -1.</li> <li>Query 2: The element at <code>queries[2] = 5</code> is <code>nums[5] = 3</code>. The nearest index with the same value is 1, and the distance between them is 3 (following the circular path: <code>5 -&gt; 6 -&gt; 0 -&gt; 1</code>).</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,2,3,4], queries = [0,1,2,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,-1,-1,-1]</span></p> <p><strong>Explanation:</strong></p> <p>Each value in <code>nums</code> is unique, so no index shares the same value as the queried element. This results in -1 for all queries.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li> <li><code>0 &lt;= queries[i] &lt; nums.length</code></li> </ul>
Medium
16.7K
54.7K
16,677
54,725
30.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> solveQueries(vector<int>& nums, vector<int>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> solveQueries(int[] nums, int[] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def solveQueries(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 solveQueries(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 */\nint* solveQueries(int* nums, int numsSize, int* queries, int queriesSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> SolveQueries(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 solveQueries = function(nums, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function solveQueries(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 solveQueries($nums, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func solveQueries(_ nums: [Int], _ queries: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun solveQueries(nums: IntArray, queries: IntArray): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> solveQueries(List<int> nums, List<int> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func solveQueries(nums []int, queries []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer[]} queries\n# @return {Integer[]}\ndef solve_queries(nums, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def solveQueries(nums: Array[Int], queries: Array[Int]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn solve_queries(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (solve-queries nums queries)\n (-> (listof exact-integer?) (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec solve_queries(Nums :: [integer()], Queries :: [integer()]) -> [integer()].\nsolve_queries(Nums, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec solve_queries(nums :: [integer], queries :: [integer]) :: [integer]\n def solve_queries(nums, queries) do\n \n end\nend"}]
[1,3,1,4,1,3,2] [0,3,5]
{ "name": "solveQueries", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[]", "name": "queries" } ], "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', 'Binary Search']
3,489
Zero Array Transformation IV
zero-array-transformation-iv
<p>You are given an integer array <code>nums</code> of length <code>n</code> and a 2D array <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code>.</p> <p>Each <code>queries[i]</code> represents the following action on <code>nums</code>:</p> <ul> <li>Select a <span data-keyword="subset">subset</span> of indices in the range <code>[l<sub>i</sub>, r<sub>i</sub>]</code> from <code>nums</code>.</li> <li>Decrement the value at each selected index by <strong>exactly</strong> <code>val<sub>i</sub></code>.</li> </ul> <p>A <strong>Zero Array</strong> is an array with all its elements equal to 0.</p> <p>Return the <strong>minimum</strong> possible <strong>non-negative</strong> value of <code>k</code>, such that after processing the first <code>k</code> queries in <strong>sequence</strong>, <code>nums</code> becomes a <strong>Zero Array</strong>. If no such <code>k</code> exists, return -1.</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 = [2,0,2], queries = [[0,2,1],[0,2,1],[1,1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <ul> <li><strong>For query 0 (l = 0, r = 2, val = 1):</strong> <ul> <li>Decrement the values at indices <code>[0, 2]</code> by 1.</li> <li>The array will become <code>[1, 0, 1]</code>.</li> </ul> </li> <li><strong>For query 1 (l = 0, r = 2, val = 1):</strong> <ul> <li>Decrement the values at indices <code>[0, 2]</code> by 1.</li> <li>The array will become <code>[0, 0, 0]</code>, which is a Zero Array. Therefore, the minimum value of <code>k</code> is 2.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,3,2,1], queries = [[1,3,2],[0,2,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>It is impossible to make nums a Zero Array even after all the queries.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,2,1], queries = [[0,1,1],[1,2,1],[2,3,2],[3,4,1],[4,4,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <ul> <li><strong>For query 0 (l = 0, r = 1, val = 1):</strong> <ul> <li>Decrement the values at indices <code>[0, 1]</code> by <code><font face="monospace">1</font></code>.</li> <li>The array will become <code>[0, 1, 3, 2, 1]</code>.</li> </ul> </li> <li><strong>For query 1 (l = 1, r = 2, val = 1):</strong> <ul> <li>Decrement the values at indices <code>[1, 2]</code> by 1.</li> <li>The array will become <code>[0, 0, 2, 2, 1]</code>.</li> </ul> </li> <li><strong>For query 2 (l = 2, r = 3, val = 2):</strong> <ul> <li>Decrement the values at indices <code>[2, 3]</code> by 2.</li> <li>The array will become <code>[0, 0, 0, 0, 1]</code>.</li> </ul> </li> <li><strong>For query 3 (l = 3, r = 4, val = 1):</strong> <ul> <li>Decrement the value at index 4 by 1.</li> <li>The array will become <code>[0, 0, 0, 0, 0]</code>. Therefore, the minimum value of <code>k</code> is 4.</li> </ul> </li> </ul> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,2,6], queries = [[0,1,1],[0,2,1],[1,4,2],[4,4,4],[3,4,1],[4,4,5]]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10</code></li> <li><code>0 &lt;= nums[i] &lt;= 1000</code></li> <li><code>1 &lt;= queries.length &lt;= 1000</code></li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>, val<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; nums.length</code></li> <li><code>1 &lt;= val<sub>i</sub> &lt;= 10</code></li> </ul>
Medium
8K
27.8K
8,027
27,757
28.9%
['zero-array-transformation-i', 'zero-array-transformation-ii', 'zero-array-transformation-iii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minZeroArray(vector<int>& nums, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minZeroArray(int[] nums, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minZeroArray(self, nums, queries):\n \"\"\"\n :type nums: List[int]\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minZeroArray(self, nums: List[int], queries: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minZeroArray(int* nums, int numsSize, int** queries, int queriesSize, int* queriesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinZeroArray(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 minZeroArray = function(nums, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minZeroArray(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 minZeroArray($nums, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minZeroArray(_ nums: [Int], _ queries: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minZeroArray(nums: IntArray, queries: Array<IntArray>): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minZeroArray(List<int> nums, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minZeroArray(nums []int, queries [][]int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer[][]} queries\n# @return {Integer}\ndef min_zero_array(nums, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minZeroArray(nums: Array[Int], queries: Array[Array[Int]]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_zero_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-zero-array nums queries)\n (-> (listof exact-integer?) (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_zero_array(Nums :: [integer()], Queries :: [[integer()]]) -> integer().\nmin_zero_array(Nums, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_zero_array(nums :: [integer], queries :: [[integer]]) :: integer\n def min_zero_array(nums, queries) do\n \n end\nend"}]
[2,0,2] [[0,2,1],[0,2,1],[1,1,3]]
{ "name": "minZeroArray", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[][]", "name": "queries" } ], "return": { "type": "integer" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Dynamic Programming']
3,490
Count Beautiful Numbers
count-beautiful-numbers
<p data-end="387" data-start="189">You are given two positive integers, <code><font face="monospace">l</font></code> and <code><font face="monospace">r</font></code>. A positive integer is called <strong data-end="276" data-start="263">beautiful</strong> if the product of its digits is divisible by the sum of its digits.</p> <p data-end="529" data-start="448">Return the count of <strong>beautiful</strong> numbers between <code>l</code> and <code>r</code>, inclusive.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 10, r = 20</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The beautiful numbers in the range are 10 and 20.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">l = 1, r = 15</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>The beautiful numbers in the range are 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= l &lt;= r &lt; 10<sup>9</sup></code></li> </ul>
Hard
3.5K
17.1K
3,458
17,079
20.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int beautifulNumbers(int l, int r) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int beautifulNumbers(int l, int r) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def beautifulNumbers(self, l, r):\n \"\"\"\n :type l: int\n :type r: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def beautifulNumbers(self, l: int, r: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int beautifulNumbers(int l, int r) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int BeautifulNumbers(int l, int r) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} l\n * @param {number} r\n * @return {number}\n */\nvar beautifulNumbers = function(l, r) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function beautifulNumbers(l: number, r: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $l\n * @param Integer $r\n * @return Integer\n */\n function beautifulNumbers($l, $r) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func beautifulNumbers(_ l: Int, _ r: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun beautifulNumbers(l: Int, r: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int beautifulNumbers(int l, int r) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func beautifulNumbers(l int, r int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} l\n# @param {Integer} r\n# @return {Integer}\ndef beautiful_numbers(l, r)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def beautifulNumbers(l: Int, r: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn beautiful_numbers(l: i32, r: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (beautiful-numbers l r)\n (-> exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec beautiful_numbers(L :: integer(), R :: integer()) -> integer().\nbeautiful_numbers(L, R) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec beautiful_numbers(l :: integer, r :: integer) :: integer\n def beautiful_numbers(l, r) do\n \n end\nend"}]
10 20
{ "name": "beautifulNumbers", "params": [ { "name": "l", "type": "integer" }, { "type": "integer", "name": "r" } ], "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']
3,491
Phone Number Prefix
phone-number-prefix
null
Easy
716
952
716
952
75.2%
[]
[]
Algorithms
null
["1","2","4","3"]
{ "name": "phonePrefix", "params": [ { "name": "numbers", "type": "string[]" } ], "return": { "type": "boolean" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'String', 'Trie', 'Sorting']
3,492
Maximum Containers on a Ship
maximum-containers-on-a-ship
<p>You are given a positive integer <code>n</code> representing an <code>n x n</code> cargo deck on a ship. Each cell on the deck can hold one container with a weight of <strong>exactly</strong> <code>w</code>.</p> <p>However, the total weight of all containers, if loaded onto the deck, must not exceed the ship&#39;s maximum weight capacity, <code>maxWeight</code>.</p> <p>Return the <strong>maximum</strong> number of containers that can be loaded onto the ship.</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 = 2, w = 3, maxWeight = 15</span></p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation: </strong></p> <p>The deck has 4 cells, and each container weighs 3. The total weight of loading all containers is 12, which does not exceed <code>maxWeight</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, w = 5, maxWeight = 20</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation: </strong></p> <p>The deck has 9 cells, and each container weighs 5. The maximum number of containers that can be loaded without exceeding <code>maxWeight</code> is 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 1000</code></li> <li><code>1 &lt;= w &lt;= 1000</code></li> <li><code>1 &lt;= maxWeight &lt;= 10<sup>9</sup></code></li> </ul>
Easy
38.9K
52.3K
38,873
52,313
74.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxContainers(int n, int w, int maxWeight) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxContainers(int n, int w, int maxWeight) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxContainers(self, n, w, maxWeight):\n \"\"\"\n :type n: int\n :type w: int\n :type maxWeight: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxContainers(self, n: int, w: int, maxWeight: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxContainers(int n, int w, int maxWeight) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxContainers(int n, int w, int maxWeight) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} n\n * @param {number} w\n * @param {number} maxWeight\n * @return {number}\n */\nvar maxContainers = function(n, w, maxWeight) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxContainers(n: number, w: number, maxWeight: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer $w\n * @param Integer $maxWeight\n * @return Integer\n */\n function maxContainers($n, $w, $maxWeight) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxContainers(_ n: Int, _ w: Int, _ maxWeight: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxContainers(n: Int, w: Int, maxWeight: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxContainers(int n, int w, int maxWeight) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxContainers(n int, w int, maxWeight int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer} n\n# @param {Integer} w\n# @param {Integer} max_weight\n# @return {Integer}\ndef max_containers(n, w, max_weight)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxContainers(n: Int, w: Int, maxWeight: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_containers(n: i32, w: i32, max_weight: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-containers n w maxWeight)\n (-> exact-integer? exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_containers(N :: integer(), W :: integer(), MaxWeight :: integer()) -> integer().\nmax_containers(N, W, MaxWeight) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_containers(n :: integer, w :: integer, max_weight :: integer) :: integer\n def max_containers(n, w, max_weight) do\n \n end\nend"}]
2 3 15
{ "name": "maxContainers", "params": [ { "name": "n", "type": "integer" }, { "type": "integer", "name": "w" }, { "type": "integer", "name": "maxWeight" } ], "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']
3,493
Properties Graph
properties-graph
<p>You are given a 2D integer array <code>properties</code> having dimensions <code>n x m</code> and an integer <code>k</code>.</p> <p>Define a function <code>intersect(a, b)</code> that returns the <strong>number of distinct integers</strong> common to both arrays <code>a</code> and <code>b</code>.</p> <p>Construct an <strong>undirected</strong> graph where each index <code>i</code> corresponds to <code>properties[i]</code>. There is an edge between node <code>i</code> and node <code>j</code> if and only if <code>intersect(properties[i], properties[j]) &gt;= k</code>, where <code>i</code> and <code>j</code> are in the range <code>[0, n - 1]</code> and <code>i != j</code>.</p> <p>Return the number of <strong>connected components</strong> in the resulting graph.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">properties = [[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The graph formed has 3 connected components:</p> <p><img height="171" src="https://assets.leetcode.com/uploads/2025/02/27/image.png" width="279" /></p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">properties = [[1,2,3],[2,3,4],[4,3,5]], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The graph formed has 1 connected component:</p> <p><img alt="" src="https://assets.leetcode.com/uploads/2025/02/27/screenshot-from-2025-02-27-23-58-34.png" style="width: 219px; height: 171px;" /></p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">properties = [[1,1],[1,1]], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p><code>intersect(properties[0], properties[1]) = 1</code>, which is less than <code>k</code>. This means there is no edge between <code>properties[0]</code> and <code>properties[1]</code> in the graph.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == properties.length &lt;= 100</code></li> <li><code>1 &lt;= m == properties[i].length &lt;= 100</code></li> <li><code>1 &lt;= properties[i][j] &lt;= 100</code></li> <li><code>1 &lt;= k &lt;= m</code></li> </ul>
Medium
19.3K
41.7K
19,346
41,745
46.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int numberOfComponents(vector<vector<int>>& properties, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int numberOfComponents(int[][] properties, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def numberOfComponents(self, properties, k):\n \"\"\"\n :type properties: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def numberOfComponents(self, properties: List[List[int]], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int numberOfComponents(int** properties, int propertiesSize, int* propertiesColSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int NumberOfComponents(int[][] properties, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} properties\n * @param {number} k\n * @return {number}\n */\nvar numberOfComponents = function(properties, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function numberOfComponents(properties: number[][], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $properties\n * @param Integer $k\n * @return Integer\n */\n function numberOfComponents($properties, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func numberOfComponents(_ properties: [[Int]], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun numberOfComponents(properties: Array<IntArray>, k: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int numberOfComponents(List<List<int>> properties, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func numberOfComponents(properties [][]int, k int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} properties\n# @param {Integer} k\n# @return {Integer}\ndef number_of_components(properties, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def numberOfComponents(properties: Array[Array[Int]], k: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn number_of_components(properties: Vec<Vec<i32>>, k: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (number-of-components properties k)\n (-> (listof (listof exact-integer?)) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec number_of_components(Properties :: [[integer()]], K :: integer()) -> integer().\nnumber_of_components(Properties, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec number_of_components(properties :: [[integer]], k :: integer) :: integer\n def number_of_components(properties, k) do\n \n end\nend"}]
[[1,2],[1,1],[3,4],[4,5],[5,6],[7,7]] 1
{ "name": "numberOfComponents", "params": [ { "name": "properties", "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', 'Depth-First Search', 'Breadth-First Search', 'Union Find', 'Graph']
3,494
Find the Minimum Amount of Time to Brew Potions
find-the-minimum-amount-of-time-to-brew-potions
<p>You are given two integer arrays, <code>skill</code> and <code><font face="monospace">mana</font></code>, of length <code>n</code> and <code>m</code>, respectively.</p> <p>In a laboratory, <code>n</code> wizards must brew <code>m</code> potions <em>in order</em>. Each potion has a mana capacity <code>mana[j]</code> and <strong>must</strong> pass through <strong>all</strong> the wizards sequentially to be brewed properly. The time taken by the <code>i<sup>th</sup></code> wizard on the <code>j<sup>th</sup></code> potion is <code>time<sub>ij</sub> = skill[i] * mana[j]</code>.</p> <p>Since the brewing process is delicate, a potion <strong>must</strong> be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be <em>synchronized</em> so that each wizard begins working on a potion <strong>exactly</strong> when it arrives. ​</p> <p>Return the <strong>minimum</strong> amount of time required for the potions to be brewed properly.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">skill = [1,5,2,4], mana = [5,1,4,2]</span></p> <p><strong>Output:</strong> <span class="example-io">110</span></p> <p><strong>Explanation:</strong></p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;">Potion Number</th> <th style="border: 1px solid black;">Start time</th> <th style="border: 1px solid black;">Wizard 0 done by</th> <th style="border: 1px solid black;">Wizard 1 done by</th> <th style="border: 1px solid black;">Wizard 2 done by</th> <th style="border: 1px solid black;">Wizard 3 done by</th> </tr> <tr> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;">0</td> <td style="border: 1px solid black;">5</td> <td style="border: 1px solid black;">30</td> <td style="border: 1px solid black;">40</td> <td style="border: 1px solid black;">60</td> </tr> <tr> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">52</td> <td style="border: 1px solid black;">53</td> <td style="border: 1px solid black;">58</td> <td style="border: 1px solid black;">60</td> <td style="border: 1px solid black;">64</td> </tr> <tr> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">54</td> <td style="border: 1px solid black;">58</td> <td style="border: 1px solid black;">78</td> <td style="border: 1px solid black;">86</td> <td style="border: 1px solid black;">102</td> </tr> <tr> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">86</td> <td style="border: 1px solid black;">88</td> <td style="border: 1px solid black;">98</td> <td style="border: 1px solid black;">102</td> <td style="border: 1px solid black;">110</td> </tr> </tbody> </table> <p>As an example for why wizard 0 cannot start working on the 1<sup>st</sup> potion before time <code>t = 52</code>, consider the case where the wizards started preparing the 1<sup>st</sup> potion at time <code>t = 50</code>. At time <code>t = 58</code>, wizard 2 is done with the 1<sup>st</sup> potion, but wizard 3 will still be working on the 0<sup>th</sup> potion till time <code>t = 60</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">skill = [1,1,1], mana = [1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <ol> <li>Preparation of the 0<sup>th</sup> potion begins at time <code>t = 0</code>, and is completed by time <code>t = 3</code>.</li> <li>Preparation of the 1<sup>st</sup> potion begins at time <code>t = 1</code>, and is completed by time <code>t = 4</code>.</li> <li>Preparation of the 2<sup>nd</sup> potion begins at time <code>t = 2</code>, and is completed by time <code>t = 5</code>.</li> </ol> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">skill = [1,2,3,4], mana = [1,2]</span></p> <p><strong>Output:</strong> 21</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skill.length</code></li> <li><code>m == mana.length</code></li> <li><code>1 &lt;= n, m &lt;= 5000</code></li> <li><code>1 &lt;= mana[i], skill[i] &lt;= 5000</code></li> </ul>
Medium
9.1K
27.6K
9,124
27,637
33.0%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minTime(vector<int>& skill, vector<int>& mana) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minTime(int[] skill, int[] mana) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minTime(self, skill, mana):\n \"\"\"\n :type skill: List[int]\n :type mana: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minTime(self, skill: List[int], mana: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minTime(int* skill, int skillSize, int* mana, int manaSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinTime(int[] skill, int[] mana) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} skill\n * @param {number[]} mana\n * @return {number}\n */\nvar minTime = function(skill, mana) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minTime(skill: number[], mana: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $skill\n * @param Integer[] $mana\n * @return Integer\n */\n function minTime($skill, $mana) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minTime(_ skill: [Int], _ mana: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minTime(skill: IntArray, mana: IntArray): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minTime(List<int> skill, List<int> mana) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minTime(skill []int, mana []int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} skill\n# @param {Integer[]} mana\n# @return {Integer}\ndef min_time(skill, mana)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minTime(skill: Array[Int], mana: Array[Int]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_time(skill: Vec<i32>, mana: Vec<i32>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-time skill mana)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_time(Skill :: [integer()], Mana :: [integer()]) -> integer().\nmin_time(Skill, Mana) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_time(skill :: [integer], mana :: [integer]) :: integer\n def min_time(skill, mana) do\n \n end\nend"}]
[1,5,2,4] [5,1,4,2]
{ "name": "minTime", "params": [ { "name": "skill", "type": "integer[]" }, { "type": "integer[]", "name": "mana" } ], "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', 'Simulation', 'Prefix Sum']
3,495
Minimum Operations to Make Array Elements Zero
minimum-operations-to-make-array-elements-zero
<p>You are given a 2D array <code>queries</code>, where <code>queries[i]</code> is of the form <code>[l, r]</code>. Each <code>queries[i]</code> defines an array of integers <code>nums</code> consisting of elements ranging from <code>l</code> to <code>r</code>, both <strong>inclusive</strong>.</p> <p>In one operation, you can:</p> <ul> <li>Select two integers <code>a</code> and <code>b</code> from the array.</li> <li>Replace them with <code>floor(a / 4)</code> and <code>floor(b / 4)</code>.</li> </ul> <p>Your task is to determine the <strong>minimum</strong> number of operations required to reduce all elements of the array to zero for each query. Return the sum of the results for all queries.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">queries = [[1,2],[2,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>For <code>queries[0]</code>:</p> <ul> <li>The initial array is <code>nums = [1, 2]</code>.</li> <li>In the first operation, select <code>nums[0]</code> and <code>nums[1]</code>. The array becomes <code>[0, 0]</code>.</li> <li>The minimum number of operations required is 1.</li> </ul> <p>For <code>queries[1]</code>:</p> <ul> <li>The initial array is <code>nums = [2, 3, 4]</code>.</li> <li>In the first operation, select <code>nums[0]</code> and <code>nums[2]</code>. The array becomes <code>[0, 3, 1]</code>.</li> <li>In the second operation, select <code>nums[1]</code> and <code>nums[2]</code>. The array becomes <code>[0, 0, 0]</code>.</li> <li>The minimum number of operations required is 2.</li> </ul> <p>The output is <code>1 + 2 = 3</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">queries = [[2,6]]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>For <code>queries[0]</code>:</p> <ul> <li>The initial array is <code>nums = [2, 3, 4, 5, 6]</code>.</li> <li>In the first operation, select <code>nums[0]</code> and <code>nums[3]</code>. The array becomes <code>[0, 3, 4, 1, 6]</code>.</li> <li>In the second operation, select <code>nums[2]</code> and <code>nums[4]</code>. The array becomes <code>[0, 3, 1, 1, 1]</code>.</li> <li>In the third operation, select <code>nums[1]</code> and <code>nums[2]</code>. The array becomes <code>[0, 0, 0, 1, 1]</code>.</li> <li>In the fourth operation, select <code>nums[3]</code> and <code>nums[4]</code>. The array becomes <code>[0, 0, 0, 0, 0]</code>.</li> <li>The minimum number of operations required is 4.</li> </ul> <p>The output is 4.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i].length == 2</code></li> <li><code>queries[i] == [l, r]</code></li> <li><code>1 &lt;= l &lt; r &lt;= 10<sup>9</sup></code></li> </ul>
Hard
5.1K
16.8K
5,116
16,821
30.4%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minOperations(vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minOperations(int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minOperations(self, queries):\n \"\"\"\n :type queries: List[List[int]]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minOperations(self, queries: List[List[int]]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minOperations(int** queries, int queriesSize, int* queriesColSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinOperations(int[][] queries) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[][]} queries\n * @return {number}\n */\nvar minOperations = function(queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minOperations(queries: number[][]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[][] $queries\n * @return Integer\n */\n function minOperations($queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minOperations(_ queries: [[Int]]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minOperations(queries: Array<IntArray>): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minOperations(List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minOperations(queries [][]int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[][]} queries\n# @return {Integer}\ndef min_operations(queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minOperations(queries: Array[Array[Int]]): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_operations(queries: Vec<Vec<i32>>) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-operations queries)\n (-> (listof (listof exact-integer?)) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_operations(Queries :: [[integer()]]) -> integer().\nmin_operations(Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_operations(queries :: [[integer]]) :: integer\n def min_operations(queries) do\n \n end\nend"}]
[[1,2],[2,4]]
{ "name": "minOperations", "params": [ { "name": "queries", "type": "integer[][]" } ], "return": { "type": "long" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array', 'Math', 'Bit Manipulation']
3,496
Maximize Score After Pair Deletions
maximize-score-after-pair-deletions
null
Medium
324
517
324
517
62.7%
[]
[]
Algorithms
null
[2,4,1]
{ "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']
3,497
Analyze Subscription Conversion
analyze-subscription-conversion
<p>Table: <code>UserActivity</code></p> <pre> +------------------+---------+ | Column Name | Type | +------------------+---------+ | user_id | int | | activity_date | date | | activity_type | varchar | | activity_duration| int | +------------------+---------+ (user_id, activity_date, activity_type) is the unique key for this table. activity_type is one of (&#39;free_trial&#39;, &#39;paid&#39;, &#39;cancelled&#39;). activity_duration is the number of minutes the user spent on the platform that day. Each row represents a user&#39;s activity on a specific date. </pre> <p>A subscription service wants to analyze user behavior patterns. The company offers a <code>7</code>-day <strong>free trial</strong>, after which users can subscribe to a <strong>paid plan</strong> or <strong>cancel</strong>. Write a solution to:</p> <ol> <li>Find users who converted from free trial to paid subscription</li> <li>Calculate each user&#39;s <strong>average daily activity duration</strong> during their <strong>free trial</strong> period (rounded to <code>2</code> decimal places)</li> <li>Calculate each user&#39;s <strong>average daily activity duration</strong> during their <strong>paid</strong> subscription period (rounded to <code>2</code> decimal places)</li> </ol> <p>Return <em>the result table ordered by </em><code>user_id</code><em> in <strong>ascending</strong> order</em>.</p> <p>The result format is in the following example.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>UserActivity table:</p> <pre class="example-io"> +---------+---------------+---------------+-------------------+ | user_id | activity_date | activity_type | activity_duration | +---------+---------------+---------------+-------------------+ | 1 | 2023-01-01 | free_trial | 45 | | 1 | 2023-01-02 | free_trial | 30 | | 1 | 2023-01-05 | free_trial | 60 | | 1 | 2023-01-10 | paid | 75 | | 1 | 2023-01-12 | paid | 90 | | 1 | 2023-01-15 | paid | 65 | | 2 | 2023-02-01 | free_trial | 55 | | 2 | 2023-02-03 | free_trial | 25 | | 2 | 2023-02-07 | free_trial | 50 | | 2 | 2023-02-10 | cancelled | 0 | | 3 | 2023-03-05 | free_trial | 70 | | 3 | 2023-03-06 | free_trial | 60 | | 3 | 2023-03-08 | free_trial | 80 | | 3 | 2023-03-12 | paid | 50 | | 3 | 2023-03-15 | paid | 55 | | 3 | 2023-03-20 | paid | 85 | | 4 | 2023-04-01 | free_trial | 40 | | 4 | 2023-04-03 | free_trial | 35 | | 4 | 2023-04-05 | paid | 45 | | 4 | 2023-04-07 | cancelled | 0 | +---------+---------------+---------------+-------------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +---------+--------------------+-------------------+ | user_id | trial_avg_duration | paid_avg_duration | +---------+--------------------+-------------------+ | 1 | 45.00 | 76.67 | | 3 | 70.00 | 63.33 | | 4 | 37.50 | 45.00 | +---------+--------------------+-------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>User 1:</strong> <ul> <li>Had 3 days of free trial with durations of 45, 30, and 60 minutes.</li> <li>Average trial duration: (45 + 30 + 60) / 3 = 45.00 minutes.</li> <li>Had 3 days of paid subscription with durations of 75, 90, and 65 minutes.</li> <li>Average paid duration: (75 + 90 + 65) / 3 = 76.67 minutes.</li> </ul> </li> <li><strong>User 2:</strong> <ul> <li>Had 3 days of free trial with durations of 55, 25, and 50 minutes.</li> <li>Average trial duration: (55 + 25 + 50) / 3 = 43.33 minutes.</li> <li>Did not convert to a paid subscription (only had free_trial and cancelled activities).</li> <li>Not included in the output because they didn&#39;t convert to paid.</li> </ul> </li> <li><strong>User 3:</strong> <ul> <li>Had 3 days of free trial with durations of 70, 60, and 80 minutes.</li> <li>Average trial duration: (70 + 60 + 80) / 3 = 70.00 minutes.</li> <li>Had 3 days of paid subscription with durations of 50, 55, and 85 minutes.</li> <li>Average paid duration: (50 + 55 + 85) / 3 = 63.33 minutes.</li> </ul> </li> <li><strong>User 4:</strong> <ul> <li>Had 2 days of free trial with durations of 40 and 35 minutes.</li> <li>Average trial duration: (40 + 35) / 2 = 37.50 minutes.</li> <li>Had 1 day of paid subscription with duration of 45 minutes before cancelling.</li> <li>Average paid duration: 45.00 minutes.</li> </ul> </li> </ul> <p>The result table only includes users who converted from free trial to paid subscription (users 1, 3, and 4), and is ordered by user_id in ascending order.</p> </div>
Medium
1.8K
2.2K
1,831
2,244
81.6%
[]
['CREATE TABLE if not exists UserActivity (\n user_id INT,\n activity_date DATE,\n activity_type VARCHAR(20),\n activity_duration INT\n)', 'Truncate table UserActivity', "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-01', 'free_trial', '45')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-02', 'free_trial', '30')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-05', 'free_trial', '60')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-10', 'paid', '75')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-12', 'paid', '90')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('1', '2023-01-15', 'paid', '65')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('2', '2023-02-01', 'free_trial', '55')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('2', '2023-02-03', 'free_trial', '25')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('2', '2023-02-07', 'free_trial', '50')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('2', '2023-02-10', 'cancelled', '0')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-05', 'free_trial', '70')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-06', 'free_trial', '60')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-08', 'free_trial', '80')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-12', 'paid', '50')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-15', 'paid', '55')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('3', '2023-03-20', 'paid', '85')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('4', '2023-04-01', 'free_trial', '40')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('4', '2023-04-03', 'free_trial', '35')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('4', '2023-04-05', 'paid', '45')", "insert into UserActivity (user_id, activity_date, activity_type, activity_duration) values ('4', '2023-04-07', 'cancelled', '0')"]
Database
[{"value": "mysql", "text": "MySQL", "defaultCode": "# Write your MySQL query statement below\n"}, {"value": "mssql", "text": "MS SQL Server", "defaultCode": "/* Write your T-SQL query statement below */\n"}, {"value": "oraclesql", "text": "Oracle", "defaultCode": "/* Write your PL/SQL query statement below */\n"}, {"value": "pythondata", "text": "Pandas", "defaultCode": "import pandas as pd\n\ndef analyze_subscription_conversion(user_activity: pd.DataFrame) -> pd.DataFrame:\n "}, {"value": "postgresql", "text": "PostgreSQL", "defaultCode": "-- Write your PostgreSQL query statement below\n"}]
{"headers":{"UserActivity":["user_id","activity_date","activity_type","activity_duration"]},"rows":{"UserActivity":[[1,"2023-01-01","free_trial",45],[1,"2023-01-02","free_trial",30],[1,"2023-01-05","free_trial",60],[1,"2023-01-10","paid",75],[1,"2023-01-12","paid",90],[1,"2023-01-15","paid",65],[2,"2023-02-01","free_trial",55],[2,"2023-02-03","free_trial",25],[2,"2023-02-07","free_trial",50],[2,"2023-02-10","cancelled",0],[3,"2023-03-05","free_trial",70],[3,"2023-03-06","free_trial",60],[3,"2023-03-08","free_trial",80],[3,"2023-03-12","paid",50],[3,"2023-03-15","paid",55],[3,"2023-03-20","paid",85],[4,"2023-04-01","free_trial",40],[4,"2023-04-03","free_trial",35],[4,"2023-04-05","paid",45],[4,"2023-04-07","cancelled",0]]}}
{"mysql": ["CREATE TABLE if not exists UserActivity (\n user_id INT,\n activity_date DATE,\n activity_type VARCHAR(20),\n activity_duration INT\n)"], "mssql": ["CREATE TABLE UserActivity (\n user_id INT,\n activity_date DATE,\n activity_type VARCHAR(20),\n activity_duration INT\n)"], "oraclesql": ["CREATE TABLE UserActivity (\n user_id NUMBER,\n activity_date DATE,\n activity_type VARCHAR2(20),\n activity_duration NUMBER\n)", "ALTER SESSION SET nls_date_format='YYYY-MM-DD'"], "database": true, "name": "analyze_subscription_conversion", "postgresql": ["CREATE TABLE IF NOT EXISTS UserActivity (\n user_id INTEGER,\n activity_date DATE,\n activity_type VARCHAR(20),\n activity_duration INTEGER\n);\n"], "pythondata": ["UserActivity = pd.DataFrame({\n 'user_id': pd.Series(dtype='int'),\n 'activity_date': pd.Series(dtype='datetime64[ns]'),\n 'activity_type': pd.Series(dtype='str'),\n 'activity_duration': pd.Series(dtype='int')\n})"], "database_schema": {"UserActivity": {"user_id": "INT", "activity_date": "DATE", "activity_type": "VARCHAR(20)", "activity_duration": "INT"}}}
{"mysql": ["MySQL", "<p><code>MySQL 8.0</code>.</p>"], "mssql": ["MS SQL Server", "<p><code>mssql server 2019</code>.</p>"], "oraclesql": ["Oracle", "<p><code>Oracle Sql 11.2</code>.</p>"], "pythondata": ["Pandas", "<p>Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4</p>"], "postgresql": ["PostgreSQL", "<p>PostgreSQL 16</p>"]}
['Database']
3,498
Reverse Degree of a String
reverse-degree-of-a-string
<p>Given a string <code>s</code>, calculate its <strong>reverse degree</strong>.</p> <p>The <strong>reverse degree</strong> is calculated as follows:</p> <ol> <li>For each character, multiply its position in the <em>reversed</em> alphabet (<code>&#39;a&#39;</code> = 26, <code>&#39;b&#39;</code> = 25, ..., <code>&#39;z&#39;</code> = 1) with its position in the string <strong>(1-indexed)</strong>.</li> <li>Sum these products for all characters in the string.</li> </ol> <p>Return the <strong>reverse degree</strong> of <code>s</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">s = &quot;abc&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">148</span></p> <p><strong>Explanation:</strong></p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;">Letter</th> <th style="border: 1px solid black;">Index in Reversed Alphabet</th> <th style="border: 1px solid black;">Index in String</th> <th style="border: 1px solid black;">Product</th> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;a&#39;</code></td> <td style="border: 1px solid black;">26</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">26</td> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;b&#39;</code></td> <td style="border: 1px solid black;">25</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">50</td> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;c&#39;</code></td> <td style="border: 1px solid black;">24</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">72</td> </tr> </tbody> </table> <p>The reversed degree is <code>26 + 50 + 72 = 148</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;zaza&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">160</span></p> <p><strong>Explanation:</strong></p> <table style="border: 1px solid black;"> <tbody> <tr> <th style="border: 1px solid black;">Letter</th> <th style="border: 1px solid black;">Index in Reversed Alphabet</th> <th style="border: 1px solid black;">Index in String</th> <th style="border: 1px solid black;">Product</th> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;z&#39;</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">1</td> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;a&#39;</code></td> <td style="border: 1px solid black;">26</td> <td style="border: 1px solid black;">2</td> <td style="border: 1px solid black;">52</td> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;z&#39;</code></td> <td style="border: 1px solid black;">1</td> <td style="border: 1px solid black;">3</td> <td style="border: 1px solid black;">3</td> </tr> <tr> <td style="border: 1px solid black;"><code>&#39;a&#39;</code></td> <td style="border: 1px solid black;">26</td> <td style="border: 1px solid black;">4</td> <td style="border: 1px solid black;">104</td> </tr> </tbody> </table> <p>The reverse degree is <code>1 + 52 + 3 + 104 = 160</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> contains only lowercase English letters.</li> </ul>
Easy
33.2K
37.4K
33,165
37,359
88.8%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int reverseDegree(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int reverseDegree(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def reverseDegree(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def reverseDegree(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int reverseDegree(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int ReverseDegree(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar reverseDegree = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function reverseDegree(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function reverseDegree($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func reverseDegree(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun reverseDegree(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int reverseDegree(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func reverseDegree(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef reverse_degree(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def reverseDegree(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn reverse_degree(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (reverse-degree s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec reverse_degree(S :: unicode:unicode_binary()) -> integer().\nreverse_degree(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec reverse_degree(s :: String.t) :: integer\n def reverse_degree(s) do\n \n end\nend"}]
"abc"
{ "name": "reverseDegree", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "integer" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['String', 'Simulation']
3,499
Maximize Active Section with Trade I
maximize-active-section-with-trade-i
<p>You are given a binary string <code>s</code> of length <code>n</code>, where:</p> <ul> <li><code>&#39;1&#39;</code> represents an <strong>active</strong> section.</li> <li><code>&#39;0&#39;</code> represents an <strong>inactive</strong> section.</li> </ul> <p>You can perform <strong>at most one trade</strong> to maximize the number of active sections in <code>s</code>. In a trade, you:</p> <ul> <li>Convert a contiguous block of <code>&#39;1&#39;</code>s that is surrounded by <code>&#39;0&#39;</code>s to all <code>&#39;0&#39;</code>s.</li> <li>Afterward, convert a contiguous block of <code>&#39;0&#39;</code>s that is surrounded by <code>&#39;1&#39;</code>s to all <code>&#39;1&#39;</code>s.</li> </ul> <p>Return the <strong>maximum</strong> number of active sections in <code>s</code> after making the optimal trade.</p> <p><strong>Note:</strong> Treat <code>s</code> as if it is <strong>augmented</strong> with a <code>&#39;1&#39;</code> at both ends, forming <code>t = &#39;1&#39; + s + &#39;1&#39;</code>. The augmented <code>&#39;1&#39;</code>s <strong>do not</strong> contribute to the final count.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;01&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0100&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <ul> <li>String <code>&quot;0100&quot;</code> &rarr; Augmented to <code>&quot;101001&quot;</code>.</li> <li>Choose <code>&quot;0100&quot;</code>, convert <code>&quot;10<u><strong>1</strong></u>001&quot;</code> &rarr; <code>&quot;1<u><strong>0000</strong></u>1&quot;</code> &rarr; <code>&quot;1<u><strong>1111</strong></u>1&quot;</code>.</li> <li>The final string without augmentation is <code>&quot;1111&quot;</code>. The maximum number of active sections is 4.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1000100&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">7</span></p> <p><strong>Explanation:</strong></p> <ul> <li>String <code>&quot;1000100&quot;</code> &rarr; Augmented to <code>&quot;110001001&quot;</code>.</li> <li>Choose <code>&quot;000100&quot;</code>, convert <code>&quot;11000<u><strong>1</strong></u>001&quot;</code> &rarr; <code>&quot;11<u><strong>000000</strong></u>1&quot;</code> &rarr; <code>&quot;11<u><strong>111111</strong></u>1&quot;</code>.</li> <li>The final string without augmentation is <code>&quot;1111111&quot;</code>. The maximum number of active sections is 7.</li> </ul> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;01010&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <ul> <li>String <code>&quot;01010&quot;</code> &rarr; Augmented to <code>&quot;1010101&quot;</code>.</li> <li>Choose <code>&quot;010&quot;</code>, convert <code>&quot;10<u><strong>1</strong></u>0101&quot;</code> &rarr; <code>&quot;1<u><strong>000</strong></u>101&quot;</code> &rarr; <code>&quot;1<u><strong>111</strong></u>101&quot;</code>.</li> <li>The final string without augmentation is <code>&quot;11110&quot;</code>. The maximum number of active sections is 4.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code></li> </ul>
Medium
11.5K
38.3K
11,493
38,342
30.0%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxActiveSectionsAfterTrade(string s) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxActiveSectionsAfterTrade(String s) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxActiveSectionsAfterTrade(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxActiveSectionsAfterTrade(self, s: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxActiveSectionsAfterTrade(char* s) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxActiveSectionsAfterTrade(string s) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {string} s\n * @return {number}\n */\nvar maxActiveSectionsAfterTrade = function(s) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxActiveSectionsAfterTrade(s: string): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param String $s\n * @return Integer\n */\n function maxActiveSectionsAfterTrade($s) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxActiveSectionsAfterTrade(_ s: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxActiveSectionsAfterTrade(s: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxActiveSectionsAfterTrade(String s) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxActiveSectionsAfterTrade(s string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @return {Integer}\ndef max_active_sections_after_trade(s)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxActiveSectionsAfterTrade(s: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_active_sections_after_trade(s: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-active-sections-after-trade s)\n (-> string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_active_sections_after_trade(S :: unicode:unicode_binary()) -> integer().\nmax_active_sections_after_trade(S) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_active_sections_after_trade(s :: String.t) :: integer\n def max_active_sections_after_trade(s) do\n \n end\nend"}]
"01"
{ "name": "maxActiveSectionsAfterTrade", "params": [ { "name": "s", "type": "string" } ], "return": { "type": "integer" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['String', 'Enumeration']
3,500
Minimum Cost to Divide Array Into Subarrays
minimum-cost-to-divide-array-into-subarrays
<p>You are given two integer arrays, <code>nums</code> and <code>cost</code>, of the same size, and an integer <code>k</code>.</p> <p>You can divide <code>nums</code> into <span data-keyword="subarray-nonempty">subarrays</span>. The cost of the <code>i<sup>th</sup></code> subarray consisting of elements <code>nums[l..r]</code> is:</p> <ul> <li><code>(nums[0] + nums[1] + ... + nums[r] + k * i) * (cost[l] + cost[l + 1] + ... + cost[r])</code>.</li> </ul> <p><strong>Note</strong> that <code>i</code> represents the order of the subarray: 1 for the first subarray, 2 for the second, and so on.</p> <p>Return the <strong>minimum</strong> total cost possible from any valid division.</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 = [3,1,4], cost = [4,6,6], k = 1</span></p> <p><strong>Output:</strong> <span class="example-io">110</span></p> <p><strong>Explanation:</strong></p> The minimum total cost possible can be achieved by dividing <code>nums</code> into subarrays <code>[3, 1]</code> and <code>[4]</code>. <ul> <li>The cost of the first subarray <code>[3,1]</code> is <code>(3 + 1 + 1 * 1) * (4 + 6) = 50</code>.</li> <li>The cost of the second subarray <code>[4]</code> is <code>(3 + 1 + 4 + 1 * 2) * 6 = 60</code>.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,8,5,1,14,2,2,12,1], cost = [7,2,8,4,2,2,1,1,2], k = 7</span></p> <p><strong>Output:</strong> 985</p> <p><strong>Explanation:</strong></p> The minimum total cost possible can be achieved by dividing <code>nums</code> into subarrays <code>[4, 8, 5, 1]</code>, <code>[14, 2, 2]</code>, and <code>[12, 1]</code>. <ul> <li>The cost of the first subarray <code>[4, 8, 5, 1]</code> is <code>(4 + 8 + 5 + 1 + 7 * 1) * (7 + 2 + 8 + 4) = 525</code>.</li> <li>The cost of the second subarray <code>[14, 2, 2]</code> is <code>(4 + 8 + 5 + 1 + 14 + 2 + 2 + 7 * 2) * (2 + 2 + 1) = 250</code>.</li> <li>The cost of the third subarray <code>[12, 1]</code> is <code>(4 + 8 + 5 + 1 + 14 + 2 + 2 + 12 + 1 + 7 * 3) * (1 + 2) = 210</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 1000</code></li> <li><code>cost.length == nums.length</code></li> <li><code>1 &lt;= nums[i], cost[i] &lt;= 1000</code></li> <li><code>1 &lt;= k &lt;= 1000</code></li> </ul>
Hard
2.6K
11.5K
2,556
11,480
22.3%
['minimum-cost-to-split-an-array']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, vector<int>& cost, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minimumCost(int[] nums, int[] cost, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumCost(self, nums, cost, k):\n \"\"\"\n :type nums: List[int]\n :type cost: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumCost(self, nums: List[int], cost: List[int], k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minimumCost(int* nums, int numsSize, int* cost, int costSize, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinimumCost(int[] nums, int[] cost, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number[]} cost\n * @param {number} k\n * @return {number}\n */\nvar minimumCost = function(nums, cost, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumCost(nums: number[], cost: number[], k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer[] $cost\n * @param Integer $k\n * @return Integer\n */\n function minimumCost($nums, $cost, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumCost(_ nums: [Int], _ cost: [Int], _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumCost(nums: IntArray, cost: IntArray, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumCost(List<int> nums, List<int> cost, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumCost(nums []int, cost []int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer[]} cost\n# @param {Integer} k\n# @return {Integer}\ndef minimum_cost(nums, cost, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumCost(nums: Array[Int], cost: Array[Int], k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_cost(nums: Vec<i32>, cost: Vec<i32>, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-cost nums cost k)\n (-> (listof exact-integer?) (listof exact-integer?) exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_cost(Nums :: [integer()], Cost :: [integer()], K :: integer()) -> integer().\nminimum_cost(Nums, Cost, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_cost(nums :: [integer], cost :: [integer], k :: integer) :: integer\n def minimum_cost(nums, cost, k) do\n \n end\nend"}]
[3,1,4] [4,6,6] 1
{ "name": "minimumCost", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer[]", "name": "cost" }, { "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', 'Dynamic Programming', 'Prefix Sum']
3,501
Maximize Active Section with Trade II
maximize-active-section-with-trade-ii
<p>You are given a binary string <code>s</code> of length <code>n</code>, where:</p> <ul> <li><code>&#39;1&#39;</code> represents an <strong>active</strong> section.</li> <li><code>&#39;0&#39;</code> represents an <strong>inactive</strong> section.</li> </ul> <p>You can perform <strong>at most one trade</strong> to maximize the number of active sections in <code>s</code>. In a trade, you:</p> <ul> <li>Convert a contiguous block of <code>&#39;1&#39;</code>s that is surrounded by <code>&#39;0&#39;</code>s to all <code>&#39;0&#39;</code>s.</li> <li>Afterward, convert a contiguous block of <code>&#39;0&#39;</code>s that is surrounded by <code>&#39;1&#39;</code>s to all <code>&#39;1&#39;</code>s.</li> </ul> <p>Additionally, you are given a <strong>2D array</strong> <code>queries</code>, where <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents a <span data-keyword="substring-nonempty">substring</span> <code>s[l<sub>i</sub>...r<sub>i</sub>]</code>.</p> <p>For each query, determine the <strong>maximum</strong> possible number of active sections in <code>s</code> after making the optimal trade on the substring <code>s[l<sub>i</sub>...r<sub>i</sub>]</code>.</p> <p>Return an array <code>answer</code>, where <code>answer[i]</code> is the result for <code>queries[i]</code>.</p> <p><strong>Note</strong></p> <ul> <li>For each query, treat <code>s[l<sub>i</sub>...r<sub>i</sub>]</code> as if it is <strong>augmented</strong> with a <code>&#39;1&#39;</code> at both ends, forming <code>t = &#39;1&#39; + s[l<sub>i</sub>...r<sub>i</sub>] + &#39;1&#39;</code>. The augmented <code>&#39;1&#39;</code>s <strong>do not</strong> contribute to the final count.</li> <li>The queries are independent of each other.</li> </ul> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;01&quot;, queries = [[0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">[1]</span></p> <p><strong>Explanation:</strong></p> <p>Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 1.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0100&quot;, queries = [[0,3],[0,2],[1,3],[2,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">[4,3,1,1]</span></p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Query <code>[0, 3]</code> &rarr; Substring <code>&quot;0100&quot;</code> &rarr; Augmented to <code>&quot;101001&quot;</code><br /> Choose <code>&quot;0100&quot;</code>, convert <code>&quot;0100&quot;</code> &rarr; <code>&quot;0000&quot;</code> &rarr; <code>&quot;1111&quot;</code>.<br /> The final string without augmentation is <code>&quot;1111&quot;</code>. The maximum number of active sections is 4.</p> </li> <li> <p>Query <code>[0, 2]</code> &rarr; Substring <code>&quot;010&quot;</code> &rarr; Augmented to <code>&quot;10101&quot;</code><br /> Choose <code>&quot;010&quot;</code>, convert <code>&quot;010&quot;</code> &rarr; <code>&quot;000&quot;</code> &rarr; <code>&quot;111&quot;</code>.<br /> The final string without augmentation is <code>&quot;1110&quot;</code>. The maximum number of active sections is 3.</p> </li> <li> <p>Query <code>[1, 3]</code> &rarr; Substring <code>&quot;100&quot;</code> &rarr; Augmented to <code>&quot;11001&quot;</code><br /> Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 1.</p> </li> <li> <p>Query <code>[2, 3]</code> &rarr; Substring <code>&quot;00&quot;</code> &rarr; Augmented to <code>&quot;1001&quot;</code><br /> Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 1.</p> </li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1000100&quot;, queries = [[1,5],[0,6],[0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[6,7,2]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="383" data-start="217"> <p data-end="383" data-start="219">Query <code>[1, 5]</code> &rarr; Substring <code data-end="255" data-start="246">&quot;00010&quot;</code> &rarr; Augmented to <code data-end="282" data-start="271">&quot;1000101&quot;</code><br data-end="285" data-start="282" /> Choose <code data-end="303" data-start="294">&quot;00010&quot;</code>, convert <code data-end="322" data-start="313">&quot;00010&quot;</code> &rarr; <code data-end="322" data-start="313">&quot;00000&quot;</code> &rarr; <code data-end="334" data-start="325">&quot;11111&quot;</code>.<br /> The final string without augmentation is <code data-end="404" data-start="396">&quot;1111110&quot;</code>. The maximum number of active sections is 6.</p> </li> <li data-end="561" data-start="385"> <p data-end="561" data-start="387">Query <code>[0, 6]</code> &rarr; Substring <code data-end="425" data-start="414">&quot;1000100&quot;</code> &rarr; Augmented to <code data-end="454" data-start="441">&quot;110001001&quot;</code><br data-end="457" data-start="454" /> Choose <code data-end="477" data-start="466">&quot;000100&quot;</code>, convert <code data-end="498" data-start="487">&quot;000100&quot;</code> &rarr; <code data-end="498" data-start="487">&quot;000000&quot;</code> &rarr; <code data-end="512" data-start="501">&quot;111111&quot;</code>.<br /> The final string without augmentation is <code data-end="404" data-start="396">&quot;1111111&quot;</code>. The maximum number of active sections is 7.</p> </li> <li data-end="741" data-start="563"> <p data-end="741" data-start="565">Query <code>[0, 4]</code> &rarr; Substring <code data-end="601" data-start="592">&quot;10001&quot;</code> &rarr; Augmented to <code data-end="627" data-start="617">&quot;1100011&quot;</code><br data-end="630" data-start="627" /> Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 2.</p> </li> </ul> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;01010&quot;, queries = [[0,3],[1,4],[1,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">[4,4,2]</span></p> <p><strong>Explanation:</strong></p> <ul> <li> <p>Query <code>[0, 3]</code> &rarr; Substring <code>&quot;0101&quot;</code> &rarr; Augmented to <code>&quot;101011&quot;</code><br /> Choose <code>&quot;010&quot;</code>, convert <code>&quot;010&quot;</code> &rarr; <code>&quot;000&quot;</code> &rarr; <code>&quot;111&quot;</code>.<br /> The final string without augmentation is <code>&quot;11110&quot;</code>. The maximum number of active sections is 4.</p> </li> <li> <p>Query <code>[1, 4]</code> &rarr; Substring <code>&quot;1010&quot;</code> &rarr; Augmented to <code>&quot;110101&quot;</code><br /> Choose <code>&quot;010&quot;</code>, convert <code>&quot;010&quot;</code> &rarr; <code>&quot;000&quot;</code> &rarr; <code>&quot;111&quot;</code>.<br /> The final string without augmentation is <code>&quot;01111&quot;</code>. The maximum number of active sections is 4.</p> </li> <li> <p>Query <code>[1, 3]</code> &rarr; Substring <code>&quot;101&quot;</code> &rarr; Augmented to <code>&quot;11011&quot;</code><br /> Because there is no block of <code>&#39;1&#39;</code>s surrounded by <code>&#39;0&#39;</code>s, no valid trade is possible. The maximum number of active sections is 2.</p> </li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == s.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li> <li><code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code></li> <li><code>0 &lt;= l<sub>i</sub> &lt;= r<sub>i</sub> &lt; n</code></li> </ul>
Hard
786
4.1K
786
4,087
19.2%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> maxActiveSectionsAfterTrade(string s, vector<vector<int>>& queries) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public List<Integer> maxActiveSectionsAfterTrade(String s, int[][] queries) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxActiveSectionsAfterTrade(self, s, queries):\n \"\"\"\n :type s: str\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxActiveSectionsAfterTrade(self, s: 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* maxActiveSectionsAfterTrade(char* s, int** queries, int queriesSize, int* queriesColSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public IList<int> MaxActiveSectionsAfterTrade(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 maxActiveSectionsAfterTrade = function(s, queries) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxActiveSectionsAfterTrade(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 maxActiveSectionsAfterTrade($s, $queries) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxActiveSectionsAfterTrade(_ s: String, _ queries: [[Int]]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxActiveSectionsAfterTrade(s: String, queries: Array<IntArray>): List<Int> {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> maxActiveSectionsAfterTrade(String s, List<List<int>> queries) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxActiveSectionsAfterTrade(s string, queries [][]int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {Integer[][]} queries\n# @return {Integer[]}\ndef max_active_sections_after_trade(s, queries)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxActiveSectionsAfterTrade(s: String, queries: Array[Array[Int]]): List[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_active_sections_after_trade(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-active-sections-after-trade s queries)\n (-> string? (listof (listof exact-integer?)) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_active_sections_after_trade(S :: unicode:unicode_binary(), Queries :: [[integer()]]) -> [integer()].\nmax_active_sections_after_trade(S, Queries) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_active_sections_after_trade(s :: String.t, queries :: [[integer]]) :: [integer]\n def max_active_sections_after_trade(s, queries) do\n \n end\nend"}]
"01" [[0,1]]
{ "name": "maxActiveSectionsAfterTrade", "params": [ { "name": "s", "type": "string" }, { "type": "integer[][]", "name": "queries" } ], "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', 'String', 'Binary Search', 'Segment Tree']
3,502
Minimum Cost to Reach Every Position
minimum-cost-to-reach-every-position
<p data-end="438" data-start="104">You are given an integer array <code data-end="119" data-start="113">cost</code> of size <code data-end="131" data-start="128">n</code>. You are currently at position <code data-end="166" data-start="163">n</code> (at the end of the line) in a line of <code data-end="187" data-start="180">n + 1</code> people (numbered from 0 to <code data-end="218" data-start="215">n</code>).</p> <p data-end="438" data-start="104">You wish to move forward in the line, but each person in front of you charges a specific amount to <strong>swap</strong> places. The cost to swap with person <code data-end="375" data-start="372">i</code> is given by <code data-end="397" data-start="388">cost[i]</code>.</p> <p data-end="487" data-start="440">You are allowed to swap places with people as follows:</p> <ul data-end="632" data-start="488"> <li data-end="572" data-start="488">If they are in front of you, you <strong>must</strong> pay them <code data-end="546" data-start="537">cost[i]</code> to swap with them.</li> <li data-end="632" data-start="573">If they are behind you, they can swap with you for free.</li> </ul> <p data-end="755" data-start="634">Return an array <code>answer</code> of size <code>n</code>, where <code>answer[i]</code> is the <strong data-end="680" data-start="664">minimum</strong> total cost to reach each position <code>i</code> in the line<font face="monospace">.</font></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">cost = [5,3,4,1,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,3,3,1,1,1]</span></p> <p><strong>Explanation:</strong></p> <p>We can get to each position in the following way:</p> <ul> <li><code>i = 0</code>. We can swap with person 0 for a cost of 5.</li> <li><span class="example-io"><code><font face="monospace">i = </font>1</code>. We can swap with person 1 for a cost of 3.</span></li> <li><span class="example-io"><code>i = 2</code>. We can swap with person 1 for a cost of 3, then swap with person 2 for free.</span></li> <li><span class="example-io"><code>i = 3</code>. We can swap with person 3 for a cost of 1.</span></li> <li><span class="example-io"><code>i = 4</code>. We can swap with person 3 for a cost of 1, then swap with person 4 for free.</span></li> <li><span class="example-io"><code>i = 5</code>. We can swap with person 3 for a cost of 1, then swap with person 5 for free.</span></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">cost = [1,2,4,6,7]</span></p> <p><strong>Output:</strong> <span class="example-io">[1,1,1,1,1]</span></p> <p><strong>Explanation:</strong></p> <p>We can swap with person 0 for a cost of <span class="example-io">1, then we will be able to reach any position <code>i</code> for free.</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == cost.length &lt;= 100</code></li> <li><code>1 &lt;= cost[i] &lt;= 100</code></li> </ul>
Easy
30.9K
37K
30,854
36,960
83.5%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n vector<int> minCosts(vector<int>& cost) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int[] minCosts(int[] cost) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minCosts(self, cost):\n \"\"\"\n :type cost: List[int]\n :rtype: List[int]\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minCosts(self, cost: List[int]) -> List[int]:\n "}, {"value": "c", "text": "C", "defaultCode": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* minCosts(int* cost, int costSize, int* returnSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int[] MinCosts(int[] cost) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} cost\n * @return {number[]}\n */\nvar minCosts = function(cost) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minCosts(cost: number[]): number[] {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $cost\n * @return Integer[]\n */\n function minCosts($cost) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minCosts(_ cost: [Int]) -> [Int] {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minCosts(cost: IntArray): IntArray {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n List<int> minCosts(List<int> cost) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minCosts(cost []int) []int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} cost\n# @return {Integer[]}\ndef min_costs(cost)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minCosts(cost: Array[Int]): Array[Int] = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_costs(cost: Vec<i32>) -> Vec<i32> {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-costs cost)\n (-> (listof exact-integer?) (listof exact-integer?))\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_costs(Cost :: [integer()]) -> [integer()].\nmin_costs(Cost) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_costs(cost :: [integer]) :: [integer]\n def min_costs(cost) do\n \n end\nend"}]
[5,3,4,1,3,2]
{ "name": "minCosts", "params": [ { "name": "cost", "type": "integer[]" } ], "return": { "type": "integer[]" } }
{"cpp": ["C++", "<p>Compiled with <code> clang 19 </code> using the latest C++ 23 standard, and <code>libstdc++</code> provided by GCC 14.</p>\r\n\r\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>"], "java": ["Java", "<p><code>OpenJDK 21</code>. Using compile arguments: <code>--enable-preview --release 21</code></p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>"], "python": ["Python", "<p><code>Python 2.7.18</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/2/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/2/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/2/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>\r\n\r\n<p>Note that Python 2.7 <a href=\"https://www.python.org/dev/peps/pep-0373/\" target=\"_blank\">is not maintained anymore</a>. For the latest Python, please choose Python3 instead.</p>"], "c": ["C", "<p>Compiled with <code>gcc 14</code> using the gnu11 standard.</p>\r\n\r\n<p>Your code is compiled with level one optimization (<code>-O2</code>). <a href=\"https://github.com/google/sanitizers/wiki/AddressSanitizer\" target=\"_blank\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\r\n\r\n<p>Most standard library headers are already included automatically for your convenience.</p>\r\n\r\n<p>For hash table operations, you may use <a href=\"https://troydhanson.github.io/uthash/\" target=\"_blank\">uthash</a>. \"uthash.h\" is included by default. Below are some examples:</p>\r\n\r\n<p><b>1. Adding an item to a hash.</b>\r\n<pre>\r\nstruct hash_entry {\r\n int id; /* we'll use this field as the key */\r\n char name[10];\r\n UT_hash_handle hh; /* makes this structure hashable */\r\n};\r\n\r\nstruct hash_entry *users = NULL;\r\n\r\nvoid add_user(struct hash_entry *s) {\r\n HASH_ADD_INT(users, id, s);\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>2. Looking up an item in a hash:</b>\r\n<pre>\r\nstruct hash_entry *find_user(int user_id) {\r\n struct hash_entry *s;\r\n HASH_FIND_INT(users, &user_id, s);\r\n return s;\r\n}\r\n</pre>\r\n</p>\r\n\r\n<p><b>3. Deleting an item in a hash:</b>\r\n<pre>\r\nvoid delete_user(struct hash_entry *user) {\r\n HASH_DEL(users, user); \r\n}\r\n</pre>\r\n</p>"], "csharp": ["C#", "<p><a href=\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13\" target=\"_blank\">C# 13</a> with .NET 9 runtime</p>"], "javascript": ["JavaScript", "<p><code>Node.js 22.14.0</code>.</p>\r\n\r\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\"http://node.green/\" target=\"_blank\">new ES6 features</a>.</p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "ruby": ["Ruby", "<p><code>Ruby 3.2</code></p>\r\n\r\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>"], "swift": ["Swift", "<p><code>Swift 6.0</code>.</p>\r\n\r\n<p>You may use the following packages:<br/>\r\n<a href=\"https://github.com/apple/swift-algorithms/tree/1.2.0\" target=\"_blank\">swift-algorithms 1.2.0</a><br/>\r\n<a href=\"https://github.com/apple/swift-collections/tree/1.1.4\" target=\"_blank\">swift-collections 1.1.4</a><br/>\r\n<a href=\"https://github.com/apple/swift-numerics/tree/1.0.2\" target=\"_blank\">swift-numerics 1.0.2</a></p>"], "golang": ["Go", "<p><code>Go 1.23</code></p>\r\n<p>Support <a href=\"https://pkg.go.dev/github.com/emirpasic/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/[email protected]</a> and <a href=\"https://pkg.go.dev/github.com/emirpasic/gods/[email protected]\" target=\"_blank\">https://pkg.go.dev/github.com/emirpasic/gods/[email protected]</a> library.</p>"], "python3": ["Python3", "<p><code>Python 3.11</code>.</p>\r\n\r\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\"https://docs.python.org/3/library/array.html\" target=\"_blank\">array</a>, <a href=\"https://docs.python.org/3/library/bisect.html\" target=\"_blank\">bisect</a>, <a href=\"https://docs.python.org/3/library/collections.html\" target=\"_blank\">collections</a>. If you need more libraries, you can import it yourself.</p>\r\n\r\n<p>For Map/TreeMap data structure, you may use <a href=\"http://www.grantjenks.com/docs/sortedcontainers/\" target=\"_blank\">sortedcontainers</a> library.</p>"], "scala": ["Scala", "<p><code>Scala 3.3.1</code>.</p>"], "kotlin": ["Kotlin", "<p><code>Kotlin 2.1.10</code>.</p>"], "rust": ["Rust", "<p><code>Rust 1.85.0</code>. Your code will be compiled with <code>opt-level</code> 2.</p>\r\n\r\n<p>Supports <a href=\"https://crates.io/crates/rand\" target=\"_blank\">rand v0.8</a> and <a href=\"https://crates.io/crates/regex\" target=\"_blank\">regex\u00a0v1</a> and <a href=\"https://crates.io/crates/itertools\" target=\"_blank\">itertools\u00a0v0.14</a> from crates.io</p>"], "php": ["PHP", "<p><code>PHP 8.2</code>.</p>\r\n<p>With bcmath module</p>"], "typescript": ["Typescript", "<p><code>TypeScript 5.7.3, Node.js 22.14.0</code>.</p>\r\n\r\n<p>Compile Options: <code>--alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024</code></p>\r\n\r\n<p><a href=\"https://lodash.com\" target=\"_blank\">lodash.js</a> library is included by default.</p>\r\n\r\n<p>For Priority Queue / Queue data structures, you may use 6.3.2 version of <a href=\"https://github.com/datastructures-js/priority-queue/tree/v6.3.2\" target=\"_blank\">datastructures-js/priority-queue</a> and 4.2.3 version of <a href=\"https://github.com/datastructures-js/queue/tree/v4.2.3\" target=\"_blank\">datastructures-js/queue</a> and 1.0.4 version of <a href=\"https://github.com/datastructures-js/deque/tree/v1.0.4\" target=\"_blank\">datastructures-js/deque</a>.</p>"], "racket": ["Racket", "<p><a href=\"https://docs.racket-lang.org/guide/performance.html#%28tech._c%29\" target=\"_blank\">Racket CS</a> v8.15</p>\r\n\r\n<p>Using <code>#lang racket</code></p>\r\n\r\n<p>Required <code>data/gvector data/queue data/order data/heap</code> automatically for your convenience</p>"], "erlang": ["Erlang", "Erlang/OTP 26"], "elixir": ["Elixir", "Elixir 1.17 with Erlang/OTP 26"], "dart": ["Dart", "<p>Dart 3.2. You may use package <a href=\"https://pub.dev/packages/collection/versions/1.18.0\" target=\"_blank\">collection</a></p>\r\n\r\n<p>Your code will be run directly without compiling</p>"]}
['Array']
3,503
Longest Palindrome After Substring Concatenation I
longest-palindrome-after-substring-concatenation-i
<p>You are given two strings, <code>s</code> and <code>t</code>.</p> <p>You can create a new string by selecting a <span data-keyword="substring">substring</span> from <code>s</code> (possibly empty) and a substring from <code>t</code> (possibly empty), then concatenating them <strong>in order</strong>.</p> <p>Return the length of the <strong>longest</strong> <span data-keyword="palindrome-string">palindrome</span> that can be formed this way.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;a&quot;, t = &quot;a&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Concatenating <code>&quot;a&quot;</code> from <code>s</code> and <code>&quot;a&quot;</code> from <code>t</code> results in <code>&quot;aa&quot;</code>, which is a palindrome of length 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;, t = &quot;def&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Since all characters are different, the longest palindrome is any single character, so the answer is 1.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;b&quot;, t = &quot;aaaa&quot;</span></p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <p>Selecting &quot;<code>aaaa</code>&quot; from <code>t</code> is the longest palindrome, so the answer is 4.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abcde&quot;, t = &quot;ecdba&quot;</span></p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong></p> <p>Concatenating <code>&quot;abc&quot;</code> from <code>s</code> and <code>&quot;ba&quot;</code> from <code>t</code> results in <code>&quot;abcba&quot;</code>, which is a palindrome of length 5.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 30</code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul>
Medium
18.4K
42.6K
18,444
42,644
43.3%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int longestPalindrome(string s, string t) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int longestPalindrome(String s, String t) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestPalindrome(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 longestPalindrome(self, s: str, t: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int longestPalindrome(char* s, char* t) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LongestPalindrome(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 longestPalindrome = function(s, t) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestPalindrome(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 longestPalindrome($s, $t) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestPalindrome(_ s: String, _ t: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestPalindrome(s: String, t: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int longestPalindrome(String s, String t) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestPalindrome(s string, t string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} t\n# @return {Integer}\ndef longest_palindrome(s, t)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestPalindrome(s: String, t: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_palindrome(s: String, t: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-palindrome s t)\n (-> string? string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_palindrome(S :: unicode:unicode_binary(), T :: unicode:unicode_binary()) -> integer().\nlongest_palindrome(S, T) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_palindrome(s :: String.t, t :: String.t) :: integer\n def longest_palindrome(s, t) do\n \n end\nend"}]
"a" "a"
{ "name": "longestPalindrome", "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', 'Dynamic Programming', 'Enumeration']
3,504
Longest Palindrome After Substring Concatenation II
longest-palindrome-after-substring-concatenation-ii
<p>You are given two strings, <code>s</code> and <code>t</code>.</p> <p>You can create a new string by selecting a <span data-keyword="substring">substring</span> from <code>s</code> (possibly empty) and a substring from <code>t</code> (possibly empty), then concatenating them <strong>in order</strong>.</p> <p>Return the length of the <strong>longest</strong> <span data-keyword="palindrome-string">palindrome</span> that can be formed this way.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;a&quot;, t = &quot;a&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Concatenating <code>&quot;a&quot;</code> from <code>s</code> and <code>&quot;a&quot;</code> from <code>t</code> results in <code>&quot;aa&quot;</code>, which is a palindrome of length 2.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;, t = &quot;def&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>Since all characters are different, the longest palindrome is any single character, so the answer is 1.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;b&quot;, t = &quot;aaaa&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Selecting &quot;<code>aaaa</code>&quot; from <code>t</code> is the longest palindrome, so the answer is 4.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;abcde&quot;, t = &quot;ecdba&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p>Concatenating <code>&quot;abc&quot;</code> from <code>s</code> and <code>&quot;ba&quot;</code> from <code>t</code> results in <code>&quot;abcba&quot;</code>, which is a palindrome of length 5.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length, t.length &lt;= 1000</code></li> <li><code>s</code> and <code>t</code> consist of lowercase English letters.</li> </ul>
Hard
4.4K
29.1K
4,399
29,070
15.1%
['edit-distance']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int longestPalindrome(string s, string t) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int longestPalindrome(String s, String t) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def longestPalindrome(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 longestPalindrome(self, s: str, t: str) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int longestPalindrome(char* s, char* t) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int LongestPalindrome(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 longestPalindrome = function(s, t) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function longestPalindrome(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 longestPalindrome($s, $t) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func longestPalindrome(_ s: String, _ t: String) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun longestPalindrome(s: String, t: String): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int longestPalindrome(String s, String t) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func longestPalindrome(s string, t string) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {String} s\n# @param {String} t\n# @return {Integer}\ndef longest_palindrome(s, t)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def longestPalindrome(s: String, t: String): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn longest_palindrome(s: String, t: String) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (longest-palindrome s t)\n (-> string? string? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec longest_palindrome(S :: unicode:unicode_binary(), T :: unicode:unicode_binary()) -> integer().\nlongest_palindrome(S, T) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec longest_palindrome(s :: String.t, t :: String.t) :: integer\n def longest_palindrome(s, t) do\n \n end\nend"}]
"a" "a"
{ "name": "longestPalindrome", "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', 'Dynamic Programming']
3,505
Minimum Operations to Make Elements Within K Subarrays Equal
minimum-operations-to-make-elements-within-k-subarrays-equal
<p>You are given an integer array <code>nums</code> and two integers, <code>x</code> and <code>k</code>. You can perform the following operation any number of times (<strong>including zero</strong>):</p> <ul> <li>Increase or decrease any element of <code>nums</code> by 1.</li> </ul> <p>Return the <strong>minimum</strong> number of operations needed to have <strong>at least</strong> <code>k</code> <em>non-overlapping <span data-keyword="subarray-nonempty">subarrays</span></em> of size <strong>exactly</strong> <code>x</code> in <code>nums</code>, where all elements within each subarray are equal.</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 = [5,-2,1,3,7,3,6,4,-1], x = 3, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">8</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Use 3 operations to add 3 to <code>nums[1]</code> and use 2 operations to subtract 2 from <code>nums[3]</code>. The resulting array is <code>[5, 1, 1, 1, 7, 3, 6, 4, -1]</code>.</li> <li>Use 1 operation to add 1 to <code>nums[5]</code> and use 2 operations to subtract 2 from <code>nums[6]</code>. The resulting array is <code>[5, 1, 1, 1, 7, 4, 4, 4, -1]</code>.</li> <li>Now, all elements within each subarray <code>[1, 1, 1]</code> (from indices 1 to 3) and <code>[4, 4, 4]</code> (from indices 5 to 7) are equal. Since 8 total operations were used, 8 is the output.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [9,-2,-2,-2,1,5], x = 2, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li>Use 3 operations to subtract 3 from <code>nums[4]</code>. The resulting array is <code>[9, -2, -2, -2, -2, 5]</code>.</li> <li>Now, all elements within each subarray <code>[-2, -2]</code> (from indices 1 to 2) and <code>[-2, -2]</code> (from indices 3 to 4) are equal. Since 3 operations were used, 3 is the output.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &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> <li><code>2 &lt;= x &lt;= nums.length</code></li> <li><code>1 &lt;= k &lt;= 15</code></li> <li><code>2 &lt;= k * x &lt;= nums.length</code></li> </ul>
Hard
2.7K
9.9K
2,738
9,925
27.6%
['find-median-from-data-stream', 'minimum-moves-to-equal-array-elements-ii']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n long long minOperations(vector<int>& nums, int x, int k) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public long minOperations(int[] nums, int x, int k) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minOperations(self, nums, x, k):\n \"\"\"\n :type nums: List[int]\n :type x: int\n :type k: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minOperations(self, nums: List[int], x: int, k: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "long long minOperations(int* nums, int numsSize, int x, int k) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public long MinOperations(int[] nums, int x, int k) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} x\n * @param {number} k\n * @return {number}\n */\nvar minOperations = function(nums, x, k) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minOperations(nums: number[], x: number, k: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $x\n * @param Integer $k\n * @return Integer\n */\n function minOperations($nums, $x, $k) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minOperations(_ nums: [Int], _ x: Int, _ k: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minOperations(nums: IntArray, x: Int, k: Int): Long {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minOperations(List<int> nums, int x, int k) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minOperations(nums []int, x int, k int) int64 {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} x\n# @param {Integer} k\n# @return {Integer}\ndef min_operations(nums, x, k)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minOperations(nums: Array[Int], x: Int, k: Int): Long = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn min_operations(nums: Vec<i32>, x: i32, k: i32) -> i64 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (min-operations nums x k)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec min_operations(Nums :: [integer()], X :: integer(), K :: integer()) -> integer().\nmin_operations(Nums, X, K) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec min_operations(nums :: [integer], x :: integer, k :: integer) :: integer\n def min_operations(nums, x, k) do\n \n end\nend"}]
[5,-2,1,3,7,3,6,4,-1] 3 2
{ "name": "minOperations", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "x" }, { "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', 'Math', 'Dynamic Programming', 'Sliding Window', 'Heap (Priority Queue)']
3,506
Find Time Required to Eliminate Bacterial Strains
find-time-required-to-eliminate-bacterial-strains
null
Hard
171
293
171
293
58.4%
['minimum-time-to-build-blocks']
[]
Algorithms
null
[10,4,5] 2
{ "name": "minEliminationTime", "params": [ { "name": "timeReq", "type": "integer[]" }, { "type": "integer", "name": "splitTime" } ], "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', 'Heap (Priority Queue)']
3,507
Minimum Pair Removal to Sort Array I
minimum-pair-removal-to-sort-array-i
<p>Given an array <code>nums</code>, you can perform the following operation any number of times:</p> <ul> <li>Select the <strong>adjacent</strong> pair with the <strong>minimum</strong> sum in <code>nums</code>. If multiple such pairs exist, choose the leftmost one.</li> <li>Replace the pair with their sum.</li> </ul> <p>Return the <strong>minimum number of operations</strong> needed to make the array <strong>non-decreasing</strong>.</p> <p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous element (if it exists).</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 = [5,2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The pair <code>(3,1)</code> has the minimum sum of 4. After replacement, <code>nums = [5,2,4]</code>.</li> <li>The pair <code>(2,4)</code> has the minimum sum of 6. After replacement, <code>nums = [5,6]</code>.</li> </ul> <p>The array <code>nums</code> became non-decreasing in two operations.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,2]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>The array <code>nums</code> is already sorted.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li> </ul>
Easy
26.4K
46.3K
26,406
46,276
57.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int minimumPairRemoval(vector<int>& nums) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int minimumPairRemoval(int[] nums) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def minimumPairRemoval(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def minimumPairRemoval(self, nums: List[int]) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int minimumPairRemoval(int* nums, int numsSize) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MinimumPairRemoval(int[] nums) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @return {number}\n */\nvar minimumPairRemoval = function(nums) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function minimumPairRemoval(nums: number[]): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @return Integer\n */\n function minimumPairRemoval($nums) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func minimumPairRemoval(_ nums: [Int]) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun minimumPairRemoval(nums: IntArray): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int minimumPairRemoval(List<int> nums) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func minimumPairRemoval(nums []int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @return {Integer}\ndef minimum_pair_removal(nums)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def minimumPairRemoval(nums: Array[Int]): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn minimum_pair_removal(nums: Vec<i32>) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (minimum-pair-removal nums)\n (-> (listof exact-integer?) exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec minimum_pair_removal(Nums :: [integer()]) -> integer().\nminimum_pair_removal(Nums) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec minimum_pair_removal(nums :: [integer]) :: integer\n def minimum_pair_removal(nums) do\n \n end\nend"}]
[5,2,3,1]
{ "name": "minimumPairRemoval", "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', 'Linked List', 'Heap (Priority Queue)', 'Simulation', 'Doubly-Linked List', 'Ordered Set']
3,508
Implement Router
implement-router
<p>Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:</p> <ul> <li><code>source</code>: A unique identifier for the machine that generated the packet.</li> <li><code>destination</code>: A unique identifier for the target machine.</li> <li><code>timestamp</code>: The time at which the packet arrived at the router.</li> </ul> <p>Implement the <code>Router</code> class:</p> <p><code>Router(int memoryLimit)</code>: Initializes the Router object with a fixed memory limit.</p> <ul> <li><code>memoryLimit</code> is the <strong>maximum</strong> number of packets the router can store at any given time.</li> <li>If adding a new packet would exceed this limit, the <strong>oldest</strong> packet must be removed to free up space.</li> </ul> <p><code>bool addPacket(int source, int destination, int timestamp)</code>: Adds a packet with the given attributes to the router.</p> <ul> <li>A packet is considered a duplicate if another packet with the same <code>source</code>, <code>destination</code>, and <code>timestamp</code> already exists in the router.</li> <li>Return <code>true</code> if the packet is successfully added (i.e., it is not a duplicate); otherwise return <code>false</code>.</li> </ul> <p><code>int[] forwardPacket()</code>: Forwards the next packet in FIFO (First In First Out) order.</p> <ul> <li>Remove the packet from storage.</li> <li>Return the packet as an array <code>[source, destination, timestamp]</code>.</li> <li>If there are no packets to forward, return an empty array.</li> </ul> <p><code>int getCount(int destination, int startTime, int endTime)</code>:</p> <ul> <li>Returns the number of packets currently stored in the router (i.e., not yet forwarded) that have the specified destination and have timestamps in the inclusive range <code>[startTime, endTime]</code>.</li> </ul> <p><strong>Note</strong> that queries for <code>addPacket</code> will be made in increasing order of <code>timestamp</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong><br /> <span class="example-io">[&quot;Router&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;addPacket&quot;, &quot;forwardPacket&quot;, &quot;addPacket&quot;, &quot;getCount&quot;]<br /> [[3], [1, 4, 90], [2, 5, 90], [1, 4, 90], [3, 5, 95], [4, 5, 105], [], [5, 2, 110], [5, 100, 110]]</span></p> <p><strong>Output:</strong><br /> <span class="example-io">[null, true, true, false, true, true, [2, 5, 90], true, 1] </span></p> <p><strong>Explanation</strong></p> Router router = new Router(3); // Initialize Router with memoryLimit of 3.<br /> router.addPacket(1, 4, 90); // Packet is added. Return True.<br /> router.addPacket(2, 5, 90); // Packet is added. Return True.<br /> router.addPacket(1, 4, 90); // This is a duplicate packet. Return False.<br /> router.addPacket(3, 5, 95); // Packet is added. Return True<br /> router.addPacket(4, 5, 105); // Packet is added, <code>[1, 4, 90]</code> is removed as number of packets exceeds memoryLimit. Return True.<br /> router.forwardPacket(); // Return <code>[2, 5, 90]</code> and remove it from router.<br /> router.addPacket(5, 2, 110); // Packet is added. Return True.<br /> router.getCount(5, 100, 110); // The only packet with destination 5 and timestamp in the inclusive range <code>[100, 110]</code> is <code>[4, 5, 105]</code>. Return 1.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong><br /> <span class="example-io">[&quot;Router&quot;, &quot;addPacket&quot;, &quot;forwardPacket&quot;, &quot;forwardPacket&quot;]<br /> [[2], [7, 4, 90], [], []]</span></p> <p><strong>Output:</strong><br /> <span class="example-io">[null, true, [7, 4, 90], []] </span></p> <p><strong>Explanation</strong></p> Router router = new Router(2); // Initialize <code>Router</code> with <code>memoryLimit</code> of 2.<br /> router.addPacket(7, 4, 90); // Return True.<br /> router.forwardPacket(); // Return <code>[7, 4, 90]</code>.<br /> router.forwardPacket(); // There are no packets left, return <code>[]</code>.</div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= memoryLimit &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= source, destination &lt;= 2 * 10<sup>5</sup></code></li> <li><code>1 &lt;= timestamp &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= startTime &lt;= endTime &lt;= 10<sup>9</sup></code></li> <li>At most <code>10<sup>5</sup></code> calls will be made to <code>addPacket</code>, <code>forwardPacket</code>, and <code>getCount</code> methods altogether.</li> <li>queries for <code>addPacket</code> will be made in increasing order of <code>timestamp</code>.</li> </ul>
Medium
11.3K
53.8K
11,340
53,763
21.1%
[]
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Router {\npublic:\n Router(int memoryLimit) {\n \n }\n \n bool addPacket(int source, int destination, int timestamp) {\n \n }\n \n vector<int> forwardPacket() {\n \n }\n \n int getCount(int destination, int startTime, int endTime) {\n \n }\n};\n\n/**\n * Your Router object will be instantiated and called as such:\n * Router* obj = new Router(memoryLimit);\n * bool param_1 = obj->addPacket(source,destination,timestamp);\n * vector<int> param_2 = obj->forwardPacket();\n * int param_3 = obj->getCount(destination,startTime,endTime);\n */"}, {"value": "java", "text": "Java", "defaultCode": "class Router {\n\n public Router(int memoryLimit) {\n \n }\n \n public boolean addPacket(int source, int destination, int timestamp) {\n \n }\n \n public int[] forwardPacket() {\n \n }\n \n public int getCount(int destination, int startTime, int endTime) {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * Router obj = new Router(memoryLimit);\n * boolean param_1 = obj.addPacket(source,destination,timestamp);\n * int[] param_2 = obj.forwardPacket();\n * int param_3 = obj.getCount(destination,startTime,endTime);\n */"}, {"value": "python", "text": "Python", "defaultCode": "class Router(object):\n\n def __init__(self, memoryLimit):\n \"\"\"\n :type memoryLimit: int\n \"\"\"\n \n\n def addPacket(self, source, destination, timestamp):\n \"\"\"\n :type source: int\n :type destination: int\n :type timestamp: int\n :rtype: bool\n \"\"\"\n \n\n def forwardPacket(self):\n \"\"\"\n :rtype: List[int]\n \"\"\"\n \n\n def getCount(self, destination, startTime, endTime):\n \"\"\"\n :type destination: int\n :type startTime: int\n :type endTime: int\n :rtype: int\n \"\"\"\n \n\n\n# Your Router object will be instantiated and called as such:\n# obj = Router(memoryLimit)\n# param_1 = obj.addPacket(source,destination,timestamp)\n# param_2 = obj.forwardPacket()\n# param_3 = obj.getCount(destination,startTime,endTime)"}, {"value": "python3", "text": "Python3", "defaultCode": "class Router:\n\n def __init__(self, memoryLimit: int):\n \n\n def addPacket(self, source: int, destination: int, timestamp: int) -> bool:\n \n\n def forwardPacket(self) -> List[int]:\n \n\n def getCount(self, destination: int, startTime: int, endTime: int) -> int:\n \n\n\n# Your Router object will be instantiated and called as such:\n# obj = Router(memoryLimit)\n# param_1 = obj.addPacket(source,destination,timestamp)\n# param_2 = obj.forwardPacket()\n# param_3 = obj.getCount(destination,startTime,endTime)"}, {"value": "c", "text": "C", "defaultCode": "\n\n\ntypedef struct {\n \n} Router;\n\n\nRouter* routerCreate(int memoryLimit) {\n \n}\n\nbool routerAddPacket(Router* obj, int source, int destination, int timestamp) {\n \n}\n\nint* routerForwardPacket(Router* obj, int* retSize) {\n \n}\n\nint routerGetCount(Router* obj, int destination, int startTime, int endTime) {\n \n}\n\nvoid routerFree(Router* obj) {\n \n}\n\n/**\n * Your Router struct will be instantiated and called as such:\n * Router* obj = routerCreate(memoryLimit);\n * bool param_1 = routerAddPacket(obj, source, destination, timestamp);\n \n * int* param_2 = routerForwardPacket(obj, retSize);\n \n * int param_3 = routerGetCount(obj, destination, startTime, endTime);\n \n * routerFree(obj);\n*/"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Router {\n\n public Router(int memoryLimit) {\n \n }\n \n public bool AddPacket(int source, int destination, int timestamp) {\n \n }\n \n public int[] ForwardPacket() {\n \n }\n \n public int GetCount(int destination, int startTime, int endTime) {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * Router obj = new Router(memoryLimit);\n * bool param_1 = obj.AddPacket(source,destination,timestamp);\n * int[] param_2 = obj.ForwardPacket();\n * int param_3 = obj.GetCount(destination,startTime,endTime);\n */"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number} memoryLimit\n */\nvar Router = function(memoryLimit) {\n \n};\n\n/** \n * @param {number} source \n * @param {number} destination \n * @param {number} timestamp\n * @return {boolean}\n */\nRouter.prototype.addPacket = function(source, destination, timestamp) {\n \n};\n\n/**\n * @return {number[]}\n */\nRouter.prototype.forwardPacket = function() {\n \n};\n\n/** \n * @param {number} destination \n * @param {number} startTime \n * @param {number} endTime\n * @return {number}\n */\nRouter.prototype.getCount = function(destination, startTime, endTime) {\n \n};\n\n/** \n * Your Router object will be instantiated and called as such:\n * var obj = new Router(memoryLimit)\n * var param_1 = obj.addPacket(source,destination,timestamp)\n * var param_2 = obj.forwardPacket()\n * var param_3 = obj.getCount(destination,startTime,endTime)\n */"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "class Router {\n constructor(memoryLimit: number) {\n \n }\n\n addPacket(source: number, destination: number, timestamp: number): boolean {\n \n }\n\n forwardPacket(): number[] {\n \n }\n\n getCount(destination: number, startTime: number, endTime: number): number {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * var obj = new Router(memoryLimit)\n * var param_1 = obj.addPacket(source,destination,timestamp)\n * var param_2 = obj.forwardPacket()\n * var param_3 = obj.getCount(destination,startTime,endTime)\n */"}, {"value": "php", "text": "PHP", "defaultCode": "class Router {\n /**\n * @param Integer $memoryLimit\n */\n function __construct($memoryLimit) {\n \n }\n \n /**\n * @param Integer $source\n * @param Integer $destination\n * @param Integer $timestamp\n * @return Boolean\n */\n function addPacket($source, $destination, $timestamp) {\n \n }\n \n /**\n * @return Integer[]\n */\n function forwardPacket() {\n \n }\n \n /**\n * @param Integer $destination\n * @param Integer $startTime\n * @param Integer $endTime\n * @return Integer\n */\n function getCount($destination, $startTime, $endTime) {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * $obj = Router($memoryLimit);\n * $ret_1 = $obj->addPacket($source, $destination, $timestamp);\n * $ret_2 = $obj->forwardPacket();\n * $ret_3 = $obj->getCount($destination, $startTime, $endTime);\n */"}, {"value": "swift", "text": "Swift", "defaultCode": "\nclass Router {\n\n init(_ memoryLimit: Int) {\n \n }\n \n func addPacket(_ source: Int, _ destination: Int, _ timestamp: Int) -> Bool {\n \n }\n \n func forwardPacket() -> [Int] {\n \n }\n \n func getCount(_ destination: Int, _ startTime: Int, _ endTime: Int) -> Int {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * let obj = Router(memoryLimit)\n * let ret_1: Bool = obj.addPacket(source, destination, timestamp)\n * let ret_2: [Int] = obj.forwardPacket()\n * let ret_3: Int = obj.getCount(destination, startTime, endTime)\n */"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Router(memoryLimit: Int) {\n\n fun addPacket(source: Int, destination: Int, timestamp: Int): Boolean {\n \n }\n\n fun forwardPacket(): IntArray {\n \n }\n\n fun getCount(destination: Int, startTime: Int, endTime: Int): Int {\n \n }\n\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * var obj = Router(memoryLimit)\n * var param_1 = obj.addPacket(source,destination,timestamp)\n * var param_2 = obj.forwardPacket()\n * var param_3 = obj.getCount(destination,startTime,endTime)\n */"}, {"value": "dart", "text": "Dart", "defaultCode": "class Router {\n\n Router(int memoryLimit) {\n \n }\n \n bool addPacket(int source, int destination, int timestamp) {\n \n }\n \n List<int> forwardPacket() {\n \n }\n \n int getCount(int destination, int startTime, int endTime) {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * Router obj = Router(memoryLimit);\n * bool param1 = obj.addPacket(source,destination,timestamp);\n * List<int> param2 = obj.forwardPacket();\n * int param3 = obj.getCount(destination,startTime,endTime);\n */"}, {"value": "golang", "text": "Go", "defaultCode": "type Router struct {\n \n}\n\n\nfunc Constructor(memoryLimit int) Router {\n \n}\n\n\nfunc (this *Router) AddPacket(source int, destination int, timestamp int) bool {\n \n}\n\n\nfunc (this *Router) ForwardPacket() []int {\n \n}\n\n\nfunc (this *Router) GetCount(destination int, startTime int, endTime int) int {\n \n}\n\n\n/**\n * Your Router object will be instantiated and called as such:\n * obj := Constructor(memoryLimit);\n * param_1 := obj.AddPacket(source,destination,timestamp);\n * param_2 := obj.ForwardPacket();\n * param_3 := obj.GetCount(destination,startTime,endTime);\n */"}, {"value": "ruby", "text": "Ruby", "defaultCode": "class Router\n\n=begin\n :type memory_limit: Integer\n=end\n def initialize(memory_limit)\n \n end\n\n\n=begin\n :type source: Integer\n :type destination: Integer\n :type timestamp: Integer\n :rtype: Boolean\n=end\n def add_packet(source, destination, timestamp)\n \n end\n\n\n=begin\n :rtype: Integer[]\n=end\n def forward_packet()\n \n end\n\n\n=begin\n :type destination: Integer\n :type start_time: Integer\n :type end_time: Integer\n :rtype: Integer\n=end\n def get_count(destination, start_time, end_time)\n \n end\n\n\nend\n\n# Your Router object will be instantiated and called as such:\n# obj = Router.new(memory_limit)\n# param_1 = obj.add_packet(source, destination, timestamp)\n# param_2 = obj.forward_packet()\n# param_3 = obj.get_count(destination, start_time, end_time)"}, {"value": "scala", "text": "Scala", "defaultCode": "class Router(_memoryLimit: Int) {\n\n def addPacket(source: Int, destination: Int, timestamp: Int): Boolean = {\n \n }\n\n def forwardPacket(): Array[Int] = {\n \n }\n\n def getCount(destination: Int, startTime: Int, endTime: Int): Int = {\n \n }\n\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * val obj = new Router(memoryLimit)\n * val param_1 = obj.addPacket(source,destination,timestamp)\n * val param_2 = obj.forwardPacket()\n * val param_3 = obj.getCount(destination,startTime,endTime)\n */"}, {"value": "rust", "text": "Rust", "defaultCode": "struct Router {\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 Router {\n\n fn new(memoryLimit: i32) -> Self {\n \n }\n \n fn add_packet(&self, source: i32, destination: i32, timestamp: i32) -> bool {\n \n }\n \n fn forward_packet(&self) -> Vec<i32> {\n \n }\n \n fn get_count(&self, destination: i32, start_time: i32, end_time: i32) -> i32 {\n \n }\n}\n\n/**\n * Your Router object will be instantiated and called as such:\n * let obj = Router::new(memoryLimit);\n * let ret_1: bool = obj.add_packet(source, destination, timestamp);\n * let ret_2: Vec<i32> = obj.forward_packet();\n * let ret_3: i32 = obj.get_count(destination, startTime, endTime);\n */"}, {"value": "racket", "text": "Racket", "defaultCode": "(define router%\n (class object%\n (super-new)\n \n ; memory-limit : exact-integer?\n (init-field\n memory-limit)\n \n ; add-packet : exact-integer? exact-integer? exact-integer? -> boolean?\n (define/public (add-packet source destination timestamp)\n )\n ; forward-packet : -> (listof exact-integer?)\n (define/public (forward-packet)\n )\n ; get-count : exact-integer? exact-integer? exact-integer? -> exact-integer?\n (define/public (get-count destination start-time end-time)\n )))\n\n;; Your router% object will be instantiated and called as such:\n;; (define obj (new router% [memory-limit memory-limit]))\n;; (define param_1 (send obj add-packet source destination timestamp))\n;; (define param_2 (send obj forward-packet))\n;; (define param_3 (send obj get-count destination start-time end-time))"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec router_init_(MemoryLimit :: integer()) -> any().\nrouter_init_(MemoryLimit) ->\n .\n\n-spec router_add_packet(Source :: integer(), Destination :: integer(), Timestamp :: integer()) -> boolean().\nrouter_add_packet(Source, Destination, Timestamp) ->\n .\n\n-spec router_forward_packet() -> [integer()].\nrouter_forward_packet() ->\n .\n\n-spec router_get_count(Destination :: integer(), StartTime :: integer(), EndTime :: integer()) -> integer().\nrouter_get_count(Destination, StartTime, EndTime) ->\n .\n\n\n%% Your functions will be called as such:\n%% router_init_(MemoryLimit),\n%% Param_1 = router_add_packet(Source, Destination, Timestamp),\n%% Param_2 = router_forward_packet(),\n%% Param_3 = router_get_count(Destination, StartTime, EndTime),\n\n%% router_init_ will be called before every test case, in which you can do some necessary initializations."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Router do\n @spec init_(memory_limit :: integer) :: any\n def init_(memory_limit) do\n \n end\n\n @spec add_packet(source :: integer, destination :: integer, timestamp :: integer) :: boolean\n def add_packet(source, destination, timestamp) do\n \n end\n\n @spec forward_packet() :: [integer]\n def forward_packet() do\n \n end\n\n @spec get_count(destination :: integer, start_time :: integer, end_time :: integer) :: integer\n def get_count(destination, start_time, end_time) do\n \n end\nend\n\n# Your functions will be called as such:\n# Router.init_(memory_limit)\n# param_1 = Router.add_packet(source, destination, timestamp)\n# param_2 = Router.forward_packet()\n# param_3 = Router.get_count(destination, start_time, end_time)\n\n# Router.init_ will be called before every test case, in which you can do some necessary initializations."}]
["Router","addPacket","addPacket","addPacket","addPacket","addPacket","forwardPacket","addPacket","getCount"] [[3],[1,4,90],[2,5,90],[1,4,90],[3,5,95],[4,5,105],[],[5,2,110],[5,100,110]]
{ "classname": "Router", "constructor": { "params": [ { "name": "memoryLimit", "type": "integer" } ] }, "methods": [ { "params": [ { "type": "integer", "name": "source" }, { "type": "integer", "name": "destination" }, { "type": "integer", "name": "timestamp" } ], "return": { "type": "boolean" }, "name": "addPacket" }, { "params": [], "name": "forwardPacket", "return": { "type": "integer[]" } }, { "params": [ { "type": "integer", "name": "destination" }, { "type": "integer", "name": "startTime" }, { "type": "integer", "name": "endTime" } ], "name": "getCount", "return": { "type": "integer" } } ], "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', 'Binary Search', 'Design', 'Queue', 'Ordered Set']
3,509
Maximum Product of Subsequences With an Alternating Sum Equal to K
maximum-product-of-subsequences-with-an-alternating-sum-equal-to-k
<p>You are given an integer array <code>nums</code> and two integers, <code>k</code> and <code>limit</code>. Your task is to find a non-empty <strong><span data-keyword="subsequence-array">subsequence</span></strong> of <code>nums</code> that:</p> <ul> <li>Has an <strong>alternating sum</strong> equal to <code>k</code>.</li> <li><strong>Maximizes</strong> the product of all its numbers <em>without the product exceeding</em> <code>limit</code>.</li> </ul> <p>Return the <em>product</em> of the numbers in such a subsequence. If no subsequence satisfies the requirements, return -1.</p> <p>The <strong>alternating sum</strong> of a <strong>0-indexed</strong> array is defined as the <strong>sum</strong> of the elements at <strong>even</strong> indices <strong>minus</strong> the <strong>sum</strong> of the elements at <strong>odd</strong> indices.</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,2,3], k = 2, limit = 10</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The subsequences with an alternating sum of 2 are:</p> <ul> <li><code>[1, 2, 3]</code> <ul> <li>Alternating Sum: <code>1 - 2 + 3 = 2</code></li> <li>Product: <code>1 * 2 * 3 = 6</code></li> </ul> </li> <li><code>[2]</code> <ul> <li>Alternating Sum: 2</li> <li>Product: 2</li> </ul> </li> </ul> <p>The maximum product within the limit is 6.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [0,2,3], k = -5, limit = 12</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong></p> <p>A subsequence with an alternating sum of exactly -5 does not exist.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,2,3,3], k = 0, limit = 9</span></p> <p><strong>Output:</strong> <span class="example-io">9</span></p> <p><strong>Explanation:</strong></p> <p>The subsequences with an alternating sum of 0 are:</p> <ul> <li><code>[2, 2]</code> <ul> <li>Alternating Sum: <code>2 - 2 = 0</code></li> <li>Product: <code>2 * 2 = 4</code></li> </ul> </li> <li><code>[3, 3]</code> <ul> <li>Alternating Sum: <code>3 - 3 = 0</code></li> <li>Product: <code>3 * 3 = 9</code></li> </ul> </li> <li><code>[2, 2, 3, 3]</code> <ul> <li>Alternating Sum: <code>2 - 2 + 3 - 3 = 0</code></li> <li>Product: <code>2 * 2 * 3 * 3 = 36</code></li> </ul> </li> </ul> <p>The subsequence <code>[2, 2, 3, 3]</code> has the greatest product with an alternating sum equal to <code>k</code>, but <code>36 &gt; 9</code>. The next greatest product is 9, which is within the limit.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 150</code></li> <li><code>0 &lt;= nums[i] &lt;= 12</code></li> <li><code>-10<sup>5</sup> &lt;= k &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= limit &lt;= 5000</code></li> </ul>
Hard
2.9K
29.6K
2,924
29,646
9.9%
['maximum-alternating-subsequence-sum']
[]
Algorithms
[{"value": "cpp", "text": "C++", "defaultCode": "class Solution {\npublic:\n int maxProduct(vector<int>& nums, int k, int limit) {\n \n }\n};"}, {"value": "java", "text": "Java", "defaultCode": "class Solution {\n public int maxProduct(int[] nums, int k, int limit) {\n \n }\n}"}, {"value": "python", "text": "Python", "defaultCode": "class Solution(object):\n def maxProduct(self, nums, k, limit):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :type limit: int\n :rtype: int\n \"\"\"\n "}, {"value": "python3", "text": "Python3", "defaultCode": "class Solution:\n def maxProduct(self, nums: List[int], k: int, limit: int) -> int:\n "}, {"value": "c", "text": "C", "defaultCode": "int maxProduct(int* nums, int numsSize, int k, int limit) {\n \n}"}, {"value": "csharp", "text": "C#", "defaultCode": "public class Solution {\n public int MaxProduct(int[] nums, int k, int limit) {\n \n }\n}"}, {"value": "javascript", "text": "JavaScript", "defaultCode": "/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} limit\n * @return {number}\n */\nvar maxProduct = function(nums, k, limit) {\n \n};"}, {"value": "typescript", "text": "TypeScript", "defaultCode": "function maxProduct(nums: number[], k: number, limit: number): number {\n \n};"}, {"value": "php", "text": "PHP", "defaultCode": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $k\n * @param Integer $limit\n * @return Integer\n */\n function maxProduct($nums, $k, $limit) {\n \n }\n}"}, {"value": "swift", "text": "Swift", "defaultCode": "class Solution {\n func maxProduct(_ nums: [Int], _ k: Int, _ limit: Int) -> Int {\n \n }\n}"}, {"value": "kotlin", "text": "Kotlin", "defaultCode": "class Solution {\n fun maxProduct(nums: IntArray, k: Int, limit: Int): Int {\n \n }\n}"}, {"value": "dart", "text": "Dart", "defaultCode": "class Solution {\n int maxProduct(List<int> nums, int k, int limit) {\n \n }\n}"}, {"value": "golang", "text": "Go", "defaultCode": "func maxProduct(nums []int, k int, limit int) int {\n \n}"}, {"value": "ruby", "text": "Ruby", "defaultCode": "# @param {Integer[]} nums\n# @param {Integer} k\n# @param {Integer} limit\n# @return {Integer}\ndef max_product(nums, k, limit)\n \nend"}, {"value": "scala", "text": "Scala", "defaultCode": "object Solution {\n def maxProduct(nums: Array[Int], k: Int, limit: Int): Int = {\n \n }\n}"}, {"value": "rust", "text": "Rust", "defaultCode": "impl Solution {\n pub fn max_product(nums: Vec<i32>, k: i32, limit: i32) -> i32 {\n \n }\n}"}, {"value": "racket", "text": "Racket", "defaultCode": "(define/contract (max-product nums k limit)\n (-> (listof exact-integer?) exact-integer? exact-integer? exact-integer?)\n )"}, {"value": "erlang", "text": "Erlang", "defaultCode": "-spec max_product(Nums :: [integer()], K :: integer(), Limit :: integer()) -> integer().\nmax_product(Nums, K, Limit) ->\n ."}, {"value": "elixir", "text": "Elixir", "defaultCode": "defmodule Solution do\n @spec max_product(nums :: [integer], k :: integer, limit :: integer) :: integer\n def max_product(nums, k, limit) do\n \n end\nend"}]
[1,2,3] 2 10
{ "name": "maxProduct", "params": [ { "name": "nums", "type": "integer[]" }, { "type": "integer", "name": "k" }, { "type": "integer", "name": "limit" } ], "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']