题目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] = 4In 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.
A 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] = 2is a permutation, but array A such that:
A[0] = 4 A[1] = 1 A[2] = 3is 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] = 2the 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] = 4the 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;
}