新乡网站建设哪家实力强,永久免费的网站哪个好,怎么去掉wordpress加载动画,网站界面设计技巧打卡记录 将 x 减到 0 的最小操作数#xff08;逆向思维 滑动窗口#xff09;
链接
将 x 减到 0 的最小操作数#xff0c;可以逆向思考#xff0c;求一个数组中的最大长度的滑动窗口#xff0c;来使得这个窗口里的数等于 全数组之和 - x 的值。
class Solution {
publ…打卡记录 将 x 减到 0 的最小操作数逆向思维 滑动窗口
链接
将 x 减到 0 的最小操作数可以逆向思考求一个数组中的最大长度的滑动窗口来使得这个窗口里的数等于 全数组之和 - x 的值。
class Solution {
public:int minOperations(vectorint nums, int x) {int target accumulate(nums.begin(), nums.end(), 0) - x;if (target 0) return -1;int ans -1, sum 0, n nums.size();for (int i 0, j 0; i n; i) {sum nums[i];while (sum target) sum - nums[j];if (sum target) ans max(ans, i - j 1);}return ans 0 ? -1 : n - ans;}
};