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. leetcode166

    public class Solution { public String fractionToDecimal(int numerator, int denominator) { HashMap< ...

  2. 1.HTML编码解码URL替换--代码整理

    public class HtmlCode { public static String encode(String str){ String s = ""; if (str.le ...

  3. MySQLNonTransientConnectionException: No operations allowed after connection closed

    原因分析 查看了Mysql的文档,以及Connector/J的文档以及在线说明发现,出现这种异常的原因是: Mysql服务器默认的"wait_timeout"是8小时,也就是说一个 ...

  4. J2SE 8的集合

    List ArrayList查询效率高LinkedList插入删除效率高 ArrayList ArrayList<String> arrayList = new ArrayList< ...

  5. DNS配置注意事项 正在连接网络

    故障现象 公司规模不是很大,大概有50多台计算机,购买了两台IBM服务器.由于内部使用的某个应用软件需要Windows域的支持,所以在这两台IBM服务器上启用了windows 2000 Server的 ...

  6. 关于gevent的一些理解(一)

    前言:gevent是python的一个并发框架,以微线程greenlet为核心,使用了epoll事件监听机制以及诸多其他优化而变得高效.而且其中有个monkey类, 将现有基于Python线程直接转化 ...

  7. Axel与Wget下载工具

    Axel工具是linux下的http/ftp中强大下载工具,支持多线程和断点续传下载.且可以从多个地址或者从一个地址的多个连接来下载同一个文件. 常用的选项: [root@wjoyxt ~]# axe ...

  8. Hibernate 的事物简单的增删查改

    Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...

  9. Kconfig详解

    当执行#make menuconfig时会出现内核的配置界面,所有配置工具都是通过读取"arch/$(ARCH)Kconfig"文件来生成配置界面,这个文件就是所有配置的总入口,它 ...

  10. mysql for update语句

    我们都知道for update语句会锁住一张表,锁表的细节很多人却不太清楚,下面我们举例看下. 在表上我们有个索引,如下: 现在在我们通过索引store_id锁表: 我们再开一个客户端,还是锁住同一个 ...