Press "Enter" to skip to content

花花酱 LeetCode 754. Reach a Number

题目大意:第i时刻可以移动+i,-i单位距离。初始在原点,问最少对少时间可以移动到target坐标。

Problem:

You are standing at position 0 on an infinite number line. There is a goal at position target.

On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.

Return the minimum number of steps required to reach the destination.

Example 1:

Example 2:

Note:

  • target will be a non-zero integer in the range [-10^9, 10^9].

 


Idea:

Math

 

Time complexity: O(sqrt(target))

Space complexity: O(1)

O(1)

 

请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.

Buy anything from Amazon to support our website
您可以通过在亚马逊上购物(任意商品)来支持我们

Paypal
Venmo
huahualeetcode
微信打赏

Be First to Comment

Leave a Reply