每日一练

这是一道牛客的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);
        }
    }
相关推荐
淘源码d10 分钟前
医学影像云PACS系统——PACS诊断工作站
java·源码·影像处理·医院管理系统·影像工作站·云pacs系统·dicom 标准通信
言乐636 分钟前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
NiceCloud喜云1 小时前
Anthropic 一周三连发:Cowork 多端、Fable 5 按需付费、J-space 论文的技术解读
java·服务器·网络·人工智能·ai
cui_ruicheng1 小时前
Python从入门到实战(六):非序列容器
开发语言·python
SamDeepThinking1 小时前
组合还是继承?我在项目里主要看这两个判断
java·后端·面试
西西学代码1 小时前
Flutter---底部导航栏(2)
开发语言·javascript·flutter
legendary_1631 小时前
SINK芯片:Type-C统一供电时代的小家电核心方案
c语言·开发语言·人工智能·智能手机
momo1 小时前
JAVA基础知识
java·开发语言
凯瑟琳.奥古斯特2 小时前
力扣1013三等分解法与C++实现
开发语言·c++·算法·leetcode·职场和发展
就叫_这个吧2 小时前
Activiti7工作流入门+案例应用
java·activiti7·工作流