leetcode 321. Create Maximum Number

uniapp APP分享;判断用户是否安装APP,已安装直接打开,未安装跳转下载页;uniapp 在外部打开APP(schemes) A将某商品分享给B,B点击后判断是否安装APP,若安装直接打开,没有安装则跳转下载页; 阅读详情

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.

Note: You should try to optimize your time and space complexity.

Example 1:

Input:
nums1 = [3, 4, 6, 5]
nums2 = [9, 1, 2, 5, 8, 3]
k = 5
Output:
[9, 8, 6, 5, 3]

给出两个数组,给出一个k,让输出一个长度为k的数组,元素从nums1,nums2中取,而且要保持在nums1和nums2中的顺序,使输出数组表示的整数最大

思路:
参照某大神的方法,把该问题分解成若干小问题。

(1) 从一个数组中取k个元素,使构成的整数最大
假如从nums2=[9,1,2,5,8,3]中取3个数字,且保持顺序,那么要取[9,8,3]
如何取:
k=3,数组中有6个元素,所以可以扔掉3个,定义为pop_num = 3
当pop_num > 0的时候,证明当前元素是可以扔掉的
比如已经保存了[9,1] 这时候来了2,比1大,所以扔掉1,pop_num = 2
又来了5,比2大,因为pop_num = 2 > 0, 再扔掉2,保存5
当pop_num = 0的时候就说明不能再扔了,后面所有都要保存。
(因为要保持顺序,所以不能说把大的保存在小的前面)

    public int[] maxNumber(int[] nums, int k) {
        if (k == 0) {
            return (new int[] {});
        }
        
        int pop_num = nums.length - k;
        int[] res = new int[nums.length];
        int index = -1;
        
        for (int i = 0; i < nums.length; i++) {
            while (index >= 0 && nums[i] > res[index] && pop_num > 0) {
                index --;
                pop_num --;
            }
            index ++;
            res[index] = nums[i];
        }
        
        
        return Arrays.copyOfRange(res, 0, k);
    }

(2)两个数组的元素拼成一个最大的数组
利用(1),假如已经从nums1中取得了[6, 5], nums2中取得了[9, 8, 3]
如何把它们拼成一个代表最大整数的数组:
这时候比较数组,不是那种先看长度的比较,而是顺序看元素大小,
一旦出现了nums1[i] < nums2[i], 就说明nums1 < nums2, 当前面所有元素一样时,再看谁更长
c++可以直接调用lexicographical_compare方法,java的话参照c++源码实现了一下lexicographical_compare方法

    //if nums1 < nums2, return true..
    public boolean compareArray(int[] nums1, int[] nums2, int first1, int last1, int first2, int last2) {
    	
    	while (first1 != last1) {
    		if (first2 == last2 || nums1[first1] > nums2[first2]) { //array1.len > array2.len => array1 > array2..
    			return false;
    		} else if (nums1[first1] < nums2[first2]) {
    			return true;
    		}
    		first1 ++;
    		first2 ++;
    	}
    
    	return (first2 != last2);
    }
    //return larger array..
    public int[] maxArray(int[] arr1, int[] arr2) {
    	if (compareArray(arr1, arr2, 0, arr1.length, 0, arr2.length)) {
    		return arr2;
    	} else {
    		return arr1;
    	}
    }

可以看出[6, 5] < [9, 8 ,3], 所以从[9,8,3]中取头一个元素9,
然后[6,5] < [8,3], 再取8
[6,5] > [3], 取6,依次类推

    public int[] maxNumber(int[] nums1, int[] nums2) {
    	int[] res = new int[nums1.length + nums2.length];
    	int index = 0;
    	
    	int first1 = 0;
    	int first2 = 0;
    	int last1 = nums1.length;
    	int last2 = nums2.length;
    	
    	while (first1 != last1 || first2 != last2) {
    		res[index] = compareArray(nums1, nums2, first1, last1, first2, last2) ? 
    		nums2[first2++] : nums1[first1++];
    		index ++;
    	}
    	
    	return res;
    }

当以上两个子问题解决之后,就可以解决这个取元素的问题了
假如在nums1中取i个元素,那么就要在nums2中取k-i个元素
就用到了

arr1 = maxNumber(nums1, i)
arr2 = maxNumber(nums2, k - i)

再用这两个得到的子数组拼成一个长度为k,且代表的整数最大的数组

arr = maxNumber(arr1, arr2)

每次都取较大的那个数组

res = max(res, arr)

完整版

