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]

We can use greedy method to solve this.

num1 can take charge of i(0<=i<=k) numbers and num2 can the charge of the rest k-i numbers

Now the question is: how to get maximum number(x digits) from an array

Recall Remove Duplicate Letters, we can use a stack. we scan the array, when the stack is not empty, and current integer in the array is greater than current stack peek(), we pop the stack, as long as the rest of the array can gurantee to provide enough integers to form an integer of k bits.

Now we have two maximum array, with one size of i and another size of k-i, now we should combine the two into one maximum integer

It's like merge sort.

But pay attention to the case where two digit are equal, which to choose?

example:

6 0

6 0 4

we should choose the second 6 first. So we should write a compare function that compare two array till the end, not just the current two digits.

参考:http://algobox.org/2015/12/24/create-maximum-number/

 public class Solution {
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int len1 = nums1.length;
int len2 = nums2.length;
int[] res = new int[k];
for (int i=Math.max(0, k-len2); i<=k && i<=len1; i++) {
int[] temp = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
if (isgreater(temp, 0, res, 0))
res = temp;
}
return res;
} public int[] merge(int[] arr1, int[] arr2, int k) {
int[] res = new int[k];
if (arr1.length == 0) return arr2;
if (arr2.length == 0) return arr1;
int cur = 0, i = 0, j = 0;
int len1 = arr1.length, len2 = arr2.length;
while (i<len1 && j<len2) {
if (isgreater(arr1, i, arr2, j)) {
res[cur++] = arr1[i++];
}
else res[cur++] = arr2[j++];
}
while (i < len1) {
res[cur++] = arr1[i++];
}
while (j < len2) {
res[cur++] = arr2[j++];
}
return res;
} public int[] maxArray(int[] arr, int count) {
int[] res = new int[count];
if (count == 0) return res;
int n = arr.length;
int j = 0; //stack head next
for (int i=0; i<n; i++) {
while (j>0 && n-i-1>=count-j && res[j-1]<arr[i]) { //stack head can be poped
j--;
}
if (j<count) {
res[j] = arr[i];
j++;
}
}
return res;
} public boolean isgreater(int[] arr1, int i1, int[] arr2, int i2) {
int len1 = arr1.length, len2 = arr2.length;
while (i1<len1 && i2<len2 && arr1[i1]==arr2[i2]) {
i1++;
i2++;
}
if (i1 == len1) return false;
if (i2 == len2) return true;
return arr1[i1]>arr2[i2];
}
}

Leetcode: Create Maximum Number的更多相关文章

  1. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  2. 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 ...

  3. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  4. 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 ...

  5. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  6. 321. Create Maximum Number

    /* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...

  7. [LeetCode] 321. Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  8. LeetCode 321. Create Maximum Number

    原题链接在这里:https://leetcode.com/problems/create-maximum-number/description/ 题目: Given two arrays of len ...

  9. [Swift]LeetCode321. 拼接最大数 | Create Maximum Number

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

随机推荐

  1. Android 加入一个动作按钮

    在XML中声明一个动作按钮 所有的动作按钮和其他的可以利用的items都定义在menu资源文件夹中的XML文件中.为了增加一个动作按钮到工具栏,需要在工程 /res/menu/ 目录下面创建一个新的X ...

  2. Java Map遍历方式的选择

    [原文] 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keyS ...

  3. MVC3下的layout页面

    1.Layout页基础:如果你有使用MasterPage的经验,你将会记得如下的几个东西 A:<%@ Master %> B:<%@ Page %> C:<asp:Con ...

  4. 20145319 《java程序设计》课程总结

    20145319 <Java程序设计>课程总结 读书笔记链接总结 - 20145319 第一周学习总结 - 20145319 第二周学习总结 - 20145319 第三周学习总结 - 20 ...

  5. Linus:利用二级指针删除单向链表

    Linus大神在slashdot上回答一些编程爱好者的提问,其中一个人问他什么样的代码是他所喜好的,大婶表述了自己一些观点之后,举了一个指针的例子,解释了什么才是core low-level codi ...

  6. python 十进制 十六进制

    把十六进制的字串转为十进制数字:>>> print int('ff', 16)   255 把十进制数字转换为以十六进制表示之字串,可调用内置的hex()函数:>>> ...

  7. C++ 线程类的一个实现

    .h #ifndef CTHREAD_H_ #define CTHREAD_H_ #include "plat.h" class CThread { public: enum { ...

  8. eclipse如何修改dynamic web module version

    eclipse如何修改dynamic web module version 一.修改工程属性: 右键eclipse的工程,选择属性,再选择Project Facets里面中选择Dynamic Web ...

  9. map和json之间的转换

    Action中在session中存放了一个map<String,Object>,跳转到a.jsp,a.jsp通过form提交到BAction,BAction可从session中获得map值 ...

  10. C++经典编程题#4:单词翻转

    总时间限制:  1000ms  内存限制:  65536kB 描述 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入 只有一行,为一个字符串,不超过500个字符.单词之间以空格隔开. 输出 ...