题解 | #牛的生长情况#

牛的生长情况

https://www.nowcoder.com/practice/5f67258999bd4e61a361f4d3017a3fd4

class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param weights int整型vector
     * @return int整型vector
     */
    vector<int> weightGrowth(vector<int>& weights) {
        // write code here
        int size = weights.size();
        vector<int> res(size, -1);
        stack<int> stk;
        stk.push(0);
        for (int i = 1; i < size; i++) {
            while (stk.size() && weights[i] > weights[stk.top()]) {
                res[stk.top()] = i-stk.top();
                stk.pop();
            } 
            stk.push(i);
        }
        return res;
    }
};

全部评论

相关推荐

12-24 14:26
东北大学 Java
一只乌鸦:重邮+东北,好经典的学校
最后再改一次简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务