commit 3a1d42abe881264e1da1fb2c4ebb2363e945d6be parent a3761c031bba3168a0c1379494890d375f8ab7f3 Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Mon, 10 Jun 2024 20:59:04 +0200 1 Random Problem Diffstat:
A | Problems/0659.cpp | | | 25 | +++++++++++++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/Problems/0659.cpp b/Problems/0659.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + bool isPossible(const vector<int> &nums) const { + unordered_map<int, int> left, end; + for (const int n : nums) + left[n]++; + + for (const int n : nums) { + if (!left[n]) continue; + left[n]--; + + if (end[n - 1] > 0) { + end[n - 1]--; + end[n]++; + } else if (left[n + 1] > 0 && left[n + 2] > 0) { + left[n + 1]--; + left[n + 2]--; + end[n + 2]++; + } else + return false; + } + + return true; + } +}; diff --git a/README.md b/README.md @@ -443,6 +443,7 @@ for solving problems. | 0653 | Easy | [Two Sum IV - Input is a BST](Problems/0653.cpp) | | 0654 | Medium | [Maximum Binary Tree](Problems/0654.cpp) | | 0655 | Medium | [Print Binary Tree](Problems/0655.cpp) | +| 0659 | Medium | [Split Array into Consecutive Subsequences](Problems/0659.cpp) | | 0661 | Easy | [Image Smoother](Problems/0661.cpp) | | 0662 | Medium | [Maximum Width of Binary Tree](Problems/0662.cpp) | | 0664 | Hard | [Strange Printer](Problems/0664.cpp) |