Skip to content

Commit ceaaee1

Browse files
committed
[LeetCode Sync] Runtime - 99 ms (94.66%), Memory - 18 MB (60.36%)
1 parent b35ac90 commit ceaaee1

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of lengths <code>m</code> and <code>n</code> respectively. <code>nums1</code> and <code>nums2</code> represent the digits of two numbers. You are also given an integer <code>k</code>.</p>
2+
3+
<p>Create the maximum number of length <code>k &lt;= m + n</code> from digits of the two numbers. The relative order of the digits from the same array must be preserved.</p>
4+
5+
<p>Return an array of the <code>k</code> digits representing the answer.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> nums1 = [3,4,6,5], nums2 = [9,1,2,5,8,3], k = 5
12+
<strong>Output:</strong> [9,8,6,5,3]
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> nums1 = [6,7], nums2 = [6,0,4], k = 5
19+
<strong>Output:</strong> [6,7,6,0,4]
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> nums1 = [3,9], nums2 = [8,9], k = 3
26+
<strong>Output:</strong> [9,8,9]
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li><code>m == nums1.length</code></li>
34+
<li><code>n == nums2.length</code></li>
35+
<li><code>1 &lt;= m, n &lt;= 500</code></li>
36+
<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 9</code></li>
37+
<li><code>1 &lt;= k &lt;= m + n</code></li>
38+
<li><code>nums1</code> and <code>nums2</code> do not have leading zeros.</li>
39+
</ul>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class Solution:
2+
def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:
3+
# def pick_largest(nums: List[int], k: int) -> List[int]:
4+
# n = len(nums)
5+
# stack = [0] * k
6+
# top = -1
7+
# remain = n - k
8+
# for num in nums:
9+
# while top >= 0 and stack[top] < num and remain > 0:
10+
# top -= 1
11+
# remain -= 1
12+
# if top + 1 < k:
13+
# top += 1
14+
# stack[top] = num
15+
# else:
16+
# remain -= 1
17+
# return stack
18+
19+
# def compare(nums1: List[int], nums2: List[int], i: int, j: int) -> bool:
20+
# if i >= len(nums1):
21+
# return False
22+
# elif j >= len(nums2):
23+
# return True
24+
# elif nums1[i] > nums2[j]:
25+
# return True
26+
# elif nums1[i] < nums2[j]:
27+
# return False
28+
# else:
29+
# return compare(nums1, nums2, i + 1, j + 1)
30+
31+
# def merge(nums1: List[int], nums2: List[int]) -> List[int]:
32+
# m, n = len(nums1), len(nums2)
33+
# i = j = 0
34+
# result = [0] * (m + n)
35+
# for k in range(m + n):
36+
# if compare(nums1, nums2, i, j):
37+
# result[k] = nums1[i]
38+
# i += 1
39+
# else:
40+
# result[k] = nums2[j]
41+
# j += 1
42+
# return result
43+
44+
# m, n = len(nums1), len(nums2)
45+
# left, right = max(0, k - n), min(k, m)
46+
# result = [0] * k
47+
# for val in range(left, right + 1):
48+
# temp = pick_largest(nums1, val)
49+
# temp2 = pick_largest(nums2, k - val)
50+
# temp3 = merge(temp, temp2)
51+
# if result < temp3:
52+
# result = temp3
53+
54+
# return result
55+
56+
def pick_largest(nums: List[int], val: int) -> List[int]:
57+
stack = []
58+
drop = len(nums) - val
59+
for num in nums:
60+
while drop and stack and stack[-1] < num:
61+
stack.pop()
62+
drop -= 1
63+
stack.append(num)
64+
return stack[:val]
65+
66+
def merge(nums1: List[int], nums2: List[int]) -> List[int]:
67+
result = []
68+
while nums1 or nums2:
69+
if nums1 > nums2:
70+
result.append(nums1.pop(0))
71+
else:
72+
result.append(nums2.pop(0))
73+
return result
74+
75+
m, n = len(nums1), len(nums2)
76+
result = []
77+
for val in range(max(0, k - n), min(k, m) + 1):
78+
part1 = pick_largest(nums1, val)
79+
part2 = pick_largest(nums2, k - val)
80+
check = merge(part1, part2)
81+
result = max(result, check)
82+
return result

0 commit comments

Comments
 (0)