Skip to content

Commit 3fe9883

Browse files
committed
Runtime: 5629 ms (Top 6.28%) | Memory: 14.3 MB (Top 62.78%)
1 parent e4f2ee9 commit 3fe9883

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

scripts/algorithms/C/Create Maximum Number/Create Maximum Number.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Runtime: 5629 ms (Top 6.28%) | Memory: 14.3 MB (Top 62.78%)
12
class Solution:
23
def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:
34
def maximum_num_each_list(nums: List[int], k_i: int) -> List[int]:
@@ -12,23 +13,23 @@ def maximum_num_each_list(nums: List[int], k_i: int) -> List[int]:
1213
s = s[:len(s)-m] # very important
1314
return s
1415
def greater(a, b, i , j): # get the number which is lexiographically greater
15-
while i< len(a) or j < len(b):
16+
while i< len(a) or j < len(b):
1617
if i == len(a): return False
1718
if j == len(b): return True
1819
if a[i] > b[j]: return True
1920
if a[i] < b[j]: return False
2021
i += 1 # we increment until each of their elements are same
2122
j += 1
22-
23+
2324
def merge(x_num, y_num):
2425
n = len(x_num)
2526
m = len(y_num)
2627
i = 0
2728
j = 0
2829
s = []
2930
while i < n or j < m:
30-
a = x_num[i] if i < n else float("-inf")
31-
b = y_num[j] if j < m else float("-inf")
31+
a = x_num[i] if i < n else float("-inf")
32+
b = y_num[j] if j < m else float("-inf")
3233

3334
if a > b or greater(x_num, y_num, i , j):
3435
# greater(x_num, y_num, i , j): this function is meant for check which list has element lexicographically greater means it will iterate through both arrays incrementing both at the same time until one of them is greater than other.
@@ -46,10 +47,9 @@ def merge(x_num, y_num):
4647
second = maximum_num_each_list(nums2, k-i)
4748
merged = merge(first, second)
4849
# these two conditions are required because list comparison in python only compares the elements even if one of their lengths is greater, so I had to add these conditions to compare elements only if length is equal.
49-
# Alternatively you can avoid this and convert them both to int and then compare, but I wanted to this as it is somewhat more efficient.
50-
if len(merged) == len(max_num_arr) and merged > max_num_arr:
50+
# Alternatively you can avoid this and convert them both to int and then compare, but I wanted to this as it is somewhat more efficient.
51+
if len(merged) == len(max_num_arr) and merged > max_num_arr:
5152
max_num_arr = merged
5253
elif len(merged) > len(max_num_arr):
5354
max_num_arr = merged
5455
return max_num_arr
55-

0 commit comments

Comments
 (0)