当前位置:网站首页>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 <= 1051 <= nums[i] <= 1041 <= 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).
边栏推荐
- Mongodb installation and basic operation
- CC2530 common registers for watchdog
- Register in PHP_ Globals parameter settings
- Eleven requirements for test management post
- Golang 匿名函数使用
- Svn usage specification
- 跟我学企业级flutter项目:简化框架demo参考
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- AcWing 第58 场周赛
- Détails du contrôle de la congestion TCP | 3. Espace de conception
猜你喜欢

CC2530 common registers for port interrupts

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

What material is sa537cl2? Analysis of mechanical properties of American standard container plate

线程池执行定时任务

Processing strategy of message queue message loss and repeated message sending

Netease UI automation test exploration: airtest+poco

CC2530 common registers for serial communication

CC2530 common registers for timer 1

(Supplement) double pointer topic

什么是质押池,如何进行质押呢?
随机推荐
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Mysql database DDL and DML
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
Processing strategy of message queue message loss and repeated message sending
Construction practice camp - graduation summary of phase 6
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
CC2530 common registers for watchdog
SVN使用规范
Characteristic polynomial and constant coefficient homogeneous linear recurrence
mysql用户管理
Interviewer: how does the JVM allocate and recycle off heap memory
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
ThreeJS 第二篇:顶点概念、几何体结构
PHP CI (CodeIgniter) log level setting