leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 2af78f2f8c305bdff64e5babdca52972933e0cfb
parent 7812da12324a5306dc5f6d52fdfceab7762d5dd5
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  8 Feb 2023 20:28:10 +0100

LeetCode 75 II: Day 5

Diffstat:
AProblems/0621.cpp | 21+++++++++++++++++++++
MREADME.md | 1+
2 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/Problems/0621.cpp b/Problems/0621.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int leastInterval(vector<char> &tasks, int n) { + if (n == 0) return tasks.size(); + + vector<int> count(26); + for (char t : tasks) count[t - 'A']++; + + int maxi = INT_MIN, cnt = 0; + for (int n : count) { + if (n == maxi) + cnt++; + else if (n > maxi) { + maxi = n; + cnt = 1; + } + } + + return max((int)tasks.size(), (maxi - 1) * (n + 1) + cnt); + } +}; diff --git a/README.md b/README.md @@ -238,6 +238,7 @@ for solving problems. | 0590 | Easy | [N-ary Tree Postorder Traversal](Problems/0590.cpp) | | 0606 | Easy | [Construct String from Binary Tree ](Problems/0606.cpp) | | 0617 | Easy | [Merge Two Binary Trees](Problems/0617.cpp) | +| 0621 | Medium | [Task Scheduler](Problems/0621.cpp) | | 0637 | Easy | [Average of Levels in Binary Tree](Problems/0637.cpp) | | 0653 | Easy | [Two Sum IV - Input is a BST](Problems/0653.cpp) | | 0654 | Medium | [Maximum Binary Tree](Problems/0654.cpp) |