Given an unsorted array provide two indices n1 and n2 such that if we only sort the elements between n1 and n2,then the whole array will become sorted.
n1-n2 should be as minimum as possible.

http://www.careercup.com/question?id=4345015

参考上面一位大神的解答,非常精彩,不过理解起来有点费力。

Let A be the array and T = 0.
While A[T] <= A[T + 1] we increase T be one unit.Doing so , we find the longest increasing sequence from the start of the array.
Now, let P = T + 1. The idea is to find if after A[T] are elements smaller than it.
So , while(P < A.length && T >= 0) if(A[P] < A[T]) --T; else ++P;
After these operations, T will be actually n1, because we know that A[1..T] is increasing(from first step) and there is no element in the array smaller than A[T] after position T.
We use the same idea to find the longest increasing array that has A[A.length - 1].
To do so, let T2 = A.length - 1.While A[T2] >= A[T2 - 1] --T2.
Now we must find if in the rest of the array is a bigger element than the one in the A[T2]. We could go until position T found previously, and if there is a P so A[P] >= A[T2] we increase T2.Finally T2 will actually be n2. Time complexity O(N) and space complexity O(1). There are other solutions in O(N logN) time and O(1) space using sorting or O(N log N) time and O(N) space using heaps.

就是找出左边的上升序列,然后在它右边找有没有小于它的,然后不断缩小上升序列的区间,直到序列右边没有小于它的值。

对于右边也找出上升序列,然后往左边找有没有大于它的,然后也不断缩小上升序列的区间。

这样两个区间的中间就是要排序的最小区间了。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4
 5 void solve(vector<int> &a)
 6 {
 7     int i = 0;
 8     while(i < a.size() - 1)
 9     {
10         if (a[i] <= a[i+1])
11             i++;
12         else
13             break;
14     }
15
16     int k = i + 1;
17
18     while(i >= 0 && k < a.size())
19     {
20         if (a[k] < a[i])
21             i--;
22         else
23             k++;
24     }
25
26     int j = a.size() - 1;
27
28     while(j >= 1)
29     {
30         if (a[j-1] <= a[j])
31             j--;
32         else
33             break;
34     }
35
36     k = j - 1;
37
38     while(k > i && j < a.size())
39     {
40         if (a[k] > a[j])
41             j++;
42         else
43             k--;
44     }
45
46     cout << i + 1 << ' ' << j - 1 << endl;
47 }
48
49 int main()
50 {
51     vector<int> a;
52     for(int i = 0; i < 10; i++)
53         a.push_back(i);
54
55     solve(a);
56
57     vector<int> b;
58     b.push_back(1);
59     b.push_back(2);
60     b.push_back(3);
61     b.push_back(5);
62     b.push_back(4);
63
64     solve(b);
65
66     vector<int> c;
67     for(int i = 10; i >= 1; i--)
68         c.push_back(i);
69     solve(c);
70 }

转载于:https://www.cnblogs.com/chkkch/archive/2012/11/05/2756070.html

