【2302. 统计得分小于 K 的子数组数目】前缀和+二分
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 的子数组数目】前缀和+二分的更多相关文章
- Java实现 LeetCode 713 乘积小于K的子数组(子集数量+双指针)
713. 乘积小于K的子数组 给定一个正整数数组 nums. 找出该数组内乘积小于 k 的连续的子数组的个数. 示例 1: 输入: nums = [10,5,2,6], k = 100 输出: 8 解 ...
- [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 ...
- Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)
560. 和为K的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] ...
- 累加和为 K 的子数组问题
累加和为 K 的子数组问题 作者:Grey 原文地址: 博客园:累加和为 K 的子数组问题 CSDN:累加和为 K 的子数组问题 题目说明 数组全为正数,且每个数各不相同,求累加和为K的子数组组合有哪 ...
- 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 ...
- [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 ...
- 66.Subarray Sum Equals K(子数组和为K的个数)
Level: Medium 题目描述: Given an array of integers and an integer k, you need to find the total number ...
- [LeetCode]560. 和为K的子数组(前缀和)
题目 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为 ...
- 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 ...
- 【Leetcode】560. 和为K的子数组&974. 和可被 K 整除的子数组(前缀和+哈希表)
public class Solution { public int subarraySum(int[] nums, int k) { int count = 0, pre = 0; HashMap ...
随机推荐
- 简单总结Tomcat/SpringMVC/Spring/SpringBoot的关系
Tomcat是一个Web应用服务器,可以作为Servlet容器.它的作用是,解析客户端client发起的request,并组装出HttpRequest.创建HttpResponse,将二者交于内部的H ...
- .NET Core开发实战(第24课:文件提供程序:让你可以将文件放在任何地方)--学习笔记
24 | 文件提供程序:让你可以将文件放在任何地方 文件提供程序核心类型: 1.IFileProvider 2.IFileInfo 3.IDirectoryContents IFileProvider ...
- Hive分区和分桶的区别
1.前言 Hive的分区和分桶都是细化数据管理,加快数据查询和分析,两者有什么区别呢?下面讲解一下分区和分桶的原理. 2.分区 (1)分区原理 Hive的分区表可以有一个或多个分区键,用于确定数据的存 ...
- [Java]format string is malformed java
format string is malformed java 最近在做代码审查,发现很多在使用 String.format 的时候遇到了IDEA报的 Format string 'xxx' is m ...
- 未配置Datasource时, 启动 SpringBoot 程序报错的问题
SpringBoot will show error if there is no datasource configuration in application.yml/application.pr ...
- 【Unity3D】2D动画
1 图片处理 通过 PS 软件将以下 gif 文件中的黑色背景删除,并将其中的 18 个图层分别保存为 png 格式图片. 2 游戏对象 1)游戏对象层级结构 2)Transform组件参 ...
- Oracle 分析函数详解(Analytic Functions)--示例部分
Analytic functions are commonly used in data warehousing environments. In the list of analytic funct ...
- Java开发技巧杂记
杂记一 创建项目的spring initializr,要求是创建spring boot3.0,且不在支持java8语言,且要求语言大于17,所以创建项目时,无法创建srping2.0项目了:如果要创建 ...
- QT - Day 3
对话框 分类 模态对话框 QDialog dlg(this); dlg.resize(200,100); dlg.exec(); //窗口阻塞 非模态对话框 QDialog *dlg2 = new Q ...
- 突破Windows的极限
偶然碰到这类技术博客,甚感欣慰,但奈何技术水平达不到,很多都难以理解,故记录在此,用作日后学习. 国内有类似的中文翻译,比如:突破Windows极限:物理内存 但是外文链接已经失效,看不到原汁原味的英 ...