Largest product from 3 integers

https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Ride%20Share%20Start-Up%20Company/Ride%20Share%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/On-Site%20Question%201%20-%20SOLUTION.ipynb

On-Site Question 1 - SOLUTION

Problem

Given a list of integers, find the largest product you could make from 3 integers in the list

Requirements

You can assume that the list will always have at least 3 integers

Paper/pencil only, don‘t code this out until you‘ve solved it as far as you can by hand.

Solution

We can solve this problem in O(n) time with O(1) space, we should also be able to take into account negative numbers, so that a list like: [-5,-5,1,3] returns (-5)(-5)(3) = 75 as its answer.

Hopefully you‘ve begun to realize the similarity between this problem and the Amazon stock problem from the E-Commerce Company mock interview questions! You could brute force this problem by just simply trying every single combination of three digits, but this would require O(n^3) time!

How about we use a greedy approach and keep track of some numbers. In the stock problem we kept track of max profit so far, in this problem we are actually going to keep track of several numbers:

  • The highest product of 3 numbers so far
  • The highest product of 2 numbers so far
  • The highest number so far

Since we want to keep negative numbers in account, we will also keep track of the lowest product of two and the lowest number:

  • The lowest product of 2
  • The lowest number

Once we iterate through the list and reach the end we will have the highest posiible product with 3 numbers. At each iteration we will take the current highest product of 3 and compare it to the current integer multiplied by the highest and lowest products of 2.

Let‘s see this coded out:

def solution(lst):

    # Start at index 2 (3rd element) and assign highest and lowest
    # based off of first two elements

    # Highest Number so far
    high = max(lst[0],lst[1])

    # Lowest number so far
    low = min(lst[0],lst[1])

    # Initiate Highest and lowest products of two numbers
    high_prod2 = lst[0]*lst[1]
    low_prod2 = lst[0]*lst[1]

    # Initiate highest product of 3 numbers
    high_prod3 = lst[0]*lst[1]*lst[2]

    # Iterate through list
    for num in lst[2:]:

        # Compare possible highest product of 3 numbers
        high_prod3 = max(high_prod3,num*high_prod2,num*low_prod2)

        # Check for possible new highest products of 2 numbers
        high_prod2 = max(high_prod2,num*high,num*low)

        # Check for possible new lowest products of 2 numbers
        low_prod2 = min(low_prod2,num*high,num*low)

        # Check for new possible high
        high = max(high,num)

        # Check for new possible low
        low = min(low,num)

    return high_prod3

In [5]:

l = [99,-82,82,40,75,-24,39, -82, 5, 30, -25, -94, 93, -23, 48, 50, 49,-81,41,63]

solution(l)

Out[5]:

763092

Great! Through the use of a greedy approach we have been able to complete the problem in O(n) time. Keep this sort of approach in mind when you have to iterate through a list and a brute force solution is on the order of an exponential!

Good Job!

时间: 2024-10-13 05:32:53

Largest product from 3 integers的相关文章

Project Euler:Problem 11 Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30

Largest product in a grid

这个比前面的要复杂点,但找对了规律,还是可以的. 我逻辑思维不强,只好画图来数数列的下标了. 分四次计算,存入最大值. 左右一次,上下一次,左斜一次,右斜一次. In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60

R语言学习——欧拉计划(11)Largest product in a grid

Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 0849 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 0081 49 31 73 55 79 14 29 93 71 40 67

[leetcode] Maximum Product Subarray @ python

[leetcode] Latest added: 2014-09-23 Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. Trick:

[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1]

LeetCode-152 Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 思路:使用动态规划. 设max[i]是以num[i]为结尾元素的子数组的最大乘积:

[LeetCode]Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的. 如

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2

LeetCode152:Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 这道题是上面连续子数组和问题的扩展吧.很遗憾没做来出,状态都定义对了,但是状态转移