每日一练

这是一道牛客的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);
        }
    }
相关推荐
南 阳4 分钟前
Python从入门到精通day64
开发语言·python
yaaakaaang13 分钟前
十二、代理模式
java·代理模式
花千树-01023 分钟前
Java 接入多家大模型 API 实战对比
java·开发语言·人工智能·ai·langchain·ai编程
卓怡学长25 分钟前
m326数据结构课程网络学习平台的设计与实现+vue
java·spring·tomcat·maven·intellij-idea·mybatis
han_hanker1 小时前
@Validated @Valid 用法
java·spring boot
小CC吃豆子1 小时前
详细介绍一下静态分析工具 SonarQube
java
DevOpenClub1 小时前
全国三甲医院主体信息 API 接口
java·大数据·数据库
上海合宙LuatOS1 小时前
LuatOS扩展库API——【exremotecam】网络摄像头控制
开发语言·网络·物联网·lua·luatos
言慢行善1 小时前
SpringBoot中的注解介绍
java·spring boot·后端
一勺菠萝丶1 小时前
管理后台使用手册在线预览与首次登录引导弹窗实现
java·前端·数据库