03 Prefix Sums
2015-12-18 09:44
260 查看
题目 1:CountDiv
Write a function:
int solution(int A, int B, int K);
that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:
{ i : A ≤ i ≤ B, i mod K = 0 }
For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.
Complexity:
expected worst-case time complexity is O(1);
expected worst-case space complexity is O(1).
题目 2:PassingCars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.
Array A contains only 0s and/or 1s:
0 represents a car traveling east,
1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1
We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
int solution(vector<int> &A);
that, given a non-empty zero-indexed array A of N integers, returns the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
Assume that:
N is an integer within the range [1..100,000];
each element of array A is an integer that can have one of the following values: 0, 1.
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
题目 3: MinAvgTwoSlice
A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the
sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1).
For example, array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8
contains the following example slices:
slice (1, 2), whose average is (2 + 2) / 2 = 2;
slice (3, 4), whose average is (5 + 1) / 2 = 3;
slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.
The goal is to find the starting position of a slice whose average is minimal.
Write a function:
int solution(vector<int> &A);
that, given a non-empty zero-indexed array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
Assume that:
N is an integer within the range [2..100,000];<
4000
/li>each element of array A is an integer within the range [−10,000..10,000].
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).
题目 4:GenomicRangeQuery
A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to
the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have
impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K <
M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6
The answers to these M = 3 queries are as follows:
The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide Awhose impact factor is 1, so the answer is 1.
Write a function:
vector<int> solution(string &S, vector<int> &P, vector<int> &Q);
that, given a non-empty zero-indexed string S consisting of N characters and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
Assume that:
N is an integer within the range [1..100,000];
M is an integer within the range [1..50,000];
each element of arrays P, Q is an integer within the range [0..N − 1];
P[K] ≤ Q[K], where 0 ≤ K < M;
string S consists only of upper-case English letters A, C, G, T.
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).
C 实现版
Write a function:
int solution(int A, int B, int K);
that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:
{ i : A ≤ i ≤ B, i mod K = 0 }
For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.
Complexity:
expected worst-case time complexity is O(1);
expected worst-case space complexity is O(1).
int solution(int A, int B, int K) { // write your code in C++11 if(A%K==0) return (B-A)/K+1; else return (B-(A-A%K))/K; }
题目 2:PassingCars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.
Array A contains only 0s and/or 1s:
0 represents a car traveling east,
1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1
We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
int solution(vector<int> &A);
that, given a non-empty zero-indexed array A of N integers, returns the number of pairs of passing cars.
The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.
Assume that:
N is an integer within the range [1..100,000];
each element of array A is an integer that can have one of the following values: 0, 1.
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
int solution(vector<int> &A) { // write your code in C++11 int all=0,car=0; int n=A.size(); for(int i=n-1;i>=0;i--) { if(A[i]==1) car++; else { all+=car; if(all>1E9) return -1; } } return all; }
题目 3: MinAvgTwoSlice
A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the
sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1).
For example, array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8
contains the following example slices:
slice (1, 2), whose average is (2 + 2) / 2 = 2;
slice (3, 4), whose average is (5 + 1) / 2 = 3;
slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.
The goal is to find the starting position of a slice whose average is minimal.
Write a function:
int solution(vector<int> &A);
that, given a non-empty zero-indexed array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
Assume that:
N is an integer within the range [2..100,000];<
4000
/li>each element of array A is an integer within the range [−10,000..10,000].
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(); int min_index=0; float min_val=10001; for(int i=0;i<n-1;i++) { // float avetwo=(A[i]+A[i+1])/2.0; if((A[i]+A[i+1])<2*min_val) { min_val=(A[i]+A[i+1])/2.0; min_index=i; } if(i<n-2) { //float avethree=(A[i]+A[i+1]+A[i+2])/3.0; if((A[i]+A[i+1]+A[i+2])<3*min_val) { min_val=(A[i]+A[i+1]+A[i+2])/3.0; min_index=i; } } } return min_index; }
题目 4:GenomicRangeQuery
A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to
the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have
impact factors of 1, 2, 3 and 4, respectively. You are going to answer several queries of the form: What is the minimal impact factor of nucleotides contained in a particular part of the given DNA sequence?
The DNA sequence is given as a non-empty string S = S[0]S[1]...S[N-1] consisting of N characters. There are M queries, which are given in non-empty arrays P and Q, each consisting of M integers. The K-th query (0 ≤ K <
M) requires you to find the minimal impact factor of nucleotides contained in the DNA sequence between positions P[K] and Q[K] (inclusive).
For example, consider string S = CAGCCTA and arrays P, Q such that:
P[0] = 2 Q[0] = 4 P[1] = 5 Q[1] = 5 P[2] = 0 Q[2] = 6
The answers to these M = 3 queries are as follows:
The part of the DNA between positions 2 and 4 contains nucleotides G and C (twice), whose impact factors are 3 and 2 respectively, so the answer is 2.
The part between positions 5 and 5 contains a single nucleotide T, whose impact factor is 4, so the answer is 4.
The part between positions 0 and 6 (the whole string) contains all nucleotides, in particular nucleotide Awhose impact factor is 1, so the answer is 1.
Write a function:
vector<int> solution(string &S, vector<int> &P, vector<int> &Q);
that, given a non-empty zero-indexed string S consisting of N characters and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M integers specifying the consecutive answers to all queries.
Assume that:
N is an integer within the range [1..100,000];
M is an integer within the range [1..50,000];
each element of arrays P, Q is an integer within the range [0..N − 1];
P[K] ≤ Q[K], where 0 ≤ K < M;
string S consists only of upper-case English letters A, C, G, T.
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).
void InitVector (vector<int> &vec,string S, char c) { for(int i=0;S[i]!='\0';i++) { if(i!=0) vec[i]=vec[i-1]; if(S[i]==c) vec[i]=i; } } vector<int> solution(string &S, vector<int> &P, vector<int> &Q) { // write your code in C++11 vector<int> vec; int n=P.size(); int m=Q.size(); if(n!=m) return vec; int k=S.length(); // four vector and init vector<int> A(k,-1); vector<int> C(k,-1); vector<int> G(k,-1); vector<int> T(k,-1); InitVector(A,S,'A'); InitVector(C,S,'C'); InitVector(G,S,'G'); InitVector(T,S,'T'); // result for(int i=0;i<n;i++) { if(A[Q[i]]>P[i]-1) vec.push_back(1); else if(C[Q[i]]>P[i]-1) vec.push_back(2); else if(G[Q[i]]>P[i]-1) vec.push_back(3); else vec.push_back(4); } return vec; }
C 实现版
void InitString(char *S,int *arr,char c) { arr[0]=-1; for(int i=0;S[i]!='\0';i++) { if(i!=0) arr[i]=arr[i-1]; if(S[i]==c) arr[i]=i; } } struct Results solution(char *S, int P[], int Q[], int M) { struct Results result; // write your code in C99 int *A=(int *)malloc(1e5*sizeof(int)); int *C=(int *)malloc(1e5*sizeof(int)); int *G=(int *)malloc(1e5*sizeof(int)); int *T=(int *)malloc(1e5*sizeof(int)); InitString(S,A,'A'); InitString(S,C,'C'); InitString(S,G,'G'); InitString(S,T,'T'); /// calcuate int *res=(int *)malloc(M*sizeof(int)); for(int i=0;i<M;i++) { if(A[Q[i]]>P[i]-1) res[i]=1; else if(C[Q[i]]>P[i]-1) res[i]=2; else if(G[Q[i]]>P[i]-1) res[i]=3; else res[i]=4; } result.A = res; result.M = M; return result; }
相关文章推荐
- VMware Workstation 11 中 Ubuntu 14.04 的 VMware Tools 问题 : 共享文件夹
- 理解内核initcall
- java、javac -version不一致(java编译及运行环境不一致)的环境变量设置问题解决
- 兼容ie8以下的圆角头像制作方法
- android开发游记:DrawerLayout 实现抽屉效果的导航菜单
- poj--2955--Brackets(区间dp)
- 电商平台稳定性的保证 分布式压力测试
- Android之Intent
- 迭代器失效
- Android实用的任务管理器—tractor
- linux文件权限以及权限修改命令
- NGINX 配置Thinkphp
- 错误:unrecognized command line option “-std=c++11”
- 怎样成为一个天才
- ubuntu iptables设置【转】
- oneinstack一键部署linux生产环境那点事(ubuntu)
- operator
- Java 工程师成神之路
- java中路径的问题
- 架构师学习方向