题解 | #编程题2#

编程题2

http://www.nowcoder.com/practice/1aeba6ba677949249aba82d81edc3fea

两种思路
思路1(会报超时)

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
using namespace std;

int main()
{
    int n;
    scanf("%d\n",&n);
    int array[500001];

    int i = 0;
    for (i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }

    int dp = array[0] * array[0];
    int max_v = dp;
    for (int i = 1; i < n; i++)
    {
        int min_add = array[i];
        int sum = 0;
        for (int j = i; j >= 0; j--)
        {
            min_add = min(min_add,array[j]);
            sum += array[j];
            max_v = max(max_v, min_add * sum);
        }
        dp = max(dp, max_v);
    }
    printf("%lld", dp);
    return 0;
}

思路2

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{//输入
    int n;
    int array[500001];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&array[i]);
    }

    int ans = 0;
    for(int j=100;j>=0;j--)
    {
        int sum = 0;int min_num = 101;
        for(int i=0;i<n;i++)
        {
            if(array[i]<j)
            {
                sum = 0;
                min_num = 101;
            }
            else
            {
                sum += array[i];
                min_num = min(min_num,array[i]);
                ans = max(ans,sum*min_num);
            }
        }
    }
    printf("%d",ans);
    return 0;
}

全部评论

相关推荐

我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务