diff options
Diffstat (limited to 'candy.cpp')
-rw-r--r-- | candy.cpp | 37 |
1 files changed, 0 insertions, 37 deletions
diff --git a/candy.cpp b/candy.cpp deleted file mode 100644 index 83915b0..0000000 --- a/candy.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "leetcode.h" - -class Solution { -public: - int candy(vector<int> &ratings) { - if (ratings.size() <= 1) - return ratings.size(); - int ans = 0, up = 0, down = 0, oldSign = 0; - for (int i = 1; i < ratings.size(); i++) { - int newSign = ratings[i] > ratings[i - 1] ? 1 - : ratings[i] < ratings[i - 1] ? -1 - : 0; - if ((oldSign > 0 && newSign == 0) || (oldSign < 0 && newSign >= 0)) { - ans += helper(up) + helper(down) + max(up, down); - up = 0; - down = 0; - } - if (newSign > 0) - up++; - if (newSign < 0) - down++; - if (newSign == 0) - ans++; - oldSign = newSign; - } - ans += ramp(up) + ramp(down) + max(up, down) + 1; - } - -private: - int helper(int n) { return (n * (n + 1)) / 2; } -}; - -int main() { - Solution obj; - vector<int> ratings = {}; - cout << obj.candy(ratings); -} |