Leetcode: Create Maximum Number

耗尽温柔 提交于 2020-01-12 07:45:12
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/ 

 1 public class Solution {
 2     public int[] maxNumber(int[] nums1, int[] nums2, int k) {
 3         int len1 = nums1.length;
 4         int len2 = nums2.length;
 5         int[] res = new int[k];
 6         for (int i=Math.max(0, k-len2); i<=k && i<=len1; i++) {
 7             int[] temp = merge(maxArray(nums1, i), maxArray(nums2, k-i), k);
 8             if (isgreater(temp, 0, res, 0))
 9                 res = temp;
10         }
11         return res;
12     }
13     
14     public int[] merge(int[] arr1, int[] arr2, int k) {
15         int[] res = new int[k];
16         if (arr1.length == 0) return arr2;
17         if (arr2.length == 0) return arr1;
18         int cur = 0, i = 0, j = 0;
19         int len1 = arr1.length, len2 = arr2.length;
20         while (i<len1 && j<len2) {
21             if (isgreater(arr1, i, arr2, j)) {
22                 res[cur++] = arr1[i++];
23             }
24             else res[cur++] = arr2[j++];
25         }
26         while (i < len1) {
27             res[cur++] = arr1[i++];
28         }
29         while (j < len2) {
30             res[cur++] = arr2[j++];
31         }
32         return res;
33     }
34     
35     public int[] maxArray(int[] arr, int count) {
36         int[] res = new int[count];
37         if (count == 0) return res;
38         int n = arr.length;
39         int j = 0; //stack head next
40         for (int i=0; i<n; i++) {
41             while (j>0 && n-i-1>=count-j && res[j-1]<arr[i]) { //stack head can be poped
42                 j--;
43             }
44             if (j<count) {
45                 res[j] = arr[i];
46                 j++;
47             }
48         }
49         return res;
50     }
51     
52     public boolean isgreater(int[] arr1, int i1, int[] arr2, int i2) {
53         int len1 = arr1.length, len2 = arr2.length;
54         while (i1<len1 && i2<len2 && arr1[i1]==arr2[i2]) {
55             i1++;
56             i2++;
57         }
58         if (i1 == len1) return false;
59         if (i2 == len2) return true;
60         return arr1[i1]>arr2[i2];
61     }
62 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!