`

一道简单题

J# 
阅读更多
You are allowed to pick one number in the input list and increment its value by 1. This should be done in such way that the product of all numbers in the list after this change becomes as large as possible. Given the list of numbers as vector numbers, return the maximum product you can obtain. It is guaranteed that the return value will not exceed 2^62.
原题限制number in (1, 1000),这里考虑下负数和0
设A={ai|i=1..n}
那么题目的意思是求Max{(PI(ai|i=1..n,i!=j)))|j=1..n}
首先遍历数组一遍,记录负数(<-1), -1,正数和0的个数
如果全部是正数,那么即时Max{(PI(ai|i=1..n)*(aj+1)/aj)|j=1..n}=(PI(ai|i=1..n)*Max{(aj+1)/aj)|j=1..n}=(PI(ai|i=1..n)*Max{1+1/aj)|j=1..n}==(PI(ai|i=1..n)*(1+1/Min{aj)|j=1..n})

如果有多余1个0,那么就返回0

如果有1个0,
那么如果负数(包括-1)个数为奇数,那么返回0
如果负数(包括-1)个数为偶数,那么就把0变成1计算

剩下没有0

如果负数(包括-1)个数为偶数,
那么如果正数存在,既考虑最小正数+1
如果不存在正数,就把最小(即绝对值最大)的负值+1

剩下负数(包括-1)个数为奇数,
如果有-1,那么返回0(即把-1 加1)
如果没有-1,就把最大(即绝对值最小)的负值+1

不知道考虑清楚了没,呵呵
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics