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. django 内置“信号”机制和自定义方法

    一.引子 在操作数据的时候,假设我们需要记录一些日志,这个时候,我们需要用什么来显示这个需求呢?装饰器?装饰器只能先实现整体的操作.在django 里面有这么一个东西--信号 下面我们就来了解了解它. ...

  2. Kubernetes-4.Pods

    docker version:19.03.14 kubernetes version:1.19.4 ** 已了解Kubernetes的组成.安装.以及kubectl基本命令使用 本文概述Kuberne ...

  3. HDOJ-6651(数学推导)

    Final Exam HDOJ-6651 这里主要考察我们的思维能力,要想自己至少可以通过k道题目,那么可以从老师的角度出发:怎么才能尽可能让你每一道题目都不通过,但是分数却是固定的. 假设我们每道题 ...

  4. Javascript学习,DOM对象,方法的使用

    JavaScript: ECMAScript: BOM: DOM: 事件 DOM的简单学习 功能:控制html文档内容 代码:获取页面标签(元素)对象和Element document.getElem ...

  5. JAVA-标识符、变量、数据类型

    标识符和关键字 ​ 所有的标识符否应该以字母a ~ z和 A ~Z ,美元符($).下划线(_)开始. ​ 首字符之后可以是字母a ~ z和 A ~Z ,美元符($).下划线(_)的任意字符组合. 注 ...

  6. Java8 BiFunction 简单用用

    最近来了新公司,主要用到了ElasitcSearch,大家都知道在底层查询代码中往往需要判断传入某个参数是否为空来判断设置查询,例如下方代码: BoolQueryBuilder query = Que ...

  7. 200-Java语言基础-Java编程入门-006 | Java数组定义及使用(引用数据类型)

    一.数组概述和定义格式说明 为什么要有数组(容器): 为了存储同种数据类型的多个值 数组概念: 数组是存储同一种数据类型多个元素的集合.也可以看成是一个容器. 数组既可以存储基本数据类型,也可以存储引 ...

  8. python基础学习之列表的功能方法

    列表:list 格式 li = [1,2,3,4,5,6] 列表内部随意嵌套其他格式:字符串.列表.数字.元组.字典. 列表内部有序,且内容可更改 a = [1,2,3,4]    a[0] = 5  ...

  9. 主成分分析 | Principal Components Analysis | PCA

    理论 仅仅使用基本的线性代数知识,就可以推导出一种简单的机器学习算法,主成分分析(Principal Components Analysis, PCA). 假设有 $m$ 个点的集合:$\left\{ ...

  10. IgniteMe -高校网络信息安全运维挑战赛

    1 int __cdecl main(int argc, const char **argv, const char **envp) 2 { 3 void *v3; // eax 4 int v4; ...