2017年2月17日 星期五

【Python】Codility in Python : Lesson 4 - Counting Elements【MaxCounters】

Counting Elements 練習題的第四題題目是【MaxCounters】
Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

方法一:按照規則 loop 慢慢解
```python def solution(N, A): result = [0] * N minValue = 0 maxValue = 0 for number in A: index = number - 1 if(index == N): minValue = maxValue continue result[index] = max(result[index], minValue) + 1 maxValue = max(maxValue, result[index]) for i in range(N): result[i] = max(result[i], minValue) return result ```

完整練習題 source code 請參閱:github

沒有留言:

張貼留言