要求时间复杂度: ,空间复杂度: 。
数据范围:
# 直接排序后 列出可能存在的几种可能 # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # 最大乘积 # @param A int整型一维数组 # @return long长整型 # class Solution: def solve(self , A: List[int]) -> int: # write code here # 暴力,通过5/15 # max=0 # for i in range(len(A)): # for j in range(i+1,len(A)): # for k in range(j+1,len(A)): # temp= A[i]*A[j]*A[k] # if temp > max: # max=temp # return max if len(A)==3: return A[0]*A[1]*A[2] sortedA=sorted(A) MAX1=sortedA[-1]*sortedA[-2]*sortedA[-3] MAX2=sortedA[-1]*sortedA[-2]*sortedA[0] MAX3=sortedA[1]*sortedA[2]*sortedA[0] MAX4=sortedA[1]*sortedA[0]*sortedA[-1] # maxA=sortedA[len(A)-1]*sortedA[len(A)-2]*sortedA[len(A)-3] # minA=sortedA[0]*sortedA[1]*sortedA[2] return max(MAX1, MAX2, MAX3,MAX4)
class Solution: def solve(self , A: List[int]) -> int: # write code here def quicksort(lists,i,j): if i>=j: return lists p=lists[i] low=i high=j while i<j: while i<j and lists[j]>=p: j-=1 lists[i],lists[j]=lists[j],lists[i] while i<j and lists[i]<=p: i+=1 lists[i],lists[j]=lists[j],lists[i] quicksort(lists,low,i-1) quicksort(lists,i+1,high) return lists n=len(A) lists=quicksort(A,0,n-1) return A[n-1]*A[n-2]*A[n-3] if A[n-1]*A[n-2]*A[n-3]>A[n-1]*A[0]*A[1] else A[n-1]*A[0]*A[1]
class Solution: def solve(self , A ): # write code here A = sorted(A) max1 = A[-1]*A[-2]*A[-3] max2 = A[0]*A[1]*A[-1] return max(max1, max2)
class Solution: def solve(self , A ): A.sort(reverse = True) if A[-1] >= 0&nbs***bsp;A[0] <= 0&nbs***bsp;A[0]*A[1]*A[2] > A[-1]*A[-2]*A[0]: return A[0]*A[1]*A[2] return A[-1]*A[-2]*A[0]