Участник:Larin.dv/Create Maximum Number

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/create-maximum-number/

class Solution(object):
    def maxNumber(self, nums1, nums2, k):
        rt = []
        for x in range(k+1):
            # x needed from nums1 , k-x is needed from nums2
            if (x > len(nums1) or k-x > len(nums2)):
                continue
 
            stk1 = []
            c = 0
            for n1 in nums1:
                while stk1 and stk1[-1] < n1 and len(nums1) > x + c:
                    stk1.pop()
                    c += 1
                stk1.append(n1)
 
            stk2 = []
            c = 0
            for n2 in nums2:
                while stk2 and stk2[-1] < n2 and len(nums2) > k-x + c:
                    stk2.pop()
                    c += 1
                stk2.append(n2)
 
            # merge
            newL = []
            for _ in range(k):
                newL.append(max(stk1, stk2).pop(0))
 
            rt = max(rt, newL)
 
        return rt