id
int64
1
3.67k
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,660
Jump Game IX
Medium
<p>You are given an integer array <code>nums</code>.</p> <p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p> <ul> <li>Jump to index <code>j</code> where <code>j &gt; i</code> is allowed only if <code>nums[j] &lt; nums[i]</code>.</li> <li>Jump to index <code>j</code> where <code>j &lt; i</code> is allowed only if <code>nums[j] &gt; nums[i]</code>.</li> </ul> <p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p> <p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: No jump increases the value.</li> <li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li> <li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> </ul> <p>Thus, <code>ans = [2, 2, 3]</code>.</p> <ul> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li> <li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> <li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li> </ul> <p>Thus, <code>ans = [3, 3, 3]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup>​​​​​​​</code></li> </ul>
Go
func maxValue(nums []int) []int { n := len(nums) ans := make([]int, n) preMax := make([]int, n) preMax[0] = nums[0] for i := 1; i < n; i++ { preMax[i] = max(preMax[i-1], nums[i]) } sufMin := 1 << 30 for i := n - 1; i >= 0; i-- { if preMax[i] > sufMin { ans[i] = ans[i+1] } else { ans[i] = preMax[i] } sufMin = min(sufMin, nums[i]) } return ans }
3,660
Jump Game IX
Medium
<p>You are given an integer array <code>nums</code>.</p> <p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p> <ul> <li>Jump to index <code>j</code> where <code>j &gt; i</code> is allowed only if <code>nums[j] &lt; nums[i]</code>.</li> <li>Jump to index <code>j</code> where <code>j &lt; i</code> is allowed only if <code>nums[j] &gt; nums[i]</code>.</li> </ul> <p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p> <p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: No jump increases the value.</li> <li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li> <li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> </ul> <p>Thus, <code>ans = [2, 2, 3]</code>.</p> <ul> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li> <li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> <li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li> </ul> <p>Thus, <code>ans = [3, 3, 3]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup>​​​​​​​</code></li> </ul>
Java
class Solution { public int[] maxValue(int[] nums) { int n = nums.length; int[] ans = new int[n]; int[] preMax = new int[n]; preMax[0] = nums[0]; for (int i = 1; i < n; ++i) { preMax[i] = Math.max(preMax[i - 1], nums[i]); } int sufMin = 1 << 30; for (int i = n - 1; i >= 0; --i) { ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i]; sufMin = Math.min(sufMin, nums[i]); } return ans; } }
3,660
Jump Game IX
Medium
<p>You are given an integer array <code>nums</code>.</p> <p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p> <ul> <li>Jump to index <code>j</code> where <code>j &gt; i</code> is allowed only if <code>nums[j] &lt; nums[i]</code>.</li> <li>Jump to index <code>j</code> where <code>j &lt; i</code> is allowed only if <code>nums[j] &gt; nums[i]</code>.</li> </ul> <p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p> <p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: No jump increases the value.</li> <li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li> <li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> </ul> <p>Thus, <code>ans = [2, 2, 3]</code>.</p> <ul> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li> <li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> <li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li> </ul> <p>Thus, <code>ans = [3, 3, 3]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup>​​​​​​​</code></li> </ul>
Python
class Solution: def maxValue(self, nums: List[int]) -> List[int]: n = len(nums) ans = [0] * n pre_max = [nums[0]] * n for i in range(1, n): pre_max[i] = max(pre_max[i - 1], nums[i]) suf_min = inf for i in range(n - 1, -1, -1): ans[i] = ans[i + 1] if pre_max[i] > suf_min else pre_max[i] suf_min = min(suf_min, nums[i]) return ans
3,660
Jump Game IX
Medium
<p>You are given an integer array <code>nums</code>.</p> <p>From any index <code>i</code>, you can jump to another index <code>j</code> under the following rules:</p> <ul> <li>Jump to index <code>j</code> where <code>j &gt; i</code> is allowed only if <code>nums[j] &lt; nums[i]</code>.</li> <li>Jump to index <code>j</code> where <code>j &lt; i</code> is allowed only if <code>nums[j] &gt; nums[i]</code>.</li> </ul> <p>For each index <code>i</code>, find the <strong>maximum</strong> <strong>value</strong> in <code>nums</code> that can be reached by following <strong>any</strong> sequence of valid jumps starting at <code>i</code>.</p> <p>Return an array <code>ans</code> where <code>ans[i]</code> is the <strong>maximum</strong> <strong>value</strong> reachable starting from index <code>i</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,1,3]</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: No jump increases the value.</li> <li>For <code>i = 1</code>: Jump to <code>j = 0</code> as <code>nums[j] = 2</code> is greater than <code>nums[i]</code>.</li> <li>For <code>i = 2</code>: Since <code>nums[2] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> </ul> <p>Thus, <code>ans = [2, 2, 3]</code>.</p> <ul> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [2,3,1]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,3,3]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>For <code>i = 0</code>: Jump forward to <code>j = 2</code> as <code>nums[j] = 1</code> is less than <code>nums[i] = 2</code>, then from <code>i = 2</code> jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2]</code>.</li> <li>For <code>i = 1</code>: Since <code>nums[1] = 3</code> is the maximum value in <code>nums</code>, no jump increases the value.</li> <li>For <code>i = 2</code>: Jump to <code>j = 1</code> as <code>nums[j] = 3</code> is greater than <code>nums[2] = 1</code>.</li> </ul> <p>Thus, <code>ans = [3, 3, 3]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup>​​​​​​​</code></li> </ul>
TypeScript
function maxValue(nums: number[]): number[] { const n = nums.length; const ans = Array(n).fill(0); const preMax = Array(n).fill(nums[0]); for (let i = 1; i < n; i++) { preMax[i] = Math.max(preMax[i - 1], nums[i]); } let sufMin = 1 << 30; for (let i = n - 1; i >= 0; i--) { ans[i] = preMax[i] > sufMin ? ans[i + 1] : preMax[i]; sufMin = Math.min(sufMin, nums[i]); } return ans; }
3,661
Maximum Walls Destroyed by Robots
Hard
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf"> <div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div> </div> <ul> <li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li> <li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot&#39;s bullet can travel.</li> <li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li> </ul> <p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p> <p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p> <p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p> <p>Notes:</p> <ul> <li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li> <li>Robots are not destroyed by bullets.</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">robots = [4], distance = [3], walls = [1,10]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li> <li>Thus, the answer is 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li> <li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li> <li>Thus, the answer is 3.</li> </ul> </div> <strong class="example">Example 3:</strong> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= robots.length == distance.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= walls.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= robots[i], walls[j] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= distance[i] &lt;= 10<sup>5</sup></code></li> <li>All values in <code>robots</code> are <strong>unique</strong></li> <li>All values in <code>walls</code> are <strong>unique</strong></li> </ul>
C++
class Solution { public: int maxWalls(vector<int>& robots, vector<int>& distance, vector<int>& walls) { int n = robots.size(); vector<pair<int, int>> arr(n); for (int i = 0; i < n; i++) { arr[i] = {robots[i], distance[i]}; } ranges::sort(arr, {}, &pair<int, int>::first); ranges::sort(walls); vector f(n, vector<int>(2, -1)); auto dfs = [&](this auto&& dfs, int i, int j) -> int { if (i < 0) { return 0; } if (f[i][j] != -1) { return f[i][j]; } int left = arr[i].first - arr[i].second; if (i > 0) { left = max(left, arr[i - 1].first + 1); } int l = ranges::lower_bound(walls, left) - walls.begin(); int r = ranges::lower_bound(walls, arr[i].first + 1) - walls.begin(); int ans = dfs(i - 1, 0) + (r - l); int right = arr[i].first + arr[i].second; if (i + 1 < n) { if (j == 0) { right = min(right, arr[i + 1].first - arr[i + 1].second - 1); } else { right = min(right, arr[i + 1].first - 1); } } l = ranges::lower_bound(walls, arr[i].first) - walls.begin(); r = ranges::lower_bound(walls, right + 1) - walls.begin(); ans = max(ans, dfs(i - 1, 1) + (r - l)); return f[i][j] = ans; }; return dfs(n - 1, 1); } };
3,661
Maximum Walls Destroyed by Robots
Hard
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf"> <div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div> </div> <ul> <li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li> <li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot&#39;s bullet can travel.</li> <li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li> </ul> <p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p> <p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p> <p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p> <p>Notes:</p> <ul> <li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li> <li>Robots are not destroyed by bullets.</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">robots = [4], distance = [3], walls = [1,10]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li> <li>Thus, the answer is 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li> <li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li> <li>Thus, the answer is 3.</li> </ul> </div> <strong class="example">Example 3:</strong> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= robots.length == distance.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= walls.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= robots[i], walls[j] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= distance[i] &lt;= 10<sup>5</sup></code></li> <li>All values in <code>robots</code> are <strong>unique</strong></li> <li>All values in <code>walls</code> are <strong>unique</strong></li> </ul>
Go
func maxWalls(robots []int, distance []int, walls []int) int { type pair struct { x, d int } n := len(robots) arr := make([]pair, n) for i := 0; i < n; i++ { arr[i] = pair{robots[i], distance[i]} } sort.Slice(arr, func(i, j int) bool { return arr[i].x < arr[j].x }) sort.Ints(walls) f := make(map[[2]int]int) var dfs func(int, int) int dfs = func(i, j int) int { if i < 0 { return 0 } key := [2]int{i, j} if v, ok := f[key]; ok { return v } left := arr[i].x - arr[i].d if i > 0 { left = max(left, arr[i-1].x+1) } l := sort.SearchInts(walls, left) r := sort.SearchInts(walls, arr[i].x+1) ans := dfs(i-1, 0) + (r - l) right := arr[i].x + arr[i].d if i+1 < n { if j == 0 { right = min(right, arr[i+1].x-arr[i+1].d-1) } else { right = min(right, arr[i+1].x-1) } } l = sort.SearchInts(walls, arr[i].x) r = sort.SearchInts(walls, right+1) ans = max(ans, dfs(i-1, 1)+(r-l)) f[key] = ans return ans } return dfs(n-1, 1) }
3,661
Maximum Walls Destroyed by Robots
Hard
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf"> <div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div> </div> <ul> <li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li> <li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot&#39;s bullet can travel.</li> <li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li> </ul> <p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p> <p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p> <p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p> <p>Notes:</p> <ul> <li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li> <li>Robots are not destroyed by bullets.</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">robots = [4], distance = [3], walls = [1,10]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li> <li>Thus, the answer is 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li> <li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li> <li>Thus, the answer is 3.</li> </ul> </div> <strong class="example">Example 3:</strong> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= robots.length == distance.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= walls.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= robots[i], walls[j] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= distance[i] &lt;= 10<sup>5</sup></code></li> <li>All values in <code>robots</code> are <strong>unique</strong></li> <li>All values in <code>walls</code> are <strong>unique</strong></li> </ul>
Java
class Solution { private Integer[][] f; private int[][] arr; private int[] walls; private int n; public int maxWalls(int[] robots, int[] distance, int[] walls) { n = robots.length; arr = new int[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = robots[i]; arr[i][1] = distance[i]; } Arrays.sort(arr, Comparator.comparingInt(a -> a[0])); Arrays.sort(walls); this.walls = walls; f = new Integer[n][2]; return dfs(n - 1, 1); } private int dfs(int i, int j) { if (i < 0) { return 0; } if (f[i][j] != null) { return f[i][j]; } int left = arr[i][0] - arr[i][1]; if (i > 0) { left = Math.max(left, arr[i - 1][0] + 1); } int l = lowerBound(walls, left); int r = lowerBound(walls, arr[i][0] + 1); int ans = dfs(i - 1, 0) + (r - l); int right = arr[i][0] + arr[i][1]; if (i + 1 < n) { if (j == 0) { right = Math.min(right, arr[i + 1][0] - arr[i + 1][1] - 1); } else { right = Math.min(right, arr[i + 1][0] - 1); } } l = lowerBound(walls, arr[i][0]); r = lowerBound(walls, right + 1); ans = Math.max(ans, dfs(i - 1, 1) + (r - l)); return f[i][j] = ans; } private int lowerBound(int[] arr, int target) { int idx = Arrays.binarySearch(arr, target); if (idx < 0) { return -idx - 1; } return idx; } }
3,661
Maximum Walls Destroyed by Robots
Hard
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf"> <div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div> </div> <ul> <li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li> <li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot&#39;s bullet can travel.</li> <li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li> </ul> <p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p> <p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p> <p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p> <p>Notes:</p> <ul> <li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li> <li>Robots are not destroyed by bullets.</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">robots = [4], distance = [3], walls = [1,10]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li> <li>Thus, the answer is 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li> <li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li> <li>Thus, the answer is 3.</li> </ul> </div> <strong class="example">Example 3:</strong> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= robots.length == distance.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= walls.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= robots[i], walls[j] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= distance[i] &lt;= 10<sup>5</sup></code></li> <li>All values in <code>robots</code> are <strong>unique</strong></li> <li>All values in <code>walls</code> are <strong>unique</strong></li> </ul>
Python
class Solution: def maxWalls(self, robots: List[int], distance: List[int], walls: List[int]) -> int: n = len(robots) arr = sorted(zip(robots, distance), key=lambda x: x[0]) walls.sort() @cache def dfs(i: int, j: int) -> int: if i < 0: return 0 left = arr[i][0] - arr[i][1] if i > 0: left = max(left, arr[i - 1][0] + 1) l = bisect_left(walls, left) r = bisect_left(walls, arr[i][0] + 1) ans = dfs(i - 1, 0) + r - l right = arr[i][0] + arr[i][1] if i + 1 < n: if j == 0: right = min(right, arr[i + 1][0] - arr[i + 1][1] - 1) else: right = min(right, arr[i + 1][0] - 1) l = bisect_left(walls, arr[i][0]) r = bisect_left(walls, right + 1) ans = max(ans, dfs(i - 1, 1) + r - l) return ans return dfs(n - 1, 1)
3,661
Maximum Walls Destroyed by Robots
Hard
<div data-docx-has-block-data="false" data-lark-html-role="root" data-page-id="Rax8d6clvoFeVtx7bzXcvkVynwf"> <div class="old-record-id-Y5dGdSKIMoNTttxGhHLccrpEnaf">There is an endless straight line populated with some robots and walls. You are given integer arrays <code>robots</code>, <code>distance</code>, and <code>walls</code>:</div> </div> <ul> <li><code>robots[i]</code> is the position of the <code>i<sup>th</sup></code> robot.</li> <li><code>distance[i]</code> is the <strong>maximum</strong> distance the <code>i<sup>th</sup></code> robot&#39;s bullet can travel.</li> <li><code>walls[j]</code> is the position of the <code>j<sup>th</sup></code> wall.</li> </ul> <p>Every robot has <strong>one</strong> bullet that can either fire to the left or the right <strong>at most </strong><code>distance[i]</code> meters.</p> <p>A bullet destroys every wall in its path that lies within its range. Robots are fixed obstacles: if a bullet hits another robot before reaching a wall, it <strong>immediately stops</strong> at that robot and cannot continue.</p> <p>Return the <strong>maximum</strong> number of <strong>unique</strong> walls that can be destroyed by the robots.</p> <p>Notes:</p> <ul> <li>A wall and a robot may share the same position; the wall can be destroyed by the robot at that position.</li> <li>Robots are not destroyed by bullets.</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">robots = [4], distance = [3], walls = [1,10]</span></p> <p><strong>Output:</strong> <span class="example-io">1</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 4</code> fires <strong>left</strong> with <code>distance[0] = 3</code>, covering <code>[1, 4]</code> and destroys <code>walls[0] = 1</code>.</li> <li>Thus, the answer is 1.</li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [10,2], distance = [5,1], walls = [5,2,7]</span></p> <p><strong>Output:</strong> <span class="example-io">3</span></p> <p><strong>Explanation:</strong></p> <ul> <li><code>robots[0] = 10</code> fires <strong>left</strong> with <code>distance[0] = 5</code>, covering <code>[5, 10]</code> and destroys <code>walls[0] = 5</code> and <code>walls[2] = 7</code>.</li> <li><code>robots[1] = 2</code> fires <strong>left</strong> with <code>distance[1] = 1</code>, covering <code>[1, 2]</code> and destroys <code>walls[1] = 2</code>.</li> <li>Thus, the answer is 3.</li> </ul> </div> <strong class="example">Example 3:</strong> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">robots = [1,2], distance = [100,1], walls = [10]</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>In this example, only <code>robots[0]</code> can reach the wall, but its shot to the <strong>right</strong> is blocked by <code>robots[1]</code>; thus the answer is 0.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= robots.length == distance.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= walls.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= robots[i], walls[j] &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= distance[i] &lt;= 10<sup>5</sup></code></li> <li>All values in <code>robots</code> are <strong>unique</strong></li> <li>All values in <code>walls</code> are <strong>unique</strong></li> </ul>
TypeScript
function maxWalls(robots: number[], distance: number[], walls: number[]): number { type Pair = [number, number]; const n = robots.length; const arr: Pair[] = robots.map((r, i) => [r, distance[i]]); _.sortBy(arr, p => p[0]).forEach((p, i) => (arr[i] = p)); walls.sort((a, b) => a - b); const f: number[][] = Array.from({ length: n }, () => Array(2).fill(-1)); function dfs(i: number, j: number): number { if (i < 0) { return 0; } if (f[i][j] !== -1) { return f[i][j]; } let left = arr[i][0] - arr[i][1]; if (i > 0) left = Math.max(left, arr[i - 1][0] + 1); let l = _.sortedIndex(walls, left); let r = _.sortedIndex(walls, arr[i][0] + 1); let ans = dfs(i - 1, 0) + (r - l); let right = arr[i][0] + arr[i][1]; if (i + 1 < n) { if (j === 0) { right = Math.min(right, arr[i + 1][0] - arr[i + 1][1] - 1); } else { right = Math.min(right, arr[i + 1][0] - 1); } } l = _.sortedIndex(walls, arr[i][0]); r = _.sortedIndex(walls, right + 1); ans = Math.max(ans, dfs(i - 1, 1) + (r - l)); f[i][j] = ans; return ans; } return dfs(n - 1, 1); }
3,662
Filter Characters by Frequency
Easy
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p> <p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p> <p>Return the resulting string. If no characters qualify, return an empty string.</p> <p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Character frequencies in <code>s</code>:</p> <ul> <li><code>&#39;a&#39;</code> appears 3 times</li> <li><code>&#39;d&#39;</code> appears 1 time</li> <li><code>&#39;b&#39;</code> appears 2 times</li> <li><code>&#39;c&#39;</code> appears 4 times</li> </ul> <p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p> <p><strong>Explanation:</strong></p> <p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
C++
class Solution { public: string filterCharacters(string s, int k) { int cnt[26]{}; for (char c : s) { ++cnt[c - 'a']; } string ans; for (char c : s) { if (cnt[c - 'a'] < k) { ans.push_back(c); } } return ans; } };
3,662
Filter Characters by Frequency
Easy
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p> <p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p> <p>Return the resulting string. If no characters qualify, return an empty string.</p> <p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Character frequencies in <code>s</code>:</p> <ul> <li><code>&#39;a&#39;</code> appears 3 times</li> <li><code>&#39;d&#39;</code> appears 1 time</li> <li><code>&#39;b&#39;</code> appears 2 times</li> <li><code>&#39;c&#39;</code> appears 4 times</li> </ul> <p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p> <p><strong>Explanation:</strong></p> <p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
Go
func filterCharacters(s string, k int) string { cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } ans := []rune{} for _, c := range s { if cnt[c-'a'] < k { ans = append(ans, c) } } return string(ans) }
3,662
Filter Characters by Frequency
Easy
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p> <p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p> <p>Return the resulting string. If no characters qualify, return an empty string.</p> <p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Character frequencies in <code>s</code>:</p> <ul> <li><code>&#39;a&#39;</code> appears 3 times</li> <li><code>&#39;d&#39;</code> appears 1 time</li> <li><code>&#39;b&#39;</code> appears 2 times</li> <li><code>&#39;c&#39;</code> appears 4 times</li> </ul> <p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p> <p><strong>Explanation:</strong></p> <p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
Java
class Solution { public String filterCharacters(String s, int k) { int[] cnt = new int[26]; for (char c : s.toCharArray()) { ++cnt[c - 'a']; } StringBuilder ans = new StringBuilder(); for (char c : s.toCharArray()) { if (cnt[c - 'a'] < k) { ans.append(c); } } return ans.toString(); } }
3,662
Filter Characters by Frequency
Easy
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p> <p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p> <p>Return the resulting string. If no characters qualify, return an empty string.</p> <p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Character frequencies in <code>s</code>:</p> <ul> <li><code>&#39;a&#39;</code> appears 3 times</li> <li><code>&#39;d&#39;</code> appears 1 time</li> <li><code>&#39;b&#39;</code> appears 2 times</li> <li><code>&#39;c&#39;</code> appears 4 times</li> </ul> <p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p> <p><strong>Explanation:</strong></p> <p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
Python
class Solution: def filterCharacters(self, s: str, k: int) -> str: cnt = Counter(s) ans = [] for c in s: if cnt[c] < k: ans.append(c) return "".join(ans)
3,662
Filter Characters by Frequency
Easy
<p>You are given a string <code>s</code> consisting of lowercase English letters and an integer <code>k</code>.</p> <p>Your task is to construct a new string that contains only those characters from <code>s</code> which appear <strong>fewer</strong> than <code>k</code> times in the entire string. The order of characters in the new string must be the <strong>same</strong> as their <strong>order</strong> in <code>s</code>.</p> <p>Return the resulting string. If no characters qualify, return an empty string.</p> <p>Note: <strong>Every occurrence</strong> of a character that occurs fewer than <code>k</code> times is kept.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;aadbbcccca&quot;, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;dbb&quot;</span></p> <p><strong>Explanation:</strong></p> <p>Character frequencies in <code>s</code>:</p> <ul> <li><code>&#39;a&#39;</code> appears 3 times</li> <li><code>&#39;d&#39;</code> appears 1 time</li> <li><code>&#39;b&#39;</code> appears 2 times</li> <li><code>&#39;c&#39;</code> appears 4 times</li> </ul> <p>Only <code>&#39;d&#39;</code> and <code>&#39;b&#39;</code> appear fewer than 3 times. Preserving their order, the result is <code>&quot;dbb&quot;</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;xyz&quot;, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;xyz&quot;</span></p> <p><strong>Explanation:</strong></p> <p>All characters (<code>&#39;x&#39;</code>, <code>&#39;y&#39;</code>, <code>&#39;z&#39;</code>) appear exactly once, which is fewer than 2. Thus the whole string is returned.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 100</code></li> <li><code>s</code> consists of lowercase English letters.</li> <li><code>1 &lt;= k &lt;= s.length</code></li> </ul>
TypeScript
function filterCharacters(s: string, k: number): string { const cnt: Record<string, number> = {}; for (const c of s) { cnt[c] = (cnt[c] || 0) + 1; } const ans: string[] = []; for (const c of s) { if (cnt[c] < k) { ans.push(c); } } return ans.join(''); }
3,663
Find The Least Frequent Digit
Easy
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p> <p>Return the chosen digit as an integer.</p> The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li> </ul>
C++
class Solution { public: int getLeastFrequentDigit(int n) { int cnt[10]{}; for (; n > 0; n /= 10) { ++cnt[n % 10]; } int ans = 0, f = 1 << 30; for (int x = 0; x < 10; ++x) { if (cnt[x] > 0 && cnt[x] < f) { f = cnt[x]; ans = x; } } return ans; } };
3,663
Find The Least Frequent Digit
Easy
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p> <p>Return the chosen digit as an integer.</p> The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li> </ul>
Go
func getLeastFrequentDigit(n int) (ans int) { cnt := [10]int{} for ; n > 0; n /= 10 { cnt[n%10]++ } f := 1 << 30 for x, v := range cnt { if v > 0 && v < f { f = v ans = x } } return }
3,663
Find The Least Frequent Digit
Easy
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p> <p>Return the chosen digit as an integer.</p> The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li> </ul>
Java
class Solution { public int getLeastFrequentDigit(int n) { int[] cnt = new int[10]; for (; n > 0; n /= 10) { ++cnt[n % 10]; } int ans = 0, f = 1 << 30; for (int x = 0; x < 10; ++x) { if (cnt[x] > 0 && cnt[x] < f) { f = cnt[x]; ans = x; } } return ans; } }
3,663
Find The Least Frequent Digit
Easy
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p> <p>Return the chosen digit as an integer.</p> The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li> </ul>
Python
class Solution: def getLeastFrequentDigit(self, n: int) -> int: cnt = [0] * 10 while n: n, x = divmod(n, 10) cnt[x] += 1 ans, f = 0, inf for x, v in enumerate(cnt): if 0 < v < f: f = v ans = x return ans
3,663
Find The Least Frequent Digit
Easy
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p> <p>Return the chosen digit as an integer.</p> The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>. <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li> </ul>
TypeScript
function getLeastFrequentDigit(n: number): number { const cnt: number[] = Array(10).fill(0); for (; n; n = (n / 10) | 0) { cnt[n % 10]++; } let [ans, f] = [0, Number.MAX_SAFE_INTEGER]; for (let x = 0; x < 10; ++x) { if (cnt[x] > 0 && cnt[x] < f) { f = cnt[x]; ans = x; } } return ans; }
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
C++
class Solution { public: vector<int> sortByAbsoluteValue(vector<int>& nums) { sort(nums.begin(), nums.end(), [](int a, int b) { return abs(a) < abs(b); }); return nums; } };
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Go
func sortByAbsoluteValue(nums []int) []int { slices.SortFunc(nums, func(a, b int) int { return abs(a) - abs(b) }) return nums } func abs(x int) int { if x < 0 { return -x } return x }
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Java
class Solution { public int[] sortByAbsoluteValue(int[] nums) { return Arrays.stream(nums) .boxed() .sorted(Comparator.comparingInt(Math::abs)) .mapToInt(Integer::intValue) .toArray(); } }
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Python
class Solution: def sortByAbsoluteValue(self, nums: List[int]) -> List[int]: return sorted(nums, key=lambda x: abs(x))
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
Rust
impl Solution { pub fn sort_by_absolute_value(mut nums: Vec<i32>) -> Vec<i32> { nums.sort_by_key(|&x| x.abs()); nums } }
3,667
Sort Array By Absolute Value
Easy
<p>You are given an integer array <code>nums</code>.</p> <p>Rearrange elements of <code>nums</code> in <strong>non-decreasing</strong> order of their absolute value.</p> <p>Return <strong>any</strong> rearranged array that satisfies this condition.</p> <p><strong>Note</strong>: The absolute value of an integer x is defined as:</p> <ul> <li><code>x</code> if <code>x &gt;= 0</code></li> <li><code>-x</code> if <code>x &lt; 0</code></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,1,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[-1,1,3,-4,5]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 3, 1, 4, 1, 5 respectively.</li> <li>Rearranging them in increasing order, we get 1, 1, 3, 4, 5.</li> <li>This corresponds to <code>[-1, 1, 3, -4, 5]</code>. Another possible rearrangement is <code>[1, -1, 3, -4, 5].</code></li> </ul> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [-100,100]</span></p> <p><strong>Output:</strong> <span class="example-io">[-100,100]</span></p> <p><strong>Explanation:</strong></p> <ul> <li>The absolute values of elements in <code>nums</code> are 100, 100 respectively.</li> <li>Rearranging them in increasing order, we get 100, 100.</li> <li>This corresponds to <code>[-100, 100]</code>. Another possible rearrangement is <code>[100, -100]</code>.</li> </ul> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 100</code></li> <li><code>-100 &lt;= nums[i] &lt;= 100</code></li> </ul>
TypeScript
function sortByAbsoluteValue(nums: number[]): number[] { return nums.sort((a, b) => Math.abs(a) - Math.abs(b)); }
3,668
Restore Finishing Order
Easy
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p> <ul> <li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li> <li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li> </ul> <p>Return an array containing your friends&#39; IDs in their <strong>finishing</strong> order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == order.length &lt;= 100</code></li> <li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li> <li><code>1 &lt;= friends.length &lt;= min(8, n)</code></li> <li><code>1 &lt;= friends[i] &lt;= n</code></li> <li><code>friends</code> is strictly increasing</li> </ul>
C++
class Solution { public: vector<int> recoverOrder(vector<int>& order, vector<int>& friends) { int n = order.size(); vector<int> d(n + 1); for (int i = 0; i < n; ++i) { d[order[i]] = i; } sort(friends.begin(), friends.end(), [&](int a, int b) { return d[a] < d[b]; }); return friends; } };
3,668
Restore Finishing Order
Easy
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p> <ul> <li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li> <li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li> </ul> <p>Return an array containing your friends&#39; IDs in their <strong>finishing</strong> order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == order.length &lt;= 100</code></li> <li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li> <li><code>1 &lt;= friends.length &lt;= min(8, n)</code></li> <li><code>1 &lt;= friends[i] &lt;= n</code></li> <li><code>friends</code> is strictly increasing</li> </ul>
Go
func recoverOrder(order []int, friends []int) []int { n := len(order) d := make([]int, n+1) for i, x := range order { d[x] = i } sort.Slice(friends, func(i, j int) bool { return d[friends[i]] < d[friends[j]] }) return friends }
3,668
Restore Finishing Order
Easy
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p> <ul> <li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li> <li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li> </ul> <p>Return an array containing your friends&#39; IDs in their <strong>finishing</strong> order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == order.length &lt;= 100</code></li> <li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li> <li><code>1 &lt;= friends.length &lt;= min(8, n)</code></li> <li><code>1 &lt;= friends[i] &lt;= n</code></li> <li><code>friends</code> is strictly increasing</li> </ul>
Java
class Solution { public int[] recoverOrder(int[] order, int[] friends) { int n = order.length; int[] d = new int[n + 1]; for (int i = 0; i < n; ++i) { d[order[i]] = i; } return Arrays.stream(friends) .boxed() .sorted((a, b) -> d[a] - d[b]) .mapToInt(Integer::intValue) .toArray(); } }
3,668
Restore Finishing Order
Easy
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p> <ul> <li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li> <li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li> </ul> <p>Return an array containing your friends&#39; IDs in their <strong>finishing</strong> order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == order.length &lt;= 100</code></li> <li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li> <li><code>1 &lt;= friends.length &lt;= min(8, n)</code></li> <li><code>1 &lt;= friends[i] &lt;= n</code></li> <li><code>friends</code> is strictly increasing</li> </ul>
Python
class Solution: def recoverOrder(self, order: List[int], friends: List[int]) -> List[int]: d = {x: i for i, x in enumerate(order)} return sorted(friends, key=lambda x: d[x])
3,668
Restore Finishing Order
Easy
<p>You are given an integer array <code>order</code> of length <code>n</code> and an integer array <code>friends</code>.</p> <ul> <li><code>order</code> contains every integer from 1 to <code>n</code> <strong>exactly once</strong>, representing the IDs of the participants of a race in their <strong>finishing</strong> order.</li> <li><code>friends</code> contains the IDs of your friends in the race <strong>sorted</strong> in strictly increasing order. Each ID in friends is guaranteed to appear in the <code>order</code> array.</li> </ul> <p>Return an array containing your friends&#39; IDs in their <strong>finishing</strong> order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [3,1,2,5,4], friends = [1,3,4]</span></p> <p><strong>Output:</strong> <span class="example-io">[3,1,4]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[<u><strong>3</strong></u>, <u><strong>1</strong></u>, 2, 5, <u><strong>4</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[3, 1, 4]</code>.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">order = [1,4,5,3,2], friends = [2,5]</span></p> <p><strong>Output:</strong> <span class="example-io">[5,2]</span></p> <p><strong>Explanation:</strong></p> <p>The finishing order is <code>[1, 4, <u><strong>5</strong></u>, 3, <u><strong>2</strong></u>]</code>. Therefore, the finishing order of your friends is <code>[5, 2]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= n == order.length &lt;= 100</code></li> <li><code>order</code> contains every integer from 1 to <code>n</code> exactly once</li> <li><code>1 &lt;= friends.length &lt;= min(8, n)</code></li> <li><code>1 &lt;= friends[i] &lt;= n</code></li> <li><code>friends</code> is strictly increasing</li> </ul>
TypeScript
function recoverOrder(order: number[], friends: number[]): number[] { const n = order.length; const d: number[] = Array(n + 1).fill(0); for (let i = 0; i < n; ++i) { d[order[i]] = i; } return friends.sort((a, b) => d[a] - d[b]); }
3,669
Balanced K-Factor Decomposition
Medium
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p> <p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</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 = 100, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[10,10]</span></p> <p><strong>Explanation:</strong></p> <p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li> <li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li> <li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li> <li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li> </ul> <p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li data-end="54" data-start="37"><code data-end="52" data-start="37">4 &lt;= n &lt;= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li data-end="71" data-start="57"><code data-end="69" data-start="57">2 &lt;= k &lt;= 5</code></li> <li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li> </ul>
C++
class Solution { public: static const int MX = 100001; static vector<vector<int>> g; vector<int> ans; vector<int> path; int cur; vector<int> minDifference(int n, int k) { if (g.empty()) { g.resize(MX); for (int i = 1; i < MX; i++) { for (int j = i; j < MX; j += i) { g[j].push_back(i); } } } cur = INT_MAX; ans.clear(); path.assign(k, 0); dfs(k - 1, n, INT_MAX, 0); return ans; } private: void dfs(int i, int x, int mi, int mx) { if (i == 0) { int d = max(mx, x) - min(mi, x); if (d < cur) { cur = d; path[i] = x; ans = path; } return; } for (int y : g[x]) { path[i] = y; dfs(i - 1, x / y, min(mi, y), max(mx, y)); } } }; vector<vector<int>> Solution::g;
3,669
Balanced K-Factor Decomposition
Medium
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p> <p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</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 = 100, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[10,10]</span></p> <p><strong>Explanation:</strong></p> <p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li> <li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li> <li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li> <li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li> </ul> <p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li data-end="54" data-start="37"><code data-end="52" data-start="37">4 &lt;= n &lt;= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li data-end="71" data-start="57"><code data-end="69" data-start="57">2 &lt;= k &lt;= 5</code></li> <li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li> </ul>
Go
const MX = 100001 var g [][]int func init() { g = make([][]int, MX) for i := 1; i < MX; i++ { for j := i; j < MX; j += i { g[j] = append(g[j], i) } } } var ( cur int ans []int path []int ) func minDifference(n int, k int) []int { cur = math.MaxInt32 ans = nil path = make([]int, k) dfs(k-1, n, math.MaxInt32, 0) return ans } func dfs(i, x, mi, mx int) { if i == 0 { d := max(mx, x) - min(mi, x) if d < cur { cur = d path[i] = x ans = slices.Clone(path) } return } for _, y := range g[x] { path[i] = y dfs(i-1, x/y, min(mi, y), max(mx, y)) } }
3,669
Balanced K-Factor Decomposition
Medium
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p> <p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</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 = 100, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[10,10]</span></p> <p><strong>Explanation:</strong></p> <p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li> <li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li> <li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li> <li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li> </ul> <p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li data-end="54" data-start="37"><code data-end="52" data-start="37">4 &lt;= n &lt;= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li data-end="71" data-start="57"><code data-end="69" data-start="57">2 &lt;= k &lt;= 5</code></li> <li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li> </ul>
Java
class Solution { static final int MX = 100_001; static List<Integer>[] g = new ArrayList[MX]; static { for (int i = 0; i < MX; i++) { g[i] = new ArrayList<>(); } for (int i = 1; i < MX; i++) { for (int j = i; j < MX; j += i) { g[j].add(i); } } } private int cur; private int[] ans; private int[] path; public int[] minDifference(int n, int k) { cur = Integer.MAX_VALUE; ans = null; path = new int[k]; dfs(k - 1, n, Integer.MAX_VALUE, 0); return ans; } private void dfs(int i, int x, int mi, int mx) { if (i == 0) { int d = Math.max(mx, x) - Math.min(mi, x); if (d < cur) { cur = d; path[i] = x; ans = path.clone(); } return; } for (int y : g[x]) { path[i] = y; dfs(i - 1, x / y, Math.min(mi, y), Math.max(mx, y)); } } }
3,669
Balanced K-Factor Decomposition
Medium
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p> <p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</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 = 100, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[10,10]</span></p> <p><strong>Explanation:</strong></p> <p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li> <li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li> <li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li> <li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li> </ul> <p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li data-end="54" data-start="37"><code data-end="52" data-start="37">4 &lt;= n &lt;= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li data-end="71" data-start="57"><code data-end="69" data-start="57">2 &lt;= k &lt;= 5</code></li> <li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li> </ul>
Python
mx = 10**5 + 1 g = [[] for _ in range(mx)] for i in range(1, mx): for j in range(i, mx, i): g[j].append(i) class Solution: def minDifference(self, n: int, k: int) -> List[int]: def dfs(i: int, x: int, mi: int, mx: int): if i == 0: nonlocal cur, ans d = max(mx, x) - min(mi, x) if d < cur: cur = d path[i] = x ans = path[:] return for y in g[x]: path[i] = y dfs(i - 1, x // y, min(mi, y), max(mx, y)) ans = None path = [0] * k cur = inf dfs(k - 1, n, inf, 0) return ans
3,669
Balanced K-Factor Decomposition
Medium
<p>Given two integers <code>n</code> and <code>k</code>, split the number <code>n</code> into exactly <code>k</code> positive integers such that the <strong>product</strong> of these integers is equal to <code>n</code>.</p> <p>Return <em>any</em> <em>one</em> split in which the <strong>maximum</strong> difference between any two numbers is <strong>minimized</strong>. You may return the result in <em>any order</em>.</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 = 100, k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">[10,10]</span></p> <p><strong>Explanation:</strong></p> <p data-end="157" data-start="0">The split <code>[10, 10]</code> yields <code>10 * 10 = 100</code> and a max-min difference of 0, which is minimal.</p> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">n = 44, k = 3</span></p> <p><strong>Output:</strong> <span class="example-io">[2,2,11]</span></p> <p><strong>Explanation:</strong></p> <ul> <li data-end="46" data-start="2">Split <code>[1, 1, 44]</code> yields a difference of 43</li> <li data-end="93" data-start="49">Split <code>[1, 2, 22]</code> yields a difference of 21</li> <li data-end="140" data-start="96">Split <code>[1, 4, 11]</code> yields a difference of 10</li> <li data-end="186" data-start="143">Split <code>[2, 2, 11]</code> yields a difference of 9</li> </ul> <p data-end="264" data-is-last-node="" data-is-only-node="" data-start="188">Therefore, <code>[2, 2, 11]</code> is the optimal split with the smallest difference 9.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li data-end="54" data-start="37"><code data-end="52" data-start="37">4 &lt;= n &lt;= 10<sup><span style="font-size: 10.8333px;">5</span></sup></code></li> <li data-end="71" data-start="57"><code data-end="69" data-start="57">2 &lt;= k &lt;= 5</code></li> <li data-end="145" data-is-last-node="" data-start="74"><code data-end="77" data-start="74">k</code> is strictly less than the total number of positive divisors of <code data-end="144" data-is-only-node="" data-start="141">n</code>.</li> </ul>
TypeScript
const MX = 100001; const g: number[][] = Array.from({ length: MX }, () => []); for (let i = 1; i < MX; i++) { for (let j = i; j < MX; j += i) { g[j].push(i); } } function minDifference(n: number, k: number): number[] { let cur = Number.MAX_SAFE_INTEGER; let ans: number[] | null = null; const path: number[] = Array(k).fill(0); function dfs(i: number, x: number, mi: number, mx: number): void { if (i === 0) { const d = Math.max(mx, x) - Math.min(mi, x); if (d < cur) { cur = d; path[i] = x; ans = [...path]; } return; } for (const y of g[x]) { path[i] = y; dfs(i - 1, Math.floor(x / y), Math.min(mi, y), Math.max(mx, y)); } } dfs(k - 1, n, Number.MAX_SAFE_INTEGER, 0); return ans ?? []; }