-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0754-reach-a-number.js
More file actions
31 lines (29 loc) · 902 Bytes
/
0754-reach-a-number.js
File metadata and controls
31 lines (29 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 754. Reach a Number
* https://leetcode.com/problems/reach-a-number/
* Difficulty: Medium
*
* You are standing at position 0 on an infinite number line. There is a destination at
* position target.
*
* You can make some number of moves numMoves so that:
* - On each move, you can either go left or right.
* - During the ith move (starting from i == 1 to i == numMoves), you take i steps in the
* chosen direction.
*
* Given the integer target, return the minimum number of moves required (i.e., the minimum
* numMoves) to reach the destination.
*/
/**
* @param {number} target
* @return {number}
*/
var reachNumber = function(target) {
const absTarget = Math.abs(target);
let moves = Math.floor(Math.sqrt(2 * absTarget));
while (true) {
const sum = moves * (moves + 1) / 2;
if (sum >= absTarget && (sum - absTarget) % 2 === 0) return moves;
moves++;
}
};