Create Maximum Number

本文介绍了一种算法,用于从两个数字数组中选取k个数字组成最大可能的数字,同时保持每个数组内数字的相对顺序不变。通过解决两个子问题实现目标:一是从单一数组中选择部分数字构成最大子数组;二是将两个数组合并成一个最大化的数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Example

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]

分析:http://bookshadow.com/weblog/2015/12/24/leetcode-create-maximum-number/

问题可以转化为这样的两个子问题:

1. 从数组nums中挑选出t个数,在保持元素相对顺序不变的情况下,使得选出的子数组最大化。

2. 在保持元素相对顺序不变的前提下,将数组nums1与数组nums2合并,使合并后的数组最大化。

枚举nums1子数组与nums2子数组的长度len1, len2,在满足长度之和len1+len2等于k的前提下,分别求解最大子数组,并进行合并。

然后从合并得到的子数组中取最大数组即为所求。

 1 public class Solution {
 2     public int[] maxNumber(int[] nums1, int[] nums2, int k) {
 3         int n = nums1.length, m = nums2.length;
 4         int[] ans = new int[k];
 5         for (int i = Math.max(0, k - m); i <= Math.min(n, k); ++i) {
 6             int[] candidate = merge(maxArray(nums1, i), maxArray(nums2, k - i), k);
 7             if (isGreater(candidate, 0, ans, 0)) ans = candidate;
 8         }
 9         return ans;
10     }
11 
12     private int[] merge(int[] nums1, int[] nums2, int k) {
13         int[] ans = new int[k];
14         for (int i = 0, j = 0, r = 0; r < k; ++r) {
15             ans[r] = isGreater(nums1, i, nums2, j) ? nums1[i++] : nums2[j++];
16         }
17         return ans;
18     }
19     
20     public boolean isGreater(int[] nums1, int i, int[] nums2, int j) {
21         while (i < nums1.length && j < nums2.length && nums1[i] == nums2[j]) {
22             i++;
23             j++;
24         }
25         return j == nums2.length || (i < nums1.length && nums1[i] > nums2[j]);
26     }
27     
28     public int[] maxArray(int[] arr, int k) {
29         int p = -1;
30         int[] nums = new int[k];
31         if (k == 0) return nums;
32 
33         for (int i = 0; i < arr.length; i++) {
34             while (p >= 0 && nums[p] < arr[i] && (arr.length - i + p + 1) > k) {
35                 p--;
36             }
37             if (p < nums.length - 1) {
38                 p++;
39                 nums[p] = arr[i];
40             }
41         }
42         return nums;
43     }
44 }

 

转载于:https://www.cnblogs.com/beiyeqingteng/p/5693760.html

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值