当前位置:网站首页>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).
边栏推荐
- 中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
- What material is sa537cl2? Analysis of mechanical properties of American standard container plate
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- Unreal_ Datatable implements ID self increment and sets rowname
- Golang anonymous function use
- Acwing game 58
- MongoDB 的安装和基本操作
- Extraction of the same pointcut
- Interpretation of several important concepts of satellite antenna
- [sword finger offer] 58 - I. flip the word order
猜你喜欢
【LeetCode】94. Middle order traversal of binary tree
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
NSQ source code installation and operation process
Détails du contrôle de la congestion TCP | 3. Espace de conception
什么是质押池,如何进行质押呢?
Netease UI automation test exploration: airtest+poco
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
MySQL Basics
What is the maximum number of concurrent TCP connections for a server? 65535?
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
随机推荐
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri
Is it safe to open a stock account by mobile registration? Does it need money to open an account
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
一台服务器最大并发 tcp 连接数多少?65535?
Svn usage specification
(补)双指针专题
关于学习Qt编程的好书精品推荐
Interpretation of several important concepts of satellite antenna
[Jianzhi offer] 64 Find 1+2+... +n
14 topics for performance interviews between superiors and subordinates (4)
【LeetCode】94. Middle order traversal of binary tree
PHP CI(CodeIgniter)log级别设置
Overview of satellite navigation system
SVN使用规范
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
Web crawler knowledge day03
arduino-esp32:LVGL项目(一)整体框架
Client does not support authentication protocol requested by server; consider upgrading MySQL client