Codility---FrogRiverOne
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.
// 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");
import java.util.Arrays;
class Solution {
public int solution(int X, int[] A) {
// write your code in Java SE 8
int count = 0;
int [] res = new int[A.length];
if(X > A.length)
return -1;
if(A.length ==1 && X == 1 && A[0] ==1)
return 0;
if(A.length ==1 && X == 1 && A[0] > 1)
return -1;
for(int i=0; i<A.length; i++) {
if(A[i] <= X) {
if(res[A[i]-1] == 0) {
res[A[i]-1] = 1;
count++;
}
if(count == X) {
return i;
}
}
}
return -1;
}
}
Codility---FrogRiverOne的更多相关文章
- 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 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
- [codility]CountDiv
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. ...
随机推荐
- windows - Cygwin和MinGW有什么区别?(MinGW从Cygwin 1.3.3版本中分离出来)
windows - Cygwin和MinGW有什么区别? 我想让我的C ++项目跨平台,我正在考虑使用Cygwin / MinGW. 但是他们之间有什么区别呢? 另一个问题是,如果没有Cygwin / ...
- Hibernate——(5)持久化对象和一级缓存机制
一.对象的三种状态 1.暂时态:当对象刚创建,和Session没有发生任何关系时,当程序运行完就即刻消失,被称为暂时态. 2.持久态:当执行如下代码时,对象变为持久态 Emp e = new Emp( ...
- apt-get install 的参数(add-apt-repository)
apt-get install 是 ubuntu 下的软件安装命令. sudo apt-get -y install: -y:yes,在命令行交互提示中,直接输入 yes: 1. 使用 add-apt ...
- telnet 的使用(ping 与 telnet)
基本用法 >> telnet localhost 23 // 23 表示 telnet 服务的端口号,不写端口号也可以,telnet 默认绑定的端口号就是 23 // netstat -a ...
- Redis中的管道(PipeLine)与事物(Transactions)
Redis中的管道(PipeLine)与事物(Transactions) 序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次 ...
- 区间树(segment tree)
区间树能够对保存的数据进行适当的预处理,以快速回复查询. 区间树常用于在一维数组的特定区间对查询进行快速回复.区间树的最典型也是最简单的应用就是求区间最小值的问题. 区间树的基本思路是,生成表示给定数 ...
- Java--面试通关要点
这里,笔者结合自己过往的面试经验,整理了一些核心的知识清单,帮助读者更好地回顾与复习 Java 服务端核心技术.本文会以引出问题为主,后面有时间的话,笔者陆续会抽些重要的知识点进行详细的剖析与解答. ...
- NET SignalR2
.NET SignalR2持久连接层解析 越是到年底越是感觉浑身无力,看着啥也不想动,只期盼着年终奖的到来以此来给自己打一针强心剂.估摸着大多数人都跟我一样犯着这样浑身无力的病,感觉今年算是没挣到 ...
- xcode代码统计行
输入端子 cd 通路 进入项目根文件夹 输入以下的命令.显示总行数(不包括空行.包括凝视和应用第三方类) find . -name "*.m" -or -name "*. ...
- P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址)——充分证明@是取变量(对象)的地址,而不是变量里面的内容,够清楚!
如图,为什么这样取出来的p,p1的值不一样呢? 165232328群友庾伟洪告诉我: P和P1指向了O和O1两个变量(对象)的地址, 而不是O和O1的内容(对象的实际地址) ,你想P指向真正的对 ...