Create Maximum Number

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

1. When divide k into two parts, it could be 0 for the any part. So i <= nums1.length.

时间: 2024-10-11 13:19:28

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 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 digit

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 + nfrom digits of the two. The relative order of the digits from the same array must be preserved. Return an array of the k digits

321. Create Maximum Number (c++ ——&gt; lexicographical_compare)

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 digit

leetcode_321 Create Maximum Number

题目分析: 给定两个长度分别为m和n的数组,数组元素为0-9,每个数组元素代表一个数字.从这两个数组中选出一些数字,组成一个数组,是这个数组中的数尽可能大,其长度k <= m + n.要求数组中选出的元素的相对顺序与原数组保持一致.最终返回一个包含k个数字的数组. 解题思路: 1)分别从nums1(长度为m)和nums2(长度为n)中挑选出i(max(0, k - n) <= i <= min(m, k) 和k-i个数,在保持挑选数组的元素相对顺序不变的情况下,使选出的子数组最大化,主要

The maximum number of cell styles was exceeded. You can define up to 4000 styles

POI操作Excel中,导出的数据不是很大时,则不会有问题,而数据很多或者比较多时, 就会报以下的错误,是由于cell styles太多create造成,故一般可以把cellstyle设置放到循环外面 报错如下: Caused by: java.lang.IllegalStateException: The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook

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 <= 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 digit

POJ2699 The Maximum Number of Strong Kings

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tournament can be represented by a complete graph in which each vertex denotes a player and a directed edge is from vertex x to vertex y if player x beats

Maximum number of WAL files in the pg_xlog directory (1)

Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue with our customers' servers who have a wal_keep_segments setting higher than 0. We have a monitoring script that checks the number of WAL files in the

414. Third Maximum Number

Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n). 给定一个非空数组的整数,返回该数组中的第三个最大数. 如果不存在,返回最大数量. 时间复杂度必须在O(n)中. Example 1: Input: [3, 2,