华为OD机试 - 寻找连续区间 - 滑动窗口(Java 2024 E卷 100分)

devtools/2025/5/22 1:14:31/

题目描述

给定一个含有 N N N 个正整数的数组,求出有多少个连续子数组(包括单个正整数),其元素之和满足 ∑ ≥ x \sum \geq x x

输入描述
第一行为两个整数 N N N, x x x。其中 0 < N ≤ 1 0 5 0 < N \leq 10^5 0<N105, 0 ≤ x ≤ 1 0 7 0 \leq x \leq 10^7 0x107
第二行包含 N N N 个正整数(每个数 ≤ 100 \leq 100 100

输出描述
输出满足条件的连续子数组个数

输入输出示例

示例 1:
输入:

3 7
3 4 7

输出:

4

解释:
满足条件的子数组为:

  • [ 3 , 4 ] [3,4] [3,4] (和为 7 7 7)
  • [ 3 , 4 , 7 ] [3,4,7] [3,4,7] (和为 14 14 14)
  • [ 4 , 7 ] [4,7] [4,7] (和为 11 11 11)
  • [ 7 ] [7] [7] (和为 7 7 7)

解题思路

  1. 问题分析

    • 需要统计所有满足 ∑ i = l r a i ≥ x \sum_{i=l}^r a_i \geq x i=lraix 的区间 [ l , r ] [l,r] [l,r] 的个数
    • 暴力解法时间复杂度为 O ( N 2 ) O(N^2) O(N2),无法通过大规模数据
  2. 优化思路

    • 利用滑动窗口(双指针)技术
    • 维护窗口和 s u m sum sum,当 s u m ≥ x sum \geq x sumx 时统计有效区间数
  3. 算法流程

    1. 初始化指针 l e f t = 0 left=0 left=0,窗口和 s u m = 0 sum=0 sum=0,计数器 c o u n t = 0 count=0 count=0
    2. 遍历数组,右指针 r i g h t right right 0 0 0 N − 1 N-1 N1
      • a [ r i g h t ] a[right] a[right] 加入 s u m sum sum
      • s u m ≥ x sum \geq x sumx 时:
        • 所有以 r i g h t right right 结尾的子数组 [ l , r i g h t ] [l,right] [l,right] l ≤ l e f t l \leq left lleft)都满足条件
        • 增加计数 c o u n t ← c o u n t + ( N − r i g h t ) count \leftarrow count + (N - right) countcount+(Nright)
        • 移动左指针缩小窗口: s u m ← s u m − a [ l e f t ] , l e f t ← l e f t + 1 sum \leftarrow sum - a[left], left \leftarrow left+1 sumsuma[left],leftleft+1

代码实现

Java
import java.util.Scanner;// 博主亲测版
public class 寻找连续区间 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int len = scanner.nextInt();int target = scanner.nextInt();int[] arr = new int[len];for (int i = 0; i < len; i++) {arr[i] = scanner.nextInt();}int sum = 0;int tempSum;int count = 0;for (int right = 0; right < len; right++) {sum += arr[right];tempSum = sum;for (int left = 0; left <= right; left++) {if (tempSum >= target) {tempSum -= arr[left];count++;}}}System.out.println(count);}
}
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int x = sc.nextInt();int[] a = new int[N];for (int i = 0; i < N; i++) a[i] = sc.nextInt();int count = 0, left = 0, sum = 0;for (int right = 0; right < N; right++) {sum += a[right];while (sum >= x) {count += N - right;sum -= a[left++];}}System.out.println(count);}
}
Python
n, x = map(int, input().split())
a = list(map(int, input().split()))count = left = current_sum = 0
for right in range(n):current_sum += a[right]while current_sum >= x:count += n - rightcurrent_sum -= a[left]left += 1print(count)
C++
#include <iostream>
#include <vector>
using namespace std;int main() {int N, x;cin >> N >> x;vector<int> a(N);for (int i = 0; i < N; i++) cin >> a[i];int count = 0, left = 0, sum = 0;for (int right = 0; right < N; right++) {sum += a[right];while (sum >= x) {count += N - right;sum -= a[left++];}}cout << count << endl;return 0;
}
JavaScript
const readline = require('readline');
const rl = readline.createInterface({input: process.stdin,output: process.stdout
});let input = [];
rl.on('line', line => {input.push(line);
}).on('close', () => {const [N, x] = input[0].split(' ').map(Number);const a = input[1].split(' ').map(Number);let count = 0, left = 0, sum = 0;for (let right = 0; right < N; right++) {sum += a[right];while (sum >= x) {count += N - right;sum -= a[left++];}}console.log(count);
});

