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. 给Jquery easyui 的datagrid 每行增加操作链接(转)

    http://www.thinkphp.cn/code/207.html 通过formatter方法给Jquery easyui 的datagrid 每行增加操作链接我们都知道Jquery的EasyU ...

  2. Android应用程序的自动更新升级(自身升级、通过tomcat)(转)

    Android应用程序的自动更新升级(自身升级.通过tomcat) http://blog.csdn.net/mu0206mu/article/details/7204746 刚入手android一个 ...

  3. 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能

    转载:https://blog.zengrong.net/post/1568.html 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能 今天在论坛上看到一篇文章:H ...

  4. Springmvc Exception

    对于异常处理,大多使用一个共同的异常类统一处理从dao,service,controller层抛出的异常,将页面跳转到共同的error页面. public class CommonException ...

  5. python 2 类与对象

    1.类与对象的概念 类即类别.种类,是面向对象设计最重要的概念,从一小节我们得知对象是特征与技能的结合体,而类则是一系列对象相似的特征与技能的结合体. 那么问题来了,先有的一个个具体存在的对象(比如一 ...

  6. flask_sqlalchemy

    简介: flask_sqlalchemy 是 Flask Web 框架的一个扩展. 使 Web 服务器以 (ORM: Object Relational Mapping) 对象关系映射的方式与数据库交 ...

  7. Ansiable Manage MySQL global variables

    mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...

  8. CentOS 下安装 OpenOffice4.0

    一.更新服务器 yum源 [root@APP2 /]# yum clean all [root@APP2 /]# yum makecache [root@APP2 /]# yum update 1.首 ...

  9. windows服务启动的进程无窗口

    勾选允许服务与桌面交互 指服务是否在桌面上提供用户界面,当服务启动后不论是谁登录都能使用.只有作为 LocalSystem 帐户(由“此帐户”指定)运行时,该选项才能使用. 如果一个服务需要界面(比如 ...

  10. 第五章 二叉树(e2)中序遍历