Combined into the largest number by order

Source: Internet
Author: User
Tags first string

Describe

Given two arrays, consisting of the number 0-9, the length is a and B, you need to use a, b two array of numbers together to get a positive integer length K (k <= a+b), the output of all possible combinations of the number of the largest one (the original array of numbers in the order cannot be changed)

Solution

\ (f_{i,j,k}\) represents the first string to the i\ position, the second string to the j\ , and a total of \ (k\)

It's poisonous.
I'm not in the Python right position.
It's like doing a little constant optimization.

Code
  def solution (line): tmp = Line.split (') a = Tmp[0].split (', ') b = tmp[1].split (', ') k = Min (len (a) + len (b), int (tmp[2])) f = [] N1 = Len (a) N2 = Len (b) f = [] for I in range (N1): A[i] = Int (A[i]) for J in Range (N2): b[j] = Int (b[j]) for I in range (n1 + 1): F.append ([]) for J in Range (N2 + 1): F[i].append ([]) for K in range (K + 1): F[i][j].append (0) for I in R Ange (1, n1 + 1): f[i][0][1] = a[i-1] for j in range (1, n2 + 1): f[0][j][1] = b[j-1] for I in range                    (n1 + 1): for j in range (N2 + 1): for k in range (1, K + 1): If i > 0:                    F[i][j][k] = max (F[i][j][k], Max (F[i-1][j][k], f[i-1][j][k-1] * + a[i-1])) If J > 0: F[i][j][k] = max (F[i][j][k], Max (F[i][j-1][k], f[i][j-1][k-1] * + b[j-1])) return f[n1][n2][k] 

The maximum number of sequential combinations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.