每日一练

这是一道牛客的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);
        }
    }
相关推荐
tg-zm8899962 小时前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
X***C8622 小时前
SpringBoot:几种常用的接口日期格式化方法
java·spring boot·后端
Mr_Xuhhh2 小时前
YAML相关
开发语言·python
咖啡の猫2 小时前
Python中的变量与数据类型
开发语言·python
前端达人2 小时前
你的App消息推送为什么石沉大海?看Service Worker源码我终于懂了
java·开发语言
汤姆yu2 小时前
基于springboot的电子政务服务管理系统
开发语言·python
小光学长3 小时前
基于ssm的宠物交易系统的设计与实现850mb48h(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·前端·数据库
编程大师哥3 小时前
vxe-table 透视表分组汇总及排序基础配置
java
全栈师3 小时前
C#中控制权限的逻辑写法
开发语言·c#
8***84823 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring