Unsorted, maximum ==> sorted
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
solution([4,6,2,7,3,8,9],9)
[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的更多相关文章
- 11、比对软件STAR(https://github.com/alexdobin/STAR)
转载:https://mp.weixin.qq.com/s?__biz=MzI1MjU5MjMzNA==&mid=2247484731&idx=1&sn=b15fbee5910 ...
- STAR软件的学习
下载地址与参考文档 https://github.com/alexdobin/STAR/archive/2.5.3a.tar.gz wget https://github.com/alexdobin/ ...
- 【转】所需即所获:像 IDE 一样使用 vim
转自: https://github.com/yangyangwithgnu/use_vim_as_ide 所需即所获:像 IDE 一样使用 vim yangyangwithgnu@yeah.net ...
- hadoop2.2基准测试
<hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases的class在新的版 ...
- hadoop2.2编程:hadoop性能测试
<hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases 的class在新的 ...
- Hadoop基准测试(转载)
<hadoop the definitive way>(third version)中的Benchmarking a Hadoop Cluster Test Cases的class在新的版 ...
- 经典排序算法 - 归并排序Merge sort
经典排序算法 - 归并排序Merge sort 原理,把原始数组分成若干子数组,对每个子数组进行排序, 继续把子数组与子数组合并,合并后仍然有序,直到所有合并完,形成有序的数组 举例 无序数组[6 2 ...
- PHP 使用用户自定义的比较函数对数组中的值进行排序
原文:PHP 使用用户自定义的比较函数对数组中的值进行排序 usort (PHP 4, PHP 5) usort — 使用用户自定义的比较函数对数组中的值进行排序 说明 bool ...
- go 测试sort性能
package main import "fmt" import "os" import "flag" import "bufio ...
随机推荐
- LiveBinding应用 dataBind 数据绑定
http://blog.csdn.net/embarcaderochina/article/details/50352193 firemonkey grid/listview dataBind,数据绑 ...
- 数据库的 ACID 属性
ACID(Atomicity 原子性.Consistency 一致性.Isolation 隔离性.Durability 持久性)是一系列属性. 这些属性保证了数据库事物的可靠.在数据库中,对数据的一系 ...
- eclipse 使用tomcat7.0建立Dynamic Web Project 时 web.xml的问题
最近使用Eclipse helios版本结合tomcat7.0建立动态的web项目时,发现在WEB-INF下的web.xml没有了. 解决方案: 建立web项目时,建到第三个下一步时,将 Genera ...
- 迷你MVVM框架 avalonjs 1.3.6发布
本版本是一次重要的升级,考虑要介绍许多东西,也有许多东西对大家有用,也发到首页上来了. 本来是没有1.36的,先把基于静态收集依赖的1.4设计出来后,发现改动太多,为了平缓升级起见,才减少了一部分新特 ...
- Hibernate 的update语句性能详解
Hibernate 中如果直接使用 Session.update(Object o); 会把这个表中的所有字段更新一遍. 比如: view plaincopy to clipboardprint? p ...
- Lock()与RLock()锁
资源总是有限的,程序运行如果对同一个对象进行操作,则有可能造成资源的争用,甚至导致死锁 也可能导致读写混乱 锁提供如下方法: 1.Lock.acquire([blocking]) 2.Lock.rel ...
- pod优先级与抢占测试
# kubectl describe node k8s-n2Name: k8s-n2Roles: <none>Labels: ...
- dubbo hessian+dubbo协议
Dubbo缺省协议采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,以及服务消费者机器数远大于服务提供者机器数的情况 Hessian协议用于集成Hessian的服务,Hessian底层采 ...
- shell脚本通过expect脚本实现自动输入密码(使用expect)
背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现 一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具. ...
- MVVM 的理解
MVVM 是 Model - View - ViewModel 的缩写 可以看到他和之前的MVC很像,的确有人称之为一个加强优化版的MVC. 是一种模块化开发代码分层的思想或者框架! MVVM 的优点 ...