codility

来源:互联网 发布:淘宝自己设计店铺 编辑:程序博客网 时间:2024/05/16 18:46

//lesson answer in python

http://codesays.com/solutions-to-training-by-codility/    

1、Prefix Sums

     该做法通常从后往前循环。例如pass car


//alg study

https://class.coursera.org/algs4partI-004/


//算法介绍文档

https://codility.com/media/train/4-Sorting.pdf



//详细题目介绍

http://blog.csdn.net/sunbaigui/article/category/1769905/1

http://blog.csdn.net/caopengcs/article/category/1502799

//老外的codility代码,python

https://github.com/bluemihai/my-codility-solutions

https://github.com/zjsxzy/Codility

//中国的介绍和详解

http://www.cnblogs.com/parapax/tag/codility/


、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

*****//好像是codility公司员工在github上的代码,最全的,连pdf都有

https://github.com/kevinmore/Codility


==========================

QSORT

  1. int compi(const void *a, const void *b)  
  2. {  
  3.     const int *p = a;  
  4.     const int *q = b;  
  5.   
  6.     return *p - *q;  
  7. }  
  8.   
  9. int compd(const void *a, const void *b)  
  10. {  
  11.     const int *p = a;  
  12.     const int *q = b;  
  13.   
  14.     return (*p - *q) * (-1);  
  15. }  
  16.   
  17.   
  18. int main()  
  19. {  
  20.     int a[1000];  
  21.     int i, len, type;  
  22.   
  23.     while(scanf("%d %d", &len, &type) != EOF)  
  24.     {  
  25.         for(i = 0; i < len; i ++)  
  26.         {  
  27.             scanf("%d", &a[i]);  
  28.         }  
  29.   
  30.         switch(type)  
  31.         {  
  32.             case 1 :  
  33.                 //递增排序  
  34.                 qsort(a, len, sizeof(a[0]), compi);  
  35.                 break;  
  36.             case 2 :  
  37.                 //递减排序  
  38.                 qsort(a, len, sizeof(a[0]), compd);  
  39.                 break;  
  40.         }  
  41.   
  42.         if(type == 1)  
  43.         {  
  44.             printf("递增排序结果:\n");  
  45.         }else  
  46.         {  
  47.             printf("递减排序结果:\n");  
  48.         }  
  49.         for(i = 0; i < len; i ++)  
  50.         {  
  51.   
  52.             printf("%d ", a[i]);  
  53.         }  
  54.         printf("\n");  
  55.     }  
  56.     return 0;  
  57. }   

=====================================================

搜索某个区间的和的最大值

Solution to Max-Slice-Sum by codility
Python
1
2
3
4
5
6
7
8
9
def solution(A):
    max_slice_ending_here = A[0]
    max_slice = A[0]
 
    for element in A[1:]:
        max_slice_ending_here = max(element, max_slice_ending_here+element)
        max_slice = max(max_slice, max_slice_ending_here)
 
    return max_slice


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution(A):
    days = len(A)
 
    # If the number of days is zero or one, there
    # is no time to get profit.
    if days < 2:
        return 0
 
    max_price_from_here = A[days-1]
    max_profit = 0
    for index in xrange(days-2, -1, -1):
        # max_price_from_here-A[index] means the maximum
        # profit from current day to end.
        max_profit = max(max_profit, max_price_from_here-A[index])
        max_price_from_here = max(A[index], max_price_from_here)
 
    return max_profit

如果是取减法的最大值,则是反序遍历
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def solution(A):
    days = len(A)
 
    # If the number of days is zero or one, there
    # is no time to get profit.
    if days < 2:
        return 0
 
    max_price_from_here = A[days-1]
    max_profit = 0
    for index in xrange(days-2, -1, -1):
        # max_price_from_here-A[index] means the maximum
        # profit from current day to end.
        max_profit = max(max_profit, max_price_from_here-A[index])
        max_price_from_here = max(A[index], max_price_from_here)
 
    return max_profit



===================================================
欧几里得算法,求最大gcd

When we met with an empty wrapper, we must have been this position for twice. We use i for the first time and j for the second time. Due to the modulo feature, there must be nature number, to say k, so that: i * M + k * N = j * M. Then we could easily prove that the smallest (earliest) i must be zero (for all i != 0, then (i-i) * M + k * N = (j-i) * M ). So the first eaten position would be first position that you meet again. Finally, the j would be the number of chocolates that you will eat.

0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三岁宝宝不爱说话应该怎么办 三岁宝宝不理人怎么办 两岁半宝宝不说话不连贯怎么办 宝宝三岁说话口齿不清怎么办 b超宝宝腿短怎么办 两岁宝宝学说话口吃怎么办 两岁宝宝说话少怎么办 三岁宝宝说话少怎么办 快4岁不会说话怎么办 宝宝2岁半不爱说话怎么办 两岁半宝宝注意力不集中怎么办 中国出生的外籍小孩怎么办签证 中国人入外籍后国内财产怎么办 中国人入外籍后国内资产怎么办 小孩去美国上小学怎么办 咳嗽震的胸口疼怎么办 高中孩子对手机着迷怎么办 小孩不肯读书沉迷游戏怎么办 小孩沉迷吃鸡游戏怎么办 高一数学成绩差怎么办 小孩子学数学用手指算怎么办 孩子d和b分不清怎么办 和家人走散后怎么办幼儿教案 小孩胃口不好不爱吃饭怎么办 幼儿园孩子学习记不住怎么办 幼儿园小孩数字记不住怎么办 大班教案走丢了怎么办 ppt加视频反了怎么办 拔罐之后背疼怎么办 拔完火罐后背疼怎么办 打印机红色的口堵了怎么办 打印机红色复印不出来怎么办 打印机加错颜色墨水怎么办 中班安全教案脚扭伤了怎么办 中班脚扭伤了怎么办教案 中班安全脚扭伤了怎么办 中班安全脚扭伤了怎么办反思 走丢了怎么办教案反思 汽车后轱辘不转怎么办 走丢了怎么办社会教案 小老鼠搬鸡蛋鸡蛋太大怎么办