prefix sums--codility
lesson 5: prefix sums
- PassingCars
Count the number of passing cars on the road.
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).
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),
思路:
可以计算suffix sum的方式
然后,从前面开始遍历list,遇到a = 0,result即加上当前的suffix sum的值
此题元素是0,1,故可以不用保留每一步的计算,题目有要求限制O(1)的space, 也是给出提示,用一个变量retsum值来记录,每一步的prefix sum值,每移动一步,元素是1的话,将retsum 减1, 即是下一个prefix sum 值。
Detected time complexity: O(N)
[100%]
def solution(A):
# write your code in Python 2.7
result = 0
retsum = sum(A)
for a in A:
if a == 0:
result += retsum
if result > 1000000000:
return -1
else:
retsum -= 1
return result
2. CountDiv
Compute number of integers divisible by k in range [a..b].
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.
Assume that:
- A and B are integers within the range [0..2,000,000,000];
- K is an integer within the range [1..2,000,000,000];
A ≤ B.
Complexity:
- expected worst-case time complexity is O(1);
- expected worst-case space complexity is O(1).
CountDiv solution 1
- Test score 100%
def solution(A, B, K):
# write your code in Python 2.7
ra = -1 if A == 0 else (A - 1)/K
rb = B/K
return rb -ra
solution 2
- Test score 100%
def solution(A, B, K):
# write your code in Python 2.7
c = 1 if A%K == 0 else 0
return B/K -A/K + c
def solution(A, B, K):
# write your code in Python 2.7
return (B/K - A/K) if (A%K != 0 ) else (B/K - A/K + 1)
3. GenomicRangeQuery
Find the minimal nucleotide from a range of sequence DNA.
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 A whose impact factor is 1, so the answer is 1.
the function should return the values [2, 4, 1], as explained above.
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).
解法一:
- Test score 62%
- Detected time complexity:
O(N * M) - 最初的想法是:根据给出的范围,用set保存,看是否有各元素
- 时间复杂度,明显达不到O(N+M)
def getMinFactor(S,l,i,j):
if j == (l-1):
tmp = set(S[i:]) #
else:
tmp = set(S[i:(j+1)])
if 'A' in tmp:
return 1
elif 'C' in tmp:
return 2
elif 'G' in tmp:
return 3
else:
return 4
def solution(S, P, Q):
# write your code in Python 2.7
length = len(S)
result = []
for x,y in zip(P,Q):
#print x,y
result.append(getMinFactor(S,length,x,y))
return result
解法二:
- Test score 100%
- used each list to save that states whether has element or not
- 在用prefix sum 做差的方式,依次检查是否存在A,C,G,T字符
- 注意数值关系
def calcPrefixSum(S):
l = len(S)+1
pa,pc,pg = [0]*l,[0]*l,[0]*l
for idx,elem in enumerate(S):
a,c,g = 0,0,0
if elem == 'A':
a = 1
elif elem == 'C':
c = 1
elif elem == 'G':
g = 1
pa[idx+1] = pa[idx] + a
pc[idx+1] = pc[idx] + c
pg[idx+1] = pg[idx] + g
return pa,pc,pg
def solution(S, P, Q):
# write your code in Python 2.7
pA,pC,pG = calcPrefixSum(S)
result = []
for i,j in zip(P,Q):
if pA[j+1] - pA[i] > 0:
ret = 1
elif pC[j+1] - pC[i] > 0:
ret = 2
elif pG[j+1] - pG[i] > 0:
ret = 3
else:
ret = 4
result.append(ret)
return result
根据prefix sum list:
pA = [0, 0, 1, 1, 1, 1, 1, 2]
pC = [0, 1, 1, 1, 2, 3, 3, 3]
pG = [0, 0, 0, 1, 1, 1, 1, 1]
故,是下标[j+1]-[i]
4. MinAvgTwoSlice
Find the minimal average of any slice containing at least two elements.
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.
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N),
sloution
- Test score 100%
note: transfer to 2/3 ,
- 只要查看相邻两个和三个的数的平均值即可
- proof
def solution(A):
# write your code in Python 2.7
length = len(A)
minStartPos = 0
minSum = (A[0] + A[1])/2.0
for i in xrange(length - 2):
tmp = (A[i] + A[i+1])/2.0
if tmp < minSum:
minSum = tmp
minStartPos = i
tmp = (tmp*2 + A[i+2])/3.0
if tmp < minSum:
minSum = tmp
minStartPos = i
if (A[-1] + A[-2])/2.0 < minSum:
minStartPos = length - 2
return minStartPos
prefix sums--codility的更多相关文章
- 【题解】【数组】【Prefix Sums】【Codility】Genomic Range Query
A non-empty zero-indexed string S is given. String S consists of N characters from the set of upper- ...
- 【题解】【数组】【Prefix Sums】【Codility】Passing Cars
A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of arra ...
- Codeforces 837F Prefix Sums
Prefix Sums 在 n >= 4时候直接暴力. n <= 4的时候二分加矩阵快速幂去check #include<bits/stdc++.h> #define LL l ...
- CodeForces 837F - Prefix Sums | Educational Codeforces Round 26
按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...
- Educational Codeforces Round 26 [ D. Round Subset ] [ E. Vasya's Function ] [ F. Prefix Sums ]
PROBLEM D - Round Subset 题 OvO http://codeforces.com/contest/837/problem/D 837D 解 DP, dp[i][j]代表已经选择 ...
- CodeForces 1204E"Natasha, Sasha and the Prefix Sums"(动态规划 or 组合数学--卡特兰数的应用)
传送门 •参考资料 [1]:CF1204E Natasha, Sasha and the Prefix Sums(动态规划+组合数) •题意 由 n 个 1 和 m 个 -1 组成的 $C_{n+m} ...
- CF1303G Sum of Prefix Sums
点分治+李超树 因为题目要求的是树上所有路径,所以用点分治维护 因为在点分治的过程中相当于将树上经过当前$root$的一条路径分成了两段 那么先考虑如何计算两个数组合并后的答案 记数组$a$,$b$, ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- codeforces:Prefix Sums分析和实现
题目大意: 给出一个函数P,P接受一个数组A作为参数,并返回一个新的数组B,且B.length = A.length + 1,B[i] = SUM(A[0], ..., A[i]).有一个无穷数组序列 ...
- [CF1204E]Natasha,Sasha and the Prefix Sums 题解
前言 本文中的排列指由n个1, m个-1构成的序列中的一种. 题目这么长不吐槽了,但是这确实是一道好题. 题解 DP题话不多说,直接状态/变量/转移. 状态 我们定义f表示"最大prefix ...
随机推荐
- linux实践--字符集
一.ASCII码 首先懂得ASCII码表 二 八 十 十六 缩写/字符 0000 0000 0 0 00 NUL(null) 0000 0001 1 1 01 SOH(start of headlin ...
- sqlite中的时间
插入时间的sql语句 ','-61') 时间格式'2014-11-17T19:37:32' 年月日和时分秒之间多了一个字母T,保存到数据库的时候,会自动给时间加8个小时. 保存的结果为2014-11- ...
- MyBatis基本工作原理
Application是程序员开发的Java代码,蓝色为MyBatis框架. API是MyBatis提供的增删改查等功能接口. 老式SQL写法我们在Dao中写SQL: SELECT * FROM us ...
- 使用javascript模拟常见数据结构(二)
四.链表 每种语言都实现了数组.这种数据结构非常方便,提供了一个便利的[]语法来访问它的元素.然而,这种数据结构有一个缺点:(在大多数语言中)数组的大小是固定的,从数组的起点或中间插入或移除项的成本很 ...
- 一个故意消耗内存的java程序MemoryEater
公司提供的测试服务器是vmware的,号称给我6G, 物理内存事实上是按需分配的. 等到真正拿到6G物理内存,黄花菜都凉了. 看到下面的文章,觉得故意用java程序侵占6G内存, 然后把侵占到内存的释 ...
- <p>1、查询端口号占用,根据端口查看进程信息</p>
2017年6月份的时候,我就着手在公司推广git,首先我自己尝试搭建了GitLab来管理代码,并且通过以下博客记录了GitLab的搭建,以及GitLab备份,GitLab升级等事情. git学习——& ...
- 12.2 Web窗体--代码片段详解
第12章 使用Web窗体 ※ 除常规HTML元素之外,Web窗体文件还包含另外3种内容:代码片段.可编程HTML元素和控件 ※ 代码隐藏类只应包含特定于单个Web窗体的代码.如果存在多个Web窗体 ...
- IEnumerable<T> 接口和GetEnumerator 详解
IEnumerable<T> 接口 .NET Framework 4.6 and 4.5 公开枚举数,该枚举数支持在指定类型的集合上进行简单迭代. 若要浏览此类型的.NET Frame ...
- xssProject在java web项目中应用
注:转载http://337027773.blog.163.com/blog/static/54376980201451133534157/ 1.项目引入xssProtect-0.1.jar.antl ...
- C# 格式化一些知识点
这是在看<C#本质论>偶然遇见的一个问题,看不懂,那后面的就没有法看了,于是百度搜索了“C#格式化”这一关键字,于是有了下面的内容.很多一下子记不住,又怕自己忘记,还是做一个笔记吧,方便自 ...