11. Container With Most Water
生活随笔
收集整理的這篇文章主要介紹了
11. Container With Most Water
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
就是說,x軸上在1,2,...,n點(diǎn)上有許多垂直的線段,長度依次是a1, a2, ..., an。找出兩條線段,使他們和x抽圍成的面積最 大。面積公式是 Min(ai, aj) X |j - i| 最簡單的做法,for循環(huán)兩次,時(shí)間復(fù)雜度是O(N的平方) public int maxArea(int[] height) {int n = height.length;int maxArea = 0;int i = 0;int j = n-1;while(i<j){int temp = (j - i)*(height[i]>height[j]?height[j]:height[i]);//每次面積if(height[i] < height[j]) //木板低的一方移動(dòng){i++;}else{j--;}if(maxArea < temp){maxArea = temp;}}return maxArea;}轉(zhuǎn)載于:https://my.oschina.net/datacube/blog/702443
總結(jié)
以上是生活随笔為你收集整理的11. Container With Most Water的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【译】无限滚动加载最佳实践
- 下一篇: 线程基础知识系列(三)线程的同步