id
int64
1
3.65k
title
stringlengths
3
79
difficulty
stringclasses
3 values
description
stringlengths
430
25.4k
tags
stringlengths
0
131
language
stringclasses
19 values
solution
stringlengths
47
20.6k
3,177
Find the Maximum Length of a Good Subsequence II
Hard
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</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,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,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 = [1,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>3</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(50, nums.length)</code></li> </ul>
Array; Hash Table; Dynamic Programming
Python
class Solution: def maximumLength(self, nums: List[int], k: int) -> int: n = len(nums) f = [[0] * (k + 1) for _ in range(n)] mp = [defaultdict(int) for _ in range(k + 1)] g = [[0] * 3 for _ in range(k + 1)] ans = 0 for i, x in enumerate(nums): for h in range(k + 1): f[i][h] = mp[h][x] if h: if g[h - 1][0] != nums[i]: f[i][h] = max(f[i][h], g[h - 1][1]) else: f[i][h] = max(f[i][h], g[h - 1][2]) f[i][h] += 1 mp[h][nums[i]] = max(mp[h][nums[i]], f[i][h]) if g[h][0] != x: if f[i][h] >= g[h][1]: g[h][2] = g[h][1] g[h][1] = f[i][h] g[h][0] = x else: g[h][2] = max(g[h][2], f[i][h]) else: g[h][1] = max(g[h][1], f[i][h]) ans = max(ans, f[i][h]) return ans
3,177
Find the Maximum Length of a Good Subsequence II
Hard
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</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,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,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 = [1,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>3</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(50, nums.length)</code></li> </ul>
Array; Hash Table; Dynamic Programming
TypeScript
function maximumLength(nums: number[], k: number): number { const n = nums.length; const f: number[][] = Array.from({ length: n }, () => Array(k + 1).fill(0)); const mp: Map<number, number>[] = Array.from({ length: k + 1 }, () => new Map()); const g: number[][] = Array.from({ length: k + 1 }, () => Array(3).fill(0)); let ans = 0; for (let i = 0; i < n; i++) { for (let h = 0; h <= k; h++) { f[i][h] = mp[h].get(nums[i]) || 0; if (h > 0) { if (g[h - 1][0] !== nums[i]) { f[i][h] = Math.max(f[i][h], g[h - 1][1]); } else { f[i][h] = Math.max(f[i][h], g[h - 1][2]); } } f[i][h]++; mp[h].set(nums[i], Math.max(mp[h].get(nums[i]) || 0, f[i][h])); if (g[h][0] !== nums[i]) { if (f[i][h] >= g[h][1]) { g[h][2] = g[h][1]; g[h][1] = f[i][h]; g[h][0] = nums[i]; } else { g[h][2] = Math.max(g[h][2], f[i][h]); } } else { g[h][1] = Math.max(g[h][1], f[i][h]); } ans = Math.max(ans, f[i][h]); } } return ans; }
3,178
Find the Child Who Has the Ball After K Seconds
Easy
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p> <p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p> <p>Return the number of the child who receives the ball after <code>k</code> seconds.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, <u>4</u>]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3]</code></td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/pass-the-pillow/description/" target="_blank"> 2582: Pass the Pillow.</a></p>
Math; Simulation
C++
class Solution { public: int numberOfChild(int n, int k) { int mod = k % (n - 1); k /= (n - 1); return k % 2 == 1 ? n - mod - 1 : mod; } };
3,178
Find the Child Who Has the Ball After K Seconds
Easy
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p> <p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p> <p>Return the number of the child who receives the ball after <code>k</code> seconds.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, <u>4</u>]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3]</code></td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/pass-the-pillow/description/" target="_blank"> 2582: Pass the Pillow.</a></p>
Math; Simulation
Go
func numberOfChild(n int, k int) int { mod := k % (n - 1) k /= (n - 1) if k%2 == 1 { return n - mod - 1 } return mod }
3,178
Find the Child Who Has the Ball After K Seconds
Easy
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p> <p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p> <p>Return the number of the child who receives the ball after <code>k</code> seconds.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, <u>4</u>]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3]</code></td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/pass-the-pillow/description/" target="_blank"> 2582: Pass the Pillow.</a></p>
Math; Simulation
Java
class Solution { public int numberOfChild(int n, int k) { int mod = k % (n - 1); k /= (n - 1); return k % 2 == 1 ? n - mod - 1 : mod; } }
3,178
Find the Child Who Has the Ball After K Seconds
Easy
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p> <p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p> <p>Return the number of the child who receives the ball after <code>k</code> seconds.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, <u>4</u>]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3]</code></td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/pass-the-pillow/description/" target="_blank"> 2582: Pass the Pillow.</a></p>
Math; Simulation
Python
class Solution: def numberOfChild(self, n: int, k: int) -> int: k, mod = divmod(k, n - 1) return n - mod - 1 if k & 1 else mod
3,178
Find the Child Who Has the Ball After K Seconds
Easy
<p>You are given two <strong>positive</strong> integers <code>n</code> and <code>k</code>. There are <code>n</code> children numbered from <code>0</code> to <code>n - 1</code> standing in a queue <em>in order</em> from left to right.</p> <p>Initially, child 0 holds a ball and the direction of passing the ball is towards the right direction. After each second, the child holding the ball passes it to the child next to them. Once the ball reaches <strong>either</strong> end of the line, i.e. child 0 or child <code>n - 1</code>, the direction of passing is <strong>reversed</strong>.</p> <p>Return the number of the child who receives the ball after <code>k</code> seconds.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 3, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[<u>0</u>, 1, 2]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, <u>1</u>, 2]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 6</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3, 4]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3, 4]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> <tr> <td><code>3</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>4</code></td> <td><code>[0, 1, 2, 3, <u>4</u>]</code></td> </tr> <tr> <td><code>5</code></td> <td><code>[0, 1, 2, <u>3</u>, 4]</code></td> </tr> <tr> <td><code>6</code></td> <td><code>[0, 1, <u>2</u>, 3, 4]</code></td> </tr> </tbody> </table> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>Time elapsed</th> <th>Children</th> </tr> <tr> <td><code>0</code></td> <td><code>[<u>0</u>, 1, 2, 3]</code></td> </tr> <tr> <td><code>1</code></td> <td><code>[0, <u>1</u>, 2, 3]</code></td> </tr> <tr> <td><code>2</code></td> <td><code>[0, 1, <u>2</u>, 3]</code></td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 50</code></li> <li><code>1 &lt;= k &lt;= 50</code></li> </ul> <p>&nbsp;</p> <p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/pass-the-pillow/description/" target="_blank"> 2582: Pass the Pillow.</a></p>
Math; Simulation
TypeScript
function numberOfChild(n: number, k: number): number { const mod = k % (n - 1); k = (k / (n - 1)) | 0; return k % 2 ? n - mod - 1 : mod; }
3,179
Find the N-th Value After K Seconds
Medium
<p>You are given two integers <code>n</code> and <code>k</code>.</p> <p>Initially, you start with an array <code>a</code> of <code>n</code> integers where <code>a[i] = 1</code> for all <code>0 &lt;= i &lt;= n - 1</code>. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, <code>a[0]</code> remains the same, <code>a[1]</code> becomes <code>a[0] + a[1]</code>, <code>a[2]</code> becomes <code>a[0] + a[1] + a[2]</code>, and so on.</p> <p>Return the <strong>value</strong> of <code>a[n - 1]</code> after <code>k</code> seconds.</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">n = 4, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">56</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20]</td> </tr> <tr> <td>4</td> <td>[1,5,15,35]</td> </tr> <tr> <td>5</td> <td>[1,6,21,56]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">35</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4,5]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10,15]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20,35]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 1000</code></li> </ul>
Array; Math; Combinatorics; Prefix Sum; Simulation
C++
class Solution { public: int valueAfterKSeconds(int n, int k) { const int mod = 1e9 + 7; vector<int> a(n, 1); while (k-- > 0) { for (int i = 1; i < n; ++i) { a[i] = (a[i] + a[i - 1]) % mod; } } return a[n - 1]; } };
3,179
Find the N-th Value After K Seconds
Medium
<p>You are given two integers <code>n</code> and <code>k</code>.</p> <p>Initially, you start with an array <code>a</code> of <code>n</code> integers where <code>a[i] = 1</code> for all <code>0 &lt;= i &lt;= n - 1</code>. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, <code>a[0]</code> remains the same, <code>a[1]</code> becomes <code>a[0] + a[1]</code>, <code>a[2]</code> becomes <code>a[0] + a[1] + a[2]</code>, and so on.</p> <p>Return the <strong>value</strong> of <code>a[n - 1]</code> after <code>k</code> seconds.</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">n = 4, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">56</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20]</td> </tr> <tr> <td>4</td> <td>[1,5,15,35]</td> </tr> <tr> <td>5</td> <td>[1,6,21,56]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">35</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4,5]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10,15]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20,35]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 1000</code></li> </ul>
Array; Math; Combinatorics; Prefix Sum; Simulation
Go
func valueAfterKSeconds(n int, k int) int { const mod int = 1e9 + 7 a := make([]int, n) for i := range a { a[i] = 1 } for ; k > 0; k-- { for i := 1; i < n; i++ { a[i] = (a[i] + a[i-1]) % mod } } return a[n-1] }
3,179
Find the N-th Value After K Seconds
Medium
<p>You are given two integers <code>n</code> and <code>k</code>.</p> <p>Initially, you start with an array <code>a</code> of <code>n</code> integers where <code>a[i] = 1</code> for all <code>0 &lt;= i &lt;= n - 1</code>. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, <code>a[0]</code> remains the same, <code>a[1]</code> becomes <code>a[0] + a[1]</code>, <code>a[2]</code> becomes <code>a[0] + a[1] + a[2]</code>, and so on.</p> <p>Return the <strong>value</strong> of <code>a[n - 1]</code> after <code>k</code> seconds.</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">n = 4, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">56</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20]</td> </tr> <tr> <td>4</td> <td>[1,5,15,35]</td> </tr> <tr> <td>5</td> <td>[1,6,21,56]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">35</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4,5]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10,15]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20,35]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 1000</code></li> </ul>
Array; Math; Combinatorics; Prefix Sum; Simulation
Java
class Solution { public int valueAfterKSeconds(int n, int k) { final int mod = (int) 1e9 + 7; int[] a = new int[n]; Arrays.fill(a, 1); while (k-- > 0) { for (int i = 1; i < n; ++i) { a[i] = (a[i] + a[i - 1]) % mod; } } return a[n - 1]; } }
3,179
Find the N-th Value After K Seconds
Medium
<p>You are given two integers <code>n</code> and <code>k</code>.</p> <p>Initially, you start with an array <code>a</code> of <code>n</code> integers where <code>a[i] = 1</code> for all <code>0 &lt;= i &lt;= n - 1</code>. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, <code>a[0]</code> remains the same, <code>a[1]</code> becomes <code>a[0] + a[1]</code>, <code>a[2]</code> becomes <code>a[0] + a[1] + a[2]</code>, and so on.</p> <p>Return the <strong>value</strong> of <code>a[n - 1]</code> after <code>k</code> seconds.</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">n = 4, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">56</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20]</td> </tr> <tr> <td>4</td> <td>[1,5,15,35]</td> </tr> <tr> <td>5</td> <td>[1,6,21,56]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">35</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4,5]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10,15]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20,35]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 1000</code></li> </ul>
Array; Math; Combinatorics; Prefix Sum; Simulation
Python
class Solution: def valueAfterKSeconds(self, n: int, k: int) -> int: a = [1] * n mod = 10**9 + 7 for _ in range(k): for i in range(1, n): a[i] = (a[i] + a[i - 1]) % mod return a[n - 1]
3,179
Find the N-th Value After K Seconds
Medium
<p>You are given two integers <code>n</code> and <code>k</code>.</p> <p>Initially, you start with an array <code>a</code> of <code>n</code> integers where <code>a[i] = 1</code> for all <code>0 &lt;= i &lt;= n - 1</code>. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, <code>a[0]</code> remains the same, <code>a[1]</code> becomes <code>a[0] + a[1]</code>, <code>a[2]</code> becomes <code>a[0] + a[1] + a[2]</code>, and so on.</p> <p>Return the <strong>value</strong> of <code>a[n - 1]</code> after <code>k</code> seconds.</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">n = 4, k = 5</span></p> <p><strong>Output:</strong> <span class="example-io">56</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20]</td> </tr> <tr> <td>4</td> <td>[1,5,15,35]</td> </tr> <tr> <td>5</td> <td>[1,6,21,56]</td> </tr> </tbody> </table> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">35</span></p> <p><strong>Explanation:</strong></p> <table border="1"> <tbody> <tr> <th>Second</th> <th>State After</th> </tr> <tr> <td>0</td> <td>[1,1,1,1,1]</td> </tr> <tr> <td>1</td> <td>[1,2,3,4,5]</td> </tr> <tr> <td>2</td> <td>[1,3,6,10,15]</td> </tr> <tr> <td>3</td> <td>[1,4,10,20,35]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n, k &lt;= 1000</code></li> </ul>
Array; Math; Combinatorics; Prefix Sum; Simulation
TypeScript
function valueAfterKSeconds(n: number, k: number): number { const a: number[] = Array(n).fill(1); const mod: number = 10 ** 9 + 7; while (k--) { for (let i = 1; i < n; ++i) { a[i] = (a[i] + a[i - 1]) % mod; } } return a[n - 1]; }
3,180
Maximum Total Reward Using Operations I
Medium
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 2000</code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 2000</code></li> </ul>
Array; Dynamic Programming
C++
class Solution { public: int maxTotalReward(vector<int>& rewardValues) { sort(rewardValues.begin(), rewardValues.end()); int n = rewardValues.size(); int f[rewardValues.back() << 1]; memset(f, -1, sizeof(f)); function<int(int)> dfs = [&](int x) { if (f[x] != -1) { return f[x]; } auto it = upper_bound(rewardValues.begin(), rewardValues.end(), x); int ans = 0; for (; it != rewardValues.end(); ++it) { ans = max(ans, rewardValues[it - rewardValues.begin()] + dfs(x + *it)); } return f[x] = ans; }; return dfs(0); } };
3,180
Maximum Total Reward Using Operations I
Medium
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 2000</code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 2000</code></li> </ul>
Array; Dynamic Programming
Go
func maxTotalReward(rewardValues []int) int { sort.Ints(rewardValues) n := len(rewardValues) f := make([]int, rewardValues[n-1]<<1) for i := range f { f[i] = -1 } var dfs func(int) int dfs = func(x int) int { if f[x] != -1 { return f[x] } i := sort.SearchInts(rewardValues, x+1) f[x] = 0 for _, v := range rewardValues[i:] { f[x] = max(f[x], v+dfs(x+v)) } return f[x] } return dfs(0) }
3,180
Maximum Total Reward Using Operations I
Medium
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 2000</code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 2000</code></li> </ul>
Array; Dynamic Programming
Java
class Solution { private int[] nums; private Integer[] f; public int maxTotalReward(int[] rewardValues) { nums = rewardValues; Arrays.sort(nums); int n = nums.length; f = new Integer[nums[n - 1] << 1]; return dfs(0); } private int dfs(int x) { if (f[x] != null) { return f[x]; } int i = Arrays.binarySearch(nums, x + 1); i = i < 0 ? -i - 1 : i; int ans = 0; for (; i < nums.length; ++i) { ans = Math.max(ans, nums[i] + dfs(x + nums[i])); } return f[x] = ans; } }
3,180
Maximum Total Reward Using Operations I
Medium
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 2000</code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 2000</code></li> </ul>
Array; Dynamic Programming
Python
class Solution: def maxTotalReward(self, rewardValues: List[int]) -> int: @cache def dfs(x: int) -> int: i = bisect_right(rewardValues, x) ans = 0 for v in rewardValues[i:]: ans = max(ans, v + dfs(x + v)) return ans rewardValues.sort() return dfs(0)
3,180
Maximum Total Reward Using Operations I
Medium
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 2000</code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 2000</code></li> </ul>
Array; Dynamic Programming
TypeScript
function maxTotalReward(rewardValues: number[]): number { rewardValues.sort((a, b) => a - b); const search = (x: number): number => { let [l, r] = [0, rewardValues.length]; while (l < r) { const mid = (l + r) >> 1; if (rewardValues[mid] > x) { r = mid; } else { l = mid + 1; } } return l; }; const f: number[] = Array(rewardValues.at(-1)! << 1).fill(-1); const dfs = (x: number): number => { if (f[x] !== -1) { return f[x]; } let ans = 0; for (let i = search(x); i < rewardValues.length; ++i) { ans = Math.max(ans, rewardValues[i] + dfs(x + rewardValues[i])); } return (f[x] = ans); }; return dfs(0); }
3,181
Maximum Total Reward Using Operations II
Hard
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 5 * 10<sup>4</sup></code></li> </ul>
Bit Manipulation; Array; Dynamic Programming
C++
class Solution { public: int maxTotalReward(vector<int>& rewardValues) { sort(rewardValues.begin(), rewardValues.end()); rewardValues.erase(unique(rewardValues.begin(), rewardValues.end()), rewardValues.end()); bitset<100000> f{1}; for (int v : rewardValues) { int shift = f.size() - v; f |= f << shift >> (shift - v); } for (int i = rewardValues.back() * 2 - 1;; i--) { if (f.test(i)) { return i; } } } };
3,181
Maximum Total Reward Using Operations II
Hard
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 5 * 10<sup>4</sup></code></li> </ul>
Bit Manipulation; Array; Dynamic Programming
Go
func maxTotalReward(rewardValues []int) int { slices.Sort(rewardValues) rewardValues = slices.Compact(rewardValues) one := big.NewInt(1) f := big.NewInt(1) p := new(big.Int) for _, v := range rewardValues { mask := p.Sub(p.Lsh(one, uint(v)), one) f.Or(f, p.Lsh(p.And(f, mask), uint(v))) } return f.BitLen() - 1 }
3,181
Maximum Total Reward Using Operations II
Hard
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 5 * 10<sup>4</sup></code></li> </ul>
Bit Manipulation; Array; Dynamic Programming
Java
import java.math.BigInteger; class Solution { public int maxTotalReward(int[] rewardValues) { int[] nums = Arrays.stream(rewardValues).distinct().sorted().toArray(); BigInteger f = BigInteger.ONE; for (int v : nums) { BigInteger mask = BigInteger.ONE.shiftLeft(v).subtract(BigInteger.ONE); BigInteger shifted = f.and(mask).shiftLeft(v); f = f.or(shifted); } return f.bitLength() - 1; } }
3,181
Maximum Total Reward Using Operations II
Hard
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 5 * 10<sup>4</sup></code></li> </ul>
Bit Manipulation; Array; Dynamic Programming
Python
class Solution: def maxTotalReward(self, rewardValues: List[int]) -> int: nums = sorted(set(rewardValues)) f = 1 for v in nums: f |= (f & ((1 << v) - 1)) << v return f.bit_length() - 1
3,181
Maximum Total Reward Using Operations II
Hard
<p>You are given an integer array <code>rewardValues</code> of length <code>n</code>, representing the values of rewards.</p> <p>Initially, your total reward <code>x</code> is 0, and all indices are <strong>unmarked</strong>. You are allowed to perform the following operation <strong>any</strong> number of times:</p> <ul> <li>Choose an <strong>unmarked</strong> index <code>i</code> from the range <code>[0, n - 1]</code>.</li> <li>If <code>rewardValues[i]</code> is <strong>greater</strong> than your current total reward <code>x</code>, then add <code>rewardValues[i]</code> to <code>x</code> (i.e., <code>x = x + rewardValues[i]</code>), and <strong>mark</strong> the index <code>i</code>.</li> </ul> <p>Return an integer denoting the <strong>maximum </strong><em>total reward</em> you can collect by performing the operations optimally.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rewardValues = [1,1,3,3]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>During the operations, we can choose to mark the indices 0 and 2 in order, and the total reward will be 4, 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">rewardValues = [1,6,4,3,2]</span></p> <p><strong>Output:</strong> <span class="example-io">11</span></p> <p><strong>Explanation:</strong></p> <p>Mark the indices 0, 2, and 1 in order. The total reward will then be 11, which is the maximum.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= rewardValues.length &lt;= 5 * 10<sup>4</sup></code></li> <li><code>1 &lt;= rewardValues[i] &lt;= 5 * 10<sup>4</sup></code></li> </ul>
Bit Manipulation; Array; Dynamic Programming
TypeScript
function maxTotalReward(rewardValues: number[]): number { rewardValues.sort((a, b) => a - b); rewardValues = [...new Set(rewardValues)]; let f = 1n; for (const x of rewardValues) { const mask = (1n << BigInt(x)) - 1n; f = f | ((f & mask) << BigInt(x)); } return f.toString(2).length - 1; }
3,182
Find Top Scoring Students
Medium
<p>Table: <code>students</code></p> <pre> +-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | name | varchar | | major | varchar | +-------------+----------+ student_id is the primary key (combination of columns with unique values) for this table. Each row of this table contains the student ID, student name, and their major. </pre> <p>Table: <code>courses</code></p> <pre> +-------------+----------+ | Column Name | Type | +-------------+----------+ | course_id | int | | name | varchar | | credits | int | | major | varchar | +-------------+----------+ course_id is the primary key (combination of columns with unique values) for this table. Each row of this table contains the course ID, course name, the number of credits for the course, and the major it belongs to. </pre> <p>Table: <code>enrollments</code></p> <pre> +-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | course_id | int | | semester | varchar | | grade | varchar | +-------------+----------+ (student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table. Each row of this table contains the student ID, course ID, semester, and grade received. </pre> <p>Write a solution to find the students who have <strong>taken</strong> <strong>all courses</strong> offered in their <code>major</code> and have achieved a <strong>grade of A</strong> <strong>in all these courses</strong>.</p> <p>Return <em>the result table ordered by</em> <code>student_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>students table:</p> <pre class="example-io"> +------------+------------------+------------------+ | student_id | name | major | +------------+------------------+------------------+ | 1 | Alice | Computer Science | | 2 | Bob | Computer Science | | 3 | Charlie | Mathematics | | 4 | David | Mathematics | +------------+------------------+------------------+ </pre> <p>courses table:</p> <pre class="example-io"> +-----------+-----------------+---------+------------------+ | course_id | name | credits | major | +-----------+-----------------+---------+------------------+ | 101 | Algorithms | 3 | Computer Science | | 102 | Data Structures | 3 | Computer Science | | 103 | Calculus | 4 | Mathematics | | 104 | Linear Algebra | 4 | Mathematics | +-----------+-----------------+---------+------------------+ </pre> <p>enrollments table:</p> <pre class="example-io"> +------------+-----------+----------+-------+ | student_id | course_id | semester | grade | +------------+-----------+----------+-------+ | 1 | 101 | Fall 2023| A | | 1 | 102 | Fall 2023| A | | 2 | 101 | Fall 2023| B | | 2 | 102 | Fall 2023| A | | 3 | 103 | Fall 2023| A | | 3 | 104 | Fall 2023| A | | 4 | 103 | Fall 2023| A | | 4 | 104 | Fall 2023| B | +------------+-----------+----------+-------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +------------+ | student_id | +------------+ | 1 | | 3 | +------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Alice (student_id 1) is a Computer Science major and has taken both &quot;Algorithms&quot; and &quot;Data Structures&quot;, receiving an &#39;A&#39; in both.</li> <li>Bob (student_id 2) is a Computer Science major but did not receive an &#39;A&#39; in all required courses.</li> <li>Charlie (student_id 3) is a Mathematics major and has taken both &quot;Calculus&quot; and &quot;Linear Algebra&quot;, receiving an &#39;A&#39; in both.</li> <li>David (student_id 4) is a Mathematics major but did not receive an &#39;A&#39; in all required courses.</li> </ul> <p><b>Note:</b> Output table is ordered by student_id in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below SELECT student_id FROM students JOIN courses USING (major) LEFT JOIN enrollments USING (student_id, course_id) GROUP BY 1 HAVING SUM(grade = 'A') = COUNT(major) ORDER BY 1;
3,183
The Number of Ways to Make the Sum
Medium
<p>You have an <strong>infinite</strong> number of coins with values 1, 2, and 6, and <strong>only</strong> 2 coins with value 4.</p> <p>Given an integer <code>n</code>, return the number of ways to make the sum of <code>n</code> with the coins you have.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that the order of the coins doesn&#39;t matter and <code>[2, 2, 3]</code> is the same as <code>[2, 3, 2]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1]</code>, <code>[1, 1, 2]</code>, <code>[2, 2]</code>, <code>[4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 12</span></p> <p><strong>Output:</strong> <span class="example-io">22</span></p> <p><strong>Explanation:</strong></p> <p>Note that <code>[4, 4, 4]</code> is <strong>not</strong> a valid combination since we cannot use 4 three times.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1, 1]</code>, <code>[1, 1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[1, 4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Array; Dynamic Programming
C++
class Solution { public: int numberOfWays(int n) { const int mod = 1e9 + 7; int coins[3] = {1, 2, 6}; int f[n + 1]; memset(f, 0, sizeof(f)); f[0] = 1; for (int x : coins) { for (int j = x; j <= n; ++j) { f[j] = (f[j] + f[j - x]) % mod; } } int ans = f[n]; if (n >= 4) { ans = (ans + f[n - 4]) % mod; } if (n >= 8) { ans = (ans + f[n - 8]) % mod; } return ans; } };
3,183
The Number of Ways to Make the Sum
Medium
<p>You have an <strong>infinite</strong> number of coins with values 1, 2, and 6, and <strong>only</strong> 2 coins with value 4.</p> <p>Given an integer <code>n</code>, return the number of ways to make the sum of <code>n</code> with the coins you have.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that the order of the coins doesn&#39;t matter and <code>[2, 2, 3]</code> is the same as <code>[2, 3, 2]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1]</code>, <code>[1, 1, 2]</code>, <code>[2, 2]</code>, <code>[4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 12</span></p> <p><strong>Output:</strong> <span class="example-io">22</span></p> <p><strong>Explanation:</strong></p> <p>Note that <code>[4, 4, 4]</code> is <strong>not</strong> a valid combination since we cannot use 4 three times.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1, 1]</code>, <code>[1, 1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[1, 4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Array; Dynamic Programming
Go
func numberOfWays(n int) int { const mod int = 1e9 + 7 coins := []int{1, 2, 6} f := make([]int, n+1) f[0] = 1 for _, x := range coins { for j := x; j <= n; j++ { f[j] = (f[j] + f[j-x]) % mod } } ans := f[n] if n >= 4 { ans = (ans + f[n-4]) % mod } if n >= 8 { ans = (ans + f[n-8]) % mod } return ans }
3,183
The Number of Ways to Make the Sum
Medium
<p>You have an <strong>infinite</strong> number of coins with values 1, 2, and 6, and <strong>only</strong> 2 coins with value 4.</p> <p>Given an integer <code>n</code>, return the number of ways to make the sum of <code>n</code> with the coins you have.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that the order of the coins doesn&#39;t matter and <code>[2, 2, 3]</code> is the same as <code>[2, 3, 2]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1]</code>, <code>[1, 1, 2]</code>, <code>[2, 2]</code>, <code>[4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 12</span></p> <p><strong>Output:</strong> <span class="example-io">22</span></p> <p><strong>Explanation:</strong></p> <p>Note that <code>[4, 4, 4]</code> is <strong>not</strong> a valid combination since we cannot use 4 three times.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1, 1]</code>, <code>[1, 1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[1, 4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Array; Dynamic Programming
Java
class Solution { public int numberOfWays(int n) { final int mod = (int) 1e9 + 7; int[] coins = {1, 2, 6}; int[] f = new int[n + 1]; f[0] = 1; for (int x : coins) { for (int j = x; j <= n; ++j) { f[j] = (f[j] + f[j - x]) % mod; } } int ans = f[n]; if (n >= 4) { ans = (ans + f[n - 4]) % mod; } if (n >= 8) { ans = (ans + f[n - 8]) % mod; } return ans; } }
3,183
The Number of Ways to Make the Sum
Medium
<p>You have an <strong>infinite</strong> number of coins with values 1, 2, and 6, and <strong>only</strong> 2 coins with value 4.</p> <p>Given an integer <code>n</code>, return the number of ways to make the sum of <code>n</code> with the coins you have.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that the order of the coins doesn&#39;t matter and <code>[2, 2, 3]</code> is the same as <code>[2, 3, 2]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1]</code>, <code>[1, 1, 2]</code>, <code>[2, 2]</code>, <code>[4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 12</span></p> <p><strong>Output:</strong> <span class="example-io">22</span></p> <p><strong>Explanation:</strong></p> <p>Note that <code>[4, 4, 4]</code> is <strong>not</strong> a valid combination since we cannot use 4 three times.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1, 1]</code>, <code>[1, 1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[1, 4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Array; Dynamic Programming
Python
class Solution: def numberOfWays(self, n: int) -> int: mod = 10**9 + 7 coins = [1, 2, 6] f = [0] * (n + 1) f[0] = 1 for x in coins: for j in range(x, n + 1): f[j] = (f[j] + f[j - x]) % mod ans = f[n] if n >= 4: ans = (ans + f[n - 4]) % mod if n >= 8: ans = (ans + f[n - 8]) % mod return ans
3,183
The Number of Ways to Make the Sum
Medium
<p>You have an <strong>infinite</strong> number of coins with values 1, 2, and 6, and <strong>only</strong> 2 coins with value 4.</p> <p>Given an integer <code>n</code>, return the number of ways to make the sum of <code>n</code> with the coins you have.</p> <p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Note</strong> that the order of the coins doesn&#39;t matter and <code>[2, 2, 3]</code> is the same as <code>[2, 3, 2]</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 4</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1]</code>, <code>[1, 1, 2]</code>, <code>[2, 2]</code>, <code>[4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 12</span></p> <p><strong>Output:</strong> <span class="example-io">22</span></p> <p><strong>Explanation:</strong></p> <p>Note that <code>[4, 4, 4]</code> is <strong>not</strong> a valid combination since we cannot use 4 three times.</p> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 5</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Here are the four combinations: <code>[1, 1, 1, 1, 1]</code>, <code>[1, 1, 1, 2]</code>, <code>[1, 2, 2]</code>, <code>[1, 4]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> </ul>
Array; Dynamic Programming
TypeScript
function numberOfWays(n: number): number { const mod = 10 ** 9 + 7; const f: number[] = Array(n + 1).fill(0); f[0] = 1; for (const x of [1, 2, 6]) { for (let j = x; j <= n; ++j) { f[j] = (f[j] + f[j - x]) % mod; } } let ans = f[n]; if (n >= 4) { ans = (ans + f[n - 4]) % mod; } if (n >= 8) { ans = (ans + f[n - 8]) % mod; } return ans; }
3,184
Count Pairs That Form a Complete Day I
Easy
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 100</code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
C++
class Solution { public: int countCompleteDayPairs(vector<int>& hours) { int cnt[24]{}; int ans = 0; for (int x : hours) { ans += cnt[(24 - x % 24) % 24]; ++cnt[x % 24]; } return ans; } };
3,184
Count Pairs That Form a Complete Day I
Easy
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 100</code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Go
func countCompleteDayPairs(hours []int) (ans int) { cnt := [24]int{} for _, x := range hours { ans += cnt[(24-x%24)%24] cnt[x%24]++ } return }
3,184
Count Pairs That Form a Complete Day I
Easy
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 100</code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Java
class Solution { public int countCompleteDayPairs(int[] hours) { int[] cnt = new int[24]; int ans = 0; for (int x : hours) { ans += cnt[(24 - x % 24) % 24]; ++cnt[x % 24]; } return ans; } }
3,184
Count Pairs That Form a Complete Day I
Easy
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 100</code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Python
class Solution: def countCompleteDayPairs(self, hours: List[int]) -> int: cnt = Counter() ans = 0 for x in hours: ans += cnt[(24 - (x % 24)) % 24] cnt[x % 24] += 1 return ans
3,184
Count Pairs That Form a Complete Day I
Easy
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 100</code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
TypeScript
function countCompleteDayPairs(hours: number[]): number { const cnt: number[] = Array(24).fill(0); let ans: number = 0; for (const x of hours) { ans += cnt[(24 - (x % 24)) % 24]; ++cnt[x % 24]; } return ans; }
3,185
Count Pairs That Form a Complete Day II
Medium
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
C++
class Solution { public: long long countCompleteDayPairs(vector<int>& hours) { int cnt[24]{}; long long ans = 0; for (int x : hours) { ans += cnt[(24 - x % 24) % 24]; ++cnt[x % 24]; } return ans; } };
3,185
Count Pairs That Form a Complete Day II
Medium
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Go
func countCompleteDayPairs(hours []int) (ans int64) { cnt := [24]int{} for _, x := range hours { ans += int64(cnt[(24-x%24)%24]) cnt[x%24]++ } return }
3,185
Count Pairs That Form a Complete Day II
Medium
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Java
class Solution { public long countCompleteDayPairs(int[] hours) { int[] cnt = new int[24]; long ans = 0; for (int x : hours) { ans += cnt[(24 - x % 24) % 24]; ++cnt[x % 24]; } return ans; } }
3,185
Count Pairs That Form a Complete Day II
Medium
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
Python
class Solution: def countCompleteDayPairs(self, hours: List[int]) -> int: cnt = Counter() ans = 0 for x in hours: ans += cnt[(24 - (x % 24)) % 24] cnt[x % 24] += 1 return ans
3,185
Count Pairs That Form a Complete Day II
Medium
<p>Given an integer array <code>hours</code> representing times in <strong>hours</strong>, return an integer denoting the number of pairs <code>i</code>, <code>j</code> where <code>i &lt; j</code> and <code>hours[i] + hours[j]</code> forms a <strong>complete day</strong>.</p> <p>A <strong>complete day</strong> is defined as a time duration that is an <strong>exact</strong> <strong>multiple</strong> of 24 hours.</p> <p>For example, 1 day is 24 hours, 2 days is 48 hours, 3 days is 72 hours, and so on.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [12,12,30,24,24]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code> and <code>(3, 4)</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">hours = [72,48,24,3]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong> The pairs of indices that form a complete day are <code>(0, 1)</code>, <code>(0, 2)</code>, and <code>(1, 2)</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= hours.length &lt;= 5 * 10<sup>5</sup></code></li> <li><code>1 &lt;= hours[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Counting
TypeScript
function countCompleteDayPairs(hours: number[]): number { const cnt: number[] = Array(24).fill(0); let ans: number = 0; for (const x of hours) { ans += cnt[(24 - (x % 24)) % 24]; ++cnt[x % 24]; } return ans; }
3,186
Maximum Total Damage With Spell Casting
Medium
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p> <p>Each spell can be cast <strong>only once</strong>.</p> <p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Two Pointers; Binary Search; Dynamic Programming; Counting; Sorting
C++
class Solution { public: long long maximumTotalDamage(vector<int>& power) { sort(power.begin(), power.end()); this->power = power; n = power.size(); f.resize(n); nxt.resize(n); for (int i = 0; i < n; ++i) { cnt[power[i]]++; nxt[i] = upper_bound(power.begin() + i + 1, power.end(), power[i] + 2) - power.begin(); } return dfs(0); } private: unordered_map<int, int> cnt; vector<long long> f; vector<int> power; vector<int> nxt; int n; long long dfs(int i) { if (i >= n) { return 0; } if (f[i]) { return f[i]; } long long a = dfs(i + cnt[power[i]]); long long b = 1LL * power[i] * cnt[power[i]] + dfs(nxt[i]); return f[i] = max(a, b); } };
3,186
Maximum Total Damage With Spell Casting
Medium
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p> <p>Each spell can be cast <strong>only once</strong>.</p> <p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Two Pointers; Binary Search; Dynamic Programming; Counting; Sorting
Go
func maximumTotalDamage(power []int) int64 { n := len(power) sort.Ints(power) cnt := map[int]int{} nxt := make([]int, n) f := make([]int64, n) for i, x := range power { cnt[x]++ nxt[i] = sort.SearchInts(power, x+3) } var dfs func(int) int64 dfs = func(i int) int64 { if i >= n { return 0 } if f[i] != 0 { return f[i] } a := dfs(i + cnt[power[i]]) b := int64(power[i]*cnt[power[i]]) + dfs(nxt[i]) f[i] = max(a, b) return f[i] } return dfs(0) }
3,186
Maximum Total Damage With Spell Casting
Medium
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p> <p>Each spell can be cast <strong>only once</strong>.</p> <p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Two Pointers; Binary Search; Dynamic Programming; Counting; Sorting
Java
class Solution { private Long[] f; private int[] power; private Map<Integer, Integer> cnt; private int[] nxt; private int n; public long maximumTotalDamage(int[] power) { Arrays.sort(power); this.power = power; n = power.length; f = new Long[n]; cnt = new HashMap<>(n); nxt = new int[n]; for (int i = 0; i < n; ++i) { cnt.merge(power[i], 1, Integer::sum); int l = Arrays.binarySearch(power, power[i] + 3); l = l < 0 ? -l - 1 : l; nxt[i] = l; } return dfs(0); } private long dfs(int i) { if (i >= n) { return 0; } if (f[i] != null) { return f[i]; } long a = dfs(i + cnt.get(power[i])); long b = 1L * power[i] * cnt.get(power[i]) + dfs(nxt[i]); return f[i] = Math.max(a, b); } }
3,186
Maximum Total Damage With Spell Casting
Medium
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p> <p>Each spell can be cast <strong>only once</strong>.</p> <p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Two Pointers; Binary Search; Dynamic Programming; Counting; Sorting
Python
class Solution: def maximumTotalDamage(self, power: List[int]) -> int: @cache def dfs(i: int) -> int: if i >= n: return 0 a = dfs(i + cnt[power[i]]) b = power[i] * cnt[power[i]] + dfs(nxt[i]) return max(a, b) n = len(power) cnt = Counter(power) power.sort() nxt = [bisect_right(power, x + 2, lo=i + 1) for i, x in enumerate(power)] return dfs(0)
3,186
Maximum Total Damage With Spell Casting
Medium
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</strong> cast any spell with a damage of <code>power[i] - 2</code>, <code>power[i] - 1</code>, <code>power[i] + 1</code>, or <code>power[i] + 2</code>.</p> <p>Each spell can be cast <strong>only once</strong>.</p> <p>Return the <strong>maximum</strong> possible <em>total damage</em> that a magician can cast.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [1,1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 6 is produced by casting spells 0, 1, 3 with damage 1, 1, 4.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">power = [7,1,6,6]</span></p> <p><strong>Output:</strong> <span class="example-io">13</span></p> <p><strong>Explanation:</strong></p> <p>The maximum possible damage of 13 is produced by casting spells 1, 2, 3 with damage 1, 6, 6.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= power.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= power[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Hash Table; Two Pointers; Binary Search; Dynamic Programming; Counting; Sorting
TypeScript
function maximumTotalDamage(power: number[]): number { const n = power.length; power.sort((a, b) => a - b); const f: number[] = Array(n).fill(0); const cnt: Record<number, number> = {}; const nxt: number[] = Array(n).fill(0); for (let i = 0; i < n; ++i) { cnt[power[i]] = (cnt[power[i]] || 0) + 1; let [l, r] = [i + 1, n]; while (l < r) { const mid = (l + r) >> 1; if (power[mid] > power[i] + 2) { r = mid; } else { l = mid + 1; } } nxt[i] = l; } const dfs = (i: number): number => { if (i >= n) { return 0; } if (f[i]) { return f[i]; } const a = dfs(i + cnt[power[i]]); const b = power[i] * cnt[power[i]] + dfs(nxt[i]); return (f[i] = Math.max(a, b)); }; return dfs(0); }
3,187
Peaks in Array
Hard
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul> <li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword="subarray">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li> <li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face="monospace">val<sub>i</sub></font></code>.</li> </ul> <p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p> <p><strong>Notes:</strong></p> <ul> <li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</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">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0]</span></p> <p><strong>Explanation:</strong></p> <p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p> <p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p> <p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p> <p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li> <li>For all <code>i</code> that: <ul> <li><code>queries[i][0] == 1</code>: <code>0 &lt;= queries[i][1] &lt;= queries[i][2] &lt;= nums.length - 1</code></li> <li><code>queries[i][0] == 2</code>: <code>0 &lt;= queries[i][1] &lt;= nums.length - 1</code>, <code>1 &lt;= queries[i][2] &lt;= 10<sup>5</sup></code></li> </ul> </li> </ul>
Binary Indexed Tree; Segment Tree; Array
C++
class BinaryIndexedTree { private: int n; vector<int> c; public: BinaryIndexedTree(int n) : n(n) , c(n + 1) {} void update(int x, int delta) { for (; x <= n; x += x & -x) { c[x] += delta; } } int query(int x) { int s = 0; for (; x > 0; x -= x & -x) { s += c[x]; } return s; } }; class Solution { public: vector<int> countOfPeaks(vector<int>& nums, vector<vector<int>>& queries) { int n = nums.size(); BinaryIndexedTree tree(n - 1); auto update = [&](int i, int val) { if (i <= 0 || i >= n - 1) { return; } if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { tree.update(i, val); } }; for (int i = 1; i < n - 1; ++i) { update(i, 1); } vector<int> ans; for (auto& q : queries) { if (q[0] == 1) { int l = q[1] + 1, r = q[2] - 1; ans.push_back(l > r ? 0 : tree.query(r) - tree.query(l - 1)); } else { int idx = q[1], val = q[2]; for (int i = idx - 1; i <= idx + 1; ++i) { update(i, -1); } nums[idx] = val; for (int i = idx - 1; i <= idx + 1; ++i) { update(i, 1); } } } return ans; } };
3,187
Peaks in Array
Hard
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul> <li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword="subarray">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li> <li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face="monospace">val<sub>i</sub></font></code>.</li> </ul> <p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p> <p><strong>Notes:</strong></p> <ul> <li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</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">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0]</span></p> <p><strong>Explanation:</strong></p> <p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p> <p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p> <p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p> <p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li> <li>For all <code>i</code> that: <ul> <li><code>queries[i][0] == 1</code>: <code>0 &lt;= queries[i][1] &lt;= queries[i][2] &lt;= nums.length - 1</code></li> <li><code>queries[i][0] == 2</code>: <code>0 &lt;= queries[i][1] &lt;= nums.length - 1</code>, <code>1 &lt;= queries[i][2] &lt;= 10<sup>5</sup></code></li> </ul> </li> </ul>
Binary Indexed Tree; Segment Tree; Array
Go
type BinaryIndexedTree struct { n int c []int } func NewBinaryIndexedTree(n int) *BinaryIndexedTree { return &BinaryIndexedTree{n: n, c: make([]int, n+1)} } func (bit *BinaryIndexedTree) update(x, delta int) { for ; x <= bit.n; x += x & -x { bit.c[x] += delta } } func (bit *BinaryIndexedTree) query(x int) int { s := 0 for ; x > 0; x -= x & -x { s += bit.c[x] } return s } func countOfPeaks(nums []int, queries [][]int) (ans []int) { n := len(nums) tree := NewBinaryIndexedTree(n - 1) update := func(i, val int) { if i <= 0 || i >= n-1 { return } if nums[i-1] < nums[i] && nums[i] > nums[i+1] { tree.update(i, val) } } for i := 1; i < n-1; i++ { update(i, 1) } for _, q := range queries { if q[0] == 1 { l, r := q[1]+1, q[2]-1 t := 0 if l <= r { t = tree.query(r) - tree.query(l-1) } ans = append(ans, t) } else { idx, val := q[1], q[2] for i := idx - 1; i <= idx+1; i++ { update(i, -1) } nums[idx] = val for i := idx - 1; i <= idx+1; i++ { update(i, 1) } } } return }
3,187
Peaks in Array
Hard
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul> <li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword="subarray">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li> <li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face="monospace">val<sub>i</sub></font></code>.</li> </ul> <p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p> <p><strong>Notes:</strong></p> <ul> <li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</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">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0]</span></p> <p><strong>Explanation:</strong></p> <p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p> <p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p> <p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p> <p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li> <li>For all <code>i</code> that: <ul> <li><code>queries[i][0] == 1</code>: <code>0 &lt;= queries[i][1] &lt;= queries[i][2] &lt;= nums.length - 1</code></li> <li><code>queries[i][0] == 2</code>: <code>0 &lt;= queries[i][1] &lt;= nums.length - 1</code>, <code>1 &lt;= queries[i][2] &lt;= 10<sup>5</sup></code></li> </ul> </li> </ul>
Binary Indexed Tree; Segment Tree; Array
Java
class BinaryIndexedTree { private int n; private int[] c; public BinaryIndexedTree(int n) { this.n = n; this.c = new int[n + 1]; } public void update(int x, int delta) { for (; x <= n; x += x & -x) { c[x] += delta; } } public int query(int x) { int s = 0; for (; x > 0; x -= x & -x) { s += c[x]; } return s; } } class Solution { private BinaryIndexedTree tree; private int[] nums; public List<Integer> countOfPeaks(int[] nums, int[][] queries) { int n = nums.length; this.nums = nums; tree = new BinaryIndexedTree(n - 1); for (int i = 1; i < n - 1; ++i) { update(i, 1); } List<Integer> ans = new ArrayList<>(); for (var q : queries) { if (q[0] == 1) { int l = q[1] + 1, r = q[2] - 1; ans.add(l > r ? 0 : tree.query(r) - tree.query(l - 1)); } else { int idx = q[1], val = q[2]; for (int i = idx - 1; i <= idx + 1; ++i) { update(i, -1); } nums[idx] = val; for (int i = idx - 1; i <= idx + 1; ++i) { update(i, 1); } } } return ans; } private void update(int i, int val) { if (i <= 0 || i >= nums.length - 1) { return; } if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { tree.update(i, val); } } }
3,187
Peaks in Array
Hard
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul> <li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword="subarray">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li> <li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face="monospace">val<sub>i</sub></font></code>.</li> </ul> <p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p> <p><strong>Notes:</strong></p> <ul> <li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</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">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0]</span></p> <p><strong>Explanation:</strong></p> <p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p> <p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p> <p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p> <p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li> <li>For all <code>i</code> that: <ul> <li><code>queries[i][0] == 1</code>: <code>0 &lt;= queries[i][1] &lt;= queries[i][2] &lt;= nums.length - 1</code></li> <li><code>queries[i][0] == 2</code>: <code>0 &lt;= queries[i][1] &lt;= nums.length - 1</code>, <code>1 &lt;= queries[i][2] &lt;= 10<sup>5</sup></code></li> </ul> </li> </ul>
Binary Indexed Tree; Segment Tree; Array
Python
class BinaryIndexedTree: __slots__ = "n", "c" def __init__(self, n: int): self.n = n self.c = [0] * (n + 1) def update(self, x: int, delta: int) -> None: while x <= self.n: self.c[x] += delta x += x & -x def query(self, x: int) -> int: s = 0 while x: s += self.c[x] x -= x & -x return s class Solution: def countOfPeaks(self, nums: List[int], queries: List[List[int]]) -> List[int]: def update(i: int, val: int): if i <= 0 or i >= n - 1: return if nums[i - 1] < nums[i] and nums[i] > nums[i + 1]: tree.update(i, val) n = len(nums) tree = BinaryIndexedTree(n - 1) for i in range(1, n - 1): update(i, 1) ans = [] for q in queries: if q[0] == 1: l, r = q[1] + 1, q[2] - 1 ans.append(0 if l > r else tree.query(r) - tree.query(l - 1)) else: idx, val = q[1:] for i in range(idx - 1, idx + 2): update(i, -1) nums[idx] = val for i in range(idx - 1, idx + 2): update(i, 1) return ans
3,187
Peaks in Array
Hard
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul> <li><code>queries[i] = [1, l<sub>i</sub>, r<sub>i</sub>]</code>, determine the count of <strong>peak</strong> elements in the <span data-keyword="subarray">subarray</span> <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.<!-- notionvc: 73b20b7c-e1ab-4dac-86d0-13761094a9ae --></li> <li><code>queries[i] = [2, index<sub>i</sub>, val<sub>i</sub>]</code>, change <code>nums[index<sub>i</sub>]</code> to <code><font face="monospace">val<sub>i</sub></font></code>.</li> </ul> <p>Return an array <code>answer</code> containing the results of the queries of the first type in order.<!-- notionvc: a9ccef22-4061-4b5a-b4cc-a2b2a0e12f30 --></p> <p><strong>Notes:</strong></p> <ul> <li>The <strong>first</strong> and the <strong>last</strong> element of an array or a subarray<!-- notionvc: fcffef72-deb5-47cb-8719-3a3790102f73 --> <strong>cannot</strong> be a peak.</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">nums = [3,1,4,2,5], queries = [[2,3,4],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0]</span></p> <p><strong>Explanation:</strong></p> <p>First query: We change <code>nums[3]</code> to 4 and <code>nums</code> becomes <code>[3,1,4,4,5]</code>.</p> <p>Second query: The number of peaks in the <code>[3,1,4,4,5]</code> is 0.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [4,1,4,2,1,5], queries = [[2,2,4],[1,0,2],[1,0,4]]</span></p> <p><strong>Output:</strong> <span class="example-io">[0,1]</span></p> <p><strong>Explanation:</strong></p> <p>First query: <code>nums[2]</code> should become 4, but it is already set to 4.</p> <p>Second query: The number of peaks in the <code>[4,1,4]</code> is 0.</p> <p>Third query: The second 4 is a peak in the <code>[4,1,4,2,1]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li> <li><code>queries[i][0] == 1</code> or <code>queries[i][0] == 2</code></li> <li>For all <code>i</code> that: <ul> <li><code>queries[i][0] == 1</code>: <code>0 &lt;= queries[i][1] &lt;= queries[i][2] &lt;= nums.length - 1</code></li> <li><code>queries[i][0] == 2</code>: <code>0 &lt;= queries[i][1] &lt;= nums.length - 1</code>, <code>1 &lt;= queries[i][2] &lt;= 10<sup>5</sup></code></li> </ul> </li> </ul>
Binary Indexed Tree; Segment Tree; Array
TypeScript
class BinaryIndexedTree { private n: number; private c: number[]; constructor(n: number) { this.n = n; this.c = Array(n + 1).fill(0); } update(x: number, delta: number): void { for (; x <= this.n; x += x & -x) { this.c[x] += delta; } } query(x: number): number { let s = 0; for (; x > 0; x -= x & -x) { s += this.c[x]; } return s; } } function countOfPeaks(nums: number[], queries: number[][]): number[] { const n = nums.length; const tree = new BinaryIndexedTree(n - 1); const update = (i: number, val: number): void => { if (i <= 0 || i >= n - 1) { return; } if (nums[i - 1] < nums[i] && nums[i] > nums[i + 1]) { tree.update(i, val); } }; for (let i = 1; i < n - 1; ++i) { update(i, 1); } const ans: number[] = []; for (const q of queries) { if (q[0] === 1) { const [l, r] = [q[1] + 1, q[2] - 1]; ans.push(l > r ? 0 : tree.query(r) - tree.query(l - 1)); } else { const [idx, val] = [q[1], q[2]]; for (let i = idx - 1; i <= idx + 1; ++i) { update(i, -1); } nums[idx] = val; for (let i = idx - 1; i <= idx + 1; ++i) { update(i, 1); } } } return ans; }
3,188
Find Top Scoring Students II
Hard
<p>Table: <code>students</code></p> <pre> +-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | name | varchar | | major | varchar | +-------------+----------+ student_id is the primary key for this table. Each row contains the student ID, student name, and their major. </pre> <p>Table: <code>courses</code></p> <pre> +-------------+-------------------+ | Column Name | Type | +-------------+-------------------+ | course_id | int | | name | varchar | | credits | int | | major | varchar | | mandatory | enum | +-------------+-------------------+ course_id is the primary key for this table. mandatory is an enum type of (&#39;Yes&#39;, &#39;No&#39;). Each row contains the course ID, course name, credits, major it belongs to, and whether the course is mandatory. </pre> <p>Table: <code>enrollments</code></p> <pre> +-------------+----------+ | Column Name | Type | +-------------+----------+ | student_id | int | | course_id | int | | semester | varchar | | grade | varchar | | GPA | decimal | +-------------+----------+ (student_id, course_id, semester) is the primary key (combination of columns with unique values) for this table. Each row contains the student ID, course ID, semester, and grade received. </pre> <p>Write a solution to find the students who meet the following criteria:</p> <ul> <li>Have<strong> taken all mandatory courses</strong> and <strong>at least two</strong> elective courses offered in <strong>their major.</strong></li> <li>Achieved a grade of <strong>A</strong>&nbsp;in <strong>all mandatory courses</strong> and at least <strong>B</strong>&nbsp;in<strong> elective courses</strong>.</li> <li>Maintained an average <code>GPA</code> of at least&nbsp;<code>2.5</code> across all their courses (including those outside their major).</li> </ul> <p>Return <em>the result table ordered by</em> <code>student_id</code> <em>in <strong>ascending</strong> order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example:</strong></p> <div class="example-block"> <p><strong>Input:</strong></p> <p>students table:</p> <pre class="example-io"> +------------+------------------+------------------+ | student_id | name | major | +------------+------------------+------------------+ | 1 | Alice | Computer Science | | 2 | Bob | Computer Science | | 3 | Charlie | Mathematics | | 4 | David | Mathematics | +------------+------------------+------------------+ </pre> <p>courses table:</p> <pre class="example-io"> +-----------+-------------------+---------+------------------+----------+ | course_id | name | credits | major | mandatory| +-----------+-------------------+---------+------------------+----------+ | 101 | Algorithms | 3 | Computer Science | yes | | 102 | Data Structures | 3 | Computer Science | yes | | 103 | Calculus | 4 | Mathematics | yes | | 104 | Linear Algebra | 4 | Mathematics | yes | | 105 | Machine Learning | 3 | Computer Science | no | | 106 | Probability | 3 | Mathematics | no | | 107 | Operating Systems | 3 | Computer Science | no | | 108 | Statistics | 3 | Mathematics | no | +-----------+-------------------+---------+------------------+----------+ </pre> <p>enrollments table:</p> <pre class="example-io"> +------------+-----------+-------------+-------+-----+ | student_id | course_id | semester | grade | GPA | +------------+-----------+-------------+-------+-----+ | 1 | 101 | Fall 2023 | A | 4.0 | | 1 | 102 | Spring 2023 | A | 4.0 | | 1 | 105 | Spring 2023 | A | 4.0 | | 1 | 107 | Fall 2023 | B | 3.5 | | 2 | 101 | Fall 2023 | A | 4.0 | | 2 | 102 | Spring 2023 | B | 3.0 | | 3 | 103 | Fall 2023 | A | 4.0 | | 3 | 104 | Spring 2023 | A | 4.0 | | 3 | 106 | Spring 2023 | A | 4.0 | | 3 | 108 | Fall 2023 | B | 3.5 | | 4 | 103 | Fall 2023 | B | 3.0 | | 4 | 104 | Spring 2023 | B | 3.0 | +------------+-----------+-------------+-------+-----+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +------------+ | student_id | +------------+ | 1 | | 3 | +------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li>Alice (student_id 1) is a Computer Science major and has taken both Algorithms&nbsp;and Data Structures, receiving an A&nbsp;in both. She has also taken Machine Learning&nbsp;and Operating Systems&nbsp;as electives, receiving an A&nbsp;and B&nbsp;respectively.</li> <li>Bob (student_id 2) is a Computer Science major but did not receive an A&nbsp;in all required courses.</li> <li>Charlie (student_id 3) is a Mathematics major and has taken both Calculus&nbsp;and Linear Algebra, receiving an A&nbsp;in both. He has also taken Probability&nbsp;and Statistics&nbsp;as electives, receiving an A&nbsp;and B&nbsp;respectively.</li> <li>David (student_id 4) is a Mathematics major but did not receive an A&nbsp;in all required courses.</li> </ul> <p><strong>Note:</strong> Output table is ordered by student_id in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below WITH T AS ( SELECT student_id FROM enrollments GROUP BY 1 HAVING AVG(GPA) >= 2.5 ) SELECT student_id FROM T JOIN students USING (student_id) JOIN courses USING (major) LEFT JOIN enrollments USING (student_id, course_id) GROUP BY 1 HAVING SUM(mandatory = 'yes' AND grade = 'A') = SUM(mandatory = 'yes') AND SUM(mandatory = 'no' AND grade IS NOT NULL) = SUM(mandatory = 'no' AND grade IN ('A', 'B')) AND SUM(mandatory = 'no' AND grade IS NOT NULL) >= 2 ORDER BY 1;
3,189
Minimum Moves to Get a Peaceful Board
Medium
<p>Given a 2D array <code>rooks</code> of length <code>n</code>, where <code>rooks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the position of a rook on an <code>n x n</code> chess board. Your task is to move the rooks <strong>1 cell </strong>at a time vertically or horizontally (to an <em>adjacent</em> cell) such that the board becomes <strong>peaceful</strong>.</p> <p>A board is <strong>peaceful</strong> if there is <strong>exactly</strong> one rook in each row and each column.</p> <p>Return the <strong>minimum</strong> number of moves required to get a <em>peaceful board</em>.</p> <p><strong>Note</strong> that <strong>at no point</strong> can there be two rooks in the same cell.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[1,0],[1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex1-edited.gif" style="width: 150px; height: 150px;" /></div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[0,1],[0,2],[0,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex2-edited.gif" style="width: 200px; height: 200px;" /></div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == rooks.length &lt;= 500</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li> <li>The input is generated such that there are no 2 rooks in the same cell.</li> </ul>
Greedy; Array; Counting Sort; Sorting
C++
class Solution { public: int minMoves(vector<vector<int>>& rooks) { sort(rooks.begin(), rooks.end()); int ans = 0; int n = rooks.size(); for (int i = 0; i < n; ++i) { ans += abs(rooks[i][0] - i); } sort(rooks.begin(), rooks.end(), [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; }); for (int j = 0; j < n; ++j) { ans += abs(rooks[j][1] - j); } return ans; } };
3,189
Minimum Moves to Get a Peaceful Board
Medium
<p>Given a 2D array <code>rooks</code> of length <code>n</code>, where <code>rooks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the position of a rook on an <code>n x n</code> chess board. Your task is to move the rooks <strong>1 cell </strong>at a time vertically or horizontally (to an <em>adjacent</em> cell) such that the board becomes <strong>peaceful</strong>.</p> <p>A board is <strong>peaceful</strong> if there is <strong>exactly</strong> one rook in each row and each column.</p> <p>Return the <strong>minimum</strong> number of moves required to get a <em>peaceful board</em>.</p> <p><strong>Note</strong> that <strong>at no point</strong> can there be two rooks in the same cell.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[1,0],[1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex1-edited.gif" style="width: 150px; height: 150px;" /></div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[0,1],[0,2],[0,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex2-edited.gif" style="width: 200px; height: 200px;" /></div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == rooks.length &lt;= 500</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li> <li>The input is generated such that there are no 2 rooks in the same cell.</li> </ul>
Greedy; Array; Counting Sort; Sorting
Go
func minMoves(rooks [][]int) (ans int) { sort.Slice(rooks, func(i, j int) bool { return rooks[i][0] < rooks[j][0] }) for i, row := range rooks { ans += int(math.Abs(float64(row[0] - i))) } sort.Slice(rooks, func(i, j int) bool { return rooks[i][1] < rooks[j][1] }) for j, col := range rooks { ans += int(math.Abs(float64(col[1] - j))) } return }
3,189
Minimum Moves to Get a Peaceful Board
Medium
<p>Given a 2D array <code>rooks</code> of length <code>n</code>, where <code>rooks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the position of a rook on an <code>n x n</code> chess board. Your task is to move the rooks <strong>1 cell </strong>at a time vertically or horizontally (to an <em>adjacent</em> cell) such that the board becomes <strong>peaceful</strong>.</p> <p>A board is <strong>peaceful</strong> if there is <strong>exactly</strong> one rook in each row and each column.</p> <p>Return the <strong>minimum</strong> number of moves required to get a <em>peaceful board</em>.</p> <p><strong>Note</strong> that <strong>at no point</strong> can there be two rooks in the same cell.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[1,0],[1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex1-edited.gif" style="width: 150px; height: 150px;" /></div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[0,1],[0,2],[0,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex2-edited.gif" style="width: 200px; height: 200px;" /></div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == rooks.length &lt;= 500</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li> <li>The input is generated such that there are no 2 rooks in the same cell.</li> </ul>
Greedy; Array; Counting Sort; Sorting
Java
class Solution { public int minMoves(int[][] rooks) { Arrays.sort(rooks, (a, b) -> a[0] - b[0]); int ans = 0; int n = rooks.length; for (int i = 0; i < n; ++i) { ans += Math.abs(rooks[i][0] - i); } Arrays.sort(rooks, (a, b) -> a[1] - b[1]); for (int j = 0; j < n; ++j) { ans += Math.abs(rooks[j][1] - j); } return ans; } }
3,189
Minimum Moves to Get a Peaceful Board
Medium
<p>Given a 2D array <code>rooks</code> of length <code>n</code>, where <code>rooks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the position of a rook on an <code>n x n</code> chess board. Your task is to move the rooks <strong>1 cell </strong>at a time vertically or horizontally (to an <em>adjacent</em> cell) such that the board becomes <strong>peaceful</strong>.</p> <p>A board is <strong>peaceful</strong> if there is <strong>exactly</strong> one rook in each row and each column.</p> <p>Return the <strong>minimum</strong> number of moves required to get a <em>peaceful board</em>.</p> <p><strong>Note</strong> that <strong>at no point</strong> can there be two rooks in the same cell.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[1,0],[1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex1-edited.gif" style="width: 150px; height: 150px;" /></div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[0,1],[0,2],[0,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex2-edited.gif" style="width: 200px; height: 200px;" /></div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == rooks.length &lt;= 500</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li> <li>The input is generated such that there are no 2 rooks in the same cell.</li> </ul>
Greedy; Array; Counting Sort; Sorting
Python
class Solution: def minMoves(self, rooks: List[List[int]]) -> int: rooks.sort() ans = sum(abs(x - i) for i, (x, _) in enumerate(rooks)) rooks.sort(key=lambda x: x[1]) ans += sum(abs(y - j) for j, (_, y) in enumerate(rooks)) return ans
3,189
Minimum Moves to Get a Peaceful Board
Medium
<p>Given a 2D array <code>rooks</code> of length <code>n</code>, where <code>rooks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the position of a rook on an <code>n x n</code> chess board. Your task is to move the rooks <strong>1 cell </strong>at a time vertically or horizontally (to an <em>adjacent</em> cell) such that the board becomes <strong>peaceful</strong>.</p> <p>A board is <strong>peaceful</strong> if there is <strong>exactly</strong> one rook in each row and each column.</p> <p>Return the <strong>minimum</strong> number of moves required to get a <em>peaceful board</em>.</p> <p><strong>Note</strong> that <strong>at no point</strong> can there be two rooks in the same cell.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[1,0],[1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex1-edited.gif" style="width: 150px; height: 150px;" /></div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">rooks = [[0,0],[0,1],[0,2],[0,3]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3189.Minimum%20Moves%20to%20Get%20a%20Peaceful%20Board/images/ex2-edited.gif" style="width: 200px; height: 200px;" /></div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == rooks.length &lt;= 500</code></li> <li><code>0 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= n - 1</code></li> <li>The input is generated such that there are no 2 rooks in the same cell.</li> </ul>
Greedy; Array; Counting Sort; Sorting
TypeScript
function minMoves(rooks: number[][]): number { rooks.sort((a, b) => a[0] - b[0]); let ans = rooks.reduce((sum, rook, i) => sum + Math.abs(rook[0] - i), 0); rooks.sort((a, b) => a[1] - b[1]); ans += rooks.reduce((sum, rook, j) => sum + Math.abs(rook[1] - j), 0); return ans; }
3,190
Find Minimum Operations to Make All Elements Divisible by Three
Easy
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</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]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Math
C++
class Solution { public: int minimumOperations(vector<int>& nums) { int ans = 0; for (int x : nums) { int mod = x % 3; if (mod) { ans += min(mod, 3 - mod); } } return ans; } };
3,190
Find Minimum Operations to Make All Elements Divisible by Three
Easy
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</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]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Math
Go
func minimumOperations(nums []int) (ans int) { for _, x := range nums { if mod := x % 3; mod > 0 { ans += min(mod, 3-mod) } } return }
3,190
Find Minimum Operations to Make All Elements Divisible by Three
Easy
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</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]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Math
Java
class Solution { public int minimumOperations(int[] nums) { int ans = 0; for (int x : nums) { int mod = x % 3; if (mod != 0) { ans += Math.min(mod, 3 - mod); } } return ans; } }
3,190
Find Minimum Operations to Make All Elements Divisible by Three
Easy
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</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]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Math
Python
class Solution: def minimumOperations(self, nums: List[int]) -> int: ans = 0 for x in nums: if mod := x % 3: ans += min(mod, 3 - mod) return ans
3,190
Find Minimum Operations to Make All Elements Divisible by Three
Easy
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</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]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <p>All array elements can be made divisible by 3 using 3 operations:</p> <ul> <li>Subtract 1 from 1.</li> <li>Add 1 to 2.</li> <li>Subtract 1 from 4.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [3,6,9]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 50</code></li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Math
TypeScript
function minimumOperations(nums: number[]): number { let ans = 0; for (const x of nums) { const mod = x % 3; if (mod) { ans += Math.min(mod, 3 - mod); } } return ans; }
3,191
Minimum Operations to Make Binary Array Elements Equal to One I
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, 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 = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</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 = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
Bit Manipulation; Queue; Array; Prefix Sum; Sliding Window
C++
class Solution { public: int minOperations(vector<int>& nums) { int ans = 0; int n = nums.size(); for (int i = 0; i < n; ++i) { if (nums[i] == 0) { if (i + 2 >= n) { return -1; } nums[i + 1] ^= 1; nums[i + 2] ^= 1; ++ans; } } return ans; } };
3,191
Minimum Operations to Make Binary Array Elements Equal to One I
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, 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 = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</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 = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
Bit Manipulation; Queue; Array; Prefix Sum; Sliding Window
Go
func minOperations(nums []int) (ans int) { for i, x := range nums { if x == 0 { if i+2 >= len(nums) { return -1 } nums[i+1] ^= 1 nums[i+2] ^= 1 ans++ } } return }
3,191
Minimum Operations to Make Binary Array Elements Equal to One I
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, 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 = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</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 = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
Bit Manipulation; Queue; Array; Prefix Sum; Sliding Window
Java
class Solution { public int minOperations(int[] nums) { int ans = 0; int n = nums.length; for (int i = 0; i < n; ++i) { if (nums[i] == 0) { if (i + 2 >= n) { return -1; } nums[i + 1] ^= 1; nums[i + 2] ^= 1; ++ans; } } return ans; } }
3,191
Minimum Operations to Make Binary Array Elements Equal to One I
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, 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 = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</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 = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
Bit Manipulation; Queue; Array; Prefix Sum; Sliding Window
Python
class Solution: def minOperations(self, nums: List[int]) -> int: ans = 0 for i, x in enumerate(nums): if x == 0: if i + 2 >= len(nums): return -1 nums[i + 1] ^= 1 nums[i + 2] ^= 1 ans += 1 return ans
3,191
Minimum Operations to Make Binary Array Elements Equal to One I
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</strong> <strong>all</strong> of them.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 1. If it is impossible, 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 = [0,1,1,1,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the elements at indices 0, 1 and 2. The resulting array is <code>nums = [<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,0,0]</code>.</li> <li>Choose the elements at indices 1, 2 and 3. The resulting array is <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<strong><u>0</u></strong>,0,0]</code>.</li> <li>Choose the elements at indices 3, 4 and 5. The resulting array is <code>nums = [1,1,1,<strong><u>1</u></strong>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</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 = [0,1,1,1]</span></p> <p><strong>Output:</strong> <span class="example-io">-1</span></p> <p><strong>Explanation:</strong><br /> It is impossible to make all elements equal to 1.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>0 &lt;= nums[i] &lt;= 1</code></li> </ul>
Bit Manipulation; Queue; Array; Prefix Sum; Sliding Window
TypeScript
function minOperations(nums: number[]): number { const n = nums.length; let ans = 0; for (let i = 0; i < n; ++i) { if (nums[i] === 0) { if (i + 2 >= n) { return -1; } nums[i + 1] ^= 1; nums[i + 2] ^= 1; ++ans; } } return ans; }
3,192
Minimum Operations to Make Binary Array Elements Equal to One II
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 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 = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></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,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </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;= 1</code></li> </ul>
Greedy; Array; Dynamic Programming
C++
class Solution { public: int minOperations(vector<int>& nums) { int ans = 0, v = 0; for (int x : nums) { x ^= v; if (x == 0) { v ^= 1; ++ans; } } return ans; } };
3,192
Minimum Operations to Make Binary Array Elements Equal to One II
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 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 = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></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,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </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;= 1</code></li> </ul>
Greedy; Array; Dynamic Programming
Go
func minOperations(nums []int) (ans int) { v := 0 for _, x := range nums { x ^= v if x == 0 { v ^= 1 ans++ } } return }
3,192
Minimum Operations to Make Binary Array Elements Equal to One II
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 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 = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></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,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </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;= 1</code></li> </ul>
Greedy; Array; Dynamic Programming
Java
class Solution { public int minOperations(int[] nums) { int ans = 0, v = 0; for (int x : nums) { x ^= v; if (x == 0) { v ^= 1; ++ans; } } return ans; } }
3,192
Minimum Operations to Make Binary Array Elements Equal to One II
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 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 = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></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,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </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;= 1</code></li> </ul>
Greedy; Array; Dynamic Programming
Python
class Solution: def minOperations(self, nums: List[int]) -> int: ans = v = 0 for x in nums: x ^= v if x == 0: ans += 1 v ^= 1 return ans
3,192
Minimum Operations to Make Binary Array Elements Equal to One II
Medium
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</strong> the elements from index <code>i</code> to the end of the array.</li> </ul> <p><strong>Flipping</strong> an element means changing its value from 0 to 1, and from 1 to 0.</p> <p>Return the <strong>minimum</strong> number of operations required to make all elements in <code>nums</code> equal to 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 = [0,1,1,0,1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong><br /> We can do the following operations:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [0,<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 0</code><span class="example-io">. The resulting array will be <code>nums = [<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>0</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 4</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,0,<u><strong>0</strong></u>]</code>.</span></li> <li>Choose the index <code>i = 3</code><span class="example-io">. The resulting array will be <code>nums = [1,1,1,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></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,0,0,0]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong><br /> We can do the following operation:</p> <ul> <li>Choose the index <code>i = 1</code><span class="example-io">. The resulting array will be <code>nums = [1,<u><strong>1</strong></u>,<u><strong>1</strong></u>,<u><strong>1</strong></u>]</code>.</span></li> </ul> </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;= 1</code></li> </ul>
Greedy; Array; Dynamic Programming
TypeScript
function minOperations(nums: number[]): number { let [ans, v] = [0, 0]; for (let x of nums) { x ^= v; if (x === 0) { v ^= 1; ++ans; } } return ans; }
3,193
Count the Number of Inversions
Hard
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</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">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</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">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
Array; Dynamic Programming
C++
class Solution { public: int numberOfPermutations(int n, vector<vector<int>>& requirements) { vector<int> req(n, -1); int m = 0; for (const auto& r : requirements) { req[r[0]] = r[1]; m = max(m, r[1]); } if (req[0] > 0) { return 0; } req[0] = 0; const int mod = 1e9 + 7; vector<vector<int>> f(n, vector<int>(m + 1, 0)); f[0][0] = 1; for (int i = 1; i < n; ++i) { int l = 0, r = m; if (req[i] >= 0) { l = r = req[i]; } for (int j = l; j <= r; ++j) { for (int k = 0; k <= min(i, j); ++k) { f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; } } } return f[n - 1][req[n - 1]]; } };
3,193
Count the Number of Inversions
Hard
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</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">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</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">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
Array; Dynamic Programming
Go
func numberOfPermutations(n int, requirements [][]int) int { req := make([]int, n) for i := range req { req[i] = -1 } for _, r := range requirements { req[r[0]] = r[1] } if req[0] > 0 { return 0 } req[0] = 0 m := slices.Max(req) const mod = int(1e9 + 7) f := make([][]int, n) for i := range f { f[i] = make([]int, m+1) } f[0][0] = 1 for i := 1; i < n; i++ { l, r := 0, m if req[i] >= 0 { l, r = req[i], req[i] } for j := l; j <= r; j++ { for k := 0; k <= min(i, j); k++ { f[i][j] = (f[i][j] + f[i-1][j-k]) % mod } } } return f[n-1][req[n-1]] }
3,193
Count the Number of Inversions
Hard
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</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">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</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">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
Array; Dynamic Programming
Java
class Solution { public int numberOfPermutations(int n, int[][] requirements) { int[] req = new int[n]; Arrays.fill(req, -1); int m = 0; for (var r : requirements) { req[r[0]] = r[1]; m = Math.max(m, r[1]); } if (req[0] > 0) { return 0; } req[0] = 0; final int mod = (int) 1e9 + 7; int[][] f = new int[n][m + 1]; f[0][0] = 1; for (int i = 1; i < n; ++i) { int l = 0, r = m; if (req[i] >= 0) { l = r = req[i]; } for (int j = l; j <= r; ++j) { for (int k = 0; k <= Math.min(i, j); ++k) { f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; } } } return f[n - 1][req[n - 1]]; } }
3,193
Count the Number of Inversions
Hard
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</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">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</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">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
Array; Dynamic Programming
Python
class Solution: def numberOfPermutations(self, n: int, requirements: List[List[int]]) -> int: req = [-1] * n for end, cnt in requirements: req[end] = cnt if req[0] > 0: return 0 req[0] = 0 mod = 10**9 + 7 m = max(req) f = [[0] * (m + 1) for _ in range(n)] f[0][0] = 1 for i in range(1, n): l, r = 0, m if req[i] >= 0: l = r = req[i] for j in range(l, r + 1): for k in range(min(i, j) + 1): f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod return f[n - 1][req[n - 1]]
3,193
Count the Number of Inversions
Hard
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nums</code> is called an <strong>inversion</strong> if:</p> <ul> <li><code>i &lt; j</code> and <code>nums[i] &gt; nums[j]</code></li> </ul> <p>Return the number of <span data-keyword="permutation">permutations</span> <code>perm</code> of <code>[0, 1, 2, ..., n - 1]</code> such that for <strong>all</strong> <code>requirements[i]</code>, <code>perm[0..end<sub>i</sub>]</code> has exactly <code>cnt<sub>i</sub></code> inversions.</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">n = 3, requirements = [[2,2],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The two permutations are:</p> <ul> <li><code>[2, 0, 1]</code> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </li> <li><code>[1, 2, 0]</code> <ul> <li>Prefix <code>[1, 2, 0]</code> has inversions <code>(0, 2)</code> and <code>(1, 2)</code>.</li> <li>Prefix <code>[1]</code> has 0 inversions.</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">n = 3, requirements = [[2,2],[1,1],[0,0]]</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[2, 0, 1]</code>:</p> <ul> <li>Prefix <code>[2, 0, 1]</code> has inversions <code>(0, 1)</code> and <code>(0, 2)</code>.</li> <li>Prefix <code>[2, 0]</code> has an inversion <code>(0, 1)</code>.</li> <li>Prefix <code>[2]</code> has 0 inversions.</li> </ul> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 2, requirements = [[0,0],[1,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only satisfying permutation is <code>[0, 1]</code>:</p> <ul> <li>Prefix <code>[0]</code> has 0 inversions.</li> <li>Prefix <code>[0, 1]</code> has an inversion <code>(0, 1)</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n &lt;= 300</code></li> <li><code>1 &lt;= requirements.length &lt;= n</code></li> <li><code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code></li> <li><code>0 &lt;= end<sub>i</sub> &lt;= n - 1</code></li> <li><code>0 &lt;= cnt<sub>i</sub> &lt;= 400</code></li> <li>The input is generated such that there is at least one <code>i</code> such that <code>end<sub>i</sub> == n - 1</code>.</li> <li>The input is generated such that all <code>end<sub>i</sub></code> are unique.</li> </ul>
Array; Dynamic Programming
TypeScript
function numberOfPermutations(n: number, requirements: number[][]): number { const req: number[] = Array(n).fill(-1); for (const [end, cnt] of requirements) { req[end] = cnt; } if (req[0] > 0) { return 0; } req[0] = 0; const m = Math.max(...req); const mod = 1e9 + 7; const f = Array.from({ length: n }, () => Array(m + 1).fill(0)); f[0][0] = 1; for (let i = 1; i < n; ++i) { let [l, r] = [0, m]; if (req[i] >= 0) { l = r = req[i]; } for (let j = l; j <= r; ++j) { for (let k = 0; k <= Math.min(i, j); ++k) { f[i][j] = (f[i][j] + f[i - 1][j - k]) % mod; } } } return f[n - 1][req[n - 1]]; }
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
C++
class Solution { public: double minimumAverage(vector<int>& nums) { sort(nums.begin(), nums.end()); int ans = 1 << 30, n = nums.size(); for (int i = 0; i < n; ++i) { ans = min(ans, nums[i] + nums[n - i - 1]); } return ans / 2.0; } };
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
Go
func minimumAverage(nums []int) float64 { sort.Ints(nums) n := len(nums) ans := 1 << 30 for i, x := range nums[:n/2] { ans = min(ans, x+nums[n-i-1]) } return float64(ans) / 2 }
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
Java
class Solution { public double minimumAverage(int[] nums) { Arrays.sort(nums); int n = nums.length; int ans = 1 << 30; for (int i = 0; i < n / 2; ++i) { ans = Math.min(ans, nums[i] + nums[n - i - 1]); } return ans / 2.0; } }
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
Python
class Solution: def minimumAverage(self, nums: List[int]) -> float: nums.sort() n = len(nums) return min(nums[i] + nums[-i - 1] for i in range(n // 2)) / 2
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
Rust
impl Solution { pub fn minimum_average(mut nums: Vec<i32>) -> f64 { nums.sort(); let n = nums.len(); let ans = (0..n / 2).map(|i| nums[i] + nums[n - i - 1]).min().unwrap(); ans as f64 / 2.0 } }
3,194
Minimum Average of Smallest and Largest Elements
Easy
<p>You have an array of floating point numbers <code>averages</code> which is initially empty. You are given an array <code>nums</code> of <code>n</code> integers where <code>n</code> is even.</p> <p>You repeat the following procedure <code>n / 2</code> times:</p> <ul> <li>Remove the <strong>smallest</strong> element, <code>minElement</code>, and the <strong>largest</strong> element <code>maxElement</code>,&nbsp;from <code>nums</code>.</li> <li>Add <code>(minElement + maxElement) / 2</code> to <code>averages</code>.</li> </ul> <p>Return the <strong>minimum</strong> element in <code>averages</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 = [7,8,3,4,15,13,4,1]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td>[7,8,3,4,15,13,4,1]</td> <td>[]</td> </tr> <tr> <td>1</td> <td>[7,8,3,4,13,4]</td> <td>[8]</td> </tr> <tr> <td>2</td> <td>[7,8,4,4]</td> <td>[8,8]</td> </tr> <tr> <td>3</td> <td>[7,4]</td> <td>[8,8,6]</td> </tr> <tr> <td>4</td> <td>[]</td> <td>[8,8,6,5.5]</td> </tr> </tbody> </table> The smallest element of averages, 5.5, is returned.</div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,9,8,3,10,5]</span></p> <p><strong>Output:</strong> <span class="example-io">5.5</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,9,8,3,10,5]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[9,8,3,5]</span></td> <td>[5.5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[8,5]</span></td> <td>[5.5,6]</td> </tr> <tr> <td>3</td> <td>[]</td> <td>[5.5,6,6.5]</td> </tr> </tbody> </table> </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,7,8,9]</span></p> <p><strong>Output:</strong> <span class="example-io">5.0</span></p> <p><strong>Explanation:</strong></p> <table> <tbody> <tr> <th>step</th> <th>nums</th> <th>averages</th> </tr> <tr> <td>0</td> <td><span class="example-io">[1,2,3,7,8,9]</span></td> <td>[]</td> </tr> <tr> <td>1</td> <td><span class="example-io">[2,3,7,8]</span></td> <td>[5]</td> </tr> <tr> <td>2</td> <td><span class="example-io">[3,7]</span></td> <td>[5,5]</td> </tr> <tr> <td>3</td> <td><span class="example-io">[]</span></td> <td>[5,5,5]</td> </tr> </tbody> </table> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 &lt;= n == nums.length &lt;= 50</code></li> <li><code>n</code> is even.</li> <li><code>1 &lt;= nums[i] &lt;= 50</code></li> </ul>
Array; Two Pointers; Sorting
TypeScript
function minimumAverage(nums: number[]): number { nums.sort((a, b) => a - b); const n = nums.length; let ans = Infinity; for (let i = 0; i * 2 < n; ++i) { ans = Math.min(ans, nums[i] + nums[n - 1 - i]); } return ans / 2; }
3,195
Find the Minimum Area to Cover All Ones I
Medium
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</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 = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
Array; Matrix
C++
class Solution { public: int minimumArea(vector<vector<int>>& grid) { int m = grid.size(), n = grid[0].size(); int x1 = m, y1 = n; int x2 = 0, y2 = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { x1 = min(x1, i); y1 = min(y1, j); x2 = max(x2, i); y2 = max(y2, j); } } } return (x2 - x1 + 1) * (y2 - y1 + 1); } };
3,195
Find the Minimum Area to Cover All Ones I
Medium
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</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 = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
Array; Matrix
Go
func minimumArea(grid [][]int) int { x1, y1 := len(grid), len(grid[0]) x2, y2 := 0, 0 for i, row := range grid { for j, x := range row { if x == 1 { x1, y1 = min(x1, i), min(y1, j) x2, y2 = max(x2, i), max(y2, j) } } } return (x2 - x1 + 1) * (y2 - y1 + 1) }
3,195
Find the Minimum Area to Cover All Ones I
Medium
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</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 = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
Array; Matrix
Java
class Solution { public int minimumArea(int[][] grid) { int m = grid.length, n = grid[0].length; int x1 = m, y1 = n; int x2 = 0, y2 = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) { x1 = Math.min(x1, i); y1 = Math.min(y1, j); x2 = Math.max(x2, i); y2 = Math.max(y2, j); } } } return (x2 - x1 + 1) * (y2 - y1 + 1); } }
3,195
Find the Minimum Area to Cover All Ones I
Medium
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</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 = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
Array; Matrix
Python
class Solution: def minimumArea(self, grid: List[List[int]]) -> int: x1 = y1 = inf x2 = y2 = -inf for i, row in enumerate(grid): for j, x in enumerate(row): if x == 1: x1 = min(x1, i) y1 = min(y1, j) x2 = max(x2, i) y2 = max(y2, j) return (x2 - x1 + 1) * (y2 - y1 + 1)
3,195
Find the Minimum Area to Cover All Ones I
Medium
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p> <p>Return the <strong>minimum</strong> possible area of the rectangle.</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 = [[0,1,0],[1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">6</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p> <p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3195.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20I/images/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p> <p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there is at least one 1 in <code>grid</code>.</li> </ul>
Array; Matrix
TypeScript
function minimumArea(grid: number[][]): number { const [m, n] = [grid.length, grid[0].length]; let [x1, y1] = [m, n]; let [x2, y2] = [0, 0]; for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { if (grid[i][j] === 1) { x1 = Math.min(x1, i); y1 = Math.min(y1, j); x2 = Math.max(x2, i); y2 = Math.max(y2, j); } } } return (x2 - x1 + 1) * (y2 - y1 + 1); }
3,196
Maximize Total Cost of Alternating Subarrays
Medium
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</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,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 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">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</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]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</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>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Dynamic Programming
C++
class Solution { public: long long maximumTotalCost(vector<int>& nums) { int n = nums.size(); long long f[n][2]; fill(f[0], f[n], LLONG_MIN); auto dfs = [&](this auto&& dfs, int i, int j) -> long long { if (i >= n) { return 0; } if (f[i][j] != LLONG_MIN) { return f[i][j]; } f[i][j] = nums[i] + dfs(i + 1, 1); if (j) { f[i][j] = max(f[i][j], -nums[i] + dfs(i + 1, 0)); } return f[i][j]; }; return dfs(0, 0); } };
3,196
Maximize Total Cost of Alternating Subarrays
Medium
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</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,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 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">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</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]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</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>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Dynamic Programming
Go
func maximumTotalCost(nums []int) int64 { n := len(nums) f := make([][2]int64, n) for i := range f { f[i] = [2]int64{-1e18, -1e18} } var dfs func(int, int) int64 dfs = func(i, j int) int64 { if i >= n { return 0 } if f[i][j] != -1e18 { return f[i][j] } f[i][j] = int64(nums[i]) + dfs(i+1, 1) if j > 0 { f[i][j] = max(f[i][j], int64(-nums[i])+dfs(i+1, 0)) } return f[i][j] } return dfs(0, 0) }
3,196
Maximize Total Cost of Alternating Subarrays
Medium
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</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,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 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">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</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]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</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>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Dynamic Programming
Java
class Solution { private Long[][] f; private int[] nums; private int n; public long maximumTotalCost(int[] nums) { n = nums.length; this.nums = nums; f = new Long[n][2]; return dfs(0, 0); } private long dfs(int i, int j) { if (i >= n) { return 0; } if (f[i][j] != null) { return f[i][j]; } f[i][j] = nums[i] + dfs(i + 1, 1); if (j == 1) { f[i][j] = Math.max(f[i][j], -nums[i] + dfs(i + 1, 0)); } return f[i][j]; } }
3,196
Maximize Total Cost of Alternating Subarrays
Medium
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</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,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 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">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</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]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</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>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Dynamic Programming
Python
class Solution: def maximumTotalCost(self, nums: List[int]) -> int: @cache def dfs(i: int, j: int) -> int: if i >= len(nums): return 0 ans = nums[i] + dfs(i + 1, 1) if j == 1: ans = max(ans, -nums[i] + dfs(i + 1, 0)) return ans return dfs(0, 0)
3,196
Maximize Total Cost of Alternating Subarrays
Medium
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums[r] * (&minus;1)<sup>r &minus; l</sup></code></p> <p>Your task is to <strong>split</strong> <code>nums</code> into subarrays such that the <strong>total</strong> <strong>cost</strong> of the subarrays is <strong>maximized</strong>, ensuring each element belongs to <strong>exactly one</strong> subarray.</p> <p>Formally, if <code>nums</code> is split into <code>k</code> subarrays, where <code>k &gt; 1</code>, at indices <code>i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k &minus; 1</sub></code>, where <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k - 1</sub> &lt; n - 1</code>, then the total cost will be:</p> <p><code>cost(0, i<sub>1</sub>) + cost(i<sub>1</sub> + 1, i<sub>2</sub>) + ... + cost(i<sub>k &minus; 1</sub> + 1, n &minus; 1)</code></p> <p>Return an integer denoting the <em>maximum total cost</em> of the subarrays after splitting the array optimally.</p> <p><strong>Note:</strong> If <code>nums</code> is not split into subarrays, i.e. <code>k = 1</code>, the total cost is simply <code>cost(0, n - 1)</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,4]</span></p> <p><strong>Output:</strong> <span class="example-io">10</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -2, 3, 4]</code> into subarrays <code>[1, -2, 3]</code> and <code>[4]</code>. The total cost will be <code>(1 + 2 + 3) + 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">nums = [1,-1,1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>One way to maximize the total cost is by splitting <code>[1, -1, 1, -1]</code> into subarrays <code>[1, -1]</code> and <code>[1, -1]</code>. The total cost will be <code>(1 + 1) + (1 + 1) = 4</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]</span></p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong></p> <p>We cannot split the array further, so the answer is 0.</p> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,-1]</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>Selecting the whole array gives a total cost of <code>1 + 1 = 2</code>, which is the maximum.</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>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> </ul>
Array; Dynamic Programming
TypeScript
function maximumTotalCost(nums: number[]): number { const n = nums.length; const f: number[][] = Array.from({ length: n }, () => Array(2).fill(-Infinity)); const dfs = (i: number, j: number): number => { if (i >= n) { return 0; } if (f[i][j] !== -Infinity) { return f[i][j]; } f[i][j] = nums[i] + dfs(i + 1, 1); if (j) { f[i][j] = Math.max(f[i][j], -nums[i] + dfs(i + 1, 0)); } return f[i][j]; }; return dfs(0, 0); }
3,197
Find the Minimum Area to Cover All Ones II
Hard
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</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,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
Array; Enumeration; Matrix
C++
class Solution { public: int minimumSum(vector<vector<int>>& grid) { int m = grid.size(); int n = grid[0].size(); int ans = m * n; int inf = INT_MAX / 4; auto f = [&](int i1, int j1, int i2, int j2) { int x1 = inf, y1 = inf; int x2 = -inf, y2 = -inf; for (int i = i1; i <= i2; i++) { for (int j = j1; j <= j2; j++) { if (grid[i][j] == 1) { x1 = min(x1, i); y1 = min(y1, j); x2 = max(x2, i); y2 = max(y2, j); } } } return x1 > x2 || y1 > y2 ? inf : (x2 - x1 + 1) * (y2 - y1 + 1); }; for (int i1 = 0; i1 < m - 1; i1++) { for (int i2 = i1 + 1; i2 < m - 1; i2++) { ans = min(ans, f(0, 0, i1, n - 1) + f(i1 + 1, 0, i2, n - 1) + f(i2 + 1, 0, m - 1, n - 1)); } } for (int j1 = 0; j1 < n - 1; j1++) { for (int j2 = j1 + 1; j2 < n - 1; j2++) { ans = min(ans, f(0, 0, m - 1, j1) + f(0, j1 + 1, m - 1, j2) + f(0, j2 + 1, m - 1, n - 1)); } } for (int i = 0; i < m - 1; i++) { for (int j = 0; j < n - 1; j++) { ans = min(ans, f(0, 0, i, j) + f(0, j + 1, i, n - 1) + f(i + 1, 0, m - 1, n - 1)); ans = min(ans, f(0, 0, i, n - 1) + f(i + 1, 0, m - 1, j) + f(i + 1, j + 1, m - 1, n - 1)); ans = min(ans, f(0, 0, i, j) + f(i + 1, 0, m - 1, j) + f(0, j + 1, m - 1, n - 1)); ans = min(ans, f(0, 0, m - 1, j) + f(0, j + 1, i, n - 1) + f(i + 1, j + 1, m - 1, n - 1)); } } return ans; } };
3,197
Find the Minimum Area to Cover All Ones II
Hard
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</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,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
Array; Enumeration; Matrix
Go
func minimumSum(grid [][]int) int { m := len(grid) n := len(grid[0]) ans := m * n inf := math.MaxInt32 f := func(i1, j1, i2, j2 int) int { x1, y1 := inf, inf x2, y2 := -inf, -inf for i := i1; i <= i2; i++ { for j := j1; j <= j2; j++ { if grid[i][j] == 1 { x1 = min(x1, i) y1 = min(y1, j) x2 = max(x2, i) y2 = max(y2, j) } } } if x1 == inf { return 0 } return (x2 - x1 + 1) * (y2 - y1 + 1) } for i1 := 0; i1 < m-1; i1++ { for i2 := i1 + 1; i2 < m-1; i2++ { ans = min(ans, f(0, 0, i1, n-1)+f(i1+1, 0, i2, n-1)+f(i2+1, 0, m-1, n-1)) } } for j1 := 0; j1 < n-1; j1++ { for j2 := j1 + 1; j2 < n-1; j2++ { ans = min(ans, f(0, 0, m-1, j1)+f(0, j1+1, m-1, j2)+f(0, j2+1, m-1, n-1)) } } for i := 0; i < m-1; i++ { for j := 0; j < n-1; j++ { ans = min(ans, f(0, 0, i, j)+f(0, j+1, i, n-1)+f(i+1, 0, m-1, n-1)) ans = min(ans, f(0, 0, i, n-1)+f(i+1, 0, m-1, j)+f(i+1, j+1, m-1, n-1)) ans = min(ans, f(0, 0, i, j)+f(i+1, 0, m-1, j)+f(0, j+1, m-1, n-1)) ans = min(ans, f(0, 0, m-1, j)+f(0, j+1, i, n-1)+f(i+1, j+1, m-1, n-1)) } } return ans }
3,197
Find the Minimum Area to Cover All Ones II
Hard
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</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,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
Array; Enumeration; Matrix
Java
class Solution { private final int inf = 1 << 30; private int[][] grid; public int minimumSum(int[][] grid) { this.grid = grid; int m = grid.length; int n = grid[0].length; int ans = m * n; for (int i1 = 0; i1 < m - 1; i1++) { for (int i2 = i1 + 1; i2 < m - 1; i2++) { ans = Math.min( ans, f(0, 0, i1, n - 1) + f(i1 + 1, 0, i2, n - 1) + f(i2 + 1, 0, m - 1, n - 1)); } } for (int j1 = 0; j1 < n - 1; j1++) { for (int j2 = j1 + 1; j2 < n - 1; j2++) { ans = Math.min( ans, f(0, 0, m - 1, j1) + f(0, j1 + 1, m - 1, j2) + f(0, j2 + 1, m - 1, n - 1)); } } for (int i = 0; i < m - 1; i++) { for (int j = 0; j < n - 1; j++) { ans = Math.min( ans, f(0, 0, i, j) + f(0, j + 1, i, n - 1) + f(i + 1, 0, m - 1, n - 1)); ans = Math.min( ans, f(0, 0, i, n - 1) + f(i + 1, 0, m - 1, j) + f(i + 1, j + 1, m - 1, n - 1)); ans = Math.min( ans, f(0, 0, i, j) + f(i + 1, 0, m - 1, j) + f(0, j + 1, m - 1, n - 1)); ans = Math.min( ans, f(0, 0, m - 1, j) + f(0, j + 1, i, n - 1) + f(i + 1, j + 1, m - 1, n - 1)); } } return ans; } private int f(int i1, int j1, int i2, int j2) { int x1 = inf, y1 = inf; int x2 = -inf, y2 = -inf; for (int i = i1; i <= i2; i++) { for (int j = j1; j <= j2; j++) { if (grid[i][j] == 1) { x1 = Math.min(x1, i); y1 = Math.min(y1, j); x2 = Math.max(x2, i); y2 = Math.max(y2, j); } } } return (x2 - x1 + 1) * (y2 - y1 + 1); } }
3,197
Find the Minimum Area to Cover All Ones II
Hard
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</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,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
Array; Enumeration; Matrix
Python
class Solution: def minimumSum(self, grid: List[List[int]]) -> int: def f(i1: int, j1: int, i2: int, j2: int) -> int: x1 = y1 = inf x2 = y2 = -inf for i in range(i1, i2 + 1): for j in range(j1, j2 + 1): if grid[i][j] == 1: x1 = min(x1, i) y1 = min(y1, j) x2 = max(x2, i) y2 = max(y2, j) return (x2 - x1 + 1) * (y2 - y1 + 1) m, n = len(grid), len(grid[0]) ans = m * n for i1 in range(m - 1): for i2 in range(i1 + 1, m - 1): ans = min( ans, f(0, 0, i1, n - 1) + f(i1 + 1, 0, i2, n - 1) + f(i2 + 1, 0, m - 1, n - 1), ) for j1 in range(n - 1): for j2 in range(j1 + 1, n - 1): ans = min( ans, f(0, 0, m - 1, j1) + f(0, j1 + 1, m - 1, j2) + f(0, j2 + 1, m - 1, n - 1), ) for i in range(m - 1): for j in range(n - 1): ans = min( ans, f(0, 0, i, j) + f(0, j + 1, i, n - 1) + f(i + 1, 0, m - 1, n - 1), ) ans = min( ans, f(0, 0, i, n - 1) + f(i + 1, 0, m - 1, j) + f(i + 1, j + 1, m - 1, n - 1), ) ans = min( ans, f(0, 0, i, j) + f(i + 1, 0, m - 1, j) + f(0, j + 1, m - 1, n - 1), ) ans = min( ans, f(0, 0, m - 1, j) + f(0, j + 1, i, n - 1) + f(i + 1, j + 1, m - 1, n - 1), ) return ans
3,197
Find the Minimum Area to Cover All Ones II
Hard
<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. You need to find 3 <strong>non-overlapping</strong> rectangles having <strong>non-zero</strong> areas with horizontal and vertical sides such that all the 1&#39;s in <code>grid</code> lie inside these rectangles.</p> <p>Return the <strong>minimum</strong> possible sum of the area of these rectangles.</p> <p><strong>Note</strong> that the rectangles are allowed to touch.</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,0,1],[1,1,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example0rect21.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 280px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(1, 0)</code> are covered by a rectangle of area 2.</li> <li>The 1&#39;s at <code>(0, 2)</code> and <code>(1, 2)</code> are covered by a rectangle of area 2.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">grid = [[1,0,1,0],[0,1,0,1]]</span></p> <p><strong>Output:</strong> <span class="example-io">5</span></p> <p><strong>Explanation:</strong></p> <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3100-3199/3197.Find%20the%20Minimum%20Area%20to%20Cover%20All%20Ones%20II/images/example1rect2.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 356px; height: 198px;" /></p> <ul> <li>The 1&#39;s at <code>(0, 0)</code> and <code>(0, 2)</code> are covered by a rectangle of area 3.</li> <li>The 1 at <code>(1, 1)</code> is covered by a rectangle of area 1.</li> <li>The 1 at <code>(1, 3)</code> is covered by a rectangle of area 1.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= grid.length, grid[i].length &lt;= 30</code></li> <li><code>grid[i][j]</code> is either 0 or 1.</li> <li>The input is generated such that there are at least three 1&#39;s in <code>grid</code>.</li> </ul>
Array; Enumeration; Matrix
TypeScript
function minimumSum(grid: number[][]): number { const m = grid.length; const n = grid[0].length; let ans = m * n; const inf = Number.MAX_SAFE_INTEGER; const f = (i1: number, j1: number, i2: number, j2: number): number => { let [x1, y1] = [inf, inf]; let [x2, y2] = [-inf, -inf]; for (let i = i1; i <= i2; i++) { for (let j = j1; j <= j2; j++) { if (grid[i][j] === 1) { x1 = Math.min(x1, i); y1 = Math.min(y1, j); x2 = Math.max(x2, i); y2 = Math.max(y2, j); } } } return x1 === inf ? 0 : (x2 - x1 + 1) * (y2 - y1 + 1); }; for (let i1 = 0; i1 < m - 1; i1++) { for (let i2 = i1 + 1; i2 < m - 1; i2++) { ans = Math.min( ans, f(0, 0, i1, n - 1) + f(i1 + 1, 0, i2, n - 1) + f(i2 + 1, 0, m - 1, n - 1), ); } } for (let j1 = 0; j1 < n - 1; j1++) { for (let j2 = j1 + 1; j2 < n - 1; j2++) { ans = Math.min( ans, f(0, 0, m - 1, j1) + f(0, j1 + 1, m - 1, j2) + f(0, j2 + 1, m - 1, n - 1), ); } } for (let i = 0; i < m - 1; i++) { for (let j = 0; j < n - 1; j++) { ans = Math.min(ans, f(0, 0, i, j) + f(0, j + 1, i, n - 1) + f(i + 1, 0, m - 1, n - 1)); ans = Math.min( ans, f(0, 0, i, n - 1) + f(i + 1, 0, m - 1, j) + f(i + 1, j + 1, m - 1, n - 1), ); ans = Math.min(ans, f(0, 0, i, j) + f(i + 1, 0, m - 1, j) + f(0, j + 1, m - 1, n - 1)); ans = Math.min( ans, f(0, 0, m - 1, j) + f(0, j + 1, i, n - 1) + f(i + 1, j + 1, m - 1, n - 1), ); } } return ans; }
3,198
Find Cities in Each State
Easy
<p>Table: <code>cities</code></p> <pre> +-------------+---------+ | Column Name | Type | +-------------+---------+ | state | varchar | | city | varchar | +-------------+---------+ (state, city) is the primary key (combination of columns with unique values) for this table. Each row of this table contains the state name and the city name within that state. </pre> <p>Write a solution to find <strong>all the cities in each state</strong> and combine them into a <strong>single comma-separated</strong> string.</p> <p>Return <em>the result table ordered by</em> <code>state</code>&nbsp;<em>and</em> <code>city</code>&nbsp;<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>cities table:</p> <pre class="example-io"> +-------------+---------------+ | state | city | +-------------+---------------+ | California | Los Angeles | | California | San Francisco | | California | San Diego | | Texas | Houston | | Texas | Austin | | Texas | Dallas | | New York | New York City | | New York | Buffalo | | New York | Rochester | +-------------+---------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +-------------+---------------------------------------+ | state | cities | +-------------+---------------------------------------+ | California | Los Angeles, San Diego, San Francisco | | New York | Buffalo, New York City, Rochester | | Texas | Austin, Dallas, Houston | +-------------+---------------------------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>California:</strong> All cities (&quot;Los Angeles&quot;, &quot;San Diego&quot;, &quot;San Francisco&quot;) are listed in a comma-separated string.</li> <li><strong>New York:</strong> All cities (&quot;Buffalo&quot;, &quot;New York City&quot;, &quot;Rochester&quot;) are listed in a comma-separated string.</li> <li><strong>Texas:</strong> All cities (&quot;Austin&quot;, &quot;Dallas&quot;, &quot;Houston&quot;) are listed in a comma-separated string.</li> </ul> <p><strong>Note:</strong> The output table is ordered by the state name in ascending order.</p> </div>
Database
Python
import pandas as pd def find_cities(cities: pd.DataFrame) -> pd.DataFrame: result = ( cities.groupby("state")["city"] .apply(lambda x: ", ".join(sorted(x))) .reset_index() ) result.columns = ["state", "cities"] return result
3,198
Find Cities in Each State
Easy
<p>Table: <code>cities</code></p> <pre> +-------------+---------+ | Column Name | Type | +-------------+---------+ | state | varchar | | city | varchar | +-------------+---------+ (state, city) is the primary key (combination of columns with unique values) for this table. Each row of this table contains the state name and the city name within that state. </pre> <p>Write a solution to find <strong>all the cities in each state</strong> and combine them into a <strong>single comma-separated</strong> string.</p> <p>Return <em>the result table ordered by</em> <code>state</code>&nbsp;<em>and</em> <code>city</code>&nbsp;<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>cities table:</p> <pre class="example-io"> +-------------+---------------+ | state | city | +-------------+---------------+ | California | Los Angeles | | California | San Francisco | | California | San Diego | | Texas | Houston | | Texas | Austin | | Texas | Dallas | | New York | New York City | | New York | Buffalo | | New York | Rochester | +-------------+---------------+ </pre> <p><strong>Output:</strong></p> <pre class="example-io"> +-------------+---------------------------------------+ | state | cities | +-------------+---------------------------------------+ | California | Los Angeles, San Diego, San Francisco | | New York | Buffalo, New York City, Rochester | | Texas | Austin, Dallas, Houston | +-------------+---------------------------------------+ </pre> <p><strong>Explanation:</strong></p> <ul> <li><strong>California:</strong> All cities (&quot;Los Angeles&quot;, &quot;San Diego&quot;, &quot;San Francisco&quot;) are listed in a comma-separated string.</li> <li><strong>New York:</strong> All cities (&quot;Buffalo&quot;, &quot;New York City&quot;, &quot;Rochester&quot;) are listed in a comma-separated string.</li> <li><strong>Texas:</strong> All cities (&quot;Austin&quot;, &quot;Dallas&quot;, &quot;Houston&quot;) are listed in a comma-separated string.</li> </ul> <p><strong>Note:</strong> The output table is ordered by the state name in ascending order.</p> </div>
Database
SQL
# Write your MySQL query statement below SELECT state, GROUP_CONCAT(city ORDER BY city SEPARATOR ', ') cities FROM cities GROUP BY 1 ORDER BY 1;
3,199
Count Triplets with Even XOR Set Bits I
Easy
Given three integer arrays <code>a</code>, <code>b</code>, and <code>c</code>, return the number of triplets <code>(a[i], b[j], c[k])</code>, such that the bitwise <code>XOR</code> of the elements of each triplet has an <strong>even</strong> number of <span data-keyword="set-bit">set bits</span>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1], b = [2], c = [3]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only triplet is <code>(a[0], b[0], c[0])</code> and their <code>XOR</code> is: <code>1 XOR 2 XOR 3 = 00<sub>2</sub></code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1,1], b = [2,3], c = [1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Consider these four triplets:</p> <ul> <li><code>(a[0], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[1], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[0], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> <li><code>(a[1], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= a.length, b.length, c.length &lt;= 100</code></li> <li><code>0 &lt;= a[i], b[i], c[i] &lt;= 100</code></li> </ul>
Bit Manipulation; Array
C++
class Solution { public: int tripletCount(vector<int>& a, vector<int>& b, vector<int>& c) { int cnt1[2]{}; int cnt2[2]{}; int cnt3[2]{}; for (int x : a) { ++cnt1[__builtin_popcount(x) & 1]; } for (int x : b) { ++cnt2[__builtin_popcount(x) & 1]; } for (int x : c) { ++cnt3[__builtin_popcount(x) & 1]; } int ans = 0; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { if ((i + j + k) % 2 == 0) { ans += cnt1[i] * cnt2[j] * cnt3[k]; } } } } return ans; } };
3,199
Count Triplets with Even XOR Set Bits I
Easy
Given three integer arrays <code>a</code>, <code>b</code>, and <code>c</code>, return the number of triplets <code>(a[i], b[j], c[k])</code>, such that the bitwise <code>XOR</code> of the elements of each triplet has an <strong>even</strong> number of <span data-keyword="set-bit">set bits</span>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1], b = [2], c = [3]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only triplet is <code>(a[0], b[0], c[0])</code> and their <code>XOR</code> is: <code>1 XOR 2 XOR 3 = 00<sub>2</sub></code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1,1], b = [2,3], c = [1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Consider these four triplets:</p> <ul> <li><code>(a[0], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[1], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[0], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> <li><code>(a[1], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= a.length, b.length, c.length &lt;= 100</code></li> <li><code>0 &lt;= a[i], b[i], c[i] &lt;= 100</code></li> </ul>
Bit Manipulation; Array
Go
func tripletCount(a []int, b []int, c []int) (ans int) { cnt1 := [2]int{} cnt2 := [2]int{} cnt3 := [2]int{} for _, x := range a { cnt1[bits.OnesCount(uint(x))%2]++ } for _, x := range b { cnt2[bits.OnesCount(uint(x))%2]++ } for _, x := range c { cnt3[bits.OnesCount(uint(x))%2]++ } for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { for k := 0; k < 2; k++ { if (i+j+k)%2 == 0 { ans += cnt1[i] * cnt2[j] * cnt3[k] } } } } return }
3,199
Count Triplets with Even XOR Set Bits I
Easy
Given three integer arrays <code>a</code>, <code>b</code>, and <code>c</code>, return the number of triplets <code>(a[i], b[j], c[k])</code>, such that the bitwise <code>XOR</code> of the elements of each triplet has an <strong>even</strong> number of <span data-keyword="set-bit">set bits</span>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1], b = [2], c = [3]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <p>The only triplet is <code>(a[0], b[0], c[0])</code> and their <code>XOR</code> is: <code>1 XOR 2 XOR 3 = 00<sub>2</sub></code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">a = [1,1], b = [2,3], c = [1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>Consider these four triplets:</p> <ul> <li><code>(a[0], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[1], b[1], c[0])</code>: <code>1 XOR 3 XOR 1 = 011<sub>2</sub></code></li> <li><code>(a[0], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> <li><code>(a[1], b[0], c[1])</code>: <code>1 XOR 2 XOR 5 = 110<sub>2</sub></code></li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= a.length, b.length, c.length &lt;= 100</code></li> <li><code>0 &lt;= a[i], b[i], c[i] &lt;= 100</code></li> </ul>
Bit Manipulation; Array
Java
class Solution { public int tripletCount(int[] a, int[] b, int[] c) { int[] cnt1 = new int[2]; int[] cnt2 = new int[2]; int[] cnt3 = new int[2]; for (int x : a) { ++cnt1[Integer.bitCount(x) & 1]; } for (int x : b) { ++cnt2[Integer.bitCount(x) & 1]; } for (int x : c) { ++cnt3[Integer.bitCount(x) & 1]; } int ans = 0; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { if ((i + j + k) % 2 == 0) { ans += cnt1[i] * cnt2[j] * cnt3[k]; } } } } return ans; } }