[Swift] Codility Lesson 4 - MaxCounters
๋ฌธ์
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] = 4the 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:
public func solution(_ N : Int, _ A : inout [Int]) -> [Int]
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] = 4the function should return [3, 2, 2, 4, 2], as explained above.
Write anefficientalgorithm 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].
๋ฌธ์ ๋งํฌ
MaxCounters coding task - Learn to Code - Codility
Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
app.codility.com
GitHub
GitHub - yoohyebin/swift
Contribute to yoohyebin/swift development by creating an account on GitHub.
github.com
๋ฌธ์ ๋ถ์
- ์ ์ N๊ณผ ์ ์ํ ๋ฐฐ์ด A๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค.
- 0์ผ๋ก ์ด๊ธฐํ๋ N๊ฐ์ ์์๋ฅผ ๊ฐ๊ณ ์๋ counter๋ฅผ ์์ฑํ์ฌ ๋์์ ์ํํ๋ค.
- ์ฆ๊ฐ: A[k] = X, 1 ≤ X ≤ N, counter[X]๋ฅผ 1 ์ฆ๊ฐ์ํจ๋ค.
- max counter: A[K] = N + 1, ๋ชจ๋ counter์ ๊ฐ์ counter ์์๋ค ์ค ์ต๋๊ฐ์ผ๋ก ๋ฐ๊พผ๋ค.
ํด๊ฒฐ๋ฐฉ์ 1 - ์คํจ
- N๊ฐ์ 0์ผ๋ก ์ด๊ธฐํ๋ re ๋ฐฐ์ด ์์ฑ
- ์ ๋ ฅ๋ ๋ฐฐ์ด A์ ์์ ๊ฐ์ด N+1์ด๋ผ๋ฉด ๋ฐฐ์ด re์ ๋ชจ๋ ์์๋ค์ ์ต๋๊ฐ์ผ๋ก ๋ณ๊ฒฝ
- ์ ๋ ฅ๋ ๋ฐฐ์ด A์ ์์ ๊ฐ์ด N+1์ด ์๋๋ผ๋ฉด re[์์ ๊ฐ]์ 1 ์ฆ๊ฐ
Solution 1
public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
var re: [Int] = Array(repeating: 0, count: N)
for i in A{
if i == N+1{
re = Array(repeating: re.max()!, count: N)
}else{
re[i-1] += 1
}
}
return re
}
- ์ ๋ ฅ์ด ํฐ ๊ฒฝ์ฐ timeout ์ค๋ฅ ๋ฐ์
- max( )์ ์๊ฐ ๋ณต์ก๋๋ O(N)
- Array(repeating:, count:)์ ์๊ฐ ๋ณต์ก๋๋ O(๋ฐฐ์ด ๊ธธ์ด)
ํด๊ฒฐ๋ฐฉ์ 2 - ์ฑ๊ณต
- N๊ฐ์ 0์ผ๋ก ์ด๊ธฐํ๋ counter ๋ฐฐ์ด ์์ฑ
- counter๋ฐฐ์ด์ ์ต๋๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์ maxValue์ max counter ๋์์ ํ์ฒ๋ฆฌ ํ๊ธฐ ์ํ ๋ณ์ maxCounter ์ ์ธ
- ๋ฐฐ์ด A์ ์์๋ฅผ ํ๋์ฉ ํ์ธ <๋ฐฐ์ด A์ ์์ = i>
- i๊ฐ N+1์ด๋ผ๋ฉด, maxCounter ๋ณ์์ ํ์ฌ counter ๋ฐฐ์ด์ ์ต๋๊ฐ์ด ๋ด๊ฒจ์๋ maxValue ๊ฐ์ ๋ฃ์
- i๊ฐ N+1์ด ์๋๋ผ๋ฉด
- counter[i-1]์ด maxCounter๋ณด๋ค ์๋ค๋ฉด counter[i-1]์ maxCounter๋ก ๋ณ๊ฒฝ
- counter[i-1]์ 1 ์ฆ๊ฐ
- ์ต๋๊ฐ์ ๋ด์ maxValue ๋ณ์์ ๋น๊ตํ์ฌ ์ต๋๊ฐ update
- counter ๋ฐฐ์ด์์ maxCounter ๋ณด๋ค ์์ ๊ฐ์ด ์๋ ์์๋๋ก ํ์, ์์ ๊ฐ์ด ์๋ค๋ฉด maxCounter ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
Solution 2
public func solution(_ N : Int, _ A : inout [Int]) -> [Int] {
var counter: [Int] = Array(repeating: 0, count: N)
var maxValue = 0, maxCounter = 0
for i in A{
if i == N+1{
maxCounter = maxValue
}else{
if counter[i-1] < maxCounter{
counter[i-1] = maxCounter
}
counter[i-1] += 1
maxValue = max(counter[i-1], maxValue)
}
}
for index in 0..<counter.count{
if counter[index] < maxCounter {counter[index] = maxCounter}
}
return counter
}
- ์๊ฐ ๋ณต์ก๋: O(N + M)