首页 > 其他分享 >Counting Elements - MaxCounters

Counting Elements - MaxCounters

时间:2022-12-06 00:23:02浏览次数:106  
标签:integers given Elements int counter MaxCounters array Counting operation

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 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 array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Result array should be returned as an array of integers.

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.

Write an efficient algorithm for the following assumptions:

  • 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].

 

 

 

 

 

Test Results

查看代码

class Solution {
    public int[] solution(int N, int[] A) {
        
        int[] ret = new int[N];
        int operation = -1;
        int max = 0;
        int min = 0;

        for(int i = 0; i < A.length; i++) {
            operation = A[i];
            if (operation <= N) {
                operation--;
                ret[operation] = Math.max(ret[operation] + 1, min + 1);
                max = Math.max(ret[operation], max);
            } else {
                min = max;
            }
        }

        for(int i = 0; i < N; i++) {
            ret[i] = Math.max(ret[i], min);
        }
        return ret;
    }
}

标签:integers,given,Elements,int,counter,MaxCounters,array,Counting,operation
From: https://www.cnblogs.com/KresnikShi/p/16953987.html

相关文章

  • Counting Elements - PermCheck
    QuestionAnon-emptyarrayAconsistingofNintegersisgiven.A permutation isasequencecontainingeachelementfrom1toNonce,andonlyonce.Forexam......
  • Counting Elements - FrogRiverOne
    QuestionAsmallfrogwantstogettotheothersideofariver.Thefrogisinitiallylocatedononebankoftheriver(position0)andwantstogettotheop......
  • CF 1722E Counting Rectangles(前缀和)
    题目分析(摘自这篇博客,原博主关于包含的定义有错误,在此处做了修改)给出一个长度为1e5的矩阵序列和1e5次询问,每次询问给出两个上下界矩阵,保证大矩阵包含小矩阵,请输出矩阵序......
  • LeetCode347. Top K Frequent Elements
    题意给一个序列,输出其前K个出现频次的数字方法优先队列代码classSolution{public:structnode{intval;//值intcnt;//出现次数......
  • 数数字(Digit Counting)
    DigitCountingTimelimit:3.000secondsTrungisboredwithhismathematicshomeworks.Hetakesapieceofchalkandstartswritingasequenceofconsecutiveint......
  • 【Amadeus原创】零基础使用vue3和elements创建应用
    一.创建环境1.创建D:\code\vue文件夹2.vscode打开文件夹3.打开终端,输入​​npminstall-g@vue/cli​​4.配置环境变量终端输入:​​npmconfiglist​​找到路径将路......
  • Java 8 | Collectors counting() with Examples
    https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/Collectorscounting() methodisusedtocountthenumberofelementspassedinthestr......
  • [AGC005D] ~K Perm Counting
    [AGC005D]~KPermCounting智慧转化,但不是很难想到。首先考虑容斥,设\(f_i\)表示强制\(i\)个位置\(|P_i-i|=k\)的方案数,那么答案就为\(\sum_{i=0}^nf_i(n-i)!\),......
  • Initialize all elements of an array to same value in C/C++
    UsingDesignatedInitializers//ordon'tspecifythesizeintarr[]={[0...4]=1};Usingstd::fill_nfunctionFinally,wecanusestd::fill_ninC++,......
  • LG4778 Counting swaps 题解
    LG4778Countingswaps给定你一个\(1\simn\)的排列\(p\),可进行若干次操作,每次选择两个整数\(x,y\),交换\(p_x,p_y\)。用最少的操作次数将给定排列变成单调上升的序......