//10ms
class CreateMaximumNumber {
    public int[] maxNumber(int[] nums1, int[] nums2, int k) {
    	if (nums1 == null || nums1.length == 0) {
    		return Arrays.copyOfRange(nums2, 0, k);
    	}
    	
    	if (nums2 == null || nums2.length == 0) {
    		return Arrays.copyOfRange(nums1, 0, k);
    	}
    	
    	int[] res = new int[] {};
    	
    	for (int i = 0; i <= k; i ++) {
    		int j = k - i;
    		if (i > nums1.length || j > nums2.length) {
    			continue;
    		}
    		
    		res = maxArray(res, maxNumber(maxNumber(nums1, i), maxNumber(nums2, j)));
    	}    	
    	
        return res;        
    }
    
    private int[] maxNumber(int[] nums1, int[] nums2) {
    	int[] res = new int[nums1.length + nums2.length];
    	int index = 0;
    	
    	int first1 = 0;
    	int first2 = 0;
    	int last1 = nums1.length;
    	int last2 = nums2.length;
    	
    	while (first1 != last1 || first2 != last2) {
    		res[index] = compareArray(nums1, nums2, first1, last1, first2, last2) ? 
    				nums2[first2++] : nums1[first1++];
    		index ++;
    	}
    	
    	return res;
    }
    private int[] maxNumber(int[] nums, int k) {
        if (k == 0) {
            return (new int[] {});
        }
        
        int pop_num = nums.length - k;
        int[] res = new int[nums.length];
        int index = -1;
        
        for (int i = 0; i < nums.length; i++) {
            while (index >= 0 && nums[i] > res[index] && pop_num > 0) {
                index --;
                pop_num --;
            }
            index ++;
            res[index] = nums[i];
        }
        
        
        return Arrays.copyOfRange(res, 0, k);
    }
    
    //if nums1 < nums2, return true..
    private boolean compareArray(int[] nums1, int[] nums2, int first1, int last1, int first2, int last2) {
    	
    	while (first1 != last1) {
    		if (first2 == last2 || nums1[first1] > nums2[first2]) { //array1.len > array2.len => array1 > array2..
    			return false;
    		} else if (nums1[first1] < nums2[first2]) {
    			return true;
    		}
    		first1 ++;
    		first2 ++;
    	}
    
    	return (first2 != last2);
    }
    
    private int[] maxArray(int[] arr1, int[] arr2) {
    	if (compareArray(arr1, arr2, 0, arr1.length, 0, arr2.length)) {
    		return arr2;
    	} else {
    		return arr1;
    	}
    }
}
蓝桥杯STM32 G431 hal库开发速成——GPIO输入——按键长短按,按键单双击 将TIM与GPIO输入(按键)结合起来使用,很好的练习项目。按键长短按,按键单双击。STM32定时器常见问题 阅读详情

相关推荐

MIKE11实战:平原河网水动力建模从入门到精通(附GIS数据处理技巧)

本文系统介绍了MIKE11软件在平原河网水动力建模中的完整实战流程。从GIS数据预处理、河网构建、参数率定到结果分析,详细阐述了构建可靠数字孪生模型的关键步骤与技巧,旨在帮助水利工程师高效解决平原地区复杂水流模拟的挑战,为防洪决策提供科学依据。

weixin_28419039的博客 704

Leetcode题解:用字符串快速解决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 &lt;= m + n from digits of the two. The relative order of the digits from the ...

羊城迷鹿的博客 489

ADRV9009学习资料保存:基于XC7Z100+ADRV9009的双收双发无线电射频板卡

ADRV9009学习资料保存:基于XC7Z100+ADRV9009的双收双发无线电射频板卡 一、板卡概述 基于XC7Z100+ADRV9009的双收双发无线电射频板卡是基于XilinxZYNQFPGA和ADI的无线收发芯片ADRV9009开发的专用功能板卡,用于5G小基站,无线图传,数据收发...

9429

LeetCode #321: Create Maximum Number

Problem Statement(Source) 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 d

浮生琴弦 429

抖音最新风控体系研究

小编从技术角度对抖音的风控史和落地目标,跟大家聊一聊抖音,包括海外版抖音TikTok的现状与生态。 1、非常非常早的时期,抖音刚出来的时候,没有任何封禁,这个阶段,导入一个开发库,加上一个请求头,咵咵咵的数据随便拿。 2、2018-2019年,抖音加入了设备id和xg机制,在绝大多数的接口中,携带了设备id参数,同时在请求头中加入了xg,如今的apk市场,能追溯到的最早xg是0200打头的,而小编见过的最早的xg是0000打头的。这个时期的接口,这些参数都还只是预设,没有实际的用处,设备随便写,

