首页 > 其他分享 >Time Complexity - FrogJmp

Time Complexity - FrogJmp

时间:2022-12-01 11:36:34浏览次数:113  
标签:10 FrogJmp int 30 frog Complexity distant Time position

Question

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

Write an efficient algorithm for the following assumptions:

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

要注意题目里写的efficient,report里会考虑到算法复杂度。我一开始用累加就判定为非常复杂了,于是改用取模。

 

MyReport

class Solution {
    public int solution(int X, int Y, int D) {
        // write your code in Java SE 8
        int distant = Y - X;
        int step = 0;
        if(distant % D == 0) {
            return distant/D;
        } else {
            return distant/D + 1;
        }
    }
}

 

标签:10,FrogJmp,int,30,frog,Complexity,distant,Time,position
From: https://www.cnblogs.com/KresnikShi/p/16940879.html

相关文章

  • Java 时间格式化方法 DateTimeFormatter
    在 Java8之前,一般使用 SimpleDateFormat类进行时间格式化,但是这不是同步执行的方法,所以存在多线程执行不安全的问题。如果使用的是Java8之前的JDK,变成线程安全,就......
  • Python中5大模块的使用教程(collections模块、time时间模块、random模块、os模块、sys
    1.模块的简单认识定义:模块就是我们把装有特定功能的代码进行归类的结果.从代码编写的单位来看我们的程序,从小到大的顺序:一条代码<语句块<代码块(函数,类)<模......
  • 定时器:Timer
    定时器定时器是一种控制任务延时调用,或者周期调用的技术。作用:闹钟、定时邮件发送。定时器的实现方式方式一:TimerTimer定时器Timer定时器的特点和存在的问题1、Timer是......
  • jstl一些标签 中timestamp类型在页面去掉时分秒!
    <fmt:formatDatevalue='${vo.updateDate}'pattern='yyyy-MM-dd'/>  JSTL的ifelse:有c:if没有else的处理分类:JSP/JAVA/J2EE(11)作者同类文章X jstl的......
  • rabbitmq的consumer_timeout修改
    问题:项目中使用了rabbitmq来做异步任务,最近突然发现一个耗时比较长的任务一直在重复执行。排查:排查日志后发现了超时,channel关闭。如果超过了consumer_timeout时间默认......
  • ios中getTime()的兼容性问题
    时间格式为:2017-12-1212:00:00在苹果上获取时间戳有兼容性问题 需要转换成2017/12/1212:00:00 才可以正确获取到时间戳 vargetTime=function(time){varmyDate......
  • RuntimeError: Unable to create a new session key. It is likely that the cache is
    使用Centos7.6安装的Stein版本,按照官方文档一步步安装Dashboard,输入用户名和密码后出错,如下所示 查看apache日志/var/log/httpd/error_log,发现以下错误[root@controll......
  • python中time模块的常用方法的转换关系图
      获取当前的时间戳  把时间戳转换成了时间的格式  获取时间  把时间格式数据转换为易识别的字符串 获取到表示时间的字符串,再转换为时间数据。 ......
  • Linux系统时间的设定以及自带的timesync时间同步
     1.三个阶段的系统时间设定 1.1内核启动阶段   这里是在menuconfig文件配置RTC设定系统时间选项。   CONFIG_RTC_HCTOSYS_DEVICE="rtc1",或者直接在decon......
  • Api——LocalDateTime
    publicclassTest3_LocalDateTime{publicstaticvoidmain(String[]args){//0、获取本地日期和时间对象。不可变对象LocalDateTimeldt=LocalDateTime.now();......