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,把其中涉及到性能计数器的部分摘抄出来了. ...
随机推荐
- Jquery 全选、反选问题的记录
<div id="list"> <ul id="choseList" > <li><input type=" ...
- JavaScript中逻辑运算符的使用
逻辑运算符用于对一个或多个布尔值进行逻辑运算.在JavaScript中有3个逻辑运算符,如下表所示. 运算符 描述 示例 && 逻辑与 a && b //当a和b都为 ...
- NX自动出图 (更新啦)
1.创建模板类零件图纸 2.创建顶针类零件图纸 3.创建镶件类零件图纸 4.自动创建孔表
- HTML5——移动端的点击、拖拽
移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...
- WinRT ListView间隔变色(二)
上文说到,WinRt中,我们不能在Style的Setter使用Binding.这个问题其实从SL5之前,一直都不可以.但是,为了使用强大的Binding,人们也一直想使用各种方法来达到Binding ...
- 最优化方法系列:Adam+SGD—>AMSGrad
自动调参的Adam方法已经非常给力了,不过这主要流行于工程界,在大多数科学实验室中,模型调参依然使用了传统的SGD方法,在SGD基础上增加各类学习率的主动控制,以达到对复杂模型的精细调参,以达到刷出最 ...
- Detectron:Pytorch-Caffe2-Detectron的一些跟进
pytorch官网:http://pytorch.org/上只有PyTroch的ubuntu和Mac版本,赤裸裸地歧视了一把Windows低端用户. 1. Caffe源码:Caffe源 ...
- 基于Crypto++的aes 字符串加解密实现
esaes.h: #ifndef ESAES_H #define ESAES_H #include <cryptopp/aes.h> #include <iostream> # ...
- (转) 淘淘商城系列——Redis的安装
http://blog.csdn.net/yerenyuan_pku/article/details/72849612 通过上文的学习,我相信大家已经将首页的轮播图展示出来了,接下来我们将进入一个新的 ...
- HDU_1175_连连看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1175 大意:连连看规则,只能转两次弯,先输入矩阵0表示没有棋子,正整数表示不同的棋子,然后询问,输入两点坐 ...