【阿里淘天】3月15日暑期实习机试-第一题-连续非空子数组

news/2025/3/15 17:26:21/文章来源:https://www.cnblogs.com/smartljy/p/18773937

连续非空子数组

题面

image

image

思路

正向求解的话,需要枚举所有的子数组,复杂度会来到\(O(n^3)\),完全不可行,在观察题目输入描述,\(a_i\)的取值范围非常小,故我们考虑反向求解(这也是非常经典的思路,无法直接计数,我们就计算每个答案的贡献是多少

利用类似滑动窗口的思想,去统计\(mex(1), mex(2), mex(3)\)的贡献,\(mex(0)\)的贡献是0,无需统计。

代码

def calculate_mex_sum(arr):n = len(arr)# 计算mex3_count:包含0、1、2的子数组数目mex3_count = 0last_0 = last_1 = last_2 = -1for i in range(n):if arr[i] == 0:last_0 = ielif arr[i] == 1:last_1 = ielse:last_2 = iif last_0 != -1 and last_1 != -1 and last_2 != -1:s = min(last_0, last_1, last_2)mex3_count += s + 1# 计算mex2_count:包含0和1,不含2的子数组数目mex2_count = 0start = 0for i in range(n + 1):if i == n or arr[i] == 2:if start <= i - 1:L = i - starttotal = L * (L + 1) // 2current_zero = current_one = 0total_zero = total_one = 0for j in range(start, i):num = arr[j]if num == 0:current_zero += 1current_one = 0
# 每次新出现一个0,他都可以带来current_zero个新的子数组total_zero += current_zeroelse:  # 只能是1current_one += 1current_zero = 0
# 1同理total_one += current_onemex2_count += (total - total_zero - total_one)start = i + 1# 计算mex1_count:包含至少一个0,不含1,可能含2的子数组数目mex1_count = 0start = 0for i in range(n + 1):if i == n or arr[i] == 1:if start <= i - 1:L = i - starttotal = L * (L + 1) // 2current_2 = 0total_2 = 0for j in range(start, i):num = arr[j]if num == 2:current_2 += 1else:if current_2 > 0:total_2 += current_2 * (current_2 + 1) // 2current_2 = 0if current_2 > 0:total_2 += current_2 * (current_2 + 1) // 2mex1_count += (total - total_2)start = i + 1total_sum = mex1_count * 1 + mex2_count * 2 + mex3_count * 3return total_sum# 示例测试
arr = [0, 1, 2]
print(calculate_mex_sum(arr))  # 输出6

这里的思想本质上就是滑动窗口,但是和传统的滑动窗口写法有很大不同,传统的滑动窗口一般是求出最短的满足条件的窗口,这里要求出所有的(说是最长的也对),所以在写法上要做出比较大的改变。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/899286.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python的基本运用(7)——函数(内置函数)

一、python的内建函数二、内置函数使用 (1)format()函数 1.定义:是一格式化字符串,该函数增强了字符串格式的功能. 2.基本语法是通过{}来代替一起拿% 3.案例 a.设置指定位置,默认暑顺序 hz="{}".format("dcs","18")print(hz) b.按照指定的索引…

LeetCode53最大子数组和——贪心求解

int maxSubArray(vector<int>& nums) {int len = nums.size();int max=INT_MIN;int sum=0;for(int i=0;i<len;i++){sum+=nums[i];if(max<sum) max=sum;if(sum<0) sum=0;}return max;}这是根据代码的写出的max与sum的状态,笔者认为最重要的一点在于理解连续…

推荐一个查看Windows文件夹大小的软件

最近笔记本越来越卡,C盘100G几乎全被占用,所以就想查看是哪个文件占用了C盘, 但是Windows自带的又不好用,于是再网上找了一下 https://windirstat.net/ 发现了这个软件,非常好用,一目了然,免费,可以安装也可以直接解压, 可以查看每个文件和文件夹的大小,占比, 按文件…

Copilot平替?本地部署DeepSeek-Coder V2并接入到VS Code

什么是 DeepSeek-Coder V2 DeepSeek-Coder-V2是DeepSeek团队推出的基于MoE架构的智能代码模型,支持338中编程语言,几乎覆盖所有主流和小众编程语言,一次能处理长达128K的代码文件。 Github 开源仓库地址:https://github.com/deepseek-ai/DeepSeek-Coder-V2 用过DeepSeek很多…

Builder-公用组建封装和 按引用传递 改变数据

@Entry@Componentstruct BuilderPage { // 组建内的 @Builder compButtonBuilder(icon:Resource,text:string,callback:()=>void){ Button() { Row({ space: 10 }) { Image(icon) .width(25) .height(25) Text(text) .f…

【第 8 期】搜索客 Meetup - Elasticsearch 的一些“双刃剑”特性

本次活动由 搜索客社区、极限科技(INFINI Labs) 联合举办,活动邀请到 INFINI Labs 搜索运维专家 金端 来分享 Elasticsearch 中一些典型的“双刃剑”特性,以及如何在实际使用中权衡和应对。欢迎预约直播观看 ~ 活动主题:Elasticsearch 的一些“双刃剑”特性 活动时间:202…

clion/idea/pycharm项目配置

环境搭建与配置 pycharm篇 markdown as root idea篇 clion篇构建 cmake -S src -B build $CMAKE_OPTIONS 编译 cmake --build 配置deployment,cmake(cmake配置编译目录,引用环境变量,cmake环境变量引用$CMAKE_OPTIONS) ccache配置 ccache弄一个gcc和g++的软链,放在PATH最…

web68笔记(+禁用print_r、highlight)

web68 打开就直接说 Warning: highlight_file() has been disabled for security reasons in /var/www/html/index.php on line 19 猜跟之前代码一致,加了过滤 然后这⾥还把print_r给禁了,⽤var_dump()吧 c=var_dump(scandir("/")); 还是在跟⽬录 flag.txt 既然hig…

健身相关

后仰?10次 如何管理自己的症状 急性期https://new.qq.com/rain/a/20210819A094EC00犀牛数据地点+纳税排名

文本弹窗选择

@Entry@Componentstruct TextPickerDialogPage { fruits: string[] = [苹果, 橘子, 香蕉, 鸭梨, 西瓜] @State selectedIndex: number = 0 build() { Column({ space: 50 }) { Text(this.fruits[this.selectedIndex]) .fontWeight(FontWeight.Bold) .…

AP csa FRQ Q1 Past Paper 五年真题汇总 2023-2019

Author(wechat): bigshuang2020 ap csa tutor, AP计算机科学a老师 国际教育编程老师, 擅长答疑讲解,带学生实践学习。热爱创作,作品:ap csa原创教案,真题梳理汇总,FRQ专题冲刺。2023 FRQ Question 1 This question involves the appointmentBook class, which provides me…

自带弹窗-》删除功能提示

@Entry@Componentstruct AlertDialogPage { build() { Column() { Button(删除) .backgroundColor(Color.Red) .onClick(() => { AlertDialog.show( { title: 删除该记录?, //弹窗标题 message: 删除…