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. 辨异 —— 行星 vs 恒星

    star:恒星,planet:行星: 1. 恒星 恒星是指宇宙中靠核聚变产生的能量而自身能发热发光的星体(比如太阳).过去天文学家以为恒星的位置是永恒不变的,以此为名.但事实上,恒星也会按照一定的轨迹 ...

  2. Android官方教程翻译(6)——添加ActionBar

    The action bar allows you to add buttons for the most important action items relating to the app's c ...

  3. VS2017 安装过程

    2017 安装过程 工欲善其事必先利其器 Visual Studio 2017 正式版官方下载地址:https://www.visualstudio.com/downloads/ 安装vs2017的时 ...

  4. Web 存储之localStorage

    1.localStorage的浏览器支持情况 localStorage属于永久性存储,不移除永久存在:sessionStorage属于会话结束就消失. localStorage存储的大小在5M左右,不 ...

  5. WPF动态加载3D 放大-旋转-平移

    第一步:新建WavefrontObjLoader.cs using System; using System.Collections.Generic; using System.Windows; us ...

  6. day68_淘淘商城项目_01_电商介绍 + 互联网术语 + SOA + 分布式 + 集群介绍 + 环境配置 + 框架搭建_匠心笔记

    课程计划 第一天: 1.电商行业的背景介绍--电子商务 2.淘淘商城的系统架构 a) 功能介绍 b) 架构讲解 3.工程搭建--后台工程 a) 使用maven搭建工程(工程大) b) 使用maven的 ...

  7. IP地址的正则表达式

    关键字:IP地址 正则表达式作者:txw1958出处:http://www.cnblogs.com/txw1958/archive/2011/10/13/ip_address_regular_expr ...

  8. python 教程 第二十章、 数据库编程

    第二十章. 数据库编程 环境设置 1).安装MySQL-python http://www.lfd.uci.edu/~gohlke/pythonlibs/ MySQL-python-1.2.3.win ...

  9. matlab GUI 编程

    matlab 语法的简便,在 GUI 上也不遑多让呀: uigetfile [filename, pathname] = uigetfile('*.m', 'choose a m file') 1. ...

  10. matlab 可变参数与默认参数设置

    1. 基本思路 矩阵矢量化编程,而不是循环和遍历: GPU 并行计算: 使用稀疏矩阵: 2. 实践 可变长输入参数,输出参数,需要解析(使用大括号进行索引): varargin varargout 函 ...