Comments (3)
haha~ Time Limit Exceeded
def Container_With_Most_Water(arr):
maxArea = 0;
arrLen = len(arr);
for i in range(arrLen):
for j in range(i+1,arrLen):
height = arr[i] if arr[i] < arr[j] else arr[j];
if maxArea < height * (j - i):
maxArea = height * (j - i);
return maxArea
print(Container_With_Most_Water([3, 4, 3, 8, 2, 7, 9]));
正解
def Container_With_Most_Water(arr):
maxArea = 0;
arrLen = len(arr);
i,j = 0,arrLen -1;
while (i < j):
height = arr[i] if arr[i] < arr[j] else arr[j];
maxArea = max(maxArea, height*(j-i));
if arr[i] < arr[j]:
i +=1;
else:
j -=1;
return maxArea;
print(Container_With_Most_Water([3, 4, 3, 8, 2, 7, 9]));
from daily-algorithms.
刚开始题设是相邻的线,并且可以包含 Y 轴,写了一个答案:
function resolve(arr) {
var max = 0;
var maxIndex = 0;
for (var i = 0, len = arr.length; i < len; i++) {
var tmp = Math.min(arr[i], arr[i - 1] || Number.POSITIVE_INFINITY);
if (tmp > max) {
tmp = max;
maxIndex = [i, i - 1];
}
}
return maxIndex;
}
resolve([3, 4, 3, 8, 2, 7, 9]); // -> [6, 5]
from daily-algorithms.
def resolve_v1(alist):
"""
一个非负整数序列,把(i, 0)和(i, ai)连线;
求相临边围成的最大面积,包含y轴;
"""
buckets = [alist[0]] + list(alist)
max_index, max_result = 0, 0
for i in range(len(alist)):
if min(buckets[i] + buckets[i + 1]) > max_result:
max_index, max_result = i, min(buckets[i], buckets[i + 1])
return buckets[max_index], buckets[max_index + 1]
def resolve_v2(alist):
"""
去除边界相临的限制条件,且不包含y轴;
法1: 暴力查询法,O(n^2)
"""
max_index = [0, 0]
max_result = 0
for i in range(len(alist) - 1):
for j in range(i + 1, len(alist)):
area = (j - i) * min(alist[i], alist[j])
if area > max_result:
max_result = area
max_index = [i, j]
return max_result, max_index
def resolve_v2(alist):
"""优化算法,O(n)"""
head, tail = 0, len(alist) - 1
max_result = 0
max_index = [head, tail]
while head < tail:
height = min(alist[head], alist[tail])
t_area = height * (tail - head)
if t_area > max_result:
max_result = t_area
max_index = [head, tail]
if alist[head] > alist[tail]:
tail -= 1
else:
head += 1
return max_result, max_index
from daily-algorithms.
Related Issues (16)
- Two Sum HOT 24
- Roman To Integer HOT 2
- 关于 daily-algorithms
- Longest Common Prefix HOT 2
- 3Sum HOT 14
- 3Sum Closest HOT 3
- Letter Combinations of a Phone Number HOT 4
- 4Sum HOT 4
- Reverse Integer HOT 8
- Add Two Numbers HOT 11
- Find The Longest Substring HOT 7
- Find The Longest Palindromic Substring HOT 8
- Regular Expression Matching HOT 3
- Palindrome Number HOT 3
- Integer to Roman HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daily-algorithms.