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里能不能做?就试着做了做,好像基本成了,整套代码有些毛病,勉强能算个样子,暂时不打算再 ...
随机推荐
- js实现九九乘法表
<script type="text/javascript"> var sum=0; var wite; for (var i = 1; i < 10; i++) ...
- 简单的词法分析和语法分析(C++实现,CodeBlocks+GCC编译)
说明: 分析的语言是SNL语言,详见<编译程序的设计与实现>( 刘磊.金英.张晶.张荷花.单郸编著) 词法分析就是实现了词法分析的自动机 语法分析使用递归下降法 运行结果: 词法分析 得到 ...
- Spring远程调用技术<1>-RMI
在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI) 适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...
- 使用Struts2实现数据校验
使用Struts2实现数据校验 为什么需要数据校验呢?答案很简单,假如当你登录想要京东,这时就需要数据校验了如果不输入用户名的话,那么就不会登陆成功,并且会提示出"请输入用户名"的 ...
- LeetCode 笔记系列13 Jump Game II [去掉不必要的计算]
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- [CareerCup] 2.5 Add Two Numbers 两个数字相加
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- mybatis的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- C/C++实践笔记 008
逗号运算符优先级最低逗号运算符的值是最后一个的值int a=(1,2);执行结果为2逗号运算符每一条语句都要执行,执行方向从左向右 三目运算符表达式1?表达式2:表达式3 C语言里只有0和非0的区别 ...
- C++ URLDecode和URLEncode实现——仅限gb2312,非utf8
转载--http://blog.163.com/zhangjie_0303/blog/static/9908270620148251658993/ #include <iostream> ...
- MySQL安装步骤
MySQL安装步骤 1. 下载MySQL Community Server 5.6.21,注意选择系统类型(32位/64位) 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下. ...