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,把其中涉及到性能计数器的部分摘抄出来了. ...
随机推荐
- DateFormat 多线程问题
在写实时应用解析日志的时候,有如下代码: public class CalPvLogParse { private static SimpleDateFormat logTimeFormat = ne ...
- MacOS 下安装 MySQL8.0 登陆 MySQL
按照 官方教程 ,下载安装包,点击安装后,如需在命令行启动,还需设置命令路径: 在命令行中,打开配置文件 .bash_profile: vim ~/.bash_profile 在最后一行加上: PAT ...
- OKHTTP 简单分析
内部使用了OKIO库, 此库中Source表示输入流(相当于InputStream),Sink表示输出流(相当于OutputStream) 特点: ·既支持同步请求,也支持异步请求,同步请求会阻塞当前 ...
- git分支拉取
假设你已经配置好了各种SSH Key之类并熟悉基本的git创建分支.提交分支命令.比如共有2个分支,自己在一台未配置origin电脑上想要拉取某个分支(dev)到本地.步骤如下:1.新建git项目 与 ...
- 自定义 Java Annotation ,读取注解值
1. 首先是自定义注解: package cn.veji.hibernate.po; import java.lang.annotation.ElementType; import java.lang ...
- swift VS NSObject
Any class that does not inherit from another class is known as a base class. Swift classes do not in ...
- CLISTCTRL 获取点击列
CListCtrl中的HitTest.SubItemHitTest的用法 2HitTest:得到当前鼠标位置的Item 其实关键是要有ScreenToClient这个函数的使用,我先前没有用这个函数, ...
- php利用array_filter()过滤数组空值
利用array_filter过滤数组空值 <?php $array = array( 0 => '霜天部落', 1 => false, 2 => 1, 3 => null ...
- ios xmpp 发送语音图片解决方案
ios xmpp 发送语音,图片解决方案,有需要的朋友可以参考下. 目前做IM多是用的xmpp. 因为项目需求需要实现语音和图片的发送. 发送语音图片有三种方法. 1,xmpp smack.文件传输方 ...
- 部署web服务器的配置——补充mysql和tomcat
今天想到了关于mysql的一些配置,以后关于配置mysql和tomcat相关的内容也会补充在这里. tomcat: 1. 更改内存(要设置tomcat内存,解决内存溢出的问题):安装版tomcat,打 ...