[CareerCup] 17.6 Sort Array 排列数组
17.6 Given an array of integers, write a method to find indices m and n such that if you sorted elements m through n, the entire array would be sorted. Minimize n - m (that is, find the smallest such sequence).
为了更好的理解题意,我们通过一个例子来分析,比如我们有如下的数组:
1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19
那么我们可以按照递增顺序将其分为三个部分:
left: 1, 2, 4, 7, 10, 11
middle: 7, 12
right: 6, 7, 16, 18, 19
只要从左右两端开始往中间搜索即可,遇到不是递增的数字就停止,然后我们要给中间的部分排序,排完序我们看left的最右边的数字是否比middle的最左边的数字小,right的最左边的数字是否比middle的最右边的数字大,如果不是,我们要shift_left和shift_right,向左边,我们再来验证整个数列是否是有序的,如果不是有序的,则说明不存在这样的index,参见代码如下:
int find_end_of_left_subsequence(vector<int> array) {
for (int i = ; i < array.size(); ++i) {
if (array[i] < array[i - ]) {
return i - ;
}
}
return array.size() - ;
}
int find_start_of_right_subsequence(vector<int> array) {
for (int i = array.size() - ; i >= ; --i) {
if (array[i] > array[i + ]) {
return i + ;
}
}
return ;
}
int shrink_left(vector<int> array, int min_idx, int start) {
int cmp = array[min_idx];
for (int i = start - ; i >= ; --i) {
if (array[i] <= cmp) {
return i + ;
}
}
return ;
}
int shrink_right(vector<int> array, int max_idx, int start) {
int cmp = array[max_idx];
for (int i = start; i < array.size(); ++i) {
if (array[i] >= cmp) {
return i - ;
}
}
return array.size() - ;
}
bool validate(vector<int> array, int left_idx, int right_idx) {
vector<int> middle(right_idx - left_idx + );
for (int i = left_idx; i <= right_idx; ++i) {
middle[i - left_idx] = array[i];
}
sort(middle.begin(), middle.end());
for (int i = left_idx; i <= right_idx; ++i) {
array[i] = middle[i - left_idx];
}
for (int i = ; i < array.size(); ++i) {
if (array[i - ] > array[i]) {
return false;
}
}
return true;
}
void find_unsorted_sequence(vector<int> array) {
int end_left = find_end_of_left_subsequence(array);
if (end_left >= array.size() - ) return;
int start_right = find_start_of_right_subsequence(array);
int max_idx = end_left;
int min_idx = start_right;
for (int i = end_left + ; i < start_right; ++i) {
if (array[i] < array[min_idx]) {
min_idx = i;
}
if (array[i] > array[max_idx]) {
max_idx = i;
}
}
int left_idx = shrink_left(array, min_idx, end_left);
int right_idx = shrink_right(array, max_idx, start_right);
if (validate(array, left_idx, right_idx)) {
cout << "True: " << left_idx << " " << right_idx << endl;
} else {
cout << "False: " << left_idx << " " << right_idx << endl;
}
}
[CareerCup] 17.6 Sort Array 排列数组的更多相关文章
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
- [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二
Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...
- js运用sort对json 数组进行排序
Array.sort()方法是用来对数组项进行排序的 ,默认情况下是进行升序排列.sort() 方法可以接受一个 方法为参数. sort()排序时每次比较两个数组项都回执行这个参数,并把两个比较的数组 ...
- Javascript中Array(数组)对象常用的几个方法
Javascript中Array数组的几个常用方法 pop() --获取数组中末尾的元素 shift() --获取数组中首位元素 push() --在数组中末尾增加元素 slice() --按照下 ...
- 【LEETCODE】42、922. Sort Array By Parity II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- JS对象 数组排序sort() sort()方法使数组中的元素按照一定的顺序排列。 语法: arrayObject.sort(方法函数)
数组排序sort() sort()方法使数组中的元素按照一定的顺序排列. 语法: arrayObject.sort(方法函数) 参数说明: 1.如果不指定<方法函数>,则按unicode码 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:Array(数组) 对象
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- [转贴]JavaScript中Array(数组)的属性和方法
数组有四种定义的方式 使用构造函数:var a = new Array();var b = new Array(8); var c = new Array("first", &qu ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
随机推荐
- hpunix下11gRac的安装
一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...
- Android之Inflate()
Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. Android上还有一个与Inflate( ...
- while do while以及穷举和迭代
今天的新内容1:while循环 格式: while() { } 初始状态要在循环外提前规定 状态改变要写在花括号里面 括号内是循环条件 for循环与while循环的对比: 2:do while 不管循 ...
- TextView展开和收回
第一步:接口请求返回数据 第二步:使用handler和textview.getLineCount方法判断是否超过指定行数: community_desc_more.setVisibility(View ...
- js实现冒泡排序
冒泡排序 冒泡排序(Bubble Sort),是一种计算机科学领域的较简单的排序算法. 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到 ...
- Android HttpClient post MultipartEntity - Android 上传文件
转自[http://blog.csdn.net/hellohaifei/article/details/9707089] 在Android 中使用HttpClient,MultipartEntity ...
- Dijkstra(变形) POJ 1797 Heavy Transportation
题目传送门 题意:求1到n的最大载重量 分析:那么就是最大路上的最小的边权值,改变优先规则. #include <cstdio> #include <algorithm> #i ...
- 【转载】Android内存泄露
相信一步步走过来的Android从业者,每个人都会遇到OOM的情况.如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力.今天我们就谈谈在Android平台下内存的管理之道,开始今 ...
- qq邮箱邮我组件
http://openmail.qq.com/cgi-bin/qm_help_mailme?sid=uvkgSu7e0aOrc0Qc&t=open_mailme 邮我 使用"邮我&q ...
- iOS之05-三大特性之封装
本次主要学习面向对象的三大特性:封装.继承和多态中的封装 封装 1. 好处 降低耦合率 可重复调用类中的属性 提高安全性,外部不能随便修改变量的值,保证了数据的安全性 2. set方法 1.作用:提供 ...