Java实现 LeetCode 321 拼接最大数
321. 拼接最大数
给定长度分别为 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]
class Solution {
/*
假设数组一为[3,4,6,5]、数组二为[9,1,2,5,8,3]、k = 5;
组合情况有0 + 5、1 + 4、2 + 3、3 + 2、4 + 1五种情况,就是从此五种情况取出组合最大的一种;
Math.max(0, k - n)表示若数组二的元素个数 >= k,则数组一的元素个数可以从0开始取,否则在数组二的大小基础上补.
*/
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
int m = nums1.length, n = nums2.length;
int[] res = new int[k];
for (int i = Math.max(0, k - n); i <= k && i <= m; i++) {
int[] arr = merge(maxArr(nums1, i), maxArr(nums2, k - i), k);
if (gt(arr, 0, res, 0)) res = arr;
}
return res;
}
/*
假设选择了2 + 3的情况,分别从两个数组取出相应元素个数的最大组合,对数组一来说就是[6,5],对数组二来说是[9,8,3];
n - i : 当前数组中,当前下标到结尾还有多少个元素;
j : 当前数组中i之前有多少个数加入到最大组合中;
n - i + j > k <=> n - i - 1 + j >= k : 当前下标的元素大于最大组合的末尾元素,就需要弹出,弹出后的元素减少,故j--,
n - i(数组剩余元素) - 1(去掉最大组合末尾元素) + j(最大组合中剩余元素)时刻保持 >= k;
if j < k : 先将最大组合填满再进行比较替换操作
*/
private int[] maxArr(int[] nums, int k) {
int n = nums.length;
int[] res = new int[k];
for (int i = 0, j = 0; i < n; i++) {
while (n - i + j > k && j > 0 && nums[i] > res[j-1]) j--;
if (j < k) res[j++] = nums[i];
}
return res;
}
/*
假设数组一最大组合为[6,5],数组二最大组合为[9,8,3],进行双指针排序,排序后为[9,8,6,5,3]
*/
private int[] merge(int[] nums1, int[] nums2, int k) {
int[] res = new int[k];
for (int i = 0, j = 0, r = 0; r < k; r++)
res[r] = gt(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
return res;
}
/*
比较两数组相应位置大小,相等就一直跳过,直到不相等就比较.
*/
private boolean gt(int[] nums1, int i, int[] nums2, int j) {
while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
i++;
j++;
}
return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
}
}
Java实现 LeetCode 321 拼接最大数的更多相关文章
- Leetcode 321.拼接最大数
拼接最大数 给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个 ...
- leetcode 321. 拼接最大数(单调栈,分治,贪心)
题目链接 https://leetcode-cn.com/problems/create-maximum-number/ 思路: 心都写碎了.... 也许就是不适合吧.... 你是个好人... cla ...
- Java for LeetCode 060 Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- python语法学习第十天--类与对象相关的BIF、魔法方法
一些相关的BIF: issubclass(class,classInfo)#判断是否为子类,classInfo可以为多个类的元组,其中一个是,返回true,一个类也被认为是自己的子类,object是所 ...
- python 基础知识3-列表元祖
1.列表增. # append 追加,给列表的最后面追加一个元素 li = ['alex','wufa','太白','大白'] li.append('教师') li.append(1) print(l ...
- linux-设置代理和取消代理
设置代理: export http_proxy="http://proxy-XXXXX" export https_proxy="https://proxy-XXXXX: ...
- python--正则表达式|re模块学习
学习来源:https://www.liaoxuefeng.com/wiki/1016959663602400/1017639890281664 正则表达式是一种用来匹配字符串的一种强大的武器,用一种描 ...
- 我的linux学习日记day7
一.文件权限: r:read 读取文件列表的权限, 数字4表示 w:write 写入.删除.修改的权限,数字2表示 x:execute 进入目录的权限,数字1表示 权限分配:文件所有人.文件所属主.其 ...
- Postman学习宝典(三)
Postman 入门3 - Newman Newman 官方帮助文档地址 Newman 安装 嗯,它需要安装,因为它不是音乐播放器!Newman是为Postman而生,专门用来运行Postman编写好 ...
- 笨办法学习python-ex51自我理解笔记
本章节主要讲的是web的工作原理,先大概熟悉记录一下,为以后写Django web框架打下基础. web工作原理: 1.用户从浏览器输入网址----->browser通过电脑中的网络设备(网卡) ...
- Java并发编程实战 05等待-通知机制和活跃性问题
Java并发编程系列 Java并发编程实战 01并发编程的Bug源头 Java并发编程实战 02Java如何解决可见性和有序性问题 Java并发编程实战 03互斥锁 解决原子性问题 Java并发编程实 ...
- Python list列表修改元素
Python 提供了两种修改列表(list)元素的方法,你可以每次修改单个元素,也可以每次修改一组元素(多个). 修改单个元素 修改单个元素非常简单,直接对元素赋值即可.请看下面的例子: nums = ...
- python字符串str
字符串str常用操作方法(都会产生新的数据) 1.取值: (1)索引:s[0] (2)切片:s[起始索引:结束索引:步长] 起始索引为0,可以省略 s最后一个索引可以取-1 结束索引省略,默认取到最后 ...