Task description

A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.

Count the minimal number of jumps that the small frog must perform to reach its target.

Write a function:

class Solution { public int solution(int X, int Y, int D); }

that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.

For example, given:

X = 10 Y = 85 D = 30

the function should return 3, because the frog will be positioned as follows:

  • after the first jump, at position 10 + 30 = 40
  • after the second jump, at position 10 + 30 + 30 = 70
  • after the third jump, at position 10 + 30 + 30 + 30 = 100

Assume that:

  • X, Y and D are integers within the range [1..1,000,000,000];
  • X ≤ Y.

Complexity:

  • expected worst-case time complexity is O(1);
  • expected worst-case space complexity is O(1).
 
Solution

 
Programming language used: Java
Total time used: 6 minutes
Code: 11:01:41 UTC, java, final, score:  100
// you can also use imports, for example:
// import java.util.*; // you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message"); class Solution {
public int solution(int X, int Y, int D) {
// write your code in Java SE 8
int res = (Y-X)/D;
if((Y-X) % D == 0)
return res;
else
return res + 1; }
}

Codility---FrogJmp的更多相关文章

  1. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...

  2. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  3. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  4. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  5. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  6. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  7. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  8. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  9. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  10. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. 【31.58%】【codeforces 682D】Alyona and Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 分布式系统和CAP

    帽子理论(CAP): C:Consistency,一致性, 数据一致更新,所有数据变动都是同步的 A:Availability,可用性, 好的响应性能,完全的可用性指的是在任何故障模型下,服务都会在有 ...

  3. Call、Apply和Bind

    首先说一下bind,灵活的通过bind来改变this指针 bind方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入bind方法的第一个参数作为this, 传入bind ...

  4. 如何移除解决方案中的TFS版本控制

    1.清除(删除)项目下的所有版本控制文件,这些文件有:*.vssscc,*.vspscc删除这些版本控制文件比较简单,搜索这些后缀的文件,删除即可. 2.修改项目的解决方案文件:*.sln (1).先 ...

  5. 《冰球撞击》Android休闲桌球类游戏现已面试,快来下载吧!

    <冰球撞击>Android休闲桌球类游戏现已完工上市快来下载吧! http://pan.baidu.com/s/1dD9vIRv <冰球撞击>是一个类似玩投篮机操作方式的And ...

  6. 动态加载Dll时,通过Type生成类对象

    原文:动态加载Dll时,通过Type生成类对象 转:http://www.cnblogs.com/zfanlong1314/p/4197383.html "反射"其实就是利用程序集 ...

  7. Open SSL 开发环境配置

    Open SSL 开发环境配置 最后更新日期:2014-05-13 阅读前提:VisualStudio的基本使用.Cent OS的基本使用 环境: Windows 8.1 64bit英文版,Visua ...

  8. C# TSF 输入法的获取

    原文 C# TSF 输入法的获取 起因: 「添雨跟打器」中存在一个问题.在 windows 8/10 里面,输入法就获取不到了.我一直没有去管这样的问题.但是也大致知道,可能是 TSF 架构的问题. ...

  9. cocos2dx 2.2.3 xcode5.0,新mac项目错误

    cocos2dx 2.2.3 xcode5.0,新建mac项目报错 Undefined symbols for architecture x86_64:   "cocos2d::extens ...

  10. 多线程——继承Thread类别

    详细java此前使用多线程,让我们来看看下面的问题. 什么是多线程     简单的理解成:cpu"同一时候"运行多个任务,这就是多线程. (究其本质,当涉及到进程和线程的概念.上面 ...