您的位置:首页 > 其它

九章算法7:Array & Numbers

2016-03-15 00:29 232 查看

九章算法7:Array & Numbers

内容基于九章算法课件

http://www.jiuzhang.com/

Merge two sorted array

Given two sorted integer arrays A and B, merge B into A as one sorted array.

A = [1, 2, 3, empty, empty], B = [4, 5]

After merge, A will be filled as [1, 2, 3, 4, 5]

我的第一反应,直接insert啊…naive啊…

老师说:数组问题, 最好不要进行数组的整体移动的操作.

所以我们这次不谁小谁出列,我们进行谁大谁出列,从后面进行操作~

遇到新题的思维方法:

1.如果比较直接,如上面的 merge two sorted array, 但是好像直接做又不是那么特别好,可以考虑在直接的第一眼思维得出的解法上面稍加变动, 比如: 从左到右变成从右到左, 最小变最大, 一个指针变俩....
2.如果不是很直接的,考虑和之前哪道题像亲戚.比如合并k个数组就可以联想道合并2个数组,能不能在亲戚题的解法上进行改进解决这道题.
3.如果不直接又"举目无亲",根据题目的叙述看神马学过的算法或者数据结构貌似可以用上的,比如:求最大,最小,方案数以及可行性的想动态规划,k个数求最小用heap
4.如果还是想不出来,试试其它的能扯上的数据结构和算法....还是不行就硬想吧...


Median of a two sorted array

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays.

Given A=[1,2,3,4,5,6] and B=[2,3,4,5], the median is 3.5.

Given A=[1,2,3] and B=[4,5], the median is 3.

在线性时间复杂度之后, 还有一种更有的logn的解法.

logn用的是二分法, 二分法的本质是, 通过O(1)的时间把一个n的问题变为n2的问题.

这道题可以理解为一个更广泛的问题, 在logk的时间里找到第k大…诸如此类的有找到median, 找第1大,第2大…第k大的问题.

这道题里面: k=m+n2

这里面的算法是这样的:

1.首先我们想用二分法找到第k个数,即用logk的时间找到第k大的数,这道题稍微难理解的一点,我个人认为是如下:

正常的二分法是经过一次运算,有一半的数据量被筛除淘汰了,比如一棵二叉树上查找,你往下左走一步,右边的那颗子树就出局了;往右走一步,左面那个子树的数据就都不要了;

而这道题稍微拐一点弯的地方就在于,数据量本身直观的不是k,而且好像扔掉了k2,剩下的数据量跟二分之一有关,但好像也不是一半,然后稀里糊涂的就有点绕懵了…

我们换一个角度理解这道题运用的二分法思想,假设A,B两个排过序的数组都无限长,那么我们想找到两个数组的第k大,备选的是A的前k大的数和B的前k大的数,你也不知道在哪个里,不过肯定就这两个里面跑不了.我们通过一次O(1)的运算,排除了k2个肯定不是第k大的,由找第k大的元素变成找第(k-k2)即k2大的,所以搜索范围变成了在A剩下的数里面前k2大的,和B剩下的里面k2大的. 总共的筛选范围缩小了一半, oh yeah!

我觉得我用这种方法捋清了一点,但是说完感觉没什么卵用…

2.具体程序的逻辑如下:

对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1] , 若n为偶数,则中位数(a[n / 2] + a[n / 2 + 1]) / 2
如果我们可以在两个数列中求出第K小的元素,便可以解决该问题

不妨设数列A元素个数为n,数列B元素个数为m,各自升序排序,求第k小元素
取A[k / 2] B[k / 2] 比较,

如果 A[k / 2] > B[k / 2] 那么,所求的元素必然不在B的前k / 2个元素中(证明反证法)

k - k / 2小元素,于是得到了数据规模变小的同类问题,递归解决

如果 k / 2 大于某数列个数,所求元素必然不在另一数列的前k / 2个元素中,同上操作就好。


卖股票

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Given an example [3,2,3,1,2], return 1

Maximum subarray

Given an array of integers, find a contiguous subarray which has the largest sum.

Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

对于所有位置n, 求前n个数的和, 然后每个数减前面最小的, 搞定啦~

这两道题就像孙红雷和牛头梗,分开看的时候有点分马牛不相及,放一起仔细一看就是好兄弟啊~

maximum subarray II

Given an array of integers, find two non-overlapping subarrays which have the largest sum.

The number in each subarray should be contiguous.

Return the largest sum.

For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

每个位置砍一刀,左边做一次上道题,右边再做一次,得解~

Subarray sum

Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.

Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3].

加个hashtable, 搞定~

Subarray sum closest

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.

Given [-3, 1, 1, -3, 5], return [0, 2], [1, 3], [1, 1], [2, 2] or [0, 4].

排个序,具体写题的时候再补充.

写程序的主体事项

分好模块,每个函数模块就干一件事情,不要把所有的东西堆在一起,容易出问题还不易读懂.

Two sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.

You may assume that each input would have exactly one solution

numbers=[2, 7, 11, 15], target=9

return [1, 2]

O(n)时间O(n) 空间的很容易达到.

O(1)空间的时候,时间就不再是O(n)了,而是O(nlogn)了

前后两个指针head和tail, 如果相加大于target, tail左移,否则head右移.

题外话:二分法也是两根指针:进行了一次操作,把一根指到了中间而已

3 Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Notice

Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)

The solution set must not contain duplicate triplets.

For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)

(-1, -1, 2)

排序一下, for a这个最小的,找b+c = -a的

Partition array

Given an array nums of integers and an int k, partition the array (i.e move the elements in “nums”) such that:

All elements < k are moved to the left

All elements >= k are moved to the right

Return the partitioning index, i.e the first index i nums[i] >= k.

Notice

You should do really partition in array nums instead of just counting the numbers of integers smaller than k.

If all elements in nums are smaller than k, then
9a16
return nums.length

If nums = [3,2,2,1] and k=2, a valid answer is 1.

大于的右移,小于的左移

Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second.

Notice

It’s NOT necessary to keep the original order of lower-case letters and upper case letters.

For “abAcD”, a reasonable answer is “acbAD”

这道题的注意点是, swap是一个不能保证元素之间相对位置的操作,各种sort是否stable如下

1.quick sort 不稳定
2.merge sort 稳定
3.各种O(n^2)的算法:冒泡,选择, 插入等   稳定
4.heap sort 不稳定


这道题的总结是,不知道啥是选择和插入排序…回头看看..

Sort colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Notice

You are not suppose to use the library’s sort function for this problem.

You should do it in-place (sort numbers in the original array).

Given [1, 0, 1, 2], sort it in-place to [0, 1, 1, 2].

counting sort 数数有多少个0, 多少个1, 多少个2

先partition等于0 和不等于0 的 分两部分,然后再把不等于0 的那部分再partition

Sort Colors II

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, … k.

Notice

You are not suppose to use the library’s sort function for this problem.

Given colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-place to [1, 2, 2, 3, 4].

数组排序…尼玛…直接的我难以置信…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: