leetcode-161周赛-5248-统计【优美子数组】
题目描述:
自己的提交:超时:
class Solution:
def numberOfSubarrays(self, nums, k: int) -> int:
dp = [0]* (len(nums)+1)
res = 0
for i in range(len(nums)):
if nums[i] % 2 == 0:
dp[i+1] = dp[i]
else:
dp[i+1] = dp[i] + 1
if dp[i+1] == k:
res += 1
for i in range(len(nums)):
for j in range(len(nums)+1):
if nums[i] % 2 == 1:
dp[j] -= 1
if dp[j] == k:
res += 1
return res
参考后提交:O(N)
class Solution:
def numberOfSubarrays(self, nums, k: int) -> int:
dp = [0]* (len(nums)+1)
res = 0
for i in range(len(nums)):
if nums[i] % 2 == 0:
dp[i+1] = dp[i]
else:
dp[i+1] = dp[i] + 1
from collections import Counter
dic = Counter(dp)
for i in dp:
if i >= k:
res += dic[i-k]
return res
优化:
import collections
class Solution:
def numberOfSubarrays(self, nums, k: int) -> int:
mp = collections.Counter()
mp[0] = 1
g = 0
ans = 0
for num in nums:
if num % 2 == 1:
g += 1
ans += mp[g - k]
mp[g] += 1
return ans
leetcode-161周赛-5248-统计【优美子数组】的更多相关文章
- LeetCode 1248. 统计「优美子数组」
地址 https://www.acwing.com/solution/leetcode/content/5801/ 题目描述给你一个整数数组 nums 和一个整数 k. 如果某个子数组中恰好有 k 个 ...
- 力扣Leetcode 1248. 统计「优美子数组」
统计「优美子数组」 给你一个整数数组 nums 和一个整数 k. 如果某个 连续 子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. 请返回这个数组中「优美子数组」的数目. 示例 ...
- 【LeetCode】1248. 统计「优美子数组」
1248. 统计「优美子数组」 知识点:数组:前缀和: 题目描述 给你一个整数数组 nums 和一个整数 k. 如果某个 连续 子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. ...
- 【LeetCode】209. 长度最小的子数组
209. 长度最小的子数组 知识点:数组:前缀和:二分法:双指针:滑动窗口 题目描述 给定一个含有 n 个正整数的数组和一个正整数 target . 找出该数组中满足其和 ≥ target 的长度最小 ...
- LeetCode 209:最小长度的子数组 Minimum Size Subarray Sum
公众号: 爱写bug(ID:icodebugs) 作者:爱写bug 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子 ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode——560. 和为K的子数组
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] 为两种不 ...
- Leetcode 560.和为k的子数组
和为k的子数组 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1 ...
- LeetCode 560. 和为K的子数组(Subarray Sum Equals K)
题目描述 给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数. 示例 1 : 输入:nums = [1,1,1], k = 2 输出: 2 , [1,1] 与 [1,1] ...
随机推荐
- visual Studio如何使用断点调试程序?
1.在想要添加断点的地方右侧点击,点击成功后会出现红色原点. 2.启动程序,当进行到断点处时,程序会停止,然后可以看到一个黄色的小箭头在断点处 3.快捷键F10:进行下一句代码 4.快捷键F11:进入 ...
- 【leetcode】946. Validate Stack Sequences
题目如下: Given two sequences pushed and popped with distinct values, return true if and only if this co ...
- NVMe SSD是什么?
https://blog.51cto.com/alanwu/1766945 一直对闪存存储关注的朋友对NVMe SSD一定非常熟悉,NVMe SSD是现如今性能最好的存储盘.这种高性能盘在互联网领域已 ...
- 微信小程序常用API组件开发
关于小程序 张小龙定义小程序: 1.不需要下载安装即可使用: 2.用完即走,不用关心是否安装太多应用: 3.应用无处不在,随时可用. 特点: 1.适合业务逻辑简单的应用: 2,.适合低频应用: 3.适 ...
- spring boot2.x集成spring security5与druid1.1.13(一)
版本: spring boot 2.1.2.RELEASE druid-spring-boot-starter 1.1.13 步骤: 一.maven ...
- toleft时设置TabSequence属性为tsReversetoright时设置TabSequence属性为tsStandard
使用这2人控件时,属性taborientation设为toleft时有个问题,具体如下设为toleft时tab会跑到左侧,这时的tab上的文字是反的.当设置为toright时,tab在右侧,这时的ta ...
- Django框架(二十七)—— ContentType组件
目录 ContentType组件 一.什么是ContentType组件 二.使用ContentType 三.使用场景总结 ContentType组件 一.什么是ContentType组件 conten ...
- 注解深入浅出之Retrofit中的注解(三)
更多andorid高级架构进阶视频免费分享学习请点击:https://space.bilibili.com/474380680 Retrofit中的注解 @Query,@QueryMap,@Field ...
- java swing 中JTable实现指定单元格为下拉框
利用自定义的CellEditor实现第四列第二行为下拉框,本列其余行为文本框 利用默认的DefaultCellEditor设置第五列整列为下拉框 package mypackage; import ...
- 使用allure2生成精美报告
安装:brew install allure pip install allure-pytest 在测试执行期间收集结果 pytest -s –q --alluredir=./result/ 测试完成 ...