-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
145 lines (140 loc) · 3.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* @lc app=leetcode id=321 lang=javascript
*
* [321] Create Maximum Number
*
* https://leetcode.com/problems/create-maximum-number/description/
*
* algorithms
* Hard (25.11%)
* Total Accepted: 29.1K
* Total Submissions: 115.5K
* Testcase Example: '[3,4,6,5]\n[9,1,2,5,8,3]\n5'
*
* 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]
*
* 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]
*
*/
/**
* 思路:
*
* 设dp[i][j] 为nums1取i个num,nums2取j个num的最优解,i + j = k;
*
* 我们再单独对nums1分析,如果我们要取i个数字要怎么取
* 如果i=== nums1.length 我们直接全取就行了
* 如果 i < nums1.length 我们就只能取一部分
* 这里有个子问题是我们要找到nums1的取n个,保持排序的时候的最大值
*
*
* @param {number[]} nums1
* @param {number[]} nums2
* @param {number} k
* @return {number[]}
*/
var maxNumber = function (nums1, nums2, k) {
function findMax(nums, k) {
if (k === nums.length) {
return nums.join('');
}
let res = '';
while (k) {
let num = Math.max.apply(null, nums.slice(0, nums.length - k + 1));
res += num;
nums = nums.slice(nums.indexOf(num) + 1);
k--;
}
return res;
}
function merge(nums1, nums2) {
let res = '';
let i = 0, j=0, l1 = nums1.length, l2 = nums2.length;
while (i < l1 && j < l2) {
if (nums1[i] > nums2[j]) {
res += nums1[i];
i++;
} else if (nums1[i] < nums2[j]) {
res += nums2[j];
j++;
} else {
if (nums1.slice(i) > nums2.slice(j)) {
res += nums1[i];
i++;
} else {
res += nums2[j];
j++;
}
}
}
while (i < l1) {
res += nums1[i];
i++;
}
while (j < l2) {
res += nums2[j];
j++;
}
return res;
}
let max = '';
let n = Math.min(nums1.length, k);
let i = 0;
while (i <= n) {
const j = k - i;
if (j > nums2.length) {
i++;
continue;
}
const curRes = merge(findMax(nums1, i), findMax(nums2, j));
max = curRes > max ? curRes : max;
i++;
}
return max.split('').map(x => parseInt(x));
}
// [9, 8, 6, 5, 3]
// console.log(maxNumber([3, 4, 6, 5],[9, 1, 2, 5, 8, 3], 5));
// [6, 7, 6, 0, 4]
// console.log(maxNumber([6, 7],[6, 0, 4], 5));
// // [9,7,5]
console.log(maxNumber([8, 6, 9],[1,7, 5], 3));
// [9,6,5,5,3,6,2]
// console.log(maxNumber([2,2,0,6,8,9,6],[5,2,4,5,3,6,2], 7));
module.exports = {
id:'321',
title:'Create Maximum Number',
url:'https://leetcode.com/problems/create-maximum-number/description/',
difficulty:'Hard',
}