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

754. Reach a Number的更多相关文章

  1. 【Leetcode_easy】754. Reach a Number

    problem 754. Reach a Number solution1: class Solution { public: int reachNumber(int target) { target ...

  2. LeetCode 754. Reach a Number

    754. Reach a Number(到达终点数字) 链接:https://leetcode-cn.com/problems/reach-a-number/ 题目: 在一根无限长的数轴上,你站在0的 ...

  3. 【LeetCode】754. Reach a Number 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 日期 题目地址:https://leetcod ...

  4. LeetCode 754. Reach a Number到达终点数字

    题目 在一根无限长的数轴上,你站在0的位置.终点在target的位置. 每次你可以选择向左或向右移动.第 n 次移动(从 1 开始),可以走 n 步. 返回到达终点需要的最小移动次数. 示例 1: 输 ...

  5. 领扣-754 到达终点数字 Reach a Number MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. [LeetCode] Reach a Number 达到一个数字

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

  7. [Swift]LeetCode754. 到达终点数字 | Reach a Number

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

  8. 【leetcode】Reach a Number

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

  9. Python3解leetcode Reach a Number

    问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. ...

随机推荐

  1. #progma pack(x)说明

    1.字节对齐(内存相关) 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定变量的时候经常在特定的内存地址访问,这就需要各类型数 ...

  2. 剑指 Offer 14- I. 剪绳子 + 动态规划 + 数论

    剑指 Offer 14- I. 剪绳子 题目链接 还是343. 整数拆分的官方题解写的更清楚 本题说的将绳子剪成m段,m是大于1的任意一个正整数,也就是必须剪这个绳子,至于剪成几段,每一段多长,才能使 ...

  3. HDOJ-1301(最小生成树模板+Prim算法)

    Jungle Roads HDOJ-1301 这是最小生成树的水题,唯一要注意的就是那个n,其实输入只有n-1行. #include<iostream> #include<cstdi ...

  4. java IO流文件拷贝文件(字符流标准写法)

    public static void copyFile2(String path1, String path2) { Reader reader = null; Writer writer = nul ...

  5. logging日志的使用和设置过期自动删除

    一.logging的基础使用 1.logging的级别 import logging logging.debug('debug message') # 计算或者工作的细节 logging.info(' ...

  6. ECharts系列 (01):地图三级下钻

    前言 最近项目中用到了地图下钻功能,GitHub上找到了一个轮子 - echarts3-chinese-map-drill-down,启动项目看了一下Demo,动画衔接的很流畅,感觉做的非常棒,膜拜大 ...

  7. 多线程之volative关键字

    目录 轻量级同步机制:volative关键字 volative的作用 volatile非原子特性 volatile与synchronized比较 常用原子类进行自增自减操作 CAS 使用CAS原理实现 ...

  8. 10、Spring教程之整合MyBatis

    1.步骤 1.导入相关jar包 junit <dependency> <groupId>junit</groupId> <artifactId>juni ...

  9. Android控件 之 TextClock & AnalogClock(模拟时钟)

    TextClock •简介 关于时间的文本显示,Android 提供了 DigitalClock 和 TextClock. DigitalClock是Android第1版本发布,功能很简单,只显示时间 ...

  10. 分享15个实用VSCode插件,快来收藏吧!

    Visual Studio Code 是由微软开发的一款免费.跨平台的文本编辑器.它有卓越的性能和丰富的功能.VSCode 也有一个扩展和主题市场,为了帮助大家挑选出值得下载的插件,我们针对性的收集了 ...