Maximum Difference Between Two Elements
2016-03-18 14:35
323 查看
Maximum difference between two elements such that larger element appears after the smaller number
Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].Examples: If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2). If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9)
Method 1 (Simple)
Use two loops. In the outer loop, pick elements one by one and in the inner loop calculate the difference of the picked element with every other element in the array and compare the difference with the maximum difference calculated so far.
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Method 2 (Tricky and Efficient)
In this method, instead of taking difference of the picked element with every other element, we take the difference with the minimum element found so far. So we need to keep track of 2 things:
1) Maximum difference found so far (max_diff).
2) Minimum number visited so far (min_element).
Time Complexity: O(n)
Auxiliary Space: O(1)
Like min element, we can also keep track of max element from right side. See below code suggested by Katamaran
Method 3 (Another Tricky Solution)
First find the difference between the adjacent elements of the array and store all differences in an auxiliary array diff[] of size n-1. Now this problems turns into finding the maximum sum subarray of this difference array.
Thanks to Shubham Mittal for suggesting this solution.
Output:
98
This method is also O(n) time complexity solution, but it requires O(n) extra space
Time Complexity: O(n)
Auxiliary Space: O(n)
We can modify the above method to work in O(1) extra space. Instead of creating an auxiliary array, we can calculate diff and max sum in same loop. Following is the space optimized version.
Time Complexity: O(n)
Auxiliary Space: O(1)
Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same problem
contiguous
畅通词汇 英 [kən'tɪɡjuəs]
美 [kən'tɪɡjuəs]
adj.邻近的;连续的;接触的
释义常用度分布图海词统计
连续的
邻近的
接触的
副词: contiguously名词: contiguousness
new
very close or connected in space or time;
"contiguous events"
"immediate contact"
"the immediate vicinity"
"the immediate past"
connecting without a break; within a common boundary;
"the 48 conterminous states"
"the contiguous 48 states"
having a common boundary or edge; abutting; touching;
"Rhode Island has two bordering states; Massachusetts and Conncecticut"
"the side of Germany conterminous with France"
"Utah and the contiguous state of Idaho"
"neighboring cities"
用作形容词 (adj.)
California and Mexico are contiguous.
加利福尼亚和墨西哥相邻。
Extents determine how data is striped across multiple containers and ensure that the data pages in a container are contiguous.
区段确定如何跨越多个容器进行数据分段,并确保一个容器里的数据页面是连续的。
very close or connected in space or time;
"contiguous events"
"immediate contact"
"the immediate vicinity"
"the immediate past"
connecting without a break; within a common boundary;
"the 48 conterminous states"
"the contiguous 48 states"
having a common boundary or edge; abutting; touching;
"Rhode Island has two bordering states; Massachusetts and Conncecticut"
"the side of Germany conterminous with France"
"Utah and the contiguous state of Idaho"
"neighboring cities"
adjacent
常用词汇 英 [ə'dʒeɪsnt]
美 [ə'dʒeɪsnt]
adj.邻近的;毗连的;接近的
释义常用度分布图海词统计
邻近的
毗连的
接近的
副词: adjacently
new
adjacent是什么意思,词典释义与在线翻译:
详尽释义
双解释义
行业释义
英英释义
Adjective:nearest in space or position; immediately adjoining without intervening space;
"had adjacent rooms"
"in the next room"
"the person sitting next to me"
"our rooms were side by side"
having a common boundary or edge; abutting; touching;
"Rhode Island has two bordering states; Massachusetts and Conncecticut"
"the side of Germany conterminous with France"
"Utah and the contiguous state of Idaho"
"neighboring cities"
near or close to but not necessarily touching;
"lands adjacent to the mountains"
"New York and adjacent cities"
adjacent的用法和样例:
例句
用作形容词 (adj.)The house adjacent to ours has been sold.
与我们家邻接的房子已经售出。
His house is adjacent to mine.
他的寓所与我的相毗连。
The planes landed on adjacent runways.
这些飞机在毗连的跑道上降落。
The island of Cuba is adjacent to Florida.
古巴岛接近佛罗里达。
The hospital is adjacent to a church.
这家医院紧挨着一座教堂。
相关文章推荐
- jquery的checkbox取值赋值选中
- jQuery.deferred对象使用详解
- 详解js跨域问题
- css去掉a标签点击后的虚线框,outline,this.blur()
- JS中的二叉树遍历详解
- JS 页面加载触发事件 document.ready和onload的区别
- 前端必须掌握30个CSS3选择器(转)
- 一个好用的测试服务器接口的工具(post请求,xml,json等数据格式)
- 支持大多主流浏览器的jquery判断图片上传的大小
- framework和javascript和jsp的关联
- JavaScript知识结构
- js公用方法
- js对象中动态读取属性值 动态属性值 js正则表达式全局替换
- css3属性-webkit-font-smoothing
- 在Ubuntu 15.10与fedora 23下修改mysql 5.7.11的编码方式为utf8
- React入门教程(一)
- JavaScript快速入门
- js基本操作(基础快速入门篇)
- Effective Java读书笔记一
- h5标签语义化