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 ...
随机推荐
- 浅谈RESTful
浅谈RESTful 什么是RESTful? REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Ro ...
- mysql配置外部允许外部连接
1. 登录到mysql mysql -u root -p 2.进入到mysql 库中 use mysql 3.执行语句 update user set host=‘%’ where user=‘roo ...
- Bigger-Mai 养成计划,subprocess模块
subprocess模块是python从2.4版本开始引入的模块.主要用来取代 一些旧的模块方法,如os.system.os.spawn*.os.popen*.commands.*等.subproce ...
- mybatis逆向工程失败
[ERROR] Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.6:generate ( ...
- 近期wxss总结
最近有一些需要点击才能实现的样式切换,我用的方法有2种 1 wxml 中 对class给一个判断式 class="变量?变化后的类:变化前的类" 这样在js中设一个变量,我是设成布 ...
- react项目中页面跳转、刷新及获取网络状态
// 页面跳转 window.location.href = 'http://speedtest.wangxiaotong.com/' // 页面刷新 window.location.reload() ...
- react初探(一)之JSX、状态(state)管理、条件渲染、事件处理
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技术栈为react,目前我的情况是三大框架只会angular和Vue.在实际项目中只使用过一次angular5,其余项目都是使用Vue写的.写篇 ...
- 【转】 HDMI介绍与流程
转自:https://www.cnblogs.com/TaigaCon/p/3840653.html HDMI,全称为(High Definition Multimedia Interface)高清多 ...
- Linux压缩与解压缩文件
1 将tgz文件解压到指定目录. tar zxvf test.tgz -C 指定目录 比如:将 test.tgz 解压到 /home目录:tar zxvf test.tgz -C /home 2 将指 ...
- Java源码阅读顺序
阅读顺序参考链接:https://blog.csdn.net/qq_21033663/article/details/79571506 阅读源码:JDK 8 计划阅读的package: 1.java. ...