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 ...
随机推荐
- localhost 127.0.0.1
No1: localhost也叫local ,正确的解释是:本地服务器 127.0.0.1在windows等系统的正确解释是:本机地址(本机服务器) 他们的解析通过本机的host文件,windows自 ...
- CentOS 7 firewalld
1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disabl ...
- UI5-文档-4.13-Margins and Paddings
我们的应用程序内容仍然粘在信箱的角落里.要微调布局,可以向上一步添加的控件添加空白和填充. 我们将使用SAPUI5提供的标准类,而不是手工向控件添加CSS.这些类负责一致的分级步骤.从左到右的支持和响 ...
- 原生nodejs 学习笔记2
本章节学习流, 流的一个好处在于减少各种异步IO的回调地狱.IO操作遍及我们各种操作,比如数据库读写,文件读写, 文件转换压缩--别的不说,比如第一节,我们要将一个HTML文件返回浏览器,就涉及IO操 ...
- 扩展C#与元编程(二)
如果你对Windows Workflow Foundation(WF)一无所知,当看到扩展C#与元编程(一)中由MW编译器生成的FirstLook.mw.cs时,也许这么在想:我KAO,这是C#版的汇 ...
- Python—— *与** 参数说明
Python *与** 参数说明 '''*用来传递任意个无名字参数,这些参数会一个Tuple的形式访问''' def fall(*z): print sum(z) print "keys t ...
- Ansible介绍/安装/入门
http://docs.ansible.com/ansible/ https://galaxy.ansible.com/ Ansible是一个IT自动化工具. 它可以配置系统,部署软件,并编排更先进的 ...
- 构造函数中的super和this的使用
super用于调用父类构造函数的部分,其必须出现在构造函数的第一行.super在调用时第一件事就是去执行父类构造函数的部分,所执行的父类构造函数与super()括号中的参数相对应. this用于在一个 ...
- ubuntu修改运行级别方法
Ubuntu系统设置启动级别的问题,因自己以前遇到过,故做过笔记记录了下来:Ubuntu.Debian系列与RedHat.CentOS启动级别含义有所区别:Ubuntu系列运行级别定义如下:0 – H ...
- Electron Browser加载iframe(webview src属性)
browser或者webcontents 的高度与宽度比例对webview中src的页面结构也是有一定影响的