每日一练

这是一道牛客的dd爱框框的题

题目解析: 就是求大于x的最短子序列

我的思路:是滑动窗口

java 复制代码
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int x = in.nextInt();
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = in.nextInt();
            }
            //双指针实现
            int dest = 0,cur = 0,min = Integer.MAX_VALUE,temp = 0,resultIndex1 = 0,resultIndex2 = 0;
            while (cur < arr.length) {
                temp += arr[cur++];//进窗口
                while (temp >= x){
                    if (min > cur - dest + 1) {
                        min = cur - dest + 1;
                        resultIndex1 = dest;
                        resultIndex2 = cur;
                    }
                    temp -= arr[dest++];//出窗口,并循环判断
                }

            }
            System.out.println(resultIndex1 + 1 + " " + resultIndex2);
        }
    }
相关推荐
沐知全栈开发3 分钟前
TypeScript Array(数组)
开发语言
苏小瀚4 分钟前
[算法]---分治-快排和归并
java·算法·leetcode
陶陶name6 分钟前
Metal Compute Pipeline:Metal-C++ 环境配置与简单算子实现
开发语言·c++
认真敲代码的小火龙7 分钟前
【JAVA项目】基于JAVA的宿舍管理系统
java·开发语言·课程设计
无限进步_7 分钟前
寻找数组中缺失数字:多种算法详解与比较
c语言·开发语言·数据结构·算法·排序算法·visual studio
lsx2024069 分钟前
C 标准库 - <assert.h>
开发语言
invicinble9 分钟前
关于maven的全域理解
java·spring boot·maven
Wzx19801210 分钟前
go接受输入方式
开发语言·后端·golang
黑马源码库miui5208611 分钟前
JAVA成人用品商城系统源码微信小程序+h5+安卓+ios
android·java·微信小程序
CC.GG14 分钟前
【Qt】Qt背景与环境搭建
开发语言·qt