https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Mock%20Interviews/Social%20Network%20Company/Social%20Network%20Company%20-%20Interview%20Questions%20-%20SOLUTIONS/On-Site%20Question%203%20-%20SOLUTION.ipynb

On-Site Question 3 - SOLUTION

Problem

Create a function that takes in a list of unsorted prices (integers) and a maximum possible price value, and return a sorted list of prices

Requirements

Your function should be able to perform this in less than O(nlogn) time.

 

Solution

We can actually solve this problem by using a counting sort. Basically a counting sort works well when you know the range of integer values you will have ahead of time.

Read the wikipedia article linked above for a full break down, and an implementation is here below (using the prices situation described in the problem above).

def solution(unsorted_prices,max_price):

    # list of 0s at indices 0 to max_price
prices_to_counts = [0]* (max_price+1) # populate prices
for price in unsorted_prices:
prices_to_counts[price] +=1 # populate final sorted prices
sorted_prices = [] # For each price in prices_to_counts
for price,count in enumerate(prices_to_counts): # for the number of times the element occurs
for time in range(count): # add it to the sorted price list
sorted_prices.append(price) return sorted_prices
In [3]:
solution([4,6,2,7,3,8,9],9)
Out[3]:
[2, 3, 4, 6, 7, 8, 9]
 

This was a great exercise in learning about a new sorting algorithm, make sure to read up on it and practice this problem again!

Good Job!

Unsorted, maximum ==> sorted的更多相关文章

  1. 11、比对软件STAR(https://github.com/alexdobin/STAR)

    转载:https://mp.weixin.qq.com/s?__biz=MzI1MjU5MjMzNA==&mid=2247484731&idx=1&sn=b15fbee5910 ...

  2. STAR软件的学习

    下载地址与参考文档 https://github.com/alexdobin/STAR/archive/2.5.3a.tar.gz wget https://github.com/alexdobin/ ...

  3. 【转】所需即所获:像 IDE 一样使用 vim

    转自:  https://github.com/yangyangwithgnu/use_vim_as_ide 所需即所获:像 IDE 一样使用 vim yangyangwithgnu@yeah.net ...

  4. hadoop2.2基准测试

    <hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases的class在新的版 ...

  5. hadoop2.2编程:hadoop性能测试

    <hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases 的class在新的 ...

  6. Hadoop基准测试(转载)

    <hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases的class在新的版 ...

  7. 经典排序算法 - 归并排序Merge sort

    经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...

  8. PHP 使用用户自定义的比较函数对数组中的值进行排序

    原文:PHP 使用用户自定义的比较函数对数组中的值进行排序 usort (PHP 4, PHP 5) usort —      使用用户自定义的比较函数对数组中的值进行排序 说明       bool ...

  9. go 测试sort性能

    package main import "fmt" import "os" import "flag" import "bufio ...

随机推荐

  1. linux更新系统

    1.linux更新系统 https://www.jb51.net/os/RedHat/450223.html 仅更新系统,不更新内核 yum -y --exclude=kernel\* upgrade ...

  2. spring-boot + mybatis +pagehelper 使用分页

    转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...

  3. bootStrap的小知识

    代码模块 <code> 内联代码 <kbd> 用户输入 <pre>代码段 <var>数学字符 <samp>程序输出 表格 <thead ...

  4. 客户端如何连接 DataSnap Server 调用服务的方法

    一般http访问的地址是 http://localhost:8099/datasnap/rest/TServerMethods1/EchoString/abc 一.用FDConnection1连接Da ...

  5. PHP判断客户端是PCweb端还是移动手机端方法

    /** * * 根据php的$_SERVER['HTTP_USER_AGENT'] 中各种浏览器访问时所包含各个浏览器特定的字符串来判断是属于PC还是移动端 * @author discuz3x * ...

  6. setTranslatesAutoresizingMaskIntoConstraints

    [viewItem setTranslatesAutoresizingMaskIntoConstraints:NO]; 在给继承UIView的类设置此属性后,UIView的某些属性可能发生变化.例如f ...

  7. oracle惯用缩写的含义

    $ORACLE_HOME/bin下的utilities解释Binary              First Available        Description----------------- ...

  8. 将*.sql数据库脚本导入到sqlserver中(sql文件导入sqlserver)

    在SqlServer中这个是用生成sql脚本生成的 要是在导入数据库用数据导入/导出向导导不进去 其实要用查询分析器来打开sql文件 然后执行就可以了

  9. Jstl标签<c:forEach>的用法

    <c:forEach>除了支持数组之外,还有标准J2SE的集合类型,例如:ArrayList.List.LinkedList.Vector.Stack和Set 等等:另外还包括java.u ...

  10. pyplot文本显示

    pyplot文本显示 pyplot中文字符显示 pyplot默认不支持中文字符,因为默认字体是sans-serif,英文字体不能显示中文 方法1,修改需要输出中文字符的地方 在有中文输出的地方,添加属 ...