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. Java - CodeForces - 266A

    题目: 桌子上有n块石头排成一行,每一块都可以是红色.绿色或蓝色.计算从表中取出的石头的最小数量,以便相邻的任何两块石头具有不同的颜色.如果一排石头之间没有其他石头,则认为它们相邻. 输入: 第一行包 ...

  2. Kafka的ACK机制

    Kafka的ack机制,指的是producer的消息发送确认机制,这直接影响到Kafka集群的吞吐量和消息可靠性.而吞吐量和可靠性就像硬币的两面,两者不可兼得,只能平衡. ACK有3个可选值,分别是1 ...

  3. google recaptcha 谷歌人机身份验证超详细使用教程,前端/后端集成说明

    壹 ❀ 引 在日常页面交互中,验证码使用是极为频繁的,登录注册验证,非机器人操作验证等等,它遍布于每一个网站.说到验证码实现,Goole Recaptcha是一个非常不错的选择,那么希望通过本文的使用 ...

  4. NC24870 [USACO 2009 Dec G]Video Game Troubles

    题目链接 题目 题目描述 Farmer John's cows love their video games! FJ noticed that after playing these games th ...

  5. Python中Try/Except/else/final语句

    python中的try/except/else/finally语句   与其他语言相同,在python中,try/except语句主要是用于处理程序正常执行过程中出现的一些异常情况,如语法错误(pyt ...

  6. 【OpenGL ES】透视变换原理

    1 前言 ​ MVP矩阵变换 中主要介绍了模型变换(平移.旋转.对称.缩放)和观测变换基本原理,本文将介绍透视变换的基本原理. ​ 如下图,近平面和远平面间棱台称为视锥体,表示可见区域范围,视锥体以外 ...

  7. Js将字符串转数字的方式

    Js将字符串转数字的方式 Js字符串转换数字方方式主要有三类:转换函数.强制类型转换.弱类型隐式类型转换,利用这三类转换的方式可以有5种转换的方法. parseInt() parseInt()和Num ...

  8. Java设计模式-命令模式Command

    介绍 命令模式(Command Pattern):在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收 者是谁,也不知道被请求的操作是哪个, 我们只需在程序运行时指定具体的请求接收者即 ...

  9. Spring源码阅读系列--全局目录

    阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主要内容需要看官自己去源码中验证.全系列文章基于 spring 源码 5.x 版本. 写在开始前的话: 阅读spring 源码实 ...

  10. 我在winform项目里使用“Windows I/O完成端口”的经验分享

    少年!看你骨骼惊奇,是万中无一的练武奇才,我这儿有本武林秘籍,见与你有缘就送你了! 如来神掌 Windows I/O完成端口是一个我至今都说不好的话题,请宽容的接受我这不是科班出身的自学成才的野生程序 ...