leetcode

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

commit 83a17d55c1e80259271736114f75b0f81daa8475
parent d16a95b5a7fcc0f39540c993a3b89728d97919be
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 26 Nov 2024 16:58:11 +0100

1 Random Problem

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

diff --git a/Problems/0754.cpp b/Problems/0754.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int reachNumber(int target) const { + target = abs(target); + + const long long n = ceil((sqrt(1 + 8.0 * target) - 1.0) / 2); + const long long sum = n * (n + 1) / 2; + + if (sum == target) return n; + const long long res = sum - target; + + if (res % 2 == 0) return n; + return n + (n % 2 == 1) + 1; + } +}; diff --git a/README.md b/README.md @@ -565,6 +565,7 @@ reference and a base for solving problems. | 0746 | Easy | [Min Cost Climbing Stairs](Problems/0746.cpp) | | 0747 | Easy | [Largest Number At Least Twice of Others](Problems/0747.cpp) | | 0752 | Medium | [Open the Lock](Problems/0752.cpp) | +| 0754 | Medium | [Reach a Number](Problems/0754.cpp) | | 0756 | Medium | [Pyramid Transition Matrix](Problems/0756.cpp) | | 0763 | Medium | [Partition Labels](Problems/0763.cpp) | | 0767 | Medium | [Reorganize String](Problems/0767.cpp) |