๐ ์ฝ๋ฉํ ์คํธ
โ๐ป Github
๋ฌธ์ ์ค๋ช
๋ก๋ 6/45(์ดํ '๋ก๋'๋ก ํ๊ธฐ)๋ 1๋ถํฐ 45๊น์ง์ ์ซ์ ์ค 6๊ฐ๋ฅผ ์ฐ์ด์ ๋งํ๋ ๋ํ์ ์ธ ๋ณต๊ถ์ ๋๋ค. ์๋๋ ๋ก๋์ ์์๋ฅผ ์ ํ๋ ๋ฐฉ์์ ๋๋ค.
์์ ๋น์ฒจ ๋ด์ฉ
1 | 6๊ฐ ๋ฒํธ๊ฐ ๋ชจ๋ ์ผ์น |
2 | 5๊ฐ ๋ฒํธ๊ฐ ์ผ์น |
3 | 4๊ฐ ๋ฒํธ๊ฐ ์ผ์น |
4 | 3๊ฐ ๋ฒํธ๊ฐ ์ผ์น |
5 | 2๊ฐ ๋ฒํธ๊ฐ ์ผ์น |
6(๋์ฒจ) | ๊ทธ ์ธ |
๋ก๋๋ฅผ ๊ตฌ๋งคํ ๋ฏผ์ฐ๋ ๋น์ฒจ ๋ฒํธ ๋ฐํ์ผ์ ํ์๊ณ ๋ํ๊ณ ์์์ต๋๋ค. ํ์ง๋ง, ๋ฏผ์ฐ์ ๋์์ด ๋ก๋์ ๋์๋ฅผ ํ์ฌ, ์ผ๋ถ ๋ฒํธ๋ฅผ ์์๋ณผ ์ ์๊ฒ ๋์์ต๋๋ค. ๋น์ฒจ ๋ฒํธ ๋ฐํ ํ, ๋ฏผ์ฐ๋ ์์ ์ด ๊ตฌ๋งคํ๋ ๋ก๋๋ก ๋น์ฒจ์ด ๊ฐ๋ฅํ๋ ์ต๊ณ ์์์ ์ต์ ์์๋ฅผ ์์๋ณด๊ณ ์ถ์ด ์ก์ต๋๋ค.
์์๋ณผ ์ ์๋ ๋ฒํธ๋ฅผ 0์ผ๋ก ํ๊ธฐํ๊ธฐ๋ก ํ๊ณ , ๋ฏผ์ฐ๊ฐ ๊ตฌ๋งคํ ๋ก๋ ๋ฒํธ 6๊ฐ๊ฐ 44, 1, 0, 0, 31 25๋ผ๊ณ ๊ฐ์ ํด๋ณด๊ฒ ์ต๋๋ค. ๋น์ฒจ ๋ฒํธ 6๊ฐ๊ฐ 31, 10, 45, 1, 6, 19๋ผ๋ฉด, ๋น์ฒจ ๊ฐ๋ฅํ ์ต๊ณ ์์์ ์ต์ ์์์ ํ ์๋ ์๋์ ๊ฐ์ต๋๋ค.
๋น์ฒจ ๋ฒํธ | 31 | 10 | 45 | 1 | 6 | 19 | ๊ฒฐ๊ณผ |
์ต๊ณ ์์ ๋ฒํธ | 31 | 0→10 | 44 | 1 | 0→6 | 25 | 4๊ฐ ๋ฒํธ ์ผ์น, 3๋ฑ |
์ต์ ์์ ๋ฒํธ | 31 | 0→11 | 44 | 1 | 0→7 | 25 | 2๊ฐ ๋ฒํธ ์ผ์น, 5๋ฑ |
- ์์์ ์๊ด์์ด, ๊ตฌ๋งคํ ๋ก๋์ ๋น์ฒจ ๋ฒํธ์ ์ผ์นํ๋ ๋ฒํธ๊ฐ ์์ผ๋ฉด ๋งํ ๊ฑธ๋ก ์ธ์ ๋ฉ๋๋ค.
- ์์๋ณผ ์ ์๋ ๋ ๊ฐ์ ๋ฒํธ๋ฅผ ๊ฐ๊ฐ 10, 6์ด๋ผ๊ณ ๊ฐ์ ํ๋ฉด 3๋ฑ์ ๋น์ฒจ๋ ์ ์์ต๋๋ค.
- 3๋ฑ์ ๋ง๋๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ค๋ ์กด์ฌํฉ๋๋ค. ํ์ง๋ง, 2๋ฑ ์ด์์ผ๋ก ๋ง๋๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค.
- ์์๋ณผ ์ ์๋ ๋ ๊ฐ์ ๋ฒํธ๋ฅผ ๊ฐ๊ฐ 11, 7์ด๋ผ๊ณ ๊ฐ์ ํ๋ฉด 5๋ฑ์ ๋น์ฒจ๋ ์ ์์ต๋๋ค.
- 5๋ฑ์ ๋ง๋๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ค๋ ์กด์ฌํฉ๋๋ค. ํ์ง๋ง, 6๋ฑ(๋์ฒจ)์ผ๋ก ๋ง๋๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํฉ๋๋ค.
๋ฏผ์ฐ๊ฐ ๊ตฌ๋งคํ ๋ก๋ ๋ฒํธ๋ฅผ ๋ด์ ๋ฐฐ์ด lottos, ๋น์ฒจ ๋ฒํธ๋ฅผ ๋ด์ ๋ฐฐ์ด win_nums๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค. ์ด๋, ๋น์ฒจ ๊ฐ๋ฅํ ์ต๊ณ ์์์ ์ต์ ์์๋ฅผ ์ฐจ๋ก๋๋ก ๋ฐฐ์ด์ ๋ด์์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
- lottos๋ ๊ธธ์ด 6์ธ ์ ์ ๋ฐฐ์ด์ ๋๋ค.
- lottos์ ๋ชจ๋ ์์๋ 0 ์ด์ 45 ์ดํ์ธ ์ ์์
๋๋ค.
- 0์ ์์๋ณผ ์ ์๋ ์ซ์๋ฅผ ์๋ฏธํฉ๋๋ค.
- 0์ ์ ์ธํ ๋ค๋ฅธ ์ซ์๋ค์ lottos์ 2๊ฐ ์ด์ ๋ด๊ฒจ์์ง ์์ต๋๋ค.
- lottos์ ์์๋ค์ ์ ๋ ฌ๋์ด ์์ง ์์ ์๋ ์์ต๋๋ค.
- win_nums์ ๊ธธ์ด 6์ธ ์ ์ ๋ฐฐ์ด์ ๋๋ค.
- win_nums์ ๋ชจ๋ ์์๋ 1 ์ด์ 45 ์ดํ์ธ ์ ์์
๋๋ค.
- win_nums์๋ ๊ฐ์ ์ซ์๊ฐ 2๊ฐ ์ด์ ๋ด๊ฒจ์์ง ์์ต๋๋ค.
- win_nums์ ์์๋ค์ ์ ๋ ฌ๋์ด ์์ง ์์ ์๋ ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
lottos | win_nums | result |
[44, 1, 0, 0, 31, 25] | [31, 10, 45, 1, 6, 19] | [3, 5] |
[0, 0, 0, 0, 0, 0] | [38, 19, 20, 40, 15, 25] | [1, 6] |
[45, 4, 35, 20, 3, 9] | [20, 9, 3, 45, 4, 35] | [1, 1] |
์ ์ถ๋ ฅ ์ ์ค๋ช
์ ์ถ๋ ฅ ์ #1
๋ฌธ์ ์์์ ๊ฐ์ต๋๋ค.
์ ์ถ๋ ฅ ์ #2
์์๋ณผ ์ ์๋ ๋ฒํธ๋ค์ด ์๋์ ๊ฐ์๋ค๋ฉด, 1๋ฑ๊ณผ 6๋ฑ์ ๋น์ฒจ๋ ์ ์์ต๋๋ค.
๋น์ฒจ ๋ฒํธ 38 19 20 40 15 25 ๊ฒฐ๊ณผ
์ต๊ณ ์์ ๋ฒํธ | 0→38 | 0→19 | 0→20 | 0→40 | 0→15 | 0→25 | 6๊ฐ ๋ฒํธ ์ผ์น, 1๋ฑ |
์ต์ ์์ ๋ฒํธ | 0→21 | 0→22 | 0→23 | 0→24 | 0→26 | 0→27 | 0๊ฐ ๋ฒํธ ์ผ์น, 6๋ฑ |
์ ์ถ๋ ฅ ์ #3
๋ฏผ์ฐ๊ฐ ๊ตฌ๋งคํ ๋ก๋์ ๋ฒํธ์ ๋น์ฒจ ๋ฒํธ๊ฐ ๋ชจ๋ ์ผ์นํ๋ฏ๋ก, ์ต๊ณ ์์์ ์ต์ ์์๋ ๋ชจ๋ 1๋ฑ์ ๋๋ค.
๋ฌธ์ ํ์ด
๋์ ํ์ด
import Foundation
func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
// 0์ ๊ฐ์ ๊ตฌํ๊ธฐ
let number0 = lottos.filter{ $0 == 0 }.count
// ํ์ฌ ์์
var rank = 7
// ์ผ์นํ๋ ๊ฐ
for i in win_nums {
if lottos.contains(i) {
rank -= 1
}
}
let best = rank - number0
return [ best == 7 ? 6 : best , rank == 7 ? 6 : rank ]
}
์๊ฐ๋ณด๋ค ๊ฐ๋จํ๋ค.
0์ ๊ฐ์์ ๋ก๋๋ฒํธ์ ๋ด๋ฒํธ ์ค ์ผ์นํ๋ ๊ฐ์ ์ฐพ์ผ๋ฉด ๋์๋๋ฐ, ์์๋ฅผ ์ด๋ป๊ฒ ์ ํ๋๊ฐ ๋ฌธ์ ์๋๊ฑฐ ๊ฐ๋ค.
๋๋ ์๊ฐ์ ์กฐ๊ธ ๋ฐ๊ฟ ํ์ฌ ์์๋ฅผ ์ ํด๋๊ณ , ๋ก๋ ๋ฒํธ์ ์ผ์นํ๋ ๊ฐ์๋ฅผ ๋นผ์ฃผ๋ ๋ฐฉ์์ผ๋ก ์ตํ์ ์ ์ ํ๋ค.
ํ์ง๋ง rank ๊ฐ 7์ผ ๋๊ฐ ๋ฌธ์ ์๋ค.
๊ทธ๋์ ๊ทธ๋ฅ ์ผํญ์ฐ์ฐ์๋ฅผ ์ด์ฉํด ํด๊ฒฐํด์ฃผ์๋ค.
์ฌ๊ธฐ์ ๋์ ๋ก๋ ๋ฒํธ ์ค 0์ ์ ์ธํ๋ฉด ๋ฐ๋ณต๋ฌธ์์ ๋ ํจ์จ์ด ๋์ค์ง ์์๊น๋ผ๋ ์๊ฐ์ด ์์๋๋ฐ,
๋ฐ๋ณต๋ฌธ์ ๊ฐ์๊ฐ ํฌ์ง ์๊ณ , 0์ ์ ์ธํ๋ ๊ณผ์ ์ ์คํ์๊ฐ๋ ๊ณ ๋ คํด๋ณด์์ ๋บ๋ค.
๋ค๋ฅธ ์ฌ๋ ํ์ด
import Foundation
func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
let zeroCount = lottos.filter { $0 == 0}.count
let winCount: Int = win_nums.filter { lottos.contains($0) }.count
return [min(7-winCount-zeroCount,6), min(7-winCount,6)]
}
์ด ๋ถ์ lottos์ ๊ฐ๊ณผ winCount์ ๊ฐ์ ์ฐพ์ ๋ ๊ณ ์ฐจํจ์๋ฅผ ์ฌ์ฉํ๋ค.
๋ด๊ฐ ๊ณ ๋ฏผํ์๋ ์์๊ฐ 7์ธ ๋ถ๋ถ์ min ํจ์๋ฅผ ์ด์ฉํด ํธ์ จ๋ค.
func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
let mine = Set(lottos)
let winner = Set(win_nums)
let score = mine.intersection(winner).count
let joker = lottos.filter { ($0 == 0) }.count
let ranking = [6, 6, 5, 4, 3, 2, 1]
return [ranking[score + joker], ranking[score]]
}
์ด ๋ถ์ ์๋ก์ด ๊ด์ ์์ ์ ๊ทผ์ ํ์ ์ ์ ๊ธฐํ๋ค.
๋ ๊ฐ์ง์ ๋ฐฐ์ด์ ์งํฉ์ ๋ฃ์ด ๊ต์งํฉ์ผ๋ก ํธ์ จ๋ค.
๋ํ ์์๊ฐ 7์ธ ๋ถ๋ถ์ ์๋ก์ด ranking ๋ฐฐ์ด์ ์ฌ์ฉํด์ ํด๊ฒฐํ์๋ค.
์งํฉ์ผ๋ก ํ์์ ๋์ ์คํ์๊ฐ์ ๋น๊ตํด๋ณด๊ณ ์ถ์ด์ ์์ ์ฝ๋๋ฅผ ์ด์ง ๋ฐ๊พธ์ด ์ฃผ์๋ค.
func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
let mine = Set(lottos)
let winner = Set(win_nums)
let score = mine.intersection(winner).count
let joker = lottos.filter { ($0 == 0) }.count
let best = 7 - score - joker
let worst = 7 - score
return [ best == 7 ? 6 : best, worst == 7 ? 6 : worst ]
}
// ์งํฉ์ผ๋ก ํผ ์คํ์๊ฐ
ํ
์คํธ 1 ใ ํต๊ณผ (0.07ms, 16.4MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.07ms, 16.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.08ms, 16.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.07ms, 16.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.07ms, 16.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.09ms, 16.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.06ms, 16.5MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.05ms, 16.4MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.07ms, 16.4MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.05ms, 16.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.07ms, 16.2MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.04ms, 16.4MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.05ms, 16.4MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.06ms, 16.3MB)
ํ
์คํธ 15 ใ
// ๋ฐ๋ณต๋ฌธ๊ณผ contains๋ฅผ ์ฌ์ฉํด ํผ ์คํ์๊ฐ
ํ
์คํธ 1 ใ ํต๊ณผ (0.05ms, 16.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.04ms, 16.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.04ms, 16.4MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.05ms, 16.4MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.08ms, 16.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.05ms, 16.4MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.04ms, 16.5MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.05ms, 16.2MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.04ms, 16.1MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.04ms, 16.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.04ms, 16.6MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.06ms, 16.4MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.05ms, 16MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.04ms, 16.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.05ms, 16.4MB)
์งํฉ๋ณด๋ค ๋ฐ๋ณต๋ฌธ๊ณผ contain๊ฐ ์กฐ๊ธ ๋ ๋น ๋ฅธ ๊ฒ ๊ฐ๋ค.