当前位置:网站首页>LeetCode Daily Question (321. Create Maximum Number)
LeetCode Daily Question (321. Create Maximum Number)
2022-08-09 22:07:00 【wangjun861205】
You are given two integer arrays nums1 and nums2 of lengths m and n respectively. nums1 and nums2 represent the digits of two numbers. You are also given an integer k.
Create the maximum number of length k <= m + n from digits of the two numbers. The relative order of the digits from the same array must be preserved.
Return an array of the k digits representing the answer.
Example 1:
Input: nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
Output: [9,8,6,5,3]
Example 2:
Input: nums1 = [6,7], nums2 = [6,0,4], k = 5
Output: [6,7,6,0,4]
Example 3:
Input: nums1 = [3,9], nums2 = [8,9], k = 3
Output: [9,8,9]
Constraints:
- m == nums1.length
- n == nums2.length
- 1 <= m, n <= 500
- 0 <= nums1[i], nums2[i] <= 9
- 1 <= k <= m + n
对于任意一个数组 nums, any fixed length length Must correspond to a unique largest array, So go down this way, Suppose the final array length to be obtained is k, That final array must be max_array(nums1, length)与 max_array(nums2, k-length)组合而成的, So we just need to iterate through these cases and put max_array1 与 max_array2 Combined into a maximum length of k 的数组就可以了.
impl Solution {
fn max_with_fixed_length(mut nums: Vec<i32>, length: usize) -> Vec<i32> {
let mut stack = Vec::new();
'outer: while stack.len() + nums.len() > length && nums.len() > 0 {
if stack.is_empty() {
stack.push(nums.remove(0));
continue;
}
let curr = nums.remove(0);
while let Some(last) = stack.pop() {
if last >= curr {
stack.push(last);
stack.push(curr);
continue 'outer;
}
if stack.len() + nums.len() + 1 == length {
break;
}
}
stack.push(curr);
}
stack.append(&mut nums);
if stack.len() > length {
stack = stack[..length].to_vec();
}
stack
}
fn is_greater(mut nums1: Vec<i32>, mut nums2: Vec<i32>) -> bool {
while nums1.len() < nums2.len() {
nums1.push(0);
}
while nums1.len() > nums2.len() {
nums2.push(0);
}
for (n1, n2) in nums1.into_iter().zip(nums2.into_iter()) {
if n1 == n2 {
continue;
}
if n1 > n2 {
return true;
}
return false;
}
false
}
fn merge(nums1: &[i32], nums2: &[i32]) -> Vec<i32> {
let mut res = Vec::new();
let mut i = 0;
let mut j = 0;
while i < nums1.len() && j < nums2.len() {
if nums1[i] == nums2[j] {
if Solution::is_greater(nums1[i + 1..].to_vec(), nums2[j + 1..].to_vec()) {
res.push(nums1[i]);
i += 1;
continue;
}
res.push(nums2[j]);
j += 1;
continue;
}
if nums1[i] > nums2[j] {
res.push(nums1[i]);
i += 1;
continue;
}
res.push(nums2[j]);
j += 1;
}
if i < nums1.len() {
res.append(&mut nums1[i..].to_vec());
}
if j < nums2.len() {
res.append(&mut nums2[j..].to_vec());
}
res
}
pub fn max_number(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> Vec<i32> {
let mut ans = vec![0; k as usize];
for i in 1..=(k as usize).min(nums1.len()) {
if k as usize - i <= nums2.len() {
let max1 = Solution::max_with_fixed_length(nums1.clone(), i);
let max2 = Solution::max_with_fixed_length(nums2.clone(), k as usize - i);
let curr = Solution::merge(&max1, &max2);
if Solution::is_greater(curr.clone(), ans.clone()) {
ans = curr;
}
}
}
ans
}
}
边栏推荐
- 大健康产业商业供应链管理系统数字化提升产业链运作效率推动供应链标准化建设
- 奥特曼卡牌隐藏的百亿市场
- 分数阶混沌系统李雅普指数和分岔图
- 小满nestjs(第六章 nestjs cli 常用命令)
- Toronto Research Chemicals盐酸乙环胺应用说明
- Toronto Research Chemicals加米霉素-d4说明书
- ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
- What to do if Windows 11 can't find Internet Explorer
- 继承关系下构造方法的访问特点
- 【图文并茂】如何进行Win7系统的重装
猜你喜欢

visual studio 2022调试技巧介绍

Acrel5000web能耗系统在某学院的应用-Susie 周

Toronto Research Chemicals单羟基舒更葡糖钠说明书
![[] free column Android dynamic debugging GDB APP of safety](/img/e3/fd096ec64f682348cca9bbab1ec5bb.png)
[] free column Android dynamic debugging GDB APP of safety

2.3 监督学习-2

小满nestjs(第三章 前置知识装饰器)
![[] free column Android run Android, her - as command of safety](/img/d5/771802eb57f24c1cf88657f5c5a724.png)
[] free column Android run Android, her - as command of safety
![[Free column] APK dynamic reverse application of Android security [Three Smali injection methods]](/img/11/39a25d86c9486bb5201659bbbeaa36.png)
[Free column] APK dynamic reverse application of Android security [Three Smali injection methods]

matlab 神经网络 ANN 分类

华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款
随机推荐
An overall security understanding and method of cyberspace based on connection and security entropy
Queue topic: Implementing stacks with queues
source install/setup.bash时出现错误
【深度学习】pix2pix GAN理论及代码实现
Oracle 字段自增
MySQL Notes-06 Basic SQL Operations
hdu 2094 产生冠军(STL map || 拓扑 || STL set)
Transformer如何用于3D视觉?阿联酋MBZUAI最新《3D视觉Transformers处理》综述,涵盖100+种方法
嵌入式开发:使用FILL提高代码完整性
【IoT毕设】STM32与机智云自助开发平台的宠物智能喂养系统
数学建模代码速成~赛前一个月~matlab~代码模板~吐血总结~三大模型代码(预测模型、优化模型、评价模型)
39. 组合总和 && 40. 组合总和2 && 216. 组合总和3
SqlServer 2016 备份和还原
2.3 监督学习-2
Beat the interviewer, the CURD system can also make technical content
基于SSM实现手机销售商城系统
力扣 899. 有序队列
pytest框架之mark标记功能详细介绍
韩国网络安全体系特征与发展前景
competed中访问ref为undefined