Python 100% code


  • 0

    Solution by my own. Welcome to any implements or questions. Sorry for ugly code.

    """

        def f1(maxum,s1,s2):
            n1, n2 = -1, -1
            if M - s1 > maxum: n1 = max(max(n_1[s1:s1+maxum]),n1)
            elif s1 < M: n1 = max(max(n_1[s1:]),n1)
            if N - s2 > maxum: n2 = max(max(n_2[s2:s2+maxum]),n2)
            elif s2 < N: n2 = max(max(n_2[s2:]),n2)
            return n1,n2
            
        M, N = len(n_1), len(n_2)
        r, l = [], [[0,0]]
        for x in range(k):
            n_l, n_r = [], []
            for st in l:
                n1, n2 = f1(M + N - st[0] - st[1] - k + 1 + x , st[0], st[1])
                if n1 > n2: 
                    n_r.append(n1)
                    n_l.append([st[0]+n_1[st[0]:].index(n1)+1,st[1]])
                elif n1 < n2: 
                    n_r.append(n2)
                    n_l.append([st[0],st[1]+n_2[st[1]:].index(n2)+1])
                elif n1 == n2 and n1 != -1:
                    n_r.append(n1)
                    n_l.append([st[0]+n_1[st[0]:].index(n1)+1,st[1]])
                    n_r.append(n2)
                    n_l.append([st[0],st[1]+n_2[st[1]:].index(n1)+1])
            nr = -1
            for i in range(len(n_r)):
                if n_r[i] > nr: n_n_l = set(); n_n_l.add((n_l[i][0],n_l[i][1])); nr = n_r[i]
                elif n_r[i] == nr: n_n_l.add((n_l[i][0],n_l[i][1]))
            r.append(nr)
            l = n_n_l
        return r

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.