复杂度分析

  • 时间复杂度 O ( N ) O(N) O(N)
    每个元素最多被访问两次(右指针和左指针各一次)
  • 空间复杂度 O ( 1 ) O(1) O(1)
    仅使用常数个额外变量

测试用例

测试用例 1
输入:

5 10
1 2 3 4 5

输出:

9

解释:
满足条件的子数组:
[ 1 , 2 , 3 , 4 ] [1,2,3,4] [1,2,3,4], [ 1 , 2 , 3 , 4 , 5 ] [1,2,3,4,5] [1,2,3,4,5],
[ 2 , 3 , 4 ] [2,3,4] [2,3,4], [ 2 , 3 , 4 , 5 ] [2,3,4,5] [2,3,4,5],
[ 3 , 4 , 5 ] [3,4,5] [3,4,5], [ 4 , 5 ] [4,5] [4,5], [ 5 ] [5] [5]

测试用例 2
输入:

1 100
50

输出:

0

测试用例 3
输入:

4 5
5 1 1 5

输出:

6

问题总结

  1. 关键点

    • 利用滑动窗口维护当前子数组和
    • 当和满足条件时,快速计算以当前右指针结尾的有效子数组数
  2. 算法选择

    • 滑动窗口适用于正数数组的区间和问题
    • 保证线性时间复杂度,适合大规模数据
  3. 扩展思考

    • 如果包含负数,需要改用前缀和+二分查找
    • 类似问题:求满足 ∑ ≤ x \sum \leq x x 的子数组数

http://www.ppmy.cn/devtools/173298.html

相关文章

GreenPlum学习

简介 Greenplum是一个面向数据仓库应用的关系型数据库&#xff0c;因为有良好的体系结构&#xff0c;所以在数据存储、高并发、高可用、线性扩展、反应速度、易用性和性价比等方面有非常明显的优势。Greenplum是一种基于PostgreSQL的分布式数据库&#xff0c;其采用sharednothi…

[Python] 贪心算法简单版

贪心算法-简单版 贪心算法的一般使用场景是给定一个列表ls, 让你在使用最少的数据的情况下达到或超过n. 我们就来使用上面讲到的这个朴素的例题来讲讲贪心算法的基本模板: 2-1.排序 既然要用最少的数据, 我们就要优先用大的数据拼, 为了实现这个效果, 我们得先给列表从大到小…

REVISITING MAE PRE-TRAINING FOR 3D MEDICALIMAGE SEGMENTATION

Abstract 自我监督学习&#xff08;SSL&#xff09;提供了一个令人兴奋的机会&#xff0c;可以释放大量尚未开发的临床数据集的潜力&#xff0c;为各种下游应用程序提供标签数据的稀缺。虽然SSL已经彻底改变了自然语言处理和计算机视觉等领域&#xff0c;但它们在3D医学图像计…

java学习笔记11——泛型

泛型 在集合中使用泛型 总结&#xff1a; 01-泛型在集合、比较器中的使用 1.什么是泛型? 所谓泛型&#xff0c;就是允许在定义类、接口时通过一个、标识表示类中某个属性的类型或者是某个方法的 返回值或参数的类型。这个类型参数将在使用时(例如&#xff0c;继承或实现这个接…

git push的时候出现无法访问的解决

fatal: 无法访问 https://github.com/...&#xff1a;gnutls_handshake() failed: Error in the pull function. push的时候没有输入自己的github账号密码&#xff0c;为了解决每次push都要登录github这个问题&#xff0c;采用ssh密钥的方式认证&#xff0c;可以免去每次都输入…

Java 与 Kotlin 对比学习指南(二)

以下是一份 超详细的 Java 与 Kotlin 对比学习指南&#xff0c;涵盖语法、设计理念和实际场景的深度对比&#xff0c;帮助您从 Java 平滑过渡到 Kotlin。 一、基础语法对比 1. 程序入口 Java public class Main {public static void main(String[] args) {System.out.printl…

责任链模式_行为型_GOF23

责任链模式 责任链模式&#xff08;Chain of Responsibility Pattern&#xff09;是一种行为型设计模式&#xff0c;核心思想是将多个处理请求的对象连成一条链&#xff0c;请求沿链传递直到被处理。它像现实中的“多级审批流程”——请假或报销时&#xff0c;申请会逐级提交给…

Perl 哈希

Perl 哈希 引言 Perl 是一种强大的脚本语言&#xff0c;广泛应用于文本处理、系统管理、网络编程等领域。在 Perl 中&#xff0c;哈希&#xff08;Hash&#xff09;是一种非常灵活且强大的数据结构&#xff0c;它允许用户以键值对的形式存储数据。本文将详细介绍 Perl 哈希的…