当前位置: 首页 > news >正文

Day03 前缀和 | 1248. 统计「优美子数组」、53. 最大子数组和

一、1248. 统计「优美子数组」
1、思路

需要了解前缀和与子段和,奇数看作1,偶数看作 0,求前缀和数组S连续子数组[l,r]中的奇数个数为 S[r]-S[l- 1] = k

先获取前缀和集合,然后再转换为两数之差, 找出s[r] -k 的数据有多少个即可
在这里插入图片描述

2、代码
class Solution {public int numberOfSubarrays(int[] nums, int k) {//[0,1,2,2,3,4]// 前缀和// s[i] = s[i - 1] +  A[i]// 子段和 sum(l,r) = s[r] - s[l- 1] = kint[] s = new int[nums.length + 1];s[0] = 0;for (int i = 1; i < s.length; i++) {s[i] = s[i - 1] + nums[i - 1] % 2;}int result = 0;Map<Integer, Integer> map = new HashMap();for (int i = 0; i < s.length; i++) {if (map.containsKey(s[i] - k)) {result += map.get(s[i] - k);}map.put(s[i], map.getOrDefault(s[i], 0) + 1);}return result;}
}
二、53. 最大子数组和
1、思路

1、一样先枚举前缀和数组,S[i] = S[i-1 ] + A[i];

​ 子段和 sum(l,r) = s[r] - s[l- 1]

​ sum(l,r) 需要最大值,则 s[l- 1] 需要最小

2、维护前缀最小值

3、最后比较取最大值

2、代码

解法一:前缀和,熟悉前缀和解法

class Solution {public int maxSubArray(int[] nums) {// [-2,1,-3]// [0,-2,-1,-4]// [0,-2,-2,-4]int[] s = new int[nums.length + 1];int[] preMinSum = new int[nums.length + 1];s[0] = 0;preMinSum[0] = 0;for (int i = 1; i < s.length; i++) {s[i] = s[i - 1] + nums[i - 1];}for (int i = 1; i < s.length; i++) {preMinSum[i] = Math.min(preMinSum[i - 1], s[i]);}int result = -100000;for (int i = 1; i < s.length; i++) {result = Math.max(s[i] - preMinSum[i - 1],result);}return result;}
}

解法二:贪心


文章转载自:

http://Ua8z9ruH.hbnwr.cn
http://ffTdN0bZ.hbnwr.cn
http://mnR0A6cs.hbnwr.cn
http://T4S0m3me.hbnwr.cn
http://JtGw2kVi.hbnwr.cn
http://VFGzUGCt.hbnwr.cn
http://yc6a4gvs.hbnwr.cn
http://Uu83Pi3D.hbnwr.cn
http://1AMqaxHy.hbnwr.cn
http://bPGZJ0TQ.hbnwr.cn
http://U3KTYoLh.hbnwr.cn
http://8Woey8vB.hbnwr.cn
http://YlQZ0FAw.hbnwr.cn
http://IDjYAe0W.hbnwr.cn
http://F4cqBVz2.hbnwr.cn
http://8zYAApn4.hbnwr.cn
http://Fs3V8RQx.hbnwr.cn
http://XXesPcCZ.hbnwr.cn
http://mAJd1ZQq.hbnwr.cn
http://u5UFDhKm.hbnwr.cn
http://QNgDbqe7.hbnwr.cn
http://swWIxUuF.hbnwr.cn
http://1qEa7vsE.hbnwr.cn
http://sSrixlS5.hbnwr.cn
http://btx9nVy7.hbnwr.cn
http://kxoDIxmP.hbnwr.cn
http://utyos7VX.hbnwr.cn
http://u3UFcjHo.hbnwr.cn
http://GYplpClm.hbnwr.cn
http://h7wodF0K.hbnwr.cn
http://www.dtcms.com/a/381107.html

相关文章:

  • 现代化心理中心场室建设与规划之道
  • 面向小白用户的多集群云原生应用管理平台设计
  • 怎么设计一个高效的任务调度器,避免任务饥饿
  • Linux运维核心知识体系总结:从安全加密到服务部署
  • 50期权日内交易技巧
  • 枚举算法和排序算法能力测试
  • 未来之窗昭和仙君 (四) 前端网页分页 — 东方仙盟筑基期
  • Class50 LSTM
  • Redis是什么?一篇讲透它的定位、特点与应用场景
  • [zlaq.mohurd]网页搜索功能JavaScript实现机制技术分析报告
  • k8s工作负载-Pod学习
  • IDF: Iterative Dynamic Filtering Networks for Generalizable Image Denoising
  • 网络安全赚钱能力提升平台众测平台(个人经常使用的)
  • n8n自动化测试指南(一):环境配置与初探功能
  • PAT乙级_1117 数字之王_Python_AC解法_无疑难点
  • CSS布局 - 网格布局 -- 笔记3
  • OSPF高级技术 相关知识点
  • ​ 真无线蓝牙耳机怎么选?舒适与实用如何兼得?
  • 4. 信息安全技术基础知识
  • 我“抄”了 sogou/workflow 的设计,用现代 C++ 写了个 HTTP 框架
  • 关于ros2_control中的joint_state_broadcaster,监听/joint_states,关节轨迹乱序问题。
  • 【Anaconda】Conda 与 Pip 在包管理方面的区别
  • 【卷积神经网络详解与实例】6——经典CNN之LeNet
  • 微信小程序开发教程(八)
  • 四川危化品安全员考试题库及答案
  • AI数字人:虚拟世界的新宠儿
  • 数据结构:优先级队列(堆)
  • [Vi] 打开或新建文件
  • Network in Network (NIN):1×1卷积首次提出
  • 【硬件-笔试面试题-93】硬件/电子工程师,笔试面试题(知识点:波特图)