leetcode 321. 拼接最大数(单调栈)

给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。

求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。

说明: 请尽可能地优化你算法的时间和空间复杂度。

示例 1:

输入:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
输出:
[9, 8, 6, 5, 3]

代码

class Solution {public int[] maxNumber(int[] nums1, int[] nums2, int k) {int m=nums1.length,n=nums2.length;int[] res=new int[k];int s=Math.max(0,k-n),end=Math.min(k,m);for(int i=s;i<=end;i++)//遍历nums1和nums2不同取数情况返回的最大数组,返回最大的结果{int[] cur=merge(getMaxNumber(nums1, i),getMaxNumber(nums2, k-i));if(comp(cur,0,res,0))res=cur;}return res;}public boolean comp(int[] nums1,int p1 ,int[] nums2, int p2) {
//比较两个序列,如果前面元素都相同,则长度大的较大if(p2>=nums2.length) return true;if(p1>=nums1.length) return false;if(nums1[p1]>nums2[p2]) return true;if(nums1[p1]<nums2[p2]) return false;return comp(nums1, p1+1, nums2, p2+1);}public int[] merge(int[] nums1, int[] nums2) {int n=nums1.length,m=nums2.length;int cur=0,p1=0,p2=0;int []ret=new int[n+m];while (p1<n||p2<m)//合并两个数组{if(comp(nums1,p1,nums2,p2))ret[cur++]=nums1[p1++];else ret[cur++]=nums2[p2++];}return ret;}public int[] getMaxNumber(int[] nums1, int k) {//单调栈找出单个数组的最大排列int n=nums1.length,rem=n-k,top=-1;int[] stack=new int[k];for(int i=0;i<n;i++){int cur=nums1[i];while (top>=0&&cur>stack[top]&&rem>0){top--;rem--;}if(top<k-1)stack[++top]=cur;else rem--;//可删除的额度减一,当可删除的额度用尽,将不能再移除元素(为了保证返回数组的长度)}return stack;}
}

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

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

相关文章

Oracle Study之--Oracle等待事件(5)

Db file single write这个等待事件通常只发生在一种情况下&#xff0c;就是Oracle 更新数据文件头信息时&#xff08;比如发生Checkpoint&#xff09;。当这个等待事件很明显时&#xff0c;需要考虑是不是数据库中的数据文件数量太大&#xff0c;导致Oracle 需要花较长的时间来…

两台centos之间免密传输 scp

两台linux服务器之间免密scp&#xff0c;在A机器上向B远程拷贝文件 操作步骤&#xff1a;1、在A机器上&#xff0c;执行ssh-keygen -t rsa&#xff0c;一路按Enter&#xff0c;不需要输入任何内容。&#xff08;如有提示是否覆盖&#xff0c;可输入y后按回车&#xff09;2、到/…

jsp导出数据时离开页面_您应该在要离开的公司开始使用数据

jsp导出数据时离开页面If you’re new in data science, “doing data science” likely sounds like a big deal to you. You might think that you need meticulously collected data, all the tools for data science and a flawless knowledge before you can claim that y…

分步表单如何实现 html_HTML表格入门的分步指南

分步表单如何实现 htmlby Abhishek Jakhar通过阿比舍克贾卡(Abhishek Jakhar) HTML表格入门的分步指南 (A step-by-step guide to getting started with HTML tables) 总览 (Overview) The web is filled with information like football scores, cricket scores, lists of em…

laravel mysql pdo,更改Laravel中的基本PDO配置

My shared web host have some problems with query prepares and I want to enable PDOs emulated prepares, theres no option for this in the config\database.php.Is there any way I can do that in Laravel?解决方案You can add an "options" array to add o…

Java多线程-工具篇-BlockingQueue

Java多线程-工具篇-BlockingQueue 转载 http://www.cnblogs.com/jackyuj/archive/2010/11/24/1886553.html 这也是我们在多线程环境下&#xff0c;为什么需要BlockingQueue的原因。作为BlockingQueue的使用者&#xff0c;我们再也不需要关心什么时候需要阻塞线程&#xff0c;什…

leetcode 204. 计数质数

统计所有小于非负整数 n 的质数的数量。 示例 1&#xff1a; 输入&#xff1a;n 10 输出&#xff1a;4 解释&#xff1a;小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 解题思路 大于等于5的质数一定和6的倍数相邻。例如5和7&#xff0c;11和13,17和19等等&#xff1b…

JAVA 网络编程小记

在进行JAVA网络编程时&#xff0c;发现写入的数据对方等200ms左右才会收到。起初认为是JAVA自已进行了 Cache。进行flush也没有效果。查看JDK代码&#xff0c;Write操作直接调用的native方法&#xff0c;说明JAVA层面并没有缓存。再看flush&#xff0c;只是一个空方法. FileOut…

vue生成静态js文件_如何立即使用Vue.js生成静态网站

vue生成静态js文件by Ondřej Polesn通过OndřejPolesn 如何立即使用Vue.js生成静态网站 (How to generate a static website with Vue.js in no time) You have decided to build a static site, but where do you start? How do you select the right tool for the job wit…

查看文件夹大小的4种方法,总有一种是你喜欢的

有必要检查文件夹的大小,以确定它们是否占用了太多的存储空间。此外,如果你通过互联网或其他存储设备传输文件夹,还需要查看文件夹大小。 幸运的是,在Windows设备上查看文件夹大小非常容易。窗口中提供了图形化和基于命令行的应用程序,为你提供了多种方法。 如何在Windo…

Python 获取服务器的CPU个数

在使用gunicorn时&#xff0c;需要设置workers&#xff0c; 例如&#xff1a; gunicorn --workers3 app:app -b 0.0.0.0:9000 其中&#xff0c;worker的数量并不是越多越好&#xff0c;推荐值是CPU的个数x21&#xff0c; CPU个数使用如下的方式获取&#xff1a; python -c impo…

多种数据库连接工具_20多种热门数据工具及其不具备的功能

多种数据库连接工具In the past few months, the data ecosystem has continued to burgeon as some parts of the stack consolidate and as new challenges arise. Our first attempt to help stakeholders navigate this ecosystem highlighted 25 Hot New Data Tools and W…

怎么连接 mysql_怎样连接连接数据库

这个博客是为了说明怎么连接数据库第一步&#xff1a;肯定是要下载数据库&#xff0c;本人用的SqlServer2008&#xff0c;是从别人的U盘中拷来的。第二步&#xff1a;数据库的登录方式设置为混合登录&#xff0c;步骤如下&#xff1a;1.打开数据库这是数据库界面&#xff0c;要…

webstorm环境安装配置(less+autoprefixer)

node安装&#xff1a; 参考地址&#xff1a;http://www.runoob.com/nodejs/nodejs-install-setup.html 1.下载node安装包并完成安装 2.在开始菜单打开node 3.查看是否安装完成&#xff08;npm是node自带安装的&#xff09; 命令&#xff1a;node -v npm -v less安装&#xff1a…

leetcode 659. 分割数组为连续子序列(贪心算法)

给你一个按升序排序的整数数组 num&#xff08;可能包含重复数字&#xff09;&#xff0c;请你将它们分割成一个或多个子序列&#xff0c;其中每个子序列都由连续整数组成且长度至少为 3 。 如果可以完成上述分割&#xff0c;则返回 true &#xff1b;否则&#xff0c;返回 fa…

将JAVA编译为EXE的几种方法

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> 将JAVA编译为EXE的几种方法 -------------------------------------------------------------------------------- 将Java应用程序本地编译为EXE的几种方法(建议使用JOVE和JET)  a.…

文本训练集_训练文本中的不稳定性

文本训练集介绍 (Introduction) In text generation, conventionally, maximum likelihood estimation is used to train a model to generate a text one token at a time. Each generated token will be compared against the ground-truth data. If any token is different …

山东省赛 传递闭包

https://vjudge.net/contest/311348#problem/A 思路&#xff1a;用floyd传递闭包处理点与点之间的关系&#xff0c;之后开数组记录每个数字比它大的个数和小的个数&#xff0c;如果这个个数超过n/2那么它不可能作为中位数&#xff0c;其他的都有可能。 #include<bits/stdc.h…

如何使用动态工具提示构建React Native图表

by Vikrant Negi通过Vikrant Negi 如何使用动态工具提示构建React Native图表 (How to build React Native charts with dynamic tooltips) Creating charts, be it on the web or on mobile apps, has always been an interesting and challenging task especially in React …

如何解决ajax跨域问题(转)

由 于此前很少写前端的代码(哈哈&#xff0c;不合格的程序员啊)&#xff0c;最近项目中用到json作为系统间交互的手段&#xff0c;自然就伴随着众多ajax请求&#xff0c;随之而来的就是要解决 ajax的跨域问题。本篇将讲述一个小白从遇到跨域不知道是跨域问题&#xff0c;到知道…