일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- recommendation system
- 코딩테스트
- 부스트캠프
- 시각화
- TF-IDF
- 추천시스템
- 코테
- 분산 시스템
- pytorch
- SGD
- 추천 시스템
- Overfitting
- codingtest
- wordcloud
- Tensor
- 웹크롤링
- 웹스크래핑
- 머신러닝
- 파이썬
- 백준
- 프로그래머스
- 딥러닝
- 알고리즘
- Python
- coursera
- 데이터
- 데이터 엔지니어링
- Cosine-similarity
- selenium
- 협업 필터링
- Today
- Total
개발자식
[Codility Lesson4] MaxCounters 본문
문제 : MaxCounters
Test results - Codility
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. T
app.codility.com
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].
나의 풀이
1차 시도
def solution(N, A):
temp = [0]*(N+1)
for i in range(len(A)):
if A[i] <= N:
temp[A[i]]+=1
else:
temp = [max(temp)]*(N+1)
return temp[1:]
- O(N*M) , SCORE :66%
- max() : O(N)
2차 시도
def solution(N, A):
temp = [0]*(N+1)
maxnum = 0
for i in range(len(A)):
if A[i] <= N:
temp[A[i]]+=1
if maxnum < temp[A[i]]:
maxnum = temp[A[i]]
else:
temp = [maxnum]*(N+1)
return temp[1:]
- O(N + M), SCORE : 88%
3차 시도
- 2차 시도 Performance tests를 참고해서 배열A의 모든 값이 N보다 큰 경우를 따로 처리해주었다.
def solution(N, A):
temp = [0]*(N+1)
maxnum = 0
if min(A) > N:
return temp[1:]
for i in range(len(A)):
if A[i] <= N:
temp[A[i]]+=1
if maxnum < temp[A[i]]:
maxnum = temp[A[i]]
else:
temp = [maxnum]*(N+1)
return temp[1:]
- O(N + M), SCORE : 100%
테스트 결과를 안봤다면 풀지 못했을거다....
codility는 극단적인 경우의 수를 꼭 고려해줘야 한다는 것을 몸으로 느끼는 중ㅠㅠ!
다른 풀이
- N+1 값이 들어왔을 때 배열을 새로 만들어서 갱신해주는 방식이 아니라 변수에 넣고 적용해주는 방식인 것 같다
def solution(N,A):
savemaximum = 0
maximum = 0
counter = [0]*N
for i in range(len(A)):
if A[i]<=N:
if counter[A[i]-1]<savemaximum:
counter[A[i]-1]=savemaximum
counter[A[i]-1]+=1
maximum = max(counter[A[i]-1],maximum)
else:
savemaximum = maximum
for i in range(N):
if counter[i]<savemaximum:
counter[i]=savemaximum
return counter
'Algorithm > Codility' 카테고리의 다른 글
[Codility Lesson4] PermCheck (0) | 2022.06.30 |
---|---|
[Codility Lesson4] Counting Elements_MissingInteger (0) | 2022.06.30 |
[Codility_Lesson 4] FrogRiverOne (0) | 2022.06.30 |
[Codility Lesson17] Dynamic programming_NumberSolitaire (0) | 2022.06.29 |
[Codility Lesson3] Time Complexity_ TapeEquilibrium (0) | 2022.06.29 |