每日一练

这是一道牛客的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);
        }
    }
相关推荐
L-影9 分钟前
HashMap详解-上篇
java·开发语言·spring·hashmap
爱笑的源码基地14 分钟前
什么是智能预问诊系统?一套java版预问诊成品源码
java·源码·程序·预问诊·智能问诊·预问诊源码
郝学胜-神的一滴27 分钟前
Effective STL 条款 17:用 swap 技巧去掉多余容量
c语言·开发语言·数据结构·c++·算法·rpc
她说..43 分钟前
幂等问题分析
java·jvm·spring boot·spring·spring cloud·intellij-idea
摇滚侠1 小时前
《On Java 中文版 基础卷》阅读笔记 集合 24
java·开发语言·笔记
小小测试开发1 小时前
rsync 源路径少一个斜杠,--delete 删的就不是你以为的那一层
java·前端·数据库·人工智能
智购科技无人售货机工厂店2 小时前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
随流寻影2 小时前
利用 C++ 实现快速排序及三种优化
开发语言·c++
默 语2 小时前
【HarmonyOS7】ArkTS通用属性实战(六):组件级像素取整 pixelRound
开发语言
Nazarick2 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