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 ...
随机推荐
- properties文件导出
功能要求根据数据库记录的key-value-remark 数据,导出保存properties文件 1. pro.load() pro.list() 处理不能解决备注.排序问题 2. 最后考虑下什么是 ...
- [hdu5215]无向图找奇偶环
题意:如标题 思路:对于奇环,一个二分图判定就ok了,有奇环<=>非二分图.对于偶环,考虑环必定出现在双联通分量里面,可以先求出图的双联通分量,对于一个双联通分量,对于双联通分量里面的每个 ...
- netty零基础入门
直接上代码,从最基本的接收消息规则开始 package cn.qdl; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelH ...
- Luogu P5603 小C与桌游【贪心+拓扑排序】
[Description]https://www.luogu.com.cn/problem/P5603 \(\;\) 题意可以简化为:一个不保证联通,n个点,m条边的DAG(有向无环图),构造一个拓扑 ...
- Python脚本:linux上将筛选的文件夹复制到另一个目录,保存目录结构以及文件和文件夹操作方法
import os,shutil def newDir(dir_path): if not os.path.exists(dir_path): os.makedirs(dir_path) def co ...
- Android 编译系统
1,Makefile编译方式 TARGET: PREREQUISITES COMMANDS 1,TARGET是需要生成的目标文件,PREREQUISTIES代表了目标所依赖的所有文件. 2,简单的Ma ...
- java ->IO流_字符流
字符流 经过前面的学习,我们基本掌握的文件的读写操作,在操作过程中字节流可以操作所有数据,可是当我们操作的文件中有中文字符,并且需要对中文字符做出处理时怎么办呢? 字节流读取字符的问题 通过以下程序读 ...
- 浅析常见的 Web 安全预防
1. CSRF 跨站请求伪造,英文名:Cross-site request forgery CSRF 攻击流程 结合下面这张图说明 CSRF 的攻击流程. 李四在网站 A 注册了用户名,并且登录到了网 ...
- Django模板之模板变量过滤器
在Django的模板语言中,通过使用 过滤器 来改变变量的显示:Django的模板语言中提供了大约六十个内置过滤器. 过滤器规则: · 过滤器的语法: {{ value|filter_ ...
- k8s搭建实操记录干货二(node)
#注:172.16.110.111为master,172.16.110.112\114为node1\node2(kubeadm join部分要等master完成后手工操作,其它可执行本脚本一键安装) ...