当前位置:网站首页>LeetCode 1658. Minimum operand to reduce x to 0
LeetCode 1658. Minimum operand to reduce x to 0
2022-07-03 16:44:00 【Daylight629】
1658. take x Reduced to 0 Minimum operands of
Give you an array of integers nums
And an integer x
. At each operation , You should remove the array nums
Leftmost or rightmost element , And then from x
Subtract the value of the element from the . Please note that , need modify Array for subsequent operations .
If you can x
just Reduced to 0
, return Minimum operands ; otherwise , return -1
.
Example 1:
Input :nums = [1,1,4,2,3], x = 5
Output :2
explain : The best solution is to remove the last two elements , take x Reduced to 0 .
Example 2:
Input :nums = [5,6,7,8,9], x = 4
Output :-1
Example 3:
Input :nums = [3,2,20,1,1,3], x = 10
Output :5
explain : The best solution is to remove the last three elements and the first two elements ( in total 5 operations ), take x Reduced to 0 .
Tips :
1 <= nums.length <= 105
1 <= nums[i] <= 104
1 <= x <= 109
Two 、 Method 1
Hash + The prefix and
class Solution {
public int minOperations(int[] nums, int x) {
int len = nums.length;
Map<Integer, Integer> map = new HashMap<>();
int[] sum = new int[len + 1];
map.put(0, 0);
int temp = 0;
for (int i = 1; i <= len; i++) {
map.put(temp = sum[i - 1] + nums[i - 1], i);
sum[i] = temp;
}
int target = sum[len] - x;
if (target < 0) {
return -1;
}
int res = Integer.MAX_VALUE;
for (int i = len; i > 0 && sum[i] >= target; i--) {
if (map.containsKey(temp = sum[i] - target)) {
res = Math.min(res, len - i + map.get(temp));
}
}
return res == Integer.MAX_VALUE ? -1 : res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(n).
边栏推荐
- IDEA-配置插件
- 执行脚本不认\r
- 深入理解 SQL 中的 Grouping Sets 语句
- 2022爱分析· 国央企数字化厂商全景报告
- MySQL Basics
- Google Earth engine (GEE) - daymet v4: daily surface weather data set (1000m resolution) including data acquisition methods for each day
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- [combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
- Caching mechanism of Hibernate / session level caching mechanism
- Arduino esp32: overall framework of lvgl project (I)
猜你喜欢
Cocos Creator 2. X automatic packaging (build + compile)
Mysql 单表字段重复数据取最新一条sql语句
Deep understanding of grouping sets statements in SQL
[combinatorics] polynomial theorem (polynomial theorem | polynomial theorem proof | polynomial theorem inference 1 item number is the number of non negative integer solutions | polynomial theorem infe
(Supplement) double pointer topic
Cocos Creator 2.x 自动打包(构建 + 编译)
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
网络安全web渗透技术
(补)双指针专题
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
随机推荐
IL Runtime
Simulink oscilloscope data is imported into Matlab and drawn
Détails du contrôle de la congestion TCP | 3. Espace de conception
[mathematical logic] equivalent calculus and reasoning calculus of propositional logic (propositional logic | equivalent calculus | principal conjunctive (disjunctive) paradigm | reasoning calculus)**
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
特征多项式与常系数齐次线性递推
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
程序猿如何快速成长
8 tips for effective performance evaluation
Client does not support authentication protocol requested by server; consider upgrading MySQL client
爱可可AI前沿推介(7.3)
Record windows10 installation tensorflow-gpu2.4.0
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
How programming apes grow rapidly
CC2530 common registers
Idea configuration plug-in
智慧之道(知行合一)
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
NSQ source code installation and operation process