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 ...
随机推荐
- spring data jpa @query的用法
@Query注解的用法(Spring Data JPA) 参考文章:http://www.tuicool.com/articles/jQJBNv . 一个使用@Query注解的简单例子 @Query( ...
- JAVA Spring JdbcTemplate ( 以 SQLSERVER 为例 ) 的简单使用
< 1 > 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...
- 5.mybatis实战教程(mybatis in action)之五:与spring3集成(附源码)
转自:https://blog.csdn.net/nnn9223643/article/details/41962097 在 这一系列文章中,前面讲到纯粹用mybatis 连接数据库, 然后 进行增删 ...
- django celery 定时任务
可参考上一篇:http://www.cnblogs.com/wumingxiaoyao/p/8515075.html 1. 安装 django-celery-beat pip3 install dja ...
- PHP图像 因其本身有错无法显示
昨天终于将客户的一个网站迁移至虚拟主机上,满怀希望的敲入网址.唰的一声,网站很轻松的被打开了. 心里那个高兴啊~~~ 咦,怎么产品图片都没有显示出来.一块块都是空白.敲入img src对应的地址,看看 ...
- linux控制台批量杀进程
-| cd /app/tomcat8/bin/ ./startup.sh
- ABAP-ALV报表导出格式恢复初始画面
进入一个ALV表格,想下载数据,一般点清单-->输出-->电子数据表. 会出来一个对话框,可选择导出成各类格式. 在下端有一个“始终使用选定的格式”,一旦勾上,就再也不会弹出选择框了. 以 ...
- 工作中用到和应该知道的eclipse快捷键
Eclipse最初是由IBM公司开发的替代商业软件Visual Age for Java的下一代IDE开发环境,2001年11月贡献给开源社区,现在它由非营利软件供应商联盟Eclipse基金会(Ecl ...
- Spring MVC国际化
本文基于Spring MVC 注解-让Spring跑起来.本文提到的国际化是Spring实现国际化的方案之一. (1) 在applicationContext.xml中添加以下配置信息: <!- ...
- MySQL 事务 是对数据进行操作,对结构没有影响,比如创建表、删除表,事务就不起作用