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.的更多相关文章

  1. counting elements--codility

    lesson 4: counting elements 1. FrogRiverOne 2. PermCheck 3. MissingInteger 4. MaxCounters lesson 4: ...

  2. MC34063A development aid

    http://www.nomad.ee/micros/mc34063a/index.shtml This is a simple-minded design tool that allows you ...

  3. counter counters 计数器

    counter-reset counter-reset:counter1 /* 重置计数器为 0 */ counter-reset:counter1 0 /* 重置计数器为 0 */ counter- ...

  4. MapReduce 计数器简介

    转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...

  5. Python collections系列之计数器

    计数器(counter) Counter是对字典(无序)类型的补充,用于追踪值的出现次数. 使用counter需要导入 collections 类 ps:具备字典的所有功能 + 自己的功能 1.创建一 ...

  6. css计数器详解

    什么是css计数器 体验更佳排版请戳原文链接:http://blog.liuxianan.com/css-counters.html 就是采用css给一些html元素自动生成编号,比如类似1.3.2这 ...

  7. HBase之计数器

    HBase计数器 #创建counters表 列族['daily','weekly','monthly'] hbase(main):001:0> create 'counters','daily' ...

  8. 【原创】MapReduce计数器

    MapReduce框架内置了一些计数器的支持,当然,我们也可以设置自己的计数器用来满足一些特殊的要求. 其实计数器可以用来完成很多事,关键要看你如何用,例如你想知道map输入数据的指定记录特定的信息有 ...

  9. 转贴---Performance Counter(包含最全的Windows计数器解释)

    http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...

随机推荐

  1. Cesium加载影像

    注意:使用自定义数据源时,Cesium.Viewer类参数必须设置为 baseLayerPicker:false A. 使用天地图数据源 //天地图var provider=new Cesium.We ...

  2. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  3. python自动化--语言基础五面向对象、迭代器、range和切片的区分

    面向对象 一.面向对象简单介绍: class Test(): #类的定义 car = "buick" #类变量,定义在类里方法外,可被对象直接调用,具有全局效果 def __ini ...

  4. django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法

    django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法 找到解决方案,执行: python mana ...

  5. TortoiseSVN客户端不能记住用户名和密码

    TortoiseSVN客户端重新设置用户名和密码 在第一次使用TortoiseSVN从服务器CheckOut的时候,会要求输入用户名和密码,这时输入框下面有个选项是保存认证信息,如果选了这个选项,那么 ...

  6. vs 2017 清空 打开项目的历史记录

  7. java byte

    项目中有段代码,一直让我疑惑不解,但我是个很会偷懒的人,只要拷贝来改改能用的代码,万万不会自己动手写,虽然一直有疑惑,也懒得搭理是怎么个原理. 直到今天,又要解析协议,又要动这个地方的代码,还是来盘他 ...

  8. cstring replace

    //使用后将图纸名称存储到配置 换行符用^^替换 m_sTZMC.Replace(_T("\r\n"), _T("^^")); ini.SetValueOfKe ...

  9. MFC窗体大小变化

    对话框的大小变化后,假若对话框上的控件大小不变化,看起来会比较难看.下面就介绍怎么让对话框上的控件随着对话框的大小的变化自动调整. 首先明确的是Windows有一个WM_SIZE消息响应函数,这个函数 ...

  10. crontab定时清理日志

    1.创建shell脚本 vi test_cron.sh #!/bin/bash#echo "====`date`====" >> /game/webapp/test_c ...