每日一练

这是一道牛客的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协程的演进:从yield到async/await的完整历史
java·前端·python·async·yield·await·codecapsule
q***925112 分钟前
PHP操作redis
开发语言·redis·php
帅中的小灰灰19 分钟前
C++编程建造器设计模式
java·c++·设计模式
大佬,救命!!!26 分钟前
python实现五子棋
开发语言·python·个人开发·pygame·少儿编程·五子棋
动感小麦兜1 小时前
应用-常用工具部署命令
java·开发语言
日日行不惧千万里1 小时前
IDEA 是用什么开发的?
java·ide·intellij-idea
立志成为大牛的小牛2 小时前
数据结构——五十一、散列表的基本概念(王道408)
开发语言·数据结构·学习·程序人生·算法·散列表
百***06012 小时前
五大消息模型介绍(RabbitMQ 详细注释版)
java·rabbitmq·java-rabbitmq
转转技术团队3 小时前
MyBatis-Plus踩坑血泪史:那些年我们踩过的坑!
java·面试·mybatis
sg_knight3 小时前
IntelliJ IDEA 实用插件:GitToolBox 使用指南
java·ide·git·intellij-idea·插件·gittoolbox