754. Reach a Number Medium

@problem@discussion
#Math#Binary Search



1/**
2 * [754] Reach a Number
3 *
4 * You are standing at position 0 on an infinite number line. There is a destination at position target.
5 * You can make some number of moves numMoves so that:
6 * 
7 * 	On each move, you can either go left or right.
8 * 	During the i^th move (starting from i == 1 to i == numMoves), you take i steps in the chosen direction.
9 * 
10 * Given the integer target, return the minimum number of moves required (i.e., the minimum numMoves) to reach the destination.
11 *  
12 * Example 1:
13 * 
14 * Input: target = 2
15 * Output: 3
16 * Explanation:
17 * On the 1^st move, we step from 0 to 1 (1 step).
18 * On the 2^nd move, we step from 1 to -1 (2 steps).
19 * On the 3^rd move, we step from -1 to 2 (3 steps).
20 * 
21 * Example 2:
22 * 
23 * Input: target = 3
24 * Output: 2
25 * Explanation:
26 * On the 1^st move, we step from 0 to 1 (1 step).
27 * On the 2^nd move, we step from 1 to 3 (2 steps).
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	-10^9 <= target <= 10^9
33 * 	target != 0
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/reach-a-number/
39// discuss: https://leetcode.com/problems/reach-a-number/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn reach_number(target: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_754() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.