Codility---FrogJmp
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).
// 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的更多相关文章
- codility上的练习 (1)
codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- 看看是不是你想要的:pycharm永久激活!!!
pycharm是很强大的开发工具,但是每次注册着实让人头疼.网络上很多注册码.注册服务器等等.但都只是一年或者不能用:为次有如下解决方案.亲测有效!!! 如果想让pycharm永久被激活,比如截止日到 ...
- Spring处理跨域请求
[nio-8080-exec-8] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from s ...
- React中的state与props的再理解
props可以看做是 property 的缩写的复数,可以翻译为属性,类似于HTML 标签的自定义属性.在大多数React教程里讲 state 和 props 主要的区别在于 props 是不可变的, ...
- android制,点击EditText时刻,隐藏系统软键盘,显示光标
由于项目中要用自己定义的随机键盘,所以必须得屏蔽系统软键盘,可是在4.0的測试系统来看,使用editText.setInputType(InputType.TYPE_NULL)方法固然能隐藏键盘,可是 ...
- PhoneGap Html5 App 横竖屏切换自动退出
将以下内容添加到 AndroidManifest.xml 文件的 activity 标签中: android:configChanges="orientation|keyboardHidde ...
- 在JScript中使用正则表达式
作者:朱金灿 来源:http://blog.csdn.net/clever101 不废话了,直接用代码说明吧: main(); function main() { try { // 运用正则表达式进行 ...
- yii联查
$count = Acticle::find()->select("acticle_type.act_type,acticle.act_id,acticle.act_title,act ...
- 统计推断(statistical inference)
样本是统计推断的依据: 统计推断的基本问题可以分为两大类: 估计问题 点估计, 区间估计 假设检验 1. 点估计 设总体 X 的分布函数 F(x;θ) 的形式已知,θ 是待估参数.X1,X2,-,Xn ...
- SSM导出报表为csv文件
报表导出思路为,在后台用iDataReader将查询得到的数据写进文件并压缩,向前端返回文件位置的链接,在前端执行下载操作. web端: 1. ajax请求url,将返回的路径json字符串解析并执行 ...
- WPF 属性变更通知类的实现
原文:WPF 属性变更通知类的实现 平时用依赖属性多一些,普通属性的变更通知知道有这个方法,但是老是忘记名字,再写一遍吧. public class Student : INotifyProperty ...