class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
solution.countSubarrays(new int[]{
2,1,4,3,5
},10);
}
public long countSubarrays(int[] nums, long k) {
int n = nums.length;
long[] sum = new long[n+2];
for (int i = 0; i < n; i++) {
if (i == 0) {
sum[1]= nums[0];
}else{
sum[i+1]=sum[i]+nums[i];
}
}
sum[n+1] = sum[n];
//
long ans = 0;
for (int i = 1; i <=n; i++) {
int low = i - 1;
int high = n + 1; while( low < high){
int mid = (low + high)/2;
if(check(nums,i,mid,k,sum)){
high = mid;
}else{
low = mid + 1;
}
}
if( high >=i){
ans += (high -i);
} }
return ans;
} public boolean check(int[] nums,int i,int j,long k,long[] sum){
long x = (sum[j] - sum[i]+nums[i-1])*(j-i+1);
return x>=k;
}
}

【2302. 统计得分小于 K 的子数组数目】前缀和+二分的更多相关文章

  1. Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)

    713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...

  2. [Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  3. Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)

    560. 和为K的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] ...

  4. 累加和为 K 的子数组问题

    累加和为 K 的子数组问题 作者:Grey 原文地址: 博客园:累加和为 K 的子数组问题 CSDN:累加和为 K 的子数组问题 题目说明 数组全为正数,且每个数各不相同,求累加和为K的子数组组合有哪 ...

  5. LeetCode 560. Subarray Sum Equals K (子数组之和等于K)

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  6. [Swift]LeetCode560. 和为K的子数组 | Subarray Sum Equals K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

  7. 66.Subarray Sum Equals K(子数组和为K的个数)

    Level:   Medium 题目描述: Given an array of integers and an integer k, you need to find the total number ...

  8. [LeetCode]560. 和为K的子数组(前缀和)

    题目 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为 ...

  9. 560. Subarray Sum Equals K 求和为k的子数组个数

    [抄题]: Given an array of integers and an integer k, you need to find the total number of continuous s ...

  10. 【Leetcode】560. 和为K的子数组&974. 和可被 K 整除的子数组(前缀和+哈希表)

    public class Solution { public int subarraySum(int[] nums, int k) { int count = 0, pre = 0; HashMap ...

随机推荐

  1. Linux离线安装MySQL(5.7.22)

    1.下载tar包 (1)Window PC下载(PC需要联网) MySQL官网地址:https://www.mysql.com/ MySQL社区版下载地址: https://dev.mysql.com ...

  2. 使用JS保存数据

    1 保存到text中 demo1.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  3. colrm命令

    colrm命令 colrm命令用于编辑源代码文件,脚本文件或常规文本文件中的文本,此命令从文件中删除选定的列,列定义为一行中的单个字符.索引总是从1开始,而不是0.如果同时指定了开始和结束,则它们之间 ...

  4. Redis能保证数据不丢失吗?

    大家即使没用过Redis,也应该都听说过Redis的威名. Redis是一种Nosql类型的数据存储,全称Remote Dictionary Server,也就是远程字典服务器,用过Dictionar ...

  5. pigz命令

    多线程的解压缩文件 语法格式:pigz 参数 文件名 常用参数 -- 显示压缩后的内容 -p 设置线程数 -b 设置文件数据块大小 -q 静默执行模式 -d 将压缩文件恢复为原始文件 -r 递归处理所 ...

  6. go词法作用域陷进

    问题 // 创建一些目录,再将目录删除 // 错误写法 var rmdirs []func() for _, dir := range tempDirs() { os.MkdirAll(dir, 07 ...

  7. 【Azure 应用服务】App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(-Xms512m -Xmx1204m)?

    问题描述 App Service 使用Tomcat运行Java应用,如何设置前端网页缓存的相应参数呢(-Xms512m -Xmx1204m)? 问题回答 App Service在Windows的环境中 ...

  8. 使用rpa打开浏览器并执行js抓取页面元素详情步骤

    这里我们专门开一个文章来写如何在rpa中执行js获取页面元素. 个人觉得,复杂点的需求用js会方便很多,所以后续的文章我都会重点使用js去获取页面元素. 好,正文开始,我们先看一下rpa为我们提供的自 ...

  9. Java 手动抛异常

    1 package com.bytezero.throwable; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 impor ...

  10. python爬虫 xpath入门与lxml库基本使用,我们一同学习xpath

    目录 什么是XPath? xpath语法 知识点 节点 选取节点: 选取a节点下所有的href属性 ../ 选取父节点 bookstore/book 选取子元素li bookstore//book 不 ...