第40周 2019 07 21

it2022-05-05  159

Algorithm:164. 最大间距 Review: Tip/Tech: 基基数排序 Share:五对夫妇为CRISPR婴儿排队以避免耳聋

Algorithm

https://leetcode-cn.com/problems/maximum-gap/

164. 最大间距

这个题目真的,如果没哟线性时间这个要求啊,那么是无比的简单的,直接一个排序就搞定了,但是不得不说,这个出题不严谨,相当于是为了出题而出题。 具体的思想其实真的没啥好说的,就是首先你要想到,线性的排序,桶排序,基数排序这些。 然后开始手写算法。 其实官方的答案中的桶排序我没仔细的看,因为后来我发现,如果说应用场景的话,其实基数排序还会更多些,但是桶排序的思想是真的挺关键的,下个礼拜就把桶排序给好好地复习一下吧。

class Solution { public int maximumGap(int[] nums) { int res = 0; if (nums == null || nums.length == 0 || nums.length == 1) { return res; } int len = nums.length; int max = nums[0]for (int i = 0; i < len; ++i) { if (max < nums[i]) { max = nums[i]; } } radixSort(nums, 10, (max+"").length()); for (int i = 0; i <= len - 2; ++i) { res = Math.max(res, nums[i + 1] - nums[i]); } return res; } private static void radixSort(int[] array, int radix, int d) { int len = array.length; int[] tempArray = new int[len]; // count用于记录待排序元素的信息,用来表示该位是i的数的个数 int[] count = new int[radix]; int rate = 1; for (int i = 0; i < d; ++i) { //重置count数组,开始统计下一个关键字 Arrays.fill(count, 0); System.arraycopy(array, 0, tempArray, 0, len); // 寻找每个位置上的数字的数量 for (int item : tempArray) { int subKey = (item / rate) % radix; count[subKey] ++; } for (int j = 1; j < radix; ++j) { count[j] = count[j] + count[j - 1]; } for (int m = len - 1; m >= 0; --m) { int subKey = (tempArray[m] / rate) % radix; array[-- count[subKey]] = tempArray[m]; } rate *= radix; } } }

Review

Amazon.com Recommendations Item-to-Item Collaborative Filtering

https://www.cs.umd.edu/~samir/498/Amazon-Recommendations.pdf

这篇论文就是属于推荐系统的经典之作了 1 原来亚马逊也是基于用户的协同过滤推荐,但是需要的计算资源太多了,后来就用了基于物品的推荐。 2 基于物品的就是,首先得到用户购买过的物品,然后根据物品的相似度来进行形成推荐的物品列表,然后进行排序,给用户进行排序。

Tip/Tech

基数排序

1 最高位优先法(MSD)(Most Significant Digit first) 2 最低位优先法(LSD)(Least Significant Digit first) 基数排序是稳定的排序算法,它的平均时间复杂程度为:O(d(r+n)),空间复杂度为:O(rd+n)。

其实基数排序是桶排序一种升级版,当有不那么多的位数相差比较多的数字的时候,我们就可以用基数排序来解决大部分的问题。 基数排序我感觉我对他的使用场景还不是很明白,这里我们就直接先看代码了。。。

public void usage() { int[] test = {5, 6, 7, 1, 2, 3, 9, 4, 5, 8, 1, 2, 3}; int max = test[0]; for (int item : test) { max = Math.max(max, item); } radixSort(test, 10, (max+"").length()); } public static void radixSort(int[] array, int radix, int d) { int len = array.length; int[] tempArray = new int[len]; // count用于记录待排序元素的信息,用来表示该位是i的数的个数 int[] count = new int[radix]; int rate = 1; for (int i = 0; i < d; ++i) { //重置count数组,开始统计下一个关键字 Arrays.fill(count, 0); System.arraycopy(array, 0, tempArray, 0, len); // 寻找每个位置上的数字的数量 for (int item : tempArray) { int subKey = (item / rate) % radix; count[subKey] ++; } for (int j = 1; j < radix; ++j) { count[j] = count[j] + count[j - 1]; } for (int m = len - 1; m >= 0; --m) { int subKey = (tempArray[m] / rate) % radix; array[-- count[subKey]] = tempArray[m]; } rate *= radix; } }

Share

Exclusive: Five couples lined up for CRISPR babies to avoid deafness

五对夫妇为CRISPR婴儿排队以避免耳聋

https://www.newscientist.com/article/2208777-exclusive-five-couples-lined-up-for-crispr-babies-to-avoid-deafness/ 其实基因修改这个技术一旦成熟,那么距离创造出“超级人类”也就不远了。现在人类已经意识到了基因的缺陷会对后代造成的影响,那么接下来就是那些特权,超级富豪阶级会不会开始养一批的科学家开始不断的研究基因编辑的技术?来完善自己的后代? 虽然说每个国家的科学家都说基因编辑是不人道的,但是一旦某个国家开始为了提升自己的国家实力开始不断创造出高智商的人才,别的国家真的不会动心么?现在最有价值的的毫无疑问是人才,国家与国家之间差距永远存在,落后的国家只能通过不断的产生人才才能完成逆袭,比如当时的中国在那么落后的物质条件之下,依然造出了原子弹。 那么这个可行的技术就在面前,那些个国家真的不会动心?


最新回复(0)