Algorithm: Max Counters

后端 未结 22 1570
孤城傲影
孤城傲影 2021-02-04 07:40

I have the following problem:

You are given N counters, initially set to 0, and you have two possible operations on them:

  • increase(X) − counter X is increa
相关标签:
22条回答
  • 2021-02-04 08:36
    def solution(N, A):
        # write your code in Python 2.6
        res = [0] * N
        m = 0
        minValue = 0
        for x in A:
            if 1 <= x <= N:
                res[x - 1] = max(res[x - 1], minValue) + 1
                if res[x - 1] > m:
                    m = res[x - 1]
            else:
                minValue = m
        for i in xrange(N):
            res[i] = max(res[i], minValue)
        return res
    
    0 讨论(0)
  • 2021-02-04 08:36

    with js max score that I can get is 77%

    any improvement?

    function solution(N, A) {
    
      let counters = [];
    
      //fill counter with 0
      for (let i = 0; i < N; i += 1) {
        counters[i] = 0;
      }
    
      //loop array and set counters
      for (let i = 0; i < A.length; i += 1) {
    
        //0 index fix
        let position = A[i] - 1;
    
        if (A[i] <= N) {
          counters[position] += 1;
        } else {
          let max = Math.max(...counters);
          counters.fill(max)
        }
    
      }
    
      return counters;
    
    }
    
    0 讨论(0)
  • 2021-02-04 08:37

    C++11 code:

    #include <algorithm>
    
    vector<int> solution(int N, vector<int> &A) {
    
        vector<int> hist(N, 0);
    
        int last_update = 0;
        int max_value = 0;
    
        for (auto i : A){
    
            if (1 <= i && i <= N){
                int& val = hist[i - 1];
    
                if (val < last_update)
                    val = last_update + 1;
                else
                    val++;
    
                if (max_value < val)
                    max_value = val;
            }
    
            if (i == (N+1)){
                last_update = max_value;
            }
    
        }
    
        replace_if(hist.begin(), hist.end(), [last_update](int x){return x < last_update;}, last_update);
    
        return hist;
    }
    
    0 讨论(0)
  • 2021-02-04 08:38

    Here is the kotlin version, 100% on codility

    fun solutionMissingInteger(N: Int, A: IntArray): IntArray {
        val counters = IntArray(N)
        var max = 0
        var lastUpdate = 0
        for (index in A.indices) {
            val element = A[index]
            if (element == N + 1) {
                lastUpdate = max
            } else {
                val position = element - 1
                if (counters[position] < lastUpdate) {
                    counters[position] = lastUpdate + 1
                } else {
                    counters[position] = counters[position] +1
                }
    
                if (counters[position] > max) {
                    max = counters[position]
                }
            }
        }
        setAllCountersToMaxValue(lastUpdate, counters)
        return counters
    }
    
    private fun setAllCountersToMaxValue(lastUpdate: Int, counters: IntArray) {
        for (index in counters.indices) {
            if (counters[index] < lastUpdate)
                counters[index] = lastUpdate
        }
    }
    
    0 讨论(0)
提交回复
热议问题