Time complexity--codility
lesson 3: Time complexity
- exercise:
- Problem: You are given an integer n. Count the total of 1+2+...+n.
def sumN(N):
return N*(N+1)//2
1. TapeEquilibrium -----[100%]
Minimize the value
|(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape.
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
note: 依次求sum
def solution(A):
# write your code in Python 2.7
#left,right =A[0], sum(A)-A[0]
left,right =A[0], sum(A[1:])
result = abs(right - left)
for elem in A[1:-1]:
left,right = left + elem, right - elem
retmp = abs(right - left)
if retmp < result:
result = retmp
return result
2. FrogJmp -----[100%]
Count minimal number of jumps from position X to Y.
A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D.
Count the minimal number of jumps that the small frog must perform to reach its target.
note: O(1) time complexity, 注意是否在边界上,否则加1即可。
def solution(X, Y, D):
# write your code in Python 2.7
if X == Y:
return 0
else:
flag = (Y - X)%D
ret = (Y - X)/D
return ret if flag == 0 else ret + 1
3. PermMissingElem -----[100%]
Find the missing element in a given permutation.
A zero-indexed array A consisting of N different integers is given. The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing.
Your goal is to find that missing element.
note:
- 简单思路是排序,然后依此比较是否是增1关系,也可以用求sum的方式
- 注意边界条件,N取值[0,100,000], 元素取值[1,N+1],
- 故当没有元素的时候,返回1;当只有一个元素的时候,需要考虑元素是否是1
- 当全部有序的时候,考虑最后元素+1返回。
def solution(A):
# write your code in Python 2.7
if len(A) == 0:
return 1
elif len(A) == 1:
return A[0]+1 if A[0] == 1 else A[0] -1
A.sort()
left = A[0]
for elem in A[1:]:
if elem == left + 1:
left = elem
continue
else:
return left + 1
return A[-1]+1 if A[0] == 1 else A[0]-1
def solution(A):
# write your code in Python 2.7
length = len(A)
if length < 1:
return 1
#elif length < 2: # can belong to the next tatal_sum
# return 1 if A[0]==2 else 2
tatal = sum([i for i in xrange(1,length+2,1) ])
tmp = sum(A)
return tatal - tmp
Time complexity--codility的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- [codility] Lession1 - Iterations - BinaryGap
Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...
- 用Codility测试你的编码能力
没有宏观的架构设计,没有特定的框架语言.在Codility提出的一些小问题上,用最纯粹的方式测试你最基本的编码能力. Codility第一课:算法复杂度 各种算法书的开篇大多是算法分析,而复杂度(co ...
- the solution of CountNonDivisible by Codility
question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...
- Instant Complexity - POJ1472
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...
- Runtime Complexity of .NET Generic Collection
Runtime Complexity of .NET Generic Collection I had to implement some data structures for my compu ...
- Examples of complexity pattern
O(1):constant - the operation doesn't depend on the size of its input, e.g. adding a node to the tai ...
- 空间复杂度是什么?What does ‘Space Complexity’ mean? ------geeksforgeeks 翻译
这一章比较短! 空间复杂度(space complexity)和辅助空间(auxiliary space)经常混用,下面是正确的辅助空间和空间复杂度的定义 辅助空间:算法需要用到的额外或者暂时的存储空 ...
随机推荐
- bzoj1001平面图最小割转对偶图最短路
https://www.lydsy.com/JudgeOnline/problem.php?id=1001 很明显的求对偶图的最短路即可(由于特判写错了一直wa = = ) //#pragma com ...
- tensorflow入门(二)
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #使用numpy生成200个随机点 x_data ...
- [Eclipse]Eclipse快捷键
查看一个方法被谁调用:选中方法名字,Search-->Reference-->Workspace
- Vundle简介安装
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Myeclipse WEB工程JSP使用JNDI 数据库连接池连接Mysql数据库
在网上查了很多,最后实现了.下面写一下过程: 首先,在WEBROOT/META-INF下建一个文件context.xml,内容为: <?xml version="1.0" e ...
- Android程序员学WEB前端(3)-HTML(3)-表单嵌套-Sublime
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76522586觉得博文有用,请点赞,请评论,请关注,谢谢!~ 表单嵌套: <!DO ...
- 手游服务端框架之使用Guava构建缓存系统
缓存的作用与应用场景 缓存,在项目中的应用非常之广泛.诸如这样的场景,某些对象计算或者获取的代码比较昂贵,并且在程序里你不止一次要用到这些对象,那么,你就应该使用缓存. 缓存跟java的Coucurr ...
- Java 进阶巩固:什么是注解以及运行时注解的使用
这篇文章 2016年12月13日星期二 就写完了,当时想着等写完另外一篇关于自定义注解的一起发.结果没想到这一等就是半年多 - -. 有时候的确是这样啊,总想着等条件更好了再干,等准备完全了再开始,结 ...
- awk结合正则匹配
利用awk分析data.csv中label列各取值的分布. 在终端执行head data.csv查看数据: name,business,label,label_name 沧州光松房屋拆迁有限公司,旧房 ...
- [Math]PHI, the golden ratio
PHI, the golden ratio 黄金分割比 转载自 http://paulbourke.net/miscellaneous/miscnumbers/ 1. Definition 将一个线段 ...