Create Maximum Number
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.
Given nums1 = [3, 4, 6, 5]
, nums2 = [9, 1, 2, 5, 8, 3]
, k = 5
return [9, 8, 6, 5, 3]
Given nums1 = [6, 7]
, nums2 = [6, 0, 4]
, k = 5
return [6, 7, 6, 0, 4]
Given nums1 = [3, 9]
, nums2 = [8, 9]
, k = 3
return [9, 8, 9]
public class Solution {
/**
* @param nums1 an integer array of length m with digits 0-9
* @param nums2 an integer array of length n with digits 0-9
* @param k an integer and k <= m + n
* @return an integer array
*/
public int[] maxNumber(int[] nums1, int[] nums2, int k) {
// Write your code here
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;
} private int[] maxArray(int[] nums, int len){
int[] res = new int[len];
int j = 0;
int n = nums.length;
for(int i=0; i<n;i++){
while(j>0&&j+n-i>len&&res[j-1]<nums[i]){
j--;
}
if(j<len){
res[j] = nums[i];
j++;
}
}
return res;
} private int[] merge(int[] a, int[] b, int len){
int[] res = new int[len];
int len1 = a.length;
int len2 = b.length;
if(len1==0) return b;
if(len2==0) return a;
int i=0; int j=0;
int index=0;
while(i<len1||j<len2){
if(isGreater(a, i, b, j)){
res[index++]=a[i++];
}else{
res[index++]=b[j++];
}
}
while(i<len1){
res[index++]=a[i++];
}
while(j<len2){
res[index++]=b[j++];
}
return res;
} private boolean isGreater(int[] a, int posA, int[] b, int posB){
int len1 = a.length;
int len2 = b.length;
int i=posA; int j=posB;
while(i<len1&&j<len2&&a[i]==b[j]){
i++;
j++;
} if(i==len1) return false;
if(j==len2) return true;
return a[i]>b[j];
}
}
Create Maximum Number的更多相关文章
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 ...
- 321. Create Maximum Number 解题方法详解
321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...
- 321. Create Maximum Number
/* * 321. Create Maximum Number * 2016-7-6 by Mingyang */ public int[] maxNumber(int[] nums1, int[] ...
- 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 ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- Leetcode: Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [Swift]LeetCode321. 拼接最大数 | Create Maximum Number
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 321. Create Maximum Number (c++ ——> lexicographical_compare)
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
随机推荐
- 阿里云centos怎么用xshell5登陆
第一种是用ssh,安装Xshell5 打开XShell 新建会话输入ip 选择新建的会话,点击连接,选择接受并保护,输入root,点击确定 输入密码 已经连接成功了,用Xshell ...
- (转)AutoML for Data Augmentation
AutoML for Data Augmentation 2019-04-01 09:26:19 This blog is copied from: https://blog.insightdatas ...
- 【HNOI 2016】序列
Problem Description 给定长度为 \(n\) 的序列:\(a_1, a_2, \cdots , a_n\),记为 \(a[1 \colon n]\).类似地,\(a[l \colon ...
- java比较排序Comparable和Comparator
1 比较排序Comparable和Comparator 1.1 接口作用说明 Comparable和Comparator都是用来实现对象的比较.排序,对比时需要实现Compara ...
- 《R语言入门与实践》第六章:R 的环境系统
前言 这一章在对象的基础之上,讲解了对象所处的环境,进一步讲了环境对对象的作用,以及如何使用环境.结构如下: 环境的定义和操作 环境的规则 制作闭包 环境 R 环境的定义 在 R 中,每一个数据对象都 ...
- ggplot
安装:install.packages("ggplot2") 加载:library(ggplot2) Plot(图)= data(数据集)+ Aesthetics(美学映射)+ G ...
- 【转】RTP学习笔记
转自:https://www.cnblogs.com/yoyotl/p/5650101.html 一.定义 实时传输协议(Real- time Transport Protocol,RTP)是在Int ...
- 操纵Review被封店,申诉信
标签: 测评被封 亚马逊申诉 操纵评价申诉 分类: 亚马逊申诉模板 Hello,We recently contacted you about product review manipulation. ...
- python 读取excel文件
方法一:利用pandas import pandas as pd inputfile_1 = "F:\\大论文实验\\福贡县数据\\贫困人口数据_2015.xlsx" data1 ...
- k8s搭建rook-ceph
一.介绍 Rook官网:https://rook.io Rook是云原生计算基金会(CNCF)的孵化级项目. Rook是Kubernetes的开源云本地存储协调器,为各种存储解决方案提供平台,框架和支 ...