每日一练

这是一道牛客的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);
        }
    }
相关推荐
是小蟹呀^5 小时前
Spring Security + JWT 面试题整理
java·jwt·springsecurity
星栈独行6 小时前
翻完 Pi 源码:它和 Codex、Claude Code 有何不同
开发语言·javascript·人工智能·程序人生
spencer_tseng7 小时前
Redis + Nacos.bat
java·windows·dos
qq_448011167 小时前
C语言中的变量和函数的定义与声明
android·c语言·开发语言
troyzhxu7 小时前
列表查询的 GraphQL —— 一行代码终结你的 if-else 地狱!
java·springboot·graphql
孫治AllenSun8 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm
c238569 小时前
把 C++ 内存分配拆透:new 与 malloc 的三层血缘
开发语言·c++·算法
好好沉淀9 小时前
@NotBlank(message = “{xxx}“) 注解中花括号的含义
java
fīɡЙtīиɡ ℡9 小时前
内存泄漏产生的原因
java·spring·servlet