【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 ...
随机推荐
- 20.1 DLL模块的显式加载和符号链接--《Windows核心编程》
一.显式加载DLL模块使用函数 LoadLibrary/LoadLibraryEx HINSTANCE LoadLibrary(PCTSTR pSzDLLPathName); HINSTANCE Lo ...
- 9.文件和异常--《Python编程:从入门到实践》
9.1 从文件中读取数据 要使用文本文件中的信息,首先需要将信息读取到内存中.为此,你可以一次性读取文件的全部内容,也可以以每次一行的方式逐步读取. 9.1.1 读取整个文件 with open( ...
- Kafka-启动时报错: ERROR Fatal error during KafkaServer startup. Prepare to shutdown
一.问题描述 在启动kafka时报错: ERROR Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server. ...
- CF1903
A 若 \(k>1\),冒泡排序:否则判断是否已经有序. B 初始令 \(a_i=2^{30}-1\),然后对于每个限制,让 \(a_i\leftarrow a_i\&M_{i,j},\ ...
- IIS配置跨域
在IIS里找到HTTP响应标头 添加如下两个标头 Access-Control-Allow-Headers:Content-Type, api_key, Authorization Access-Co ...
- 【framework】View添加过程
1 前言 WMS启动流程 中介绍了 WindowManagerService 的启动流程,本文将介绍 View 的添加流程,按照进程分为以下2步: 应用进程:介绍从 WindowManagerImpl ...
- Google C++ 风格指南记录
最近在看谷歌的 C++ 风格指南发现了一些有意思的知识点,遂记录下 1. 第六章第二小节介绍了右值引用 只在定义移动构造函数与移动赋值操作时使用右值引用. 不要使用 std::forward. 定义: ...
- win32 - ReadDirectoryChangesW的使用
任务:创建一个进程,并在进程内创建一个文本,再创建另一个进程来监控第一个进程内的文本变化 //Process 1 #include <windows.h> #include <ios ...
- 小红书 x Hugging Face 邀请你一起晒「创意新春照」
不藏了,近期全网爆火的AI 写真项目 InstantID,正是来自小红书社区技术创作发布团队. 为了迎接龙年春节的到来,我们的InstantID全新推出「Spring Festival」新春风格!并与 ...
- 【Android逆向】frida 破解 滚动的天空
1. apk 安装到手机中 2. 玩十次之后,会提示 充值 3. adb shell dumpsys window | grep mCurrentFocus 查看一些当前activity是哪一个 是 ...