1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.
package com.code; public class Test04_3 {
public static int solution(int X, int[] A) {
int size = A.length;
int [] res = new int[X];
int resCount = 0;
if(X>size){
return -1;
}
if(size==1 && X==1 && A[0]==1){
return 0;
}
if(size==1 && X==1 && A[0]>1){
return -1;
}
for(int i=0;i<size;i++){
if(A[i]<=X){
if(res[A[i]-1] == 0){
res[A[i]-1] = 1;
resCount++;
}
if(resCount==X){
return i;
}
}
}
return -1;
}
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
System.out.println(solution(5, a));
int [] b = {2};
System.out.println(solution(1, b)); }
}
/**
1. FrogRiverOne 一苇渡江
Find the earliest time when a frog can jump to the other side of a river. A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0)
and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. You are given a zero-indexed array A consisting of N integers representing the falling leaves.
A[K] represents the position where one leaf falls at time K, measured in seconds. The goal is to find the earliest time when the frog can jump to the other side of the river.
The frog can cross only when leaves appear at every position across the river from 1 to X
(that is, we want to find the earliest moment when all the positions from 1 to X are covered by leaves).
You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river. For example, you are given integer X = 5 and array A such that: A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 3
A[6] = 5
A[7] = 4
In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river. Write a function: class Solution { public int solution(int X, int[] A); } that, given a non-empty zero-indexed array A consisting of N integers and integer X,
returns the earliest time when the frog can jump to the other side of the river. If the frog is never able to jump to the other side of the river, the function should return −1. For example, given X = 5 and array A such that: A[0] = 1
A[1] = 3
A[2] = 1
A[3] = 4
A[4] = 2
A[5] = 3
A[6] = 5
A[7] = 4
the function should return 6, as explained above. Assume that: N and X are integers within the range [1..100,000];
each element of array A is an integer within the range [1..X].
Complexity: expected worst-case time complexity is O(N);
expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
*/
1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.的更多相关文章
- counting elements--codility
lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...
- Codility---FrogRiverOne
Task description A small frog wants to get to the other side of a river. The frog is initially locat ...
- LeetCode 1101. The Earliest Moment When Everyone Become Friends
原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/ 题目: In a soc ...
- 【LeetCode】1101. The Earliest Moment When Everyone Become Friends 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...
- Kafka 0.10.0.1 consumer get earliest partition offset from Kafka broker cluster - scala code
Return: Map[TopicPartition, Long] Code: val props = new Properties() props.put(ConsumerConfig.BOOTST ...
- Android消息处理机制(Handler、Looper、MessageQueue与Message)
Android是消息驱动的,实现消息驱动有几个要素: 消息的表示:Message 消息队列:MessageQueue 消息循环,用于循环取出消息进行处理:Looper 消息处理,消息循环从消息队列中取 ...
- ORACLE 11gR2 DG(Physical Standby)日常维护02
环境:RHEL 6.5 + Oracle 11.2.0.4 三.监控DG的状态 3.1监控DG备库的状态 3.2监控主库传输日志链路的状态 四.备库切换为snapshot standby 4.1备库切 ...
- Django模型类Meta元数据详解
转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...
- Python任务调度模块 – APScheduler
APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用.目前最新版本为3.0 ...
随机推荐
- RabbitMQ指南之五:主题交换器(Topic Exchange)
在上一章中,我们完善了我们的日志系统,用direct交换器替换了fanout交换器,使得我们可以有选择性地接收消息.尽管如此,仍然还有限制:不能基于多个标准进行路由.在我们的日志系统中,我们可能不仅希 ...
- Asp.net MVC中文件上传的参数转对象的方法
参照博友的.NET WebApi上传文件接口(带其他参数)实现文件上传并带参数,当需要多个参数时,不想每次都通过HttpContext.Request.Params去取值,就针对HttpRequest ...
- git Eclipse项目不显示当前分支
问题: 在Eclipse中,导入新的git项目,在项目上不再显示当前所处的分支,也不再显示修改了哪些文件 解决: 右键选中项目 --> Team --> Share Project ...
- 如何让不同浏览器调用不同的CSS样式
如何让不同浏览器调用不同的CSS样式 由 于对W3C标准支持程度的不同,往往导致同一个CSS样式表在各种Web浏览器中的呈现大相径庭.以目前市场占有率最高的两个浏览器Microsoft Interne ...
- apache启动失败提示预期<IfModule>结果<IfModule>>
经过反复查看httpd.conf文件,发现原因是启动了两遍<IfModule>,也就是出现内容重复标签重复曾经遇到类似的情况Apache2: Expected </> but ...
- 系统信号-signal.h
#define SIGSEGV 11 /* segmentation violation */ #define SIGSYS 12 /* bad argument to system call */ ...
- CLISTCTRL2
回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...
- 「 HDU P3555 」 Bomb
# 题目大意 给出 $\text{T}$ 个数,求 $[1,n]$ 中含 ‘49’ 的数的个数. # 解题思路 求出不含 '49' 的数的个数,用总数减去就是答案. 数位 $DP$,用记忆化来做. 设 ...
- UVA - 12661 Funny Car Racing (Dijkstra算法)
题目: 思路: 把时间当做距离利用Dijkstra算法来做这个题. 前提:该结点e.c<=e.a,k = d[v]%(e.a+e.b); 当车在这个点的1处时,如果在第一个a这段时间内能够通过且 ...
- 【eclipse】外部 jar 包导入教程
JavaWeb 项目中,可以直接将要导入的 jar 程序包复制到你项目下的 WEB-INF/lib 文件夹下,将来程序移动到别的机子上测试时也能正常运行.如果是普通 Java 工程的话,我们可以在项目 ...