leetcode

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

commit 9a6749b89acb537e021dd3c84cb65f140bcda56c
parent 860f13b55e962740947002ba2096fc8dd500c330
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 17 Jul 2024 23:09:05 +0200

1 Random Problem

Diffstat:
AProblems/2645.cpp | 15+++++++++++++++
MREADME.md | 1+
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/Problems/2645.cpp b/Problems/2645.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int addMinimum(const string &word) const { + static const int add[3][3] = {{2, 1, 0}, {0, 2, 1}, {1, 0, 2}}; + int res = 0, prev = 2; + + for (const char c : word) { + const int idx = c - 'a'; + res += add[idx][prev]; + prev = idx; + } + + return res + 2 - prev; + } +}; diff --git a/README.md b/README.md @@ -1221,6 +1221,7 @@ for solving problems. | 2640 | Medium | [Find the Score of All Prefixes of an Array](Problems/2640.cpp) | | 2641 | Medium | [Cousins in Binary Tree II](Problems/2641.cpp) | | 2642 | Hard | [Design Graph With Shortest Path Calculator](Problems/2642.cpp) | +| 2645 | Medium | [Minimum Additions to Make Valid String](Problems/2645.cpp) | | 2657 | Medium | [Find the Prefix Common Array of Two Arrays](Problems/2657.cpp) | | 2658 | Medium | [Maximum Number of Fish in a Grid](Problems/2658.cpp) | | 2661 | Medium | [First Completely Painted Row or Column](Problems/2661.cpp) |