找LeetCode上简单的题目来撑过30天啦(DAY13)

题号:11 标题:Container With Most Water 难度:Medium

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
Notice that you may not slant the container.

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:
Input: height = [1,1]
Output: 1

Example 3:
Input: height = [4,3,2,1,4]
Output: 16

Example 4:
Input: height = [1,2,1]
Output: 2

Constraints:
• n == height.length
• 2 <= n <= 105
• 0 <= height[i] <= 104

我的程序码

int maxArea(int* height, int heightSize){
    int area=0,area_temp;
    int r,l,temp;
    l=0; r=heightSize-1;
    while(r>l){

        printf("%d,%d\n",r,l);
        if(height[r]>height[l]){
            area_temp =  height[l] * (r-l);
            temp = height[r];
            l++;
            while(temp> height[l]){
                if(r>l){
                    break;
                }
                l++;
            }
        }else{
            area_temp =  height[r] * (r-l); 
            temp = height[l];
            r--;
            while(temp> height[r]){
                if(r>l){
                    break;
                }
                r--;
            }
        }
        
        if(area_temp>area){
            area = area_temp;
        } 
    }
    
    return area;
}

DAY13心得
昨天要感谢台积电辣妹,今天要感谢男友给我解题提示,不然我一直超时,写到现在觉得最难的还是特殊CASE跟时间复杂度,然後我真的要去睡了


<<:  DAY 16- DHKE、ECDH、ElGamal

>>:  【Day14】:STM32辗压Arduino的功能—TIM(上)

DAY24:Broadcast receiver之简介

今天要来介绍广播接收程序,先从Broadcast receiver的运作机制,它的运作机制包含:送出...

JavaScript Day24 - Promise(1)

ES6:Promise Promise:代表一个即将成功或失败的非同步操作 会有这几状态: 搁置 (...

14 - tig - git 的文字介面

Git 是开发者们最常接触到的工具之一,大部分的专案都使用它作为版本控制的工具。使用者可以直接用 g...

Day29 Swagger

年轻人不要看到标题就兴奋好吗? 以目前前後端分离的趋势,前端及後端工程师势必会由两个人以上来担任,那...

拥抱传统,享受数位便利

不管是数位还是手写,各有各种的好处,端看每个人的需求。来看数位及传统载体的优劣势:手写的最大好处是在...