美文网首页
[LeetCode] 135. Candy (hard)

[LeetCode] 135. Candy (hard)

作者: 弱花 | 来源:发表于2018-11-02 11:33 被阅读0次

原题

思路:
前后两遍遍历

class Solution
{
public:
  int candy(vector<int> &ratings)
  {
    vector<int> res;
    int len = ratings.size();
    for (int i = 0; i < len; i++)
    {
      res.push_back(1);
    }

    for (int j = 0; j < len; j++)
    {
      if (j > 0 && ratings[j] > ratings[j - 1])
      {
        res[j] += res[j - 1];
      }
    }


    for (int k = len - 1; k >= 0; --k)
    {
      if (k < len - 1 && ratings[k] > ratings[k + 1] && res[k] <= res[k+1])
      {
        res[k] = res[k + 1]+1;
      }
    }
    int sum = 0;
    for (auto i : res)
    {
      cout<<i<<endl;
      sum += i;
    }
    return sum;
  }
};

相关文章

  • [LeetCode] 135. Candy (hard)

    原题 思路:前后两遍遍历

  • 经典算法题:分发糖果

    135. 分发糖果[https://leetcode.cn/problems/candy/] 难度:困难 n 个孩...

  • 135. Candy [Hard] DP

    135. Candy

  • Leetcode 135. Candy

    There are N children standing in a line. Each child is as...

  • Leetcode 135. Candy

    文章作者:Tyan博客:noahsnail.com[http://noahsnail.com] | CSDN[ht...

  • 135. Candy

    题目 思路 dp[i]:记录i的获取糖果树 从左向右扫描,保证一个方向上分数更大的糖果更多 从右向左扫描,保证另一...

  • 135. Candy

    题目分析 There are N children standing in a line. Each child ...

  • 135. Candy

    There are N children standing in a line. Each child is as...

  • 135. Candy

    题目描述:N个孩子坐在一排,每个孩子分配一个等级值,按如下要求给每个孩子分糖: 每个孩子至少有一个 等级高的孩子比...

  • 135. Candy

    There are N children standing in a line. Each child is as...

网友评论

      本文标题:[LeetCode] 135. Candy (hard)

      本文链接:https://www.haomeiwen.com/subject/chiqxqtx.html