Product of the maximum subsequence problem
Applications
A list of type integer prior nums = [1,2, -2, -1,5, -4], please find the product of the maximum of a contiguous subsequence in the sequence (the sequence comprises at least one number)
Solution 1
Ideas analysis
Analysis of the meaning of problems found, this question requirements: 1. The product of the maximum; 2. contiguous subsequence;
It is assumed that i is an index of successive sub initiation sequence; J consecutive sub-sequence ending index; for loop all possible traverse the contiguous subsequence, and calculates the product of the largest contiguous subsequence can;
about traversal process is:
- When i is 0, j is desirable 0,1,2, ... len (nums) -1;
- When i is 1, j preferably 1,2, ..., len (the nums) -1;
... - When i is len (nums) -1, j desirable len (nums) -1;
Note: if i == j indicates that it contains only a sequence number;
Code
from functools import reduce
num = [1,2,-2,-1,5,-4]
max = num[0]
max_index_start = 0
max_index_end = 0
for i in range(len(num)):
for j in range(i, len(num)):
if i == j:
cur_num = num[i]
else:
cur_list = num[i:j+1]
# 此处的recude函数表示先对集合中的第 1、2 个元素进行操作,
#得到的结果再与下一个元素用function函数运算
cur_num = reduce(lambda x,y:x*y,cur_list)
if cur_num >= max:
max = cur_num
max_index_start = i
max_index_end = j
print(max, max_index_start,max_index_end)
Execution results are as follows:
20 3 5
The above results indicated that the sequence of consecutive sub-sequences maximum product 20, consecutive sequence is nums [3] ~ nums [5]
Solution 2
Ideas analysis
Similarly, consecutive subframes is assumed that i is the subscript sequence starting; J consecutive sub-sequence ends subscript; there: MUL (i, J) MUL = (0, J) // MUL (0, i. 1- )
the above equation is interpreted: the product of a continuous subsequence = [StartElement: contiguous subsequence end element] // product [starting elements: a contiguous subsequence before starting element] the product of
EG: the nums = [1,2, -2, -1,5, -4] Solution 1 is clear from the results for the contiguous subsequence [-1.5, -4],
the above equation expands to: (-1) × 5 × (-4) = 1 × 2 × (-2) × (-1) × 5 × (-4) // 1 × 2 × (-2)
Analysis of the above formula, summarized as follows:
- When mul (0, j) = 0 product, there is described a number 0, is restarted;
- When mul (0, j) the product <0, in order to ensure maximum product, mul (0, i-1) should be negative (the same number is positive); In this case, should be looking for the maximum negative;
- When mul (0, j) the product of> 0, in order to ensure maximum product, mul (0, i-1) should be as positive (positive number is the same); In this case, to find the minimum should be negative;
Code
def maxMul(nums):
if not nums: return
#起始变量
#目前的累乘
cur_mul = 1
#前面最小的正数
min_pos = 1
#前面最大的负数
max_neg = float("-inf")
#结果
result = float("-inf")
for num in nums:
cur_mul *= num
if cur_mul > 0:
result = max(result, cur_mul//min_pos)
min_pos = min(min_pos, cur_mul)
elif cur_mul < 0 :
if max_neg != float("-inf"):
result = max(result, cur_mul//max_neg)
else:
#如果是-inf,结果更新为当前值和结果的最大值
result = max(result,num)
#找最大的负数
max_neg = max(max_neg, cur_mul)
else:
cur_mul = 1
min_pos = 1
max_neg = float("-inf")
result = max(result,num)
return result
data = [1,2,-2,0,5,-4]
print(maxMul(data))
data1 = [1,2,-2,-1,5,-4]
print(maxMul(data1))
Execution results are as follows:
5
20