Find the earliest time when a frog can jump to the other side of a river.

Source: Internet
Author: User

Task Description

A small frog wants to get to the other side of a river. the frog is currently located at position 0, and wants to get to position X. leaves fall from a tree onto the surface of the river.

You are given a non-empty 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 minutes.

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 within ss the river from 1 to X.

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 minute 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position within ss the river.

Write a function:

Int solution (int x, int A [], int N );

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

Solution C: 26

int solution(int X, int A[], int N){    int* temp = (int*)malloc(X*sizeof(int));    memset(temp, -1 , X*sizeof(int));       int numberOfLeavesNeed = X;    int i = 0;    for( i = 0; i < N && numberOfLeavesNeed>0; i++ )    {        int position = A[i]-1;        if( *(temp+position) == -1 )        {            *(temp+position) = i;            numberOfLeavesNeed--;        }    }    free(temp);    return numberOfLeavesNeed == 0 ? i-1:-1;}




This article from the "technology-alternative love" blog, please be sure to keep this source http://randywang.blog.51cto.com/4247710/1435013

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.