字节4.13笔试第四题 楼顶数楼问题



#include <iostream>
#include <vector>
using namespace std;
 
void getRes(vector<int> &heights, vector<int> &res, int len)
{
    int j, cnt;
    for (int i = 0; i < len; ++i)
    {
        cnt = 0;
        j = i - 1;
        while (j >= 0)
        {
            if (heights[j] <= heights[i])
                ++cnt;
            else
                break;
            --j;
        }
        j = i + 1;
        while (j < len)
        {
            if (heights[j] <= heights[i])
                ++cnt;
            else
                break;
            ++j;
        }
        res.push_back(cnt);
    }
}
 
int main()
{
    int t, n, temp;
    vector<int> heights;
    vector<int> res;
 
    cin >> t;
    for (int i = 0; i < t; ++i)
    {
        cin >> n;
        heights.clear();
        res.clear();
        for (int j = 0; j < n; ++j)
        {
            cin >> temp;
            heights.push_back(temp);
        }
        getRes(heights, res, n);
 
        int k;
        for (k = 0; k < (res.size() - 1); ++k)
            cout << res[k] << " ";
        cout << res[k] << endl;
    }
 
    system("pause");
    return 0;
}
只会用暴力解,而且只过了71%的用例,求求大佬们给个思路。

#字节跳动##笔试题目#
全部评论
自测了用例,都能过
2
送花
回复 分享
发布于 2020-09-01 11:28
单调栈可以么?用单调栈找左边和右边比他大的,我没做题的,我只是猜猜
点赞
送花
回复 分享
发布于 2020-04-16 00:30
秋招专场
校招火热招聘中
官网直投
维护两个单调栈
点赞
送花
回复 分享
发布于 2020-04-16 00:48
lc原题
点赞
送花
回复 分享
发布于 2020-04-16 10:40

相关推荐

1 1 评论
分享
牛客网
牛客企业服务