当前位置:网站首页>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).
边栏推荐
- Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
- CC2530 common registers for serial communication
- 关于学习Qt编程的好书精品推荐
- Golang anonymous function use
- Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
- ThreeJS 第二篇:顶点概念、几何体结构
- JSON 与 BSON 区别
- 特征多项式与常系数齐次线性递推
- Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
- QT serial port UI design and solution to display Chinese garbled code
猜你喜欢

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)

Explore Cassandra's decentralized distributed architecture

关于学习Qt编程的好书精品推荐

What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material

2022爱分析· 国央企数字化厂商全景报告

斑馬識別成狗,AI犯錯的原因被斯坦福找到了
![[statement] about searching sogk1997 and finding many web crawler results](/img/1a/8ed3ca0030ea227adcd95e8b306aca.png)
[statement] about searching sogk1997 and finding many web crawler results

What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR

Cocos Creator 2.x 自动打包(构建 + 编译)

一台服务器最大并发 tcp 连接数多少?65535?
随机推荐
[Jianzhi offer] 64 Find 1+2+... +n
CC2530 common registers for port initialization
Interpretation of several important concepts of satellite antenna
CC2530 common registers
Register in PHP_ Globals parameter settings
Record windows10 installation tensorflow-gpu2.4.0
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
What is the pledge pool and how to pledge?
A survey of state of the art on visual slam
Visual SLAM algorithms: a survey from 2010 to 2016
Explore Netease's large-scale automated testing solutions see here see here
Leetcode binary search tree
PHP converts a one-dimensional array into a two-dimensional array
Golang 匿名函数使用
CC2530 common registers for ADC single channel conversion
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
TCP congestion control details | 3 design space
Mysql 单表字段重复数据取最新一条sql语句
How to set up SVN server on this machine