1. package com.code;
  2.  
  3. public class Test04_3 {
  4. public static int solution(int X, int[] A) {
  5. int size = A.length;
  6. int [] res = new int[X];
  7. int resCount = 0;
  8. if(X>size){
  9. return -1;
  10. }
  11. if(size==1 && X==1 && A[0]==1){
  12. return 0;
  13. }
  14. if(size==1 && X==1 && A[0]>1){
  15. return -1;
  16. }
  17. for(int i=0;i<size;i++){
  18. if(A[i]<=X){
  19. if(res[A[i]-1] == 0){
  20. res[A[i]-1] = 1;
  21. resCount++;
  22. }
  23. if(resCount==X){
  24. return i;
  25. }
  26. }
  27. }
  28. return -1;
  29. }
  30. public static void main(String[] args) {
  31. int [] a = {1,2,3,4,5};
  32. System.out.println(solution(5, a));
  33. int [] b = {2};
  34. System.out.println(solution(1, b));
  35.  
  36. }
  37. }
  38. /**
  39. 1. FrogRiverOne 一苇渡江
  40. Find the earliest time when a frog can jump to the other side of a river.
  41.  
  42. 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)
  43. and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river.
  44.  
  45. You are given a zero-indexed array A consisting of N integers representing the falling leaves.
  46. A[K] represents the position where one leaf falls at time K, measured in seconds.
  47.  
  48. The goal is to find the earliest time when the frog can jump to the other side of the river.
  49. The frog can cross only when leaves appear at every position across the river from 1 to X
  50. (that is, we want to find the earliest moment when all the positions from 1 to X are covered by leaves).
  51. 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.
  52.  
  53. For example, you are given integer X = 5 and array A such that:
  54.  
  55. A[0] = 1
  56. A[1] = 3
  57. A[2] = 1
  58. A[3] = 4
  59. A[4] = 2
  60. A[5] = 3
  61. A[6] = 5
  62. A[7] = 4
  63. In second 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river.
  64.  
  65. Write a function:
  66.  
  67. class Solution { public int solution(int X, int[] A); }
  68.  
  69. that, given a non-empty zero-indexed array A consisting of N integers and integer X,
  70. returns the earliest time when the frog can jump to the other side of the river.
  71.  
  72. If the frog is never able to jump to the other side of the river, the function should return −1.
  73.  
  74. For example, given X = 5 and array A such that:
  75.  
  76. A[0] = 1
  77. A[1] = 3
  78. A[2] = 1
  79. A[3] = 4
  80. A[4] = 2
  81. A[5] = 3
  82. A[6] = 5
  83. A[7] = 4
  84. the function should return 6, as explained above.
  85.  
  86. Assume that:
  87.  
  88. N and X are integers within the range [1..100,000];
  89. each element of array A is an integer within the range [1..X].
  90. Complexity:
  91.  
  92. expected worst-case time complexity is O(N);
  93. expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments).
  94. Elements of input arrays can be modified.
  95. */

1. FrogRiverOne 一苇渡江 Find the earliest time when a frog can jump to the other side of a river.的更多相关文章

  1. counting elements--codility

    lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...

  2. Codility---FrogRiverOne

    Task description A small frog wants to get to the other side of a river. The frog is initially locat ...

  3. LeetCode 1101. The Earliest Moment When Everyone Become Friends

    原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/ 题目: In a soc ...

  4. 【LeetCode】1101. The Earliest Moment When Everyone Become Friends 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  5. 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 ...

  6. Android消息处理机制(Handler、Looper、MessageQueue与Message)

    Android是消息驱动的,实现消息驱动有几个要素: 消息的表示:Message 消息队列:MessageQueue 消息循环,用于循环取出消息进行处理:Looper 消息处理,消息循环从消息队列中取 ...

  7. ORACLE 11gR2 DG(Physical Standby)日常维护02

    环境:RHEL 6.5 + Oracle 11.2.0.4 三.监控DG的状态 3.1监控DG备库的状态 3.2监控主库传输日志链路的状态 四.备库切换为snapshot standby 4.1备库切 ...

  8. Django模型类Meta元数据详解

    转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...

  9. Python任务调度模块 – APScheduler

    APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用.目前最新版本为3.0 ...

随机推荐

  1. IIS网站部署步骤以及常见异常解决方案

    一. 简述 如果VS调试代码每次都使用浏览器打开,修改脚本和样式等还可以刷新页面显示最新修改,但是修改后台代码的话就需要停止调试再重新使用浏览器打开才能显示后台的最新修改,就比较麻烦.这里推荐附加到I ...

  2. leetcode692 Top K Frequent Words

    思路: 堆.实现: #include <bits/stdc++.h> using namespace std; class Solution { public: inline bool c ...

  3. Android ListView动态刷新某项Item

    使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...

  4. phpcms标签用法(转)

    1.显示指定catid的栏目名称和链接 {$CATEGORYS[25]['catname']}  {$CATEGORYS[25]['url']} 获取父栏目id/获取父栏目名称  $CATEGORY[ ...

  5. JS高级——文件操作

    https://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/12/2208589.html https://blog.csdn.net/pl16 ...

  6. MySql学习笔记(二) —— 正则表达式的使用

    前面介绍利用一些关键字搭配相应的SQL语句进行数据库查找过滤,但随着过滤条件的复杂性的增加,where 子句本身的复杂性也会增加.这时我们就可以利用正则表达式来进行匹配查找. 1.基本字符匹配 ' o ...

  7. ThinkPHP---案例2--部门管理功能

    [一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...

  8. Getting start with dbus in systemd (01) - Interface, method, path

    Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...

  9. <MyBatis>入门一 HelloWorld

    1.HelloWorld 导入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependen ...

  10. 介绍一个比较酷东西:HTML5 桌面通知(Notification API)

    Notification API 是 HTML5 新增的桌面通知 API,用于向用户显示通知信息.该通知是脱离浏览器的,即使用户没有停留在当前标签页,甚至最小化了浏览器,该通知信息也一样会置顶显示出来 ...