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 ...
随机推荐
- 谈谈 Python 程序的运行原理
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,谈谈 Python 程序的运行原理 这篇文章准确说是『Python 源码剖析』的 ...
- Spring_JAP_CXF_maven
发送 pom,xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...
- delphi const的用法
unit RadKeygen; interface uses Classes,SysUtils,Windows; function fun1():string; implementation cons ...
- Nginx bind() to 0.0.0.0:8000 failed (10013: 错误解决
本人配置Nginx 8000端口, 启动Nginx 失败, 查看日志logs/error.log出现如下提示 结束酷狗进程就Ok叻
- UI5-文档-4.14-Custom CSS and Theme Colors
有时我们需要定义一些更细粒度的布局,这时我们可以通过向控件添加自定义样式类来使用CSS的灵活性,并根据自己的喜好对它们进行样式化. Preview The space between the butt ...
- hibernate注解主键生成策略
Id生成策略: @GeneratedValue,JPA通用策略生成器 . JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO. TABLE:使用一个特定的数据库表格来 ...
- Tomcat SSL配置及Tomcat CA证书安装
Tomcat既可以作为独立的Servlet容器,也可以作为其他HTTP服务器附加的Servlet容器.如果Tomcat在非独立模式下工作, 通常不必配置SSL,由它从属的HTTP服务器来实现和客户的S ...
- (一)由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
- jps command not found已解决
根据当前版本安装devel 包 eg: yum install java--openjdk-devel -y jdk小工具jps介绍 jps(Java Virtual Machine Process ...
- bed文件格式解读
1)BED文件 BED 文件(Browser Extensible Data)格式是ucsc 的genome browser的一个格式 ,提供了一种灵活的方式来定义的数据行,以用来描述注释信息.BED ...