摘要:题目要求思路和代码首先采用分治法的思路,我们知道这个数字中,必然有个数组来自,而剩下的个数字必然来自。那么问题变成从中获取个数,这个数构成的数字最大,且这个数字的相对位置不变。
题目要求
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个数字中,必然有i个数组来自nums1,而剩下的k-i个数字必然来自nums2。那么问题变成从nums1中获取i个数,这i个数构成的数字最大,且这i个数字的相对位置不变。再从nums2中获取k-i个数,这k-i个数构成的数字最大,且这k-i个数字的相对位置不变。
那么我们如何将这两个结果合并起来获得我们最终的结果呢?这里很像归并算法的merge过程,我们从两个数组的开头获取最大的值加进来呗。那如果出现相同的值怎么办?那么继续比较,直到遇到第一个不相同的数字,然后选择数字较大的那个数组。
public int[] maxNumber(int[] nums1, int[] nums2, int k) { //结果集 int[] result = new int[k]; //nums1最多能够提供的数字的个数 int maxCountOfNumFromNums1 = Math.min(nums1.length, k); for(int i = Math.max(0, k - maxCountOfNumFromNums1) ; i<=Math.min(k, nums2.length) ; i++){ int j = k - i; int[] res1 = getMaxSubarray(nums1, j); int[] res2 = getMaxSubarray(nums2, i); int[] mergeRes = new int[k]; int index = 0, index1 = 0, index2 = 0; while(index1nums2[index2]) return true; if(nums1[index1] < nums2[index2]) return false; } return index1 < nums1.length; } public int[] getMaxSubarray(int[] nums, int count){ int[] res = new int[count]; int len = 0; int numLength = nums.length; for(int i = 0 ; i 0 && len + numLength - i > count && res[len-1] < nums[i]){ len--; } if(len < count){ res[len++] = nums[i]; } } return res; }
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71010.html
摘要:算法复杂度思路贪心算法,先能组成的数的组合,然后针对每一个组合,考虑每一个数组能够组成的最大的位或者位数。对不同组合生成的最大数进行比较,得到所能得到的最大的值。代码的方法去找这个数。 LeetCode[321] Create Maximum Number Given two arrays of length m and n with digits 0-9 representing ...
摘要:题目意思从两个数组中,保持元素相对位置不变的前提下,找到一个长度为的最大数组。我们每次取两个数组中剩下的最靠前元素里较大的一个。合并之前结果,得到一个长度为的最大数组。三个不同的函数分别用于取,合并,比较。 Given two arrays of length m and n with digits 0-9 representing two numbers. Create the m...
摘要:题目链接这题就遍历所有可能的切分点然后和求到最大值,和分别是有个数时候的最大值,和有个数时的最大值。部分比较简单,来看求最大值的部分。设产生的最大值是,的是,的是。现在已经选了了个,最大值是,用了个数,现在指向。 321. Create Maximum Number 题目链接:https://leetcode.com/problems... 这题就遍历所有可能的切分点n然后mergen...
摘要:详细介绍将其他值转成数字值。此方法更改数组的长度。详细介绍解题思路首先,将传入的数字转换成字符串,并分割成数组。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-05-19 09:42:39 Recently revised in 2019-05-19 16:08:24 Hello 小伙伴们,如果觉得本文还不错,记得给个 star , 小伙伴们...
摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...
阅读 3608·2023-04-26 01:43
阅读 2996·2021-10-14 09:42
阅读 5518·2021-09-30 09:59
阅读 2193·2021-09-04 16:40
阅读 1228·2019-08-30 15:52
阅读 846·2019-08-29 17:09
阅读 2018·2019-08-26 13:37
阅读 3451·2019-08-26 10:20