---恢复内容开始---

11. Container With Most Water给n个非负数a1,a2,...an,每个代表一个坐标系中的坐标,n个竖直的坐标,线的起始和结束是(i,ai)(i,0)。找到两条线,和x轴一起形成一个容器,可以装下最多的水

Medium

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

Note: You may not slant the container and n is at least 2.

 

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:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

特殊收获:在编程时,处理输入的时候,比如给vector之类的容器赋值,希望输入数字后按换行键就算输入结束,但常规的while(cin>>
n)和scanf("")!=EOF都会失效,只有按ctrl+Z才能退出,肥肠不爽,so,终于发现这个大利器!!(ch1=cin.get())!='\n')而且惊天
大发现,竟然还可以吃了吐,cin.putback(ch1);然后该读啥读啥,真是emm完美,but我运行的时候总感觉有点慢。。或许这种复杂的输入处理
方式emm会降速??那就得等后期实践下看会不会超时了希望不要GG。。
int main()
{
    Solution s;
    vector<int> input;
    int temp=0;
    char ch1;
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        cin>>temp;
        input.push_back(temp);
    }
    int size=input.size();
    for(int i=0;i<size;i++)
    {
        cout<<input[i]<<endl;
    }
    return 0;
}

Map容器的操作:

#include<iostream>
#include<map>
using namespace std;
int main()
{
    int a[5];
    map<int,int>M;
    //也可以这样直接赋值
    a[0]=1;M[a[0]]++;
    a[1]=2;M[a[1]]++;
    a[2]=2;M[a[2]]++;
    a[3]=4;M[a[3]]++;
    a[4]=1;M[a[4]]++;
    map<int,int>::iterator it;
    for(it=M.begin();it!=M.end();it++)
        cout<<it->first<<' '<<it->second<<endl;
    //用迭代器对容器进行遍历
    map<int,int>::iterator iter;
    map<int,int>water;
    //插入的时候要用make_pair成对插入不然会报错
    water.insert(make_pair(1,2));
    for(iter = water.begin(); iter != water.end() ; iter++)
        cout << iter->first << iter->second << endl;
    return 0;
}

 

这道题我想了很久,最终本来都打算通过vector容器传map通过map value改写cmp排序,然鹅然鹅!!这个竟然用贪心就能做,天了噜,哭的一把鼻涕一把泪,等下我把贪心可行的证明发上来

先排上贪心代码:

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<vector>
#include<list>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;

class Solution {
public:
    int maxArea(vector<int>& height) {
        //not all variable initialize with 0
        int res=0,i=0,j=height.size()-1;
        while(i<j)
        {
            int h=min(height[i],height[j]);
            res=max(res,(j-i)*h);
            while(i<j&&h==height[i]) i++;
            while(i<j&&h==height[j]) j--;

        }
        return res;


    }
};

int main()
{
    Solution s;
    vector<int> input;
    int temp=0;
    char ch1;
    while((ch1=cin.get())!='\n' )
    {
        cin.putback(ch1);
        cin>>temp;
        input.push_back(temp);
    }
    int res=s.maxArea(input);
    cout<<res<<endl;
    return 0;
}

证明:

res的结果如果最终不是最优说明,有比它大的,res由距离和h组成所以(PS:以下是作者脑抽,开始正视自己英语辣鸡的问题so,let's start to write annotition in English,if I can learn espanol well,I hope i can write in it too)

if we want a better res result.It must be in these three situations

better d(distance)

better h

better d&h

but if we compare them we will discover that all these situation had been included in the code.So they're not threatening.

二刷:

对,非常不负责的二刷,Python解法

这个避免完全循环的根基在于,如果当前这个解是最大的。往里推进,一定应该选将小的往里推

class Solution(object):
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        n=len(height)
        l,r=0,n-1
        ans = 0
        while l<r:
            h=min(height[r],height[l])
            tmp=(r-l)*h
            ans=max(ans,tmp)
            if height[l]>height[r]:
                r-=1
            else:
                l+=1
        return ans

solu=Solution()
height=[1,8,6,2,5,4,8,3,7]
print(solu.maxArea(height))

 

 

12 Roman Number Convert

this question really  refreshes my understanding,when i first saw this problem,i felt it's complex.Because i didn't grasp the transformation rules

but because the data range very small,it only looking for a range of 3999,so when we divide this into array,we only need to divide input number into thousands,hundreds,tens and ones.And store the result of the corresponding decimal value in an array in advance.

Now show the code

#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iomanip>
#include<vector>
#include<list>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;

class Solution {
public:
    string intToRoman(int num) {
        string m[4]={"","M","MM","MMM"};
        string c[10]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        string x[10]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        string i[10]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
        return m[num/1000]+c[num%1000/100]+x[num%100/10]+i[num%10];
    }
};

int main()
{
    Solution s;
    int input;
    cin>>input;
    string res=s.intToRoman(input);
    cout<<res<<endl;
    return 0;
}

The great thing of my blog is that my code can be run in the right enviorment directly.This will be a great help of the beginners.

 

posted on 2019-06-19 19:54  黑暗尽头的超音速炬火  阅读(164)  评论(0编辑  收藏  举报