-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0321-create-maximum-number.js
More file actions
122 lines (106 loc) · 3.19 KB
/
0321-create-maximum-number.js
File metadata and controls
122 lines (106 loc) · 3.19 KB
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
/**
* Create Maximum Number
* Time Complexity: O((M + N) * K^2)
* Space Complexity: O((M + N) * K)
*/
var maxNumber = function (nums1, nums2, k) {
const lengthOfNums1 = nums1.length;
const lengthOfNums2 = nums2.length;
let maxResultArray;
if (k === 0) {
return [];
}
maxResultArray = new Array(k).fill(0);
const compareLexicographically = (firstArray, secondArray) => {
let pointerAlpha = 0;
let pointerBeta = 0;
const limitAlpha = firstArray.length;
const limitBeta = secondArray.length;
while (pointerAlpha < limitAlpha && pointerBeta < limitBeta) {
if (firstArray[pointerAlpha] > secondArray[pointerBeta]) {
return 1;
}
if (firstArray[pointerAlpha] < secondArray[pointerBeta]) {
return -1;
}
pointerAlpha++;
pointerBeta++;
}
if (pointerAlpha < limitAlpha) return 1;
if (pointerBeta < limitBeta) return -1;
return 0;
};
const extractMaxSubsequence = (inputDigitsArray, desiredLength) => {
const sequenceStack = [];
let dropsAllowed = inputDigitsArray.length - desiredLength;
let currentInputIdx = 0;
while (currentInputIdx < inputDigitsArray.length) {
const digitVal = inputDigitsArray[currentInputIdx];
while (
sequenceStack.length > 0 &&
sequenceStack[sequenceStack.length - 1] < digitVal &&
dropsAllowed > 0
) {
sequenceStack.pop();
dropsAllowed--;
}
sequenceStack.push(digitVal);
currentInputIdx++;
}
return sequenceStack.slice(0, desiredLength);
};
const mergeSubsequences = (firstSubsequence, secondSubsequence) => {
const mergedOutput = [];
let indexA = 0;
let indexB = 0;
const lenA = firstSubsequence.length;
const lenB = secondSubsequence.length;
while (indexA < lenA || indexB < lenB) {
let sourceToPickFrom;
if (indexA < lenA && indexB < lenB) {
sourceToPickFrom =
compareLexicographically(
firstSubsequence.slice(indexA),
secondSubsequence.slice(indexB),
) >= 0
? firstSubsequence
: secondSubsequence;
} else if (indexA < lenA) {
sourceToPickFrom = firstSubsequence;
} else {
sourceToPickFrom = secondSubsequence;
}
if (sourceToPickFrom === firstSubsequence) {
mergedOutput.push(firstSubsequence[indexA]);
indexA++;
} else {
mergedOutput.push(secondSubsequence[indexB]);
indexB++;
}
}
return mergedOutput;
};
for (
let subsequenceLength1 = Math.max(0, k - lengthOfNums2);
subsequenceLength1 <= Math.min(k, lengthOfNums1);
subsequenceLength1++
) {
const subsequenceLength2 = k - subsequenceLength1;
const firstSubsequenceFound = extractMaxSubsequence(
nums1,
subsequenceLength1,
);
const secondSubsequenceFound = extractMaxSubsequence(
nums2,
subsequenceLength2,
);
const currentCombinedResult = mergeSubsequences(
firstSubsequenceFound,
secondSubsequenceFound,
);
if (compareLexicographically(currentCombinedResult, maxResultArray) > 0) {
maxResultArray = currentCombinedResult;
}
}
return maxResultArray;
};