当前位置:网站首页>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).
边栏推荐
- Mysql 单表字段重复数据取最新一条sql语句
- QT串口ui设计和解决显示中文乱码
- [sword finger offer] 58 - I. flip the word order
- Thread pool executes scheduled tasks
- 执行脚本不认\r
- Golang 装饰器模式以及在NSQ中的使用
- 线程池执行定时任务
- [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
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- 斑馬識別成狗,AI犯錯的原因被斯坦福找到了
猜你喜欢
Mysql database DDL and DML
Cocos Creator 2.x 自动打包(构建 + 编译)
(补)双指针专题
The word backspace key cannot delete the selected text, so you can only press Delete
IDEA-配置插件
Visual SLAM algorithms: a survey from 2010 to 2016
Mysql 将逗号隔开的属性字段数据由列转行
智慧之道(知行合一)
Cocos Creator 2. X automatic packaging (build + compile)
Web crawler knowledge day03
随机推荐
CC2530 common registers for port interrupts
网络安全web渗透技术
爱可可AI前沿推介(7.3)
PHP converts a one-dimensional array into a two-dimensional array
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
【剑指 Offer 】64. 求1+2+…+n
【剑指 Offer】58 - II. 左旋转字符串
Thread pool executes scheduled tasks
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
Mongodb installation and basic operation
There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
Eleven requirements for test management post
Explore Netease's large-scale automated testing solutions see here see here
What is the maximum number of concurrent TCP connections for a server? 65535?
消息队列消息丢失和消息重复发送的处理策略
【剑指 Offer 】57 - II. 和为s的连续正数序列
什么是质押池,如何进行质押呢?
Extraction of the same pointcut
[Jianzhi offer] 58 - ii Rotate string left
Interpretation of several important concepts of satellite antenna