From fdac83e3250cf9effda6f7e8252a6c9c44d965bd Mon Sep 17 00:00:00 2001
From: Shuo Given an array Build the You are guaranteed that the target array is strictly increasing, only containing numbers between 1 to Return the operations to build the target array. You are guaranteed that the answer is unique. Example 1: Example 2: Example 3: Example 4: Constraints: Given an array of integers We want to select three indices Let's define Note that ^ denotes the bitwise-xor operation. Return the number of triplets ( Example 1: Example 2: Example 3: Example 4: Example 5: Constraints:
-Given a sorted array, two integers Example 1:target
and an integer n
. In each iteration, you will read a number from list = {1,2,3..., n}
.target
array using the following operations:
+
+
+list
, and push it in the array.n
inclusive.
+Input: target = [1,3], n = 3
+Output: ["Push","Push","Pop","Push"]
+Explanation:
+Read number 1 and automatically push in the array -> [1]
+Read number 2 and automatically push in the array then Pop it -> [1]
+Read number 3 and automatically push in the array -> [1,3]
+
+
+
+Input: target = [1,2,3], n = 3
+Output: ["Push","Push","Push"]
+
+
+
+Input: target = [1,2], n = 4
+Output: ["Push","Push"]
+Explanation: You only need to read the first 2 numbers and stop.
+
+
+
+Input: target = [2,3,4], n = 4
+Output: ["Push","Pop","Push","Push","Push"]
+
+
+
+
+
+### Related Topics
+ [[Stack](../../tag/stack/README.md)]
+
+### Hints
+1 <= target.length <= 100
1 <= target[i] <= 100
1 <= n <= 100
target
is strictly increasing.Hint 1
+Use “Push” for numbers to be kept in target array and [“Push”, “Pop”] for numbers to be discarded.
+arr
.i
, j
and k
where (0 <= i < j <= k < arr.length)
.a
and b
as follows:
+
+
+a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1]
b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]
i
, j
and k
) Where a == b
.
+Input: arr = [2,3,1,6,7]
+Output: 4
+Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
+
+
+
+Input: arr = [1,1,1,1,1]
+Output: 10
+
+
+
+Input: arr = [2,3]
+Output: 0
+
+
+
+Input: arr = [1,3,5,7,9]
+Output: 3
+
+
+
+Input: arr = [7,11,12,9,5,2,7,17,22]
+Output: 8
+
+
+
+
+
+### Related Topics
+ [[Bit Manipulation](../../tag/bit-manipulation/README.md)]
+ [[Array](../../tag/array/README.md)]
+ [[Math](../../tag/math/README.md)]
+
+### Hints
+1 <= arr.length <= 300
1 <= arr[i] <= 10^8
Hint 1
+We are searching for sub-array of length ≥ 2 and we need to split it to 2 non-empty arrays so that the xor of the first array is equal to the xor of the second array. This is equivalent to searching for sub-array with xor = 0.
+Hint 2
+Keep the prefix xor of arr in another array, check the xor of all sub-arrays in O(n^2), if the xor of sub-array of length x is 0 add x-1 to the answer.
+k
and x
, find the k
closest elements to x
in the array. The result should also be sorted in ascending order.
-If there is a tie, the smaller elements are always preferred.
-
-
-Input: [1,2,3,4,5], k=4, x=3
-Output: [1,2,3,4]
-
-
Example 2:
-
-Input: [1,2,3,4,5], k=4, x=-1 -Output: [1,2,3,4] +Given a sorted array
+ +arr
, two integersk
andx
, find thek
closest elements tox
in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.+
Example 1:
+Input: arr = [1,2,3,4,5], k = 4, x = 3 +Output: [1,2,3,4] +Example 2:
+Input: arr = [1,2,3,4,5], k = 4, x = -1 +Output: [1,2,3,4]- - -Note:
--
- - -- The value k is positive and will always be smaller than the length of the sorted array.
-- Length of the given array is positive and will not exceed 104
-- Absolute value of elements in the array and x will not exceed 104
-
- --UPDATE (2017/9/19):
+
-The arr parameter had been changed to an array of integers (instead of a list of integers). Please reload the code definition to get the latest changes. -+
Constraints:
+ +
1 <= k <= arr.length
1 <= arr.length <= 10^4
x
will not exceed 104+
Constraints:
+ +0
and 10^4
.0
to -10^8
, inclusive.-10^8 <= val <= 10^8
val
does not exist in the original BST.There are two sorted arrays nums1 and nums2 of size m and n respectively.
diff --git a/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md b/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md new file mode 100644 index 000000000..0a303646b --- /dev/null +++ b/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../count-triplets-that-can-form-two-arrays-of-equal-xor "Count Triplets That Can Form Two Arrays of Equal XOR") + +[Next >](../number-of-ways-of-cutting-a-pizza "Number of Ways of Cutting a Pizza") + +## [1443. Minimum Time to Collect All Apples in a Tree (Medium)](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree "收集树上所有苹果的最少时间") + +Given an undirected tree consisting of n
vertices numbered from 0 to n-1
, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend in order to collect all apples in the tree starting at vertex 0 and coming back to this vertex.
The edges of the undirected tree are given in the array edges
, where edges[i] = [fromi, toi]
means that exists an edge connecting the vertices fromi
and toi
. Additionally, there is a boolean array hasApple
, where hasApple[i] = true
means that vertex i
has an apple, otherwise, it does not have any apple.
+
Example 1:
+ ++Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false] +Output: 8 +Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows. ++ +
Example 2:
+ ++Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false] +Output: 6 +Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows. ++ +
Example 3:
+ ++Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false] +Output: 0 ++ +
+
Constraints:
+ +1 <= n <= 10^5
edges.length == n-1
edges[i].length == 2
0 <= fromi, toi <= n-1
fromi < toi
hasApple.length == n
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
+Given a positive integer num
, output its complement number. The complement strategy is to flip the bits of its binary representation.
- -
Example 1:
+Example 1:
-Input: 5 -Output: 2 -Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2. +Input: num = 5 +Output: 2 +Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.-
- -
Example 2:
+Example 2:
-Input: 1 -Output: 0 -Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0. +Input: num = 1 +Output: 0 +Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
+
Constraints:
-Note:
- -num
is guaranteed to fit within the range of a 32-bit signed integer.num >= 1
Given a rectangular pizza represented as a rows x cols
matrix containing the following characters: 'A'
(an apple) and '.'
(empty cell) and given the integer k
. You have to cut the pizza into k
pieces using k-1
cuts.
For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.
+ +Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.
+ ++
Example 1:
+ ++Input: pizza = ["A..","AAA","..."], k = 3 +Output: 3 +Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple. ++ +
Example 2:
+ ++Input: pizza = ["A..","AA.","..."], k = 3 +Output: 1 ++ +
Example 3:
+ ++Input: pizza = ["A..","A..","..."], k = 1 +Output: 1 ++ +
+
Constraints:
+ +1 <= rows, cols <= 50
rows == pizza.length
cols == pizza[i].length
1 <= k <= 10
pizza
consists of characters 'A'
and '.'
only.-Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom -note can be constructed from the magazines ; otherwise, it will return false. -
--Each letter in the magazine string can only be used once in your ransom note. -
- -Note:
-You may assume that both strings contain only lowercase letters.
-
-canConstruct("a", "b") -> false -canConstruct("aa", "ab") -> false -canConstruct("aa", "aab") -> true +Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
+ +Each letter in the magazine string can only be used once in your ransom note.
+ ++
Example 1:
+Input: ransomNote = "a", magazine = "b" +Output: false +Example 2:
+Input: ransomNote = "aa", magazine = "ab" +Output: false +Example 3:
+Input: ransomNote = "aa", magazine = "aab" +Output: true++
Constraints:
+ +
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.
-Example 1:
+
Example 1:
+Input:nums = [1,1,1], k = 2 Output: 2- - -
Note:
-
+
Constraints:
+ +-Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself. -
+Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
-Example 1:
+
Example 1:
+Given tree s:
3 / \ @@ -26,17 +24,19 @@ Given tree s: 1 2Given tree t: +
4 / \ 1 2Return true, because t has the same structure and node values with a subtree of s. - -
Example 2:
+
+ +
Example 2:
+Given tree s:
3 / \ @@ -47,13 +47,15 @@ Given tree s: 0Given tree t: +
4 / \ 1 2Return false. - + +
### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/readme/1-300.md b/readme/1-300.md index a9af4d825..afacbba39 100644 --- a/readme/1-300.md +++ b/readme/1-300.md @@ -65,7 +65,7 @@ LeetCode Problems' Solutions | 1 | [Two Sum](https://leetcode.com/problems/two-sum "两数之和") | [Go](../problems/two-sum) | Easy | | 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers "两数相加") | [Go](../problems/add-two-numbers) | Medium | | 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters "无重复字符的最长子串") | [Go](../problems/longest-substring-without-repeating-characters) | Medium | -| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个有序数组的中位数") | [Go](../problems/median-of-two-sorted-arrays) | Hard | +| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个正序数组的中位数") | [Go](../problems/median-of-two-sorted-arrays) | Hard | | 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring "最长回文子串") | [Go](../problems/longest-palindromic-substring) | Medium | | 6 | [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion "Z 字形变换") | [Go](../problems/zigzag-conversion) | Medium | | 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer "整数反转") | [Go](../problems/reverse-integer) | Easy | diff --git a/tag/array/README.md b/tag/array/README.md index 78bfe62c8..17127a0a3 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1438 | [绝对差不超过限制的最长连续子数组](../../problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | [[数组](../array/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | | 1437 | [是否所有 1 都至少相隔 k 个元素](../../problems/check-if-all-1s-are-at-least-length-k-places-away) | [[数组](../array/README.md)] | Medium | | 1431 | [拥有最多糖果的孩子](../../problems/kids-with-the-greatest-number-of-candies) | [[数组](../array/README.md)] | Easy | @@ -254,5 +255,5 @@ | 16 | [最接近的三数之和](../../problems/3sum-closest) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | | 15 | [三数之和](../../problems/3sum) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | | 11 | [盛最多水的容器](../../problems/container-with-most-water) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | | 1 | [两数之和](../../problems/two-sum) | [[数组](../array/README.md)] [[哈希表](../hash-table/README.md)] | Easy | diff --git a/tag/binary-search/README.md b/tag/binary-search/README.md index fd809ab4a..d94a9fe77 100644 --- a/tag/binary-search/README.md +++ b/tag/binary-search/README.md @@ -92,4 +92,4 @@ | 34 | [在排序数组中查找元素的第一个和最后一个位置](../../problems/find-first-and-last-position-of-element-in-sorted-array) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | | 33 | [搜索旋转排序数组](../../problems/search-in-rotated-sorted-array) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | | 29 | [两数相除](../../problems/divide-two-integers) | [[数学](../math/README.md)] [[二分查找](../binary-search/README.md)] | Medium | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | diff --git a/tag/bit-manipulation/README.md b/tag/bit-manipulation/README.md index f3b48ab7c..31bd7b3f2 100644 --- a/tag/bit-manipulation/README.md +++ b/tag/bit-manipulation/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1434 | [每个人戴不同帽子的方案数](../../problems/number-of-ways-to-wear-different-hats-to-each-other) | [[位运算](../bit-manipulation/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1404 | [将二进制表示减到 1 的步骤数](../../problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | [[位运算](../bit-manipulation/README.md)] [[字符串](../string/README.md)] | Medium | | 1356 | [根据数字二进制下 1 的数目排序](../../problems/sort-integers-by-the-number-of-1-bits) | [[排序](../sort/README.md)] [[位运算](../bit-manipulation/README.md)] | Easy | diff --git a/tag/depth-first-search/README.md b/tag/depth-first-search/README.md index c58017106..ff73b6891 100644 --- a/tag/depth-first-search/README.md +++ b/tag/depth-first-search/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1443 | [收集树上所有苹果的最少时间](../../problems/minimum-time-to-collect-all-apples-in-a-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1391 | [检查网格中是否存在有效路径](../../problems/check-if-there-is-a-valid-path-in-a-grid) | [[深度优先搜索](../depth-first-search/README.md)] [[广度优先搜索](../breadth-first-search/README.md)] | Medium | | 1377 | [T 秒后青蛙的位置](../../problems/frog-position-after-t-seconds) | [[深度优先搜索](../depth-first-search/README.md)] | Hard | | 1376 | [通知所有员工所需的时间](../../problems/time-needed-to-inform-all-employees) | [[深度优先搜索](../depth-first-search/README.md)] | Medium | diff --git a/tag/divide-and-conquer/README.md b/tag/divide-and-conquer/README.md index e761314cd..b0960f7f8 100644 --- a/tag/divide-and-conquer/README.md +++ b/tag/divide-and-conquer/README.md @@ -27,4 +27,4 @@ | 169 | [多数元素](../../problems/majority-element) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Easy | | 53 | [最大子序和](../../problems/maximum-subarray) | [[数组](../array/README.md)] [[分治算法](../divide-and-conquer/README.md)] [[动态规划](../dynamic-programming/README.md)] | Easy | | 23 | [合并K个排序链表](../../problems/merge-k-sorted-lists) | [[堆](../heap/README.md)] [[链表](../linked-list/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | diff --git a/tag/dynamic-programming/README.md b/tag/dynamic-programming/README.md index 322c4233d..89963f0ca 100644 --- a/tag/dynamic-programming/README.md +++ b/tag/dynamic-programming/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1444 | [切披萨的方案数](../../problems/number-of-ways-of-cutting-a-pizza) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1434 | [每个人戴不同帽子的方案数](../../problems/number-of-ways-to-wear-different-hats-to-each-other) | [[位运算](../bit-manipulation/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1425 | [带限制的子序列和](../../problems/constrained-subsequence-sum) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1423 | [可获得的最大点数](../../problems/maximum-points-you-can-obtain-from-cards) | [[数组](../array/README.md)] [[动态规划](../dynamic-programming/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | diff --git a/tag/math/README.md b/tag/math/README.md index 9d25bf985..8e62d08dc 100644 --- a/tag/math/README.md +++ b/tag/math/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1427 | [Perform String Shifts](../../problems/perform-string-shifts) 🔒 | [[数组](../array/README.md)] [[数学](../math/README.md)] | Easy | | 1390 | [四因数](../../problems/four-divisors) | [[数学](../math/README.md)] | Medium | | 1363 | [形成三的最大倍数](../../problems/largest-multiple-of-three) | [[数学](../math/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | diff --git a/tag/stack/README.md b/tag/stack/README.md index d274a91de..bbfe948a8 100644 --- a/tag/stack/README.md +++ b/tag/stack/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1441 | [用栈操作构建数组](../../problems/build-an-array-with-stack-operations) | [[栈](../stack/README.md)] | Easy | | 1410 | [HTML 实体解析器](../../problems/html-entity-parser) | [[栈](../stack/README.md)] [[字符串](../string/README.md)] | Medium | | 1381 | [设计一个支持增量操作的栈](../../problems/design-a-stack-with-increment-operation) | [[栈](../stack/README.md)] [[设计](../design/README.md)] | Medium | | 1249 | [移除无效的括号](../../problems/minimum-remove-to-make-valid-parentheses) | [[栈](../stack/README.md)] [[字符串](../string/README.md)] | Medium | diff --git a/tag/tree/README.md b/tag/tree/README.md index 537be7ad1..ec6576c36 100644 --- a/tag/tree/README.md +++ b/tag/tree/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1443 | [收集树上所有苹果的最少时间](../../problems/minimum-time-to-collect-all-apples-in-a-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1430 | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](../../problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) 🔒 | [[树](../tree/README.md)] | Medium | | 1379 | [找出克隆二叉树中的相同节点](../../problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree) | [[树](../tree/README.md)] | Medium | | 1372 | [二叉树中的最长交错路径](../../problems/longest-zigzag-path-in-a-binary-tree) | [[树](../tree/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | From d45a97b0a5ad75056510d53c1c28862489d3b7ab Mon Sep 17 00:00:00 2001 From: Shuo
Given an integer array nums
and an integer k
, return the maximum sum of a non-empty subset of that array such that for every two consecutive integers in the subset, nums[i]
and nums[j]
, where i < j
, the condition j - i <= k
is satisfied.
A subset of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.
- --
Example 1:
- --Input: nums = [10,2,-10,5,20], k = 2 -Output: 37 -Explanation: The subset is [10, 2, 5, 20]. -- -
Example 2:
- --Input: nums = [-1,-2,-3], k = 1 -Output: -1 -Explanation: The subset must be non-empty, so we choose the largest number. -- -
Example 3:
- --Input: nums = [10,-2,-10,-5,20], k = 2 -Output: 23 -Explanation: The subset is [10, -2, -5, 20]. -- -
-
Constraints:
- -1 <= k <= nums.length <= 10^5
-10^4 <= nums[i] <= 10^4