当前位置:网站首页>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).
边栏推荐
- JSON 与 BSON 区别
- Netease UI automation test exploration: airtest+poco
- CC2530 common registers for timer 1
- Acwing game 58
- CC2530 common registers for crystal oscillator settings
- [combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
- 跟我学企业级flutter项目:简化框架demo参考
- 【LeetCode】94. Middle order traversal of binary tree
- 【剑指 Offer】58 - I. 翻转单词顺序
- Golang 匿名函数使用
猜你喜欢

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

Thread pool executes scheduled tasks

一台服务器最大并发 tcp 连接数多少?65535?

Deep understanding of grouping sets statements in SQL

Record windows10 installation tensorflow-gpu2.4.0

A survey of state of the art on visual slam

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

Cocos Creator 2. X automatic packaging (build + compile)

PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug

8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
随机推荐
One article takes you to understand machine learning
word 退格键删除不了选中文本,只能按delete
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Visual SLAM algorithms: a survey from 2010 to 2016
线程池执行定时任务
MySQL single table field duplicate data takes the latest SQL statement
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
[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
Extraction of the same pointcut
Unity project optimization case 1
What is the material of sa302grc? American standard container plate sa302grc chemical composition
PHP CI(CodeIgniter)log级别设置
8个酷炫可视化图表,快速写出老板爱看的可视化分析报告
JSON 与 BSON 区别
爱可可AI前沿推介(7.3)
Unreal_ Datatable implements ID self increment and sets rowname
Interviewer: how does the JVM allocate and recycle off heap memory
AcWing 第58 场周赛
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford