首页 > 其他分享 >Counting Elements - FrogRiverOne

Counting Elements - FrogRiverOne

时间:2022-12-05 00:44:41浏览次数:108  
标签:Elements Counting int leaves when frog FrogRiverOne position river

Question

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

Write an efficient algorithm for the following assumptions:

  • N and X are integers within the range [1..100,000];
  • each element of array A is an integer within the range [1..X].

 

 

Test Result

查看代码

import java.util.*;

class Solution {
    public int solution(int X, int[] A) {
        Set<Integer> set = new HashSet<Integer>();
        int cnt = 0;
        for (int i = 0; i < A.length; i++) {
            if(!set.contains(A[i]) && A[i] <= X) {
                set.add(A[i]);
                cnt++;
            }
            if(cnt == X) {
                return i;
            }
        }
        return -1;
    }
}

标签:Elements,Counting,int,leaves,when,frog,FrogRiverOne,position,river
From: https://www.cnblogs.com/KresnikShi/p/16943106.html

相关文章

  • CF 1722E Counting Rectangles(前缀和)
    题目分析(摘自这篇博客,原博主关于包含的定义有错误,在此处做了修改)给出一个长度为1e5的矩阵序列和1e5次询问,每次询问给出两个上下界矩阵,保证大矩阵包含小矩阵,请输出矩阵序......
  • LeetCode347. Top K Frequent Elements
    题意给一个序列,输出其前K个出现频次的数字方法优先队列代码classSolution{public:structnode{intval;//值intcnt;//出现次数......
  • 数数字(Digit Counting)
    DigitCountingTimelimit:3.000secondsTrungisboredwithhismathematicshomeworks.Hetakesapieceofchalkandstartswritingasequenceofconsecutiveint......
  • 【Amadeus原创】零基础使用vue3和elements创建应用
    一.创建环境1.创建D:\code\vue文件夹2.vscode打开文件夹3.打开终端,输入​​npminstall-g@vue/cli​​4.配置环境变量终端输入:​​npmconfiglist​​找到路径将路......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • [AGC005D] ~K Perm Counting
    [AGC005D]~KPermCounting智慧转化,但不是很难想到。首先考虑容斥,设\(f_i\)表示强制\(i\)个位置\(|P_i-i|=k\)的方案数,那么答案就为\(\sum_{i=0}^nf_i(n-i)!\),......
  • Initialize all elements of an array to same value in C/C++
    UsingDesignatedInitializers//ordon'tspecifythesizeintarr[]={[0...4]=1};Usingstd::fill_nfunctionFinally,wecanusestd::fill_ninC++,......
  • LG4778 Counting swaps 题解
    LG4778Countingswaps给定你一个\(1\simn\)的排列\(p\),可进行若干次操作,每次选择两个整数\(x,y\),交换\(p_x,p_y\)。用最少的操作次数将给定排列变成单调上升的序......
  • Counting Rectangles(二维数组前缀和)
    题目链接题目描述:Youhave\(n\)rectangles,the\(i\)-threctanglehasheight\(h_i\)andwidth\(w_i\).Youareasked\(q\)queriesoftheform\(h_sw_sh_b......
  • CF1650G 『Counting Shortcuts』 题解
    从洛谷博客那里搬过来的(图论专题本来打算先挑最简单的做,结果做了两个多小时(题意就是让你找从起点\(s\)到终点\(t\)的最短路以及次短路个数,本题次短路长度指的是最短......