leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0754.cpp (380B)
0 class Solution {
1 public:
2 int reachNumber(int target) const {
3 target = abs(target);
5 const long long n = ceil((sqrt(1 + 8.0 * target) - 1.0) / 2);
6 const long long sum = n * (n + 1) / 2;
8 if (sum == target) return n;
9 const long long res = sum - target;
11 if (res % 2 == 0) return n;
12 return n + (n % 2 == 1) + 1;
13 }
14 };