当前位置:网站首页>Leetcode 1248 count number of nice subarrays
Leetcode 1248 count number of nice subarrays
2022-06-11 01:43:00 【_ TCgogogo_】
Given an array of integers nums and an integer k. A continuous subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3 Output: 2 Explanation: The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1 Output: 0 Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2 Output: 16
Constraints:
1 <= nums.length <= 500001 <= nums[i] <= 10^51 <= k <= nums.length
Topic link :https://leetcode.com/problems/count-number-of-nice-subarrays/
The main idea of the topic : Seeking inclusion k Number of odd subarrays
Topic analysis : Use an array pos Record the subscript of odd numbers , Accumulate the number of even numbers on the left and right of each odd number as an array l0 and r0, Then enumerate and calculate , Right. i Odd number and number i+k-1 An odd number , The length they can form is k The subarray of is (1+l0[pos[i]]) * (1 + r0[pos[i+k-1]])
12ms, Time beats 81.3%
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
int n = nums.length, sum = 0;
int[] pos = new int[n + 1];
for (int i = 1; i <= n; i++) {
if (nums[i - 1] % 2 == 1) {
pos[++sum] = i;
}
}
int[] r0 = new int[n + 1];
int cntr0 = 0;
for (int i = n; i >= 1; i--) {
if (nums[i - 1] % 2 == 0) {
cntr0++;
} else {
r0[i] = cntr0;
cntr0 = 0;
}
}
int[] l0 = new int[n + 1];
int cntl0 = 0;
for (int i = 1; i <= n; i++) {
if (nums[i - 1] % 2 == 0) {
cntl0++;
} else {
l0[i] = cntl0;
cntl0 = 0;
}
}
int ans = 0;
for (int i = 1; i + k - 1 <= sum; i++) {
if (pos[i] != 0 && i + k - 1 <= sum && pos[i + k - 1] != 0) {
ans += (1 + l0[pos[i]]) * (1 + r0[pos[i + k - 1]]);
}
}
return ans;
}
}边栏推荐
- Px4 installation tutorial (VI) vertical fixed wing (tilting)
- 数字ic设计自学ing
- I was so excited about the college entrance examination in 2022
- MATLAB数组其他常见操作笔记
- SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)
- Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
- 懒汉式单例模式
- 2022 recognition requirements for new technologies and new products (services) in Huairou District, Beijing
- Introduction to the application process of Shenzhen China Patent Award, with a subsidy of 1million yuan
- 云呐|PDA无线固定资产盘点管理系统
猜你喜欢

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture

ROS参数服务器

threejs:流光效果封装

如何下载网页照片

Linux安装mysql数据库详解

Brief description of custom annotations

SAS判别分析(Bayes准则和proc discrim过程)

Once you know these treasure websites, you can't live without them!!!

Using MySQL database in nodejs

Current limiting and download interface request number control
随机推荐
Yunna Qingyuan fixed assets management and barcode inventory system
云呐|省级行政单位固定资产管理系统
关于mobx
数字ic设计自学ing
2.2、ROS+PX4仿真多点巡航飞行----正方形
1.4PX4程序下载
云呐|庆远固定资产管理及条码盘点系统
Leetcode permutation and combination problem backtracking
SSH Remote Login configuration sshd_ Config file details
中间件_Redis_05_Redis的持久化
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
MultipartFile和File互转工具类
threejs:如何获得几何体的boundingBox?
Introduction to support standards for specialized, special and new manufacturing enterprises in Chaoyang District, Beijing, with a subsidy of 1million yuan
卡尔曼滤波(KF)、拓展卡尔曼滤波(EKF)推导
Negative number +0+ positive number
Conda安装Pytorch后numpy出现问题
2.0、ROS与PX4通信详解
OCR文字识别经典论文详解