321. Create Maximum Number 解题方法详解
321. Create Maximum Number
题目描述
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k <= m + n from digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits. You should try to optimize your time and space complexity.
Example 1:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
return [9, 8, 6, 5, 3]
Example 2:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
return [6, 7, 6, 0, 4]
Example 3:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
return [9, 8, 9]
这个问题是要将两个数组合成一个数,我们先得解决一个简单一点的问题————从一个数组里挑出k个数字组成最大的数。
第一步
这个问题可以借助栈和贪心算法来实现。算法步骤:
- 新建一个空栈stack
- 遍历数组
nums- 弹出栈顶元素如果栈顶元素比
nums[i]小,直到
1、栈空
2、剩余的数组不足以填满栈至 k - 如果栈的大小小于 k ,则压入
nums[i]
- 弹出栈顶元素如果栈顶元素比
- 返回栈stack
因为栈的长度知道是k,所以可以用数组来实现这个栈。时间复杂度为O(n),因为每个元素最多被push和pop一次。
实现代码如下:
vector<int> maxArray(vector<int>& nums, int k){
int n = nums.size();
vector<int> result(k);
for (int i = 0, j = 0; i < n; i++){
while (n - i + j>k && j > 0 && result[j-1] < nums[i]) j--;
if (j < k) result[j++] = nums[i];
}
return result;
}
第二步
给定两个数组,长度分别为m 和n,要得到 k = m+n的最大数。
显然这个问题是原问题的特殊情况,显然这种情况下,我们首先想到的是贪心算法。
我们要做k次选择,每次我们就选两个数组中较大的那个数即可。这是显然的,但是如果两个数相等,我们应该选择哪个呢?
这就不是很显然的了,举个栗子,
nums1 = [6,7]
nums2 = [6,0,4]
k = 5
ans = [6,7,6,0,4]
所以相等的情况,我们就需要一直往后比,直到它们不相等分出大小为止。这就有点像归并排序中的merge函数了。因为每次都得一直往后比,所以时间复杂度是O(mn)。
这部分代码如下:
bool greater(vector<int>& nums1, int i, vector<int>& nums2, int j){
while (i < nums1.size() && j < nums2.size() && nums1[i] == nums2[j]){
i++;
j++;
}
return j == nums2.size() || (i<nums1.size() && nums1[i] > nums2[j]);
}
vector<int> merge(vector<int>& nums1, vector<int>& nums2, int k) {
std::vector<int> ans(k);
int i = 0, j = 0;
for (int r = 0; r<k; r++){
if( greater(nums1, i, nums2, j) ) ans[r] = nums1[i++] ;
else ans[r] = nums2[j++];
}
return ans;
}
第三步
让我们回到原问题,我们首先把k个数分成两部分,i 和 k-i,我们可以用第一步中的函数求出两个数组中的长度为 i 和 k-i 的最大值。然后用第二步的方法将它们融合。最后我们从所有的结果中找出最大值。所以代码如下:
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
int m = nums1.size();
int n = nums2.size();
vector<int> result(k);
for (int i = std::max(0 , k - n); i <= k && i <= m; i++){
auto v1 = maxArray(nums1, i);
auto v2 = maxArray(nums2, k - i);
vector<int> candidate = merge(v1, v2, k);
if (greater(candidate, 0, result, 0)) result = candidate;
}
return result;
}
321. Create Maximum Number 解题方法详解的更多相关文章
- 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- 321. Create Maximum Number
/* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- leetcode 402. Remove K Digits 、321. Create Maximum Number
402. Remove K Digits https://www.cnblogs.com/grandyang/p/5883736.html https://blog.csdn.net/fuxuemin ...
- [LeetCode] 321. Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 321. Create Maximum Number (c++ ——> lexicographical_compare)
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 321. Create Maximum Number
原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...
- 321 Create Maximum Number 拼接最大数
已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- oracle 重置序列从指定数字开始的方法详解
原文 oracle 重置序列从指定数字开始的方法详解 重置oracle序列从指定数字开始 declare n ); v_startnum ):;--从多少开始 v_step ):;--步进 tsql ...
随机推荐
- 【luogu P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀布】 题解
题目链接:https://www.luogu.org/problemnew/show/P3609 ### 看着标签什么记搜什么暴力点进来,读完题第一直觉DP? 还真是个\(DP\). 题目所描述的状态 ...
- CUDA实现数组倒序
数组倒序,将在主机上初始化的数组传输到设备上,然后用CUDA并行倒序,此时在全局内存上操作,再将结果返回到主机并验证. #include <stdio.h> #include <as ...
- JSPatch - iOS 动态补丁
JSPatch库,支持在线更新iOS应用,目前BDN项目中有用到,主要用来修复线上Crash和Bug 相关博文推荐: JSPatch – 动态更新iOS APP(这是JSPatch作者的博文) JSP ...
- Mysql--select基础查询
基本语法:select 查询列表 from 表名 查询列表可以是表中字段.常量值.表达式.函数:查询的结果是一个虚拟的表格. 注意: ①sql语言大小写不敏感 ②关键字不能分行或略写 ③一般书写方式为 ...
- Linux入门-第七周
1.编写脚本实现传入进程PID,查看对应进程/proc下CPU.内存指标. #!/bin/bash read -p "Input PID Value: " pid #读取PID进程 ...
- 五、Linux 远程登录
Linux 远程登录 Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这时我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ss ...
- docker 学习(3)
docker和宿主之间的数据共享以及docker间的数据共享仍然是让人头疼和操心的地方. 几个基本概念: docker: 一种容器管理技术,这里也指既有的开发工具链. container: 容器 im ...
- 记忆化搜索:POJ1088-滑雪(经典的记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑 ...
- loj2173 「FJOI2016」建筑师
ref 真是道组合数学神题啊--第一次见第一类斯特林数-- #include <iostream> #include <cstdio> using namespace std; ...
- Careercup - Microsoft面试题 - 23123665
2014-05-12 07:44 题目链接 原题: Given an array having unique integers, each lying within the range <x&l ...