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:
struct Results solution(int N, int A[], int M) {
struct Results result;
// write your code in C99
result.C = malloc(sizeof(int)*N);
result.L = N;
int max = ;
int tempMax = ;
int i;
for(i=;i<N;i++)
{
result.C[i]=;
}
for(i=;i<M;i++)
{
if(A[i]<=N)
{
if(result.C[A[i]-]<max)
{
result.C[A[i]-] = max+;
}
else
{
result.C[A[i]-]++;
}
if(tempMax<result.C[A[i]-])
{
tempMax = result.C[A[i]-];
}
}
else
{
max = tempMax;
}
}
for(i=;i<N;i++)
{
if(result.C[i]<max)
{
result.C[i]=max;
}
}
return result;
}
Pay attention to the last for cycle;
我将所有小于max的值通过最后一个循环同步到了max,而在原来的循环中,如果没有更新这个节点的值,则即使它小于max,也不会去更新。
这样,就避免了每一次max操作时,对所有的节点进行遍历,将时间复杂度从N*M,降到了N+M
Private-code MaxCounter的更多相关文章
- PHP常用类------生成验证码类Code
直接附上代码吧!很简单的代码,写一遍基本就会了,主要明白用GD库画图的几个步骤: 1.先创建画布,不然画在哪儿?(imagecreatetruecolor)2.根据要画的数据,选定颜色 (imagec ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
- 农业银行快捷支付php版说明和实例代码
接入的是shopnc,代码改改就可以用了,虽然不是一个完善的类,也可以按照类的方法直接调用,省得再去扣开发文档 农行在接收返回信息也会验证一次,还有一点就是页面通知返回结果 一定要用服务器通知,不然会 ...
- 一个不错的php验证码的类
类的代码: <?php class Captcha { private $width; private $height; private $codeNum; private $code; pri ...
- 【转】超实用的JavaScript技巧及最佳实践
众所周知,JavaScript是一门非常流行的编程语言,开发者用它不仅可以开发出炫丽的Web程序,还可以用它来开发一些移动应用程序(如PhoneGap或Appcelerator),它还有一些服务端实现 ...
- python3.4 build in functions from 官方文档 翻译中
2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...
- 一个漂亮的php验证码类(分享)
直接上代码: 复制代码 代码如下: //验证码类class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRS ...
- 45个实用的JavaScript技巧、窍门和最佳实践
在这篇文章中,我将分享一组JavaScript的技巧.窍门和最佳实践,这些都是JavaScript程序员应该知晓的,不管他们是使用在浏览器/引擎上,还是服务器端(SSJS——Service Side ...
- javascript实用技巧,js小知识
一.js整数的操作 使用|0和~~可以将浮点转成整型且效率方面要比同类的parseInt,Math.round 要快,在处理像素及动画位移等效果的时候会很有用.性能比较见此. var foo = (1 ...
- 使用VB6制作RTD函数
以前模仿大神在vs里使用c#实现RTD函数功能.(真是很生僻的东东啊)C#制作RTD参考:大神博客跳转.最近想VB里能不能做?就试着做了做,好像基本成了,整套代码有些毛病,勉强能算个样子,暂时不打算再 ...
随机推荐
- 探索UDP套接字编程
UDP和TCP处于同一层网络模型中,也就是运输层,基于二者之上的应用有很多,常见的基于TCP的有HTTP.Telnet等,基于UDP有DNS.NFS.SNMP等.UDP是无连接,不可靠的数据协议服务, ...
- 你知道 Twitter,但你可能不知道它的 “成长模式” 和 “参与阶梯”
当你为产品创建 “成长模式” 时,你需要回答以下关键问题(类Twitter产品:http://www.jinhusns.com/Products/Download/?type=xcj ): 目标:产品 ...
- 离散信源的分类和数学模型&&离散无记忆信源的熵
1.离散信源的分类和数学模型 在离散时间发出离散符号的信源称为离散信源.如果信源符号集为有限集,则称为有限离散信源.如果信源符号集为无限可数集,则称为无限离散信源. 离散无记忆信源的N次拓展源:设信源 ...
- NYOJ 187
快速查找素数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 现在给你一个正整数N,要你快速的找出在2.....N这些数里面所有的素数. 输入 给出一个正整数数N(N&l ...
- 数字图像处理中的4邻接,8邻接与m邻接
像素之间的邻接性: 4邻接.如果q在集合N4(p)中,则具有V中数值的两个像素p和q是4邻接的. 8邻接.如果q在集合N8(p)中,则具有V中数值的两个像素p和q是8邻接的. m邻接(混合邻接).如果 ...
- jQuery中slice()用法总结
<!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8&quo ...
- Javascript函数式编程要掌握的知识点讲解
一:理解call和apply 及arguments.callee ECMAScript3给Function的原型定义了两个方法,他们是Function.prototype.call 和 Functio ...
- jquery easyui 1.4.1 API( CHM版)
ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The r ...
- 微博公众平台(二)-- Token验证代码
Token,验证逻辑:1.将Token.timestamp.nonce放入数组 2.将数组从小到大排列 3.将数组按顺序拼装成一个字符串 4.对生成的字符串进行SHA1加密 5.将密文转换为小写 6. ...
- [原创翻译]Protocol Buffer Basics: C#
Protocol Buffer 基础知识:c# 原文地址:https://developers.google.com/protocol-buffers/docs/csharptutorial ...