Code Monkey home page Code Monkey logo

Comments (3)

pengkobe avatar pengkobe commented on May 31, 2024 1

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.

barretlee avatar barretlee commented on May 31, 2024

刚开始题设是相邻的线,并且可以包含 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.

YabZhang avatar YabZhang commented on May 31, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.