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的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. [codility] Lession1 - Iterations - BinaryGap

    Task1: A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is ...

  5. 用Codility测试你的编码能力

    没有宏观的架构设计,没有特定的框架语言.在Codility提出的一些小问题上,用最纯粹的方式测试你最基本的编码能力. Codility第一课:算法复杂度 各种算法书的开篇大多是算法分析,而复杂度(co ...

  6. the solution of CountNonDivisible by Codility

    question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...

  7. Instant Complexity - POJ1472

    Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Description Analyzing the run-time comple ...

  8. Runtime Complexity of .NET Generic Collection

    Runtime Complexity of .NET Generic Collection   I had to implement some data structures for my compu ...

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

  10. 空间复杂度是什么?What does ‘Space Complexity’ mean? ------geeksforgeeks 翻译

    这一章比较短! 空间复杂度(space complexity)和辅助空间(auxiliary space)经常混用,下面是正确的辅助空间和空间复杂度的定义 辅助空间:算法需要用到的额外或者暂时的存储空 ...

随机推荐

  1. 原生javascript-日历插件编写

    在线实例:http://lgy.1zwq.com/calendar/ 按照我们常用的日历格式,是7*6的格子,所以生成格子的总数就确定为42 例子:(如:2013年8月,这个时间为例子) /*---- ...

  2. PyCharm在win10的64位系统安装实例

    搭建环境 1.win10_X64,其他Win版本也可以. 2.PyCharm版本:Professional-2016.2.3. 搭建准备 1.到PyCharm官网下载PyCharm安装包. 2.选择W ...

  3. 利用ModSecurity防御暴力破解

    利用ModSecurity防御暴力破解 from:http://www.freebuf.com/articles/web/8749.html 2013-04-18 共553248人围观 ,发现 12 ...

  4. 上下行分流下行负载方式和能ping通但不能打开

    1 下行线路负载方式选择  目的端口+协议  否则有可能出现微信443端口图片打不开的情况. 2.彭ping通但是打不开的情况下将上行线路mtu值改小 由1500改为1450

  5. C++:创建线程初试

    1.使用CreatThread创建 #include <iostream> #include <Windows.h> using namespace std; /* 创建一个线 ...

  6. ExcelHelper.cs

    using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...

  7. C# 图片缩放,拖拽后保存成图片的功能

    窗体界面部分如下: 鼠标的缩放功能需要手动在 OpertaionImg.Designer.cs 文件里面添加一句代码,具体代码如下: //picturePhoto显示图片的控件 this.pictur ...

  8. python爬虫之requests模块

    一. 登录事例 a. 查找汽车之家新闻 标题 链接 图片写入本地 import requests from bs4 import BeautifulSoup import uuid response ...

  9. cookies分类

    Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie. 内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了,其存在时间是短暂的.硬盘Cooki ...

  10. asp.net设置gridview页码显示遇到的问题

    问题:分页部分显示的页码撑开占满整个表格底部 解决方法: 1.通过浏览器察看源,发现是分页部分的table样式受到整个页面的table设置的样式的影响,分页是一个tr里面的td里面的table 2.设 ...