qq517348412的博客 1万+

使用ST-LINK烧录stm32教程说明

使用ST-LINK烧录stm32

qq_34426854的博客 1万+

321-创造最大的数

Description Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k &lt;= m + n from digits of the two. The relative order of the digits fro...

fighting! 587

过程挖掘(Process Mining)1——始于颜值

下周开始每周1-2篇的过程挖掘分享 过程挖掘是什么? 过程挖掘与过程管理的关系? 过程挖掘与数据挖掘的关系? 管理学还是计算机科学?

hyhy12580的博客 6243

321. Create Maximum Number 找到两个数组中能合并的k个的最大数

Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k  from digits of the two. The relative order of the digits from the same array mus

u010005161的博客 1239

(UE5 5.2)HISM Mobile DrawInstance在渲染层的实现浅分析

在 (UE4 4.27) UHierarchicalInstancedStaticMesh(HISM)原理分析 这篇博客大致介绍HISM组件从游戏线程到渲染线程的重建KD-Tree和剔除并提交DrawCall逻辑,但是没有分析渲染层的大致数据结构和实现. 可以看出FHierarchicalStaticMeshSceneProxy 实际存储Instance数组数据来自于FInstancedStaticMeshSceneProxy FHierarchicalStaticMeshSceneProxy只是以FClu

qq_29523119的博客 2734

Ta-Lib源码解析(三):蜡烛图指标 (Candlestick Indicator) #(附Python重构代码和示意图)(补充中)

蜡烛图指标为识别K线形态的指标,笔者认为重点在于定义方式所提供的思路,背后的投资逻辑可以借鉴但不建议直接参考文档的翻译内容和源码逻辑存在不一致的现象,笔者分别列出中文百科中的定义和源码实际逻辑,供大家参考技术分析理论中的一些模糊词,ta-lib中给出了精确的定义源码

量化就是探索生活 3290

321. Create Maximum Number 解题方法详解

321. 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 orde...

weixin_33830216的博客 278

K8s(二十一):在 Kubernetes 集群中部署 MySQL8.0 高可用集群(1主2从)

🔴 K8s(二十一):在 Kubernetes 集群中部署 MySQL8.0 高可用集群(1主2从)

✨ 欢迎来到【Seal ^_^ 的CSDN博客】!✨ 3万+

[LeetCode]321. Create Maximum Number

参见Github. 321. Create Maximum Number

正西风落叶下长安 548

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[leetcode] 321. Create Maximum Number 解题报告

题目链接: https://leetcode.com/problems/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  from digits of t

小榕流光的专栏 4236

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

[Leetcode] 321.Create Maximum Number

[Leetcode] 321.Create Maximum Number[Description]321. Create Maximum Number[Analysis]To create max number of length k from two arrays, you need to create max number of length i from array one and max n

stary_yan的博客 375

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【LeetCode】321.Create Maximum Number(Hard)解题报告

【LeetCode】321.Create Maximum Number(Hard)解题报告 题目地址:https://leetcode.com/problems/create-maximum-number/description/ 题目描述:   Given two arrays of length m and n with digits 0-9 representing two nu...

郝春雨的博客 697

LeetCode321 Create Maximum Number

Given two arrays of lengthmandnwith digits0-9representing two numbers. Create the maximum number of lengthk <= m + nfrom digits of the two. The relative order of the digits from the same ar...

李歇特冯-兹拜因巴哈的博客 192

[leetcode] 321. Create Maximum Number

Description Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k &lt;= m + n from digits of the two. The relative order of the digits from...

Learning from the mistakes 625

LeetCode 321. Create Maximum Number

Description Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum number of length k &lt;= m + n from digits of the two. The rel...

weixin_34233421的博客 144

LeetCode: 321. Create Maximum Number

LeetCode: 321. 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 rel...

杨领well的专栏 300

Leetcode 321. Create Maximum Number

这道题的步骤如下: 1.将k拆分成两个数i,k-i,分别表示从nums1,nums2中取出的数个数 2.从nums1, nums2中取出相应个数的最大的数 3.将这两个最大的数merge成一个数 4.遍历所有的k拆分情况,得到最终Maximum Number 对于步骤2,如何从一个数组中取出给定个数(记作M)的最大数,需要用递减栈的思想: 初始化stack, 遍历数组中的数nums...

I_ren的博客 238

【LeetCode 321】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 sa...

iCode_girl的博客 176
上一篇: leetcode 316. Remove Duplicate Letters(删除重复的字母)
下一篇: leetcode 330. Patching Array (greedy 含推导)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址