首页 技术 正文
技术 2022年11月20日
0 收藏 983 点赞 4,373 浏览 1885 个字

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:

Input: target = 3
Output: 2
Explanation:
On the first move we step from 0 to 1.
On the second step we step from 1 to 3.

Example 2:

Input: target = 2
Output: 3
Explanation:
On the first move we step from 0 to 1.
On the second move we step from 1 to -1.
On the third move we step from -1 to 2.

Note:

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

Approach #1: Math. [Java]

class Solution {
public int reachNumber(int target) {
int sum = 0;
int steps = 1;
int count = 0; target = Math.abs(target); while (sum < target || (sum - target) % 2 != 0) {
sum += steps;
steps++;
count++;
} return count++;
}
}

  

Analysis:

Step 0: Get positive target value (step to get negative target is the same as to get positive value due to symmetry).

Step 1: Find the smallest step that the summation from 1 to step just exceeds or equalstarget.

Step 2: find the difference between sum and target. The goal is to get rid of the difference to reach target. For i-th move, if we switch the right move to the left, the change in summation will be 2*i less. Now the difference between sum and target has to be an even number in order for the math to check out.

Step 2.1: If the difference value is even, we can return the current step.

Step 2.2: If the difference value is odd, we need to increase the step untill the difference is even (at most 2 more steps needed).

Eg:

target = 5

Step 0: target = 5.

Step 1: sum = 1 + 2 + 3 = 6 > 5, step = 3.

Step 2: Difference = 6 – 5 = 1. Since the difference is an odd value, we will not reach the target by swirching any right move to the left. So we increase our step.

Step 2.2: We need to increase step by 2 to get an even difference (i.e. i + 2 + 3 + 4 + 5 = 15, now step = 5, difference = 15 – 5 = 10). Now that we have an even difference, we can simply switch any move to the left (i.e. change + to -) as long as the summation of the changed value equals to half of the difference. We can switch 1 and 4 or 2 and 3 or 5.

Reference:

https://leetcode.com/problems/reach-a-number/discuss/112968/Short-JAVA-Solution-with-Explanation

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839