1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
package com.code;
import java.util.Arrays;
public class Test04_4 {
public static int[] solution(int N, int[] A) {
// write your code in Java SE 8
int size = A.length;
int [] res = new int[N];
int max = 0;
for(int i=0;i<size;i++){
if(A[i]==N+1){
if(i>1 && A[i]==A[i-1]){ // handle {max,max,max,max} array
continue;
}
for(int j=0,sizeJ=res.length;j<sizeJ;j++){
res[j] = max;
}
}else{
res[A[i]-1]=res[A[i]-1]+1;
max = Math.max(max, res[A[i]-1]);
}
}
return res;
}
public static void main(String[] args) {
int [] a = {3,4,4,6,1,4,4};
System.out.println(Arrays.toString(solution(5, a)));
int[] b = {6,6,6,6};
System.out.println(Arrays.toString(solution(5, b)));
int[] c = {1};
System.out.println(Arrays.toString(solution(1, c)));
}
}
/**
1. MaxCounters 计数器
Calculate the values of counters after applying all alternating operations: increase counter by 1;
set value of all counters to current maximum.
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.
Write a function:
class Solution { public int[] solution(int N, int[] A); }
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.
*/
1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.的更多相关文章
- counting elements--codility
lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...
- MC34063A development aid
http://www.nomad.ee/micros/mc34063a/index.shtml This is a simple-minded design tool that allows you ...
- counter counters 计数器
counter-reset counter-reset:counter1 /* 重置计数器为 0 */ counter-reset:counter1 0 /* 重置计数器为 0 */ counter- ...
- MapReduce 计数器简介
转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...
- Python collections系列之计数器
计数器(counter) Counter是对字典(无序)类型的补充,用于追踪值的出现次数. 使用counter需要导入 collections 类 ps:具备字典的所有功能 + 自己的功能 1.创建一 ...
- css计数器详解
什么是css计数器 体验更佳排版请戳原文链接:http://blog.liuxianan.com/css-counters.html 就是采用css给一些html元素自动生成编号,比如类似1.3.2这 ...
- HBase之计数器
HBase计数器 #创建counters表 列族['daily','weekly','monthly'] hbase(main):001:0> create 'counters','daily' ...
- 【原创】MapReduce计数器
MapReduce框架内置了一些计数器的支持,当然,我们也可以设置自己的计数器用来满足一些特殊的要求. 其实计数器可以用来完成很多事,关键要看你如何用,例如你想知道map输入数据的指定记录特定的信息有 ...
- 转贴---Performance Counter(包含最全的Windows计数器解释)
http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...
随机推荐
- [ SPOJ Qtree1 ] Query on a tree
\(\\\) Description 给定 \(n\) 个点的树,边按输入顺序编号为\(1,2,...n-1\) . 现要求按顺序执行以下操作(共 \(m\) 次): \(CHANGE\ i\ t_i ...
- es6数值扩展
1. 二进制和八进制表示法 从 ES5 开始,在严格模式之中,八进制就不再允许使用前缀0表示,ES6 进一步明确,要使用前缀0o表示. ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0 ...
- 锐动SDK应用于在线教育方面的解决方案
在线教育 PC端.Android端的屏幕.摄像头录制和直播功能,教师不再拘泥于专业的视频教室进行直播授课. 强大的视频编辑功能,便于课件的制作和不断修改升级. 在线课堂实现了教学视频内容在PC.PAD ...
- 关于使用Axis2 webservice 处理Fault响应时抛org.apache.axis2.AxisFault的分析
使用Axis2这个框架进行webservice协议通讯,期间出了个问题,我(CLIENT)请求后,当服务端返回符合协议的SOAP异常报文,例如<soap:fault> ... 我的程序直接 ...
- dubbo之服务降级
向注册中心写入动态配置覆盖规则:(通过由监控中心或治理中心的页面完成) RegistryFactory registryFactory = ExtensionLoader.getExtensionLo ...
- 第五届蓝桥杯校内选拔第七题_(树型dp)
G将军有一支训练有素的军队,这个军队除开G将军外,每名士兵都有一个直接上级(可能是其他士兵,也可能是G将军).现在G将军将接受一个特别的任务,需要派遣一部分士兵(至少一个)组成一个敢死队,为了增加敢死 ...
- HDU_6017_Girls love 233_(dp)(记忆化搜索)
Girls Love 233 Accepts: 30 Submissions: 218 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- CLISTCTRL2
回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...
- Android开发使用控件入门--环境搭建
Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...
- @ControllerAdvice全局异常拦截
@ControllerAdvice 拦截异常并统一处理 在spring 3.2中,新增了@ControllerAdvice 注解,可以用于定义@ExceptionHandler.@InitBinder ...