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!

Largest product from 3 integers的更多相关文章

  1. projecteuler Problem 8 Largest product in a series

    The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = ...

  2. Largest product in a grid

    这个比前面的要复杂点,但找对了规律,还是可以的. 我逻辑思维不强,只好画图来数数列的下标了. 分四次计算,存入最大值. 左右一次,上下一次,左斜一次,右斜一次. In the 2020 grid be ...

  3. Largest product in a series

    这个我开始理解错了,算错了. 我以为是求连续5个数的最大值,结果,是连接5个数相乘的最大值. 英语不好,容易吃亏啊. Find the greatest product of five consecu ...

  4. 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 0 ...

  5. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  6. Problem 8: Largest product in a series

    先粘实现代码,以后需要再慢慢补充思路 s = ''' 73167176531330624919225119674426574742355349194934 9698352031277450632623 ...

  7. Project Euler 11 Largest product in a grid

    题意:在这个20×20方阵中,四个在同一方向(从下至上.从上至下.从右至左.从左至右或者对角线)上相邻的数的乘积最大是多少? 思路:暴力去枚举以 ( x , y ) 为中心拓展的四个方向 /***** ...

  8. Project Euler 8 Largest product in a series

    题意:寻找这 1000 个数中相邻 13 个数相乘积最大的值 思路:首先想到的是暴力,但是还可以利用之前记录过的数值,什么意思呢?即在计算 2 - 14 后,再计算 3 - 15 时完全可以利用之前计 ...

  9. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. leetcode66

    public class Solution { public int[] PlusOne(int[] digits) { ]; < ) { digits[digits.Length - ]++; ...

  2. php中array_map和array_walk的使用对比_php技巧

    一.array_map() 1.array_map() 函数将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组,若函数作用后无返回值,则对应的新值数组中为空. 2.回调 ...

  3. REST 服务器调试 RESTDebugger.exe 和浏览器测试

    开发一个简单的rest服务器, 增加了一些函数,比如返回系统当前时间 如何验证是否正确呢,不需要自己编写客户端调用程序了, 可以直接使用RESTDebugger.exe D:\Program File ...

  4. 序列化和反序列化(json 和pickle)dumps 为序列化, json为反序列化

    json 可以在不同语言中进行使用 下面先介绍一下json的适用方法 import json, pickle t1 = { 'name':'alex', ', ' } t1 = json.dumps( ...

  5. 【337】Text Mining Using Twitter Streaming API and Python

    Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...

  6. hadoop+zookeeper(ha架构搭建)

    http://blog.csdn.net/baidu_25820069/article/details/52225293 [条件所限,待验证]

  7. Ubuntu 安装 Zabbix 3.2详细步骤

    创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...

  8. Structs配置文件 zg项目介绍

    Structs配置文件 1.以系统代码为名称 例:

  9. django通过url传递参数(编辑操作页面)

    在做到编辑部分时,想到的办法是在编辑上跳转到页面时给他一个包含唯一标识id的url,然后通过这个url中的id去查询出该条数据,将数据内容显示在编辑页面.   1.编辑按钮 <button on ...

  10. Linux Shell 文本处理工具集锦(转载)

    内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...