02 Counting Elements

题目1 :FrogRiverOne

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

int solution(int X, vector<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.

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

代码:

// you can use includes, for example:
 #include <algorithm>
 #include <set>
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(int X, vector<int> &A) {
    // write your code in C++11
    int n=A.size();
    set<int> st;
    for(int i=0;i<n;i++)
    {
        if(A[i]<=X)
           st.insert(A[i]);
        if(st.size()==X)
         return i;
    }
    return -1;
}

题目2 :PermCheck 

A non-empty zero-indexed array A consisting of N integers is given.

permutation is a sequence containing each element from 1 to N once, and only once.

For example, array A such that:

A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2

is a permutation, but array A such that:

A[0] = 4 A[1] = 1 A[2] = 3

is not a permutation, because value 2 is missing.

The goal is to check whether array A is a permutation.

Write a function:

int solution(vector<int> &A);

that, given a zero-indexed array A, returns 1 if array A is a permutation and 0 if it is not.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
int solution(vector<int> &A) {
    // write your code in C++11
    //space complexity is O(n)
    int n=A.size();
    vector<bool> vec(n,false);
    for(int i=0;i<n;i++)
    {
        if(A[i]>n||vec[A[i]-1]==true)
          return 0;
          vec[A[i]-1]=true;
    }
    
    return 1;
}

上述实现方式 space complexity is O(n).    我们也实现 space complexity is O(1),同时 time  complexity is O(n)

int solution(vector<int> &A) {
    // write your code in C++11
    // space complexity is O(1)  time complexity is O(n)
    
    int n=A.size();
    for(int i=0;i<n;)
    {
        if(A[i]==i+1)
          i++;
        else
        {
            if(A[i]>n||A[i]<i+1||A[A[i]-1]==A[i])
             return 0;
             // swap
             int tmp=0;
             tmp=A[A[i]-1];
             A[A[i]-1]=A[i];
             A[i]=tmp;
             
        }
    }
    return 1;
}

题目3 :MissingInteger

Write a function:

int solution(vector<int> &A);

that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A.

For example, given:

A[0] = 1 A[1] = 3 A[2] = 6 A[3] = 4 A[4] = 1 A[5] = 2

the function should return 5.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
int solution(vector<int> &A) {
    // write your code in C++11
    int n=A.size();
    vector<bool> vec(n,false);
    
    for(int i=0;i<n;i++)
    {
        if(A[i]>n||A[i]<=0)
          continue;
        else
        {
          vec[A[i]-1]=true;
        }    
    }
    for(int i=0;i<n;i++)
    {
        if(vec[i]==false)
          return i+1;
    }
    return n+1;
}

题目 4:MaxCounters

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increased by 1,
  • max counter − all counters are set to the maximum value of any counter.

A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:

  • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  • if A[K] = N + 1 then operation K is max counter.

For example, given integer N = 5 and array A such that:

A[0] = 3 A[1] = 4 A[2] = 4 A[3] = 6 A[4] = 1 A[5] = 4 A[6] = 4

the values of the counters after each consecutive operation will be:

(0, 0, 1, 0, 0) (0, 0, 1, 1, 0) (0, 0, 1, 2, 0) (2, 2, 2, 2, 2) (3, 2, 2, 2, 2) (3, 2, 2, 3, 2) (3, 2, 2, 4, 2)

The goal is to calculate the value of every counter after all operations.

Write a function:

vector<int> solution(int N, vector<int> &A);

that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Complexity:

  • expected worst-case time complexity is O(N+M);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
#include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

vector<int> solution(int N, vector<int> &A) {
    // write your code in C++11
    int M=A.size();
    vector<int> result(N,0);
    int maxele=0,maxall=0;
    for(int i=0;i<M;i++)
    {
        
        if(A[i]<=N)
         {
             result[A[i]-1]=max(result[A[i]-1],maxele);
             result[A[i]-1]++;
             if(result[A[i]-1]>maxall)
                maxall=result[A[i]-1];
         }
         if(A[i]==N+1)
          maxele=maxall;
         
    }
    for(int i=0;i<N;i++)
    {
        if(result[i]<maxele)
          result[i]=maxele;
    }
    return result;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值