Private-code MaxCounter

No need for a double cycle: :

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increased by 1,
  • max counter − all counters are set to the maximum value of any counter.

A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:

  • if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
  • if A[K] = N + 1 then operation K is max counter.

For example, given integer N = 5 and array A such that:

    A[0] = 3
    A[1] = 4
    A[2] = 4
    A[3] = 6
    A[4] = 1
    A[5] = 4
    A[6] = 4

the values of the counters after each consecutive operation will be:

    (0, 0, 1, 0, 0)
    (0, 0, 1, 1, 0)
    (0, 0, 1, 2, 0)
    (2, 2, 2, 2, 2)
    (3, 2, 2, 2, 2)
    (3, 2, 2, 3, 2)
    (3, 2, 2, 4, 2)

The goal is to calculate the value of every counter after all operations.

Assume that the following declarations are given:

struct Results {
  int * C;
  int L;
};

Write a function:

struct Results solution(int N, int A[], int M);

that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.

The sequence should be returned as:

  • a structure Results (in C), or
  • a vector of integers (in C++), or
  • a record Results (in Pascal), or
  • an array of integers (in any other programming language).

For example, given:

    A[0] = 3
    A[1] = 4
    A[2] = 4
    A[3] = 6
    A[4] = 1
    A[5] = 4
    A[6] = 4

the function should return [3, 2, 2, 4, 2], as explained above.

Assume that:

  • N and M are integers within the range [1..100,000];
  • each element of array A is an integer within the range [1..N + 1].

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

Elements of input arrays can be modified.

Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

My solution is:

 1 struct Results solution(int N, int A[], int M) {
 2     struct Results result;
 3     // write your code in C99
 4     result.C = malloc(sizeof(int)*N);
 5     result.L = N;
 6     int max = 0;
 7     int tempMax = 0;
 8     int i;
 9
10     for(i=0;i<N;i++)
11     {
12         result.C[i]=0;
13     }
14
15     for(i=0;i<M;i++)
16     {
17         if(A[i]<=N)
18         {
19             if(result.C[A[i]-1]<max)
20             {
21                 result.C[A[i]-1] = max+1;
22             }
23             else
24             {
25                 result.C[A[i]-1]++;
26             }
27
28             if(tempMax<result.C[A[i]-1])
29             {
30                 tempMax = result.C[A[i]-1];
31             }
32         }
33         else
34         {
35             max = tempMax;
36         }
37     }
38
39     for(i=0;i<N;i++)
40     {
41         if(result.C[i]<max)
42         {
43             result.C[i]=max;
44         }
45     }
46     return result;
47 }

Pay attention to the last for cycle;

我将所有小于max的值通过最后一个循环同步到了max,而在原来的循环中,如果没有更新这个节点的值,则即使它小于max,也不会去更新。

这样,就避免了每一次max操作时,对所有的节点进行遍历,将时间复杂度从N*M,降到了N+M

时间: 2024-10-23 15:44:53

Private-code MaxCounter的相关文章

45个实用的JavaScript技巧、窍门和最佳实践

如你所知,JavaScript是世界上第一的编程语言,它是Web的语言,是移动混合应用(mobile hybrid apps)的语言(比如PhoneGap或者Appcelerator),是服务器端的语言(比如NodeJS或者Wakanda),并且拥有很多其他的实现.同时它也是很多新手的启蒙语言,因为它不但可以在浏览器上显示一个简单的alert信息,而且还可以用来控制一个机器人(使用nodebot,或者nodruino).掌握JavaScript并且能够写出组织规范并性能高效的代码的开发人员,已经

45种Javascript技巧大全

原文:45 Useful JavaScript Tips, Tricks and Best Practices 译文:45个有用的JavaScript技巧,窍门和最佳实践 译者:dwqs 在这篇文章中,我将分享一些JavaScript常用的技巧,窍门和最佳实践.不管JavaScript开发者是使用在浏览器/引擎上或者服务器端(SSJS--Service Side JavaScript)JavaScript解释器上,这些他们都是应该知晓的. 需要注意的是,文章中的代码片段均是在最新的Google

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

Why we should overwrite the hashCode() when we overwrite the equals()

Preface Though I have used Java programme language for almost a year, I'm not familiar with a notion 'hash'. I have gotten a little knowledge about hash recently because I am reading a book of algorithm, which introduce the hash function and so on. S

建立一个漂亮的PHP验证码类文件及调用方式

//验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子 private $code;//验证码 private $codelen = 4;//验证码长度 private $width = 130;//宽度 private $height = 50;//高度 private $img;//图形资源句柄 private $font;//指定的字体

85种网站常用JavaScript技巧

40+45种网站常用Javascript技巧转载自网络,地址不详. 1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 <table border oncontextmenu=return(false)><td>no</table> 可用于Table 2. <body onselectstart="return false"> 取消选取.防止复制 3.

John细说PHP的验证码

细说php中的验证码类创建 我这里自己写了一个验证码类,我来演示一下怎么使用,我是菜鸟一枚,大神请略过.我来讲解一下它的使用方法,总共需要两步即可. 第一步: 下载我制作好的验证码类.下载地址:http://files.cnblogs.com/files/xfjpeter/Verify.zip 第二步: 1.创建一个字的验证码文件 1 <?php 2 3 #引入验证码类文件 4 require_once('Verify.class.php'); 5 6 #实例化验证码类 7 #初始化的使用可以传

验证码 图片处理等简单实现过程-php

画图简单介绍: <?php header("Content-type: text/html; charset=utf-8"); //print_r(gd_info()); gd开启与否库验证 /**********创建资源(画布大小)**********/ $width=200; //往右 $height=200;//往下 $file='./image/boy.jpeg'; //创建空画布 $img = imagecreatetruecolor($width,$height);

PHP中的验证码类(完善验证码)

运行结果: <!--vcode.class.php--> <?php class Vcode { private $width; //宽 private $height; //高 private $num; //数量 private $code; //验证码 private $img; //图像的资源 //构造方法, 三个参数 function __construct($width=80, $height=20, $num=4) { $this->width = $width; $