321 Create Maximum Number 拼接最大数
已知长度分别为 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]
例 2:
nums1 = [6, 7]
nums2 = [6, 0, 4]
k = 5
返回 [6, 7, 6, 0, 4]
例 3:
nums1 = [3, 9]
nums2 = [8, 9]
k = 3
返回 [9, 8, 9]
详见:https://leetcode.com/problems/create-maximum-number/description/
C++:
class Solution {
public:
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;
}
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;
}
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;
}
};
参考:https://www.cnblogs.com/CarryPotMan/p/5384172.html
321 Create Maximum Number 拼接最大数的更多相关文章
- [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 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 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 ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [LeetCode] 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 ...
随机推荐
- RANS VS LES
Turbulence models
- Vue2.0如何实现父组件与子组件之间的事件发射与接收
关于vue2.0的事件发射和接收,大家都知道$dispatch和$broadcast在vue2.0已经被弃用了,取而代之的是更加方便快捷的方式,使用事件中心,组件通过它来互相通信,不管组件在哪一个层都 ...
- EF中避免查询重复执行的手段
由于ef有lazyload机制,编写的查询语句往往都没有立即执行,当你轮训结果集的时候才会将查询翻译成database端的sql语句,执行sql将结果返回到方法中.但是,下次再使用前面的结果集的时候, ...
- [luoguP1029] 最大公约数和最小公倍数问题(数论)
传送门 一.暴力枚举(加了点优化) #include <cstdio> int x, y, ans; inline int gcd(int x, int y) { return !y ? ...
- [luoguP2983] [USACO10FEB]购买巧克力Chocolate Buying(贪心)
传送门 按价格排序后贪心 ——代码 #include <cstdio> #include <iostream> #include <algorithm> int n ...
- [bzoj1574][Usaco2009 Jan]地震损坏Damage_dfs
地震损坏Damage bzoj-1574 Usaco-2009 Jan 题目大意:题目链接. 注释:略. 想法: 显然对于每一个report点,和它直接相连的点都不可能到达1.我们将它打上标记. 然后 ...
- 洛谷—— P2196 挖地雷
https://www.luogu.org/problem/show?pid=2196 题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定 ...
- gh-ost: triggerless online schema migrations:Blog by Shlomi Noach:
http://code.openark.org/blog/category/mysql https://rj03hou.github.io/mysql/gh-ost/
- Angularjs中添加HighCharts
一. 添加基本配置 1. 添加指令 angular.module('newApp') .directive('dpHighchart', ['$rootScope', function($rootSc ...
- 21行python代码实现拼写检查器
引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供很好的拼写检查,比方你输入 speling,谷歌会立即返回 spelling. 前几天,看到http://norvig.com/spe ...