[CareerCup][Google Interview] 找出最小排序次数相关推荐

  1. [CareerCup][Google Interview] 找出现次数

    Given a sorted array and a number n.How can u find the number of occurance of n in the array . shoul ...

  2. 在哪里能收到python实例代码-Python找出最小的K个数实例代码

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 这个题目完成的思路有很多,很多排序算法都可以完成既定操作,关键是复 ...

  3. 堆排序及从10亿个数据中找出最小或最大的10个数

    高频面试题目 一.堆排序 1.基础知识 * ------基本知识: * 1. 堆数据结构特征: * 大顶堆:所有父节点大于等于左右子节点,arr[i] >= arr[2i+1] &&am ...

  4. Java黑皮书课后题第7章:7.10(找出最小元素的下标)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素下标。编写测试程序,提示用户输入10个数字,调用这个方法返回最小值的下标(多个则最小

    7.10(找出最小元素的下标)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素下标.编写测试程序,提示用户输入10个数字,调用这个方法返回最小值的下标(多个则返回最小的下标) 题目 题目描述 ...

  5. Java黑皮书课后题第7章:7.9(找出最小元素)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素。编写测试程序,提示用户输入10个数字,调用这个方法返回最小值,并显示这个最小值

    7.9(找出最小元素)使用下面的方法头编写一个方法,求出一个整数数组中的最小元素.编写测试程序,提示用户输入10个数字,调用这个方法返回最小值,并显示这个最小值 题目 题目描述与运行示例 破题 代码 ...

  6. JavaScript 4行代码找出重复出现次数最多的元素及次数

    JavaScript 4行代码找出重复出现次数最多的元素及次数 今天遇到的一个面试题,看起来比较简单,解题却用几个循环嵌套,感觉不对劲,后面又想了一个比较优雅的解法,如下: 解题思路 为数组内每个相同 ...

  7. 80x86汇编语言 循环结构 找出最小的偶数并在屏幕上显示 求出数组的平均值显示在屏幕上

    题目1 写一个完整的80X86汇编语言程序:键盘输入15个数据(转换成数值,存储到一维数组中,数值的长度为字),找出最小的偶数并在屏幕上显示,若没有偶数则显示"没有偶数!". .d ...

  8. 第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element)

    第七章第十题(找出最小元素的下标)(Find the subscript of the smallest element) 7.10(找出最小元素的下标)编写一个方法,求出整数数组中最小元素的下标.如 ...

  9. php旋转数组找出最小的,LeetCode 153 寻找旋转排序数组中的最小值

    链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ...

最新文章

  1. csv科学计数法变为普通数字_数据科学家常遇到的10个错误
  2. autojs怎么post协议_超9成人都理解错了HTTP中GET与POST的区别
  3. boost::json::to_string用法的测试程序
  4. php的常量和变量的区别,变量和常量的区别_在php当中常量和变量的区别
  5. Jenkins 设置镜像_docker+jenkins自动化部署
  6. vijos-1003等价表达式
  7. Lync Server外部访问系列PART1:准备边缘
  8. 羽毛球 中的 切球 与 旋球
  9. 【计量经济学导论】08. 平稳时间序列
  10. 如何关闭访达窗口_UG NX软件基础操作,如何自定义软件快捷键
  11. 广告投放系统常用的几种防作弊方式
  12. 【obs-studio开源项目从入门到放弃】windows 窗口采集和桌面采集的解决方案
  13. android 距离传感器 api,Android传感器API之:近距离感应Proximity功能源码
  14. reactos操作系统实现(39)
  15. QA在业务变动中如何维护测试用例?
  16. 【Flutter实战 BLoC模式 RxDart Provider模式】
  17. 韩信点兵(hanxin)
  18. vue导出多页PDF截断问题
  19. 分块烘焙 + 动态挂载光照贴图
  20. 【纯属娱乐】随机森林预测双色球

热门文章

  1. HTML画笔移出画布停止,html5 canvas画布无法清除
  2. android 判断服务是否活动,Android:我如何获得当前的前台活动(从服务)?
  3. php随机访问文章,zblog PHP调用热门文章、随机文章和热评文章代码
  4. 如何调整html中音乐播放器的大小,html5实现在线响应式音乐播放器
  5. linux mariadb忘记密码,忘记MySQL/MariaDB root密码的解决方法
  6. nginx 过滤post报文 防火墙_十八般武艺之Nginx踩坑总结
  7. 火绒弹窗拦截_弹窗广告的克星:火绒安全软件图文使用教程
  8. linux文件权限其他人,3-3 修改Linux中的文件 拥有者、所属组和其他人的权限
  9. matlab dotchart,MATLAB中如何用对数方式显示图形坐标?
  10. linux下比较文件并输出,Linux使用diff命令比较文件的方法