Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

30-alstjr7437 #211

Merged
merged 4 commits into from
Jun 30, 2024
Merged

30-alstjr7437 #211

merged 4 commits into from
Jun 30, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Jun 19, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

โš ๏ธ ์‰ฌ์›€ ์ฃผ์˜
๋ฐฉ ๋ฒˆํ˜ธ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

15๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

  1. ๊ฐ ์ž๋ฆฌ ์ˆ˜๋ฅผ ์ž˜๋ผ์„œ ๋ฐ›์•„์˜จ๋‹ค.
  2. ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•  ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
  3. 6๊ณผ 9๋Š” ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 9๊ฐ€ ๋“ค์–ด์˜ค๋ฉด 6์œผ๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
  4. 6๊ณผ 9๋Š” ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 2๋ฅผ ๋‚˜๋ˆ ์„œ ๋ฐ˜์˜ฌ๋ฆผํ•œ๋‹ค.
  5. ๋”•์…”๋„ˆ๋ฆฌ์˜ value์˜ ์ตœ๋Œ€๊ฐ’์„ ๋ฝ‘๋Š”๋‹ค.
let N : String = readLine()!
var result : [Character:Int] = [:]

for i in N{
    let temp = (i == "9") ? "6" : i
    if result[temp] == nil{
        result[temp] = 1
    } else {
        result[temp]! += 1
    }
}
if result["6"] != nil{
    result["6"]! = (result["6"]! + 1) / 2
}

print(result.values.max() ?? 0)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

1. Swift์—์„œ key๊ฐ’์ด ์—†๋Š”๊ฑธ ํ™•์ธํ•˜๋ ค๋ฉด

result[key] == nil์„ ์ด์šฉํ•˜์—ฌ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” nil์„ ์‚ฌ์šฉํ•ด์„œ ํ™•์ธํ•œ๋‹ค.

2. Swift Intํ˜• ๋ฐ˜์˜ฌ๋ฆผ

1๋ฒˆ ๋ฐฉ๋ฒ•๊ณผ 2๋ฒˆ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.
1๋ฒˆ ๋ฐฉ๋ฒ•์€ round ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ธ๋ฐ ๋Œ€์‹  ์ฒ˜์Œ ๋ถ€๋ถ„์— import Foundation๋ฅผ ๋ถ™์—ฌ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
2๋ฒˆ ๋ฐฉ๋ฒ•์€ +1์„ ํ•˜๊ณ  /2๋ฅผ ํ•˜์—ฌ์„œ Float์œผ๋กœ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ๋ฐ˜์˜ฌ๋ฆผ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

// 1
result["6"]! = Int(round(Float(result["6"]!) / 2))

// 2
result["6"]! = (result["6"]! + 1) / 2
image 1๋ฒˆ์€ roundํ•จ์ˆ˜๋ฅผ ๋“ค๊ณ ์˜ค๊ณ  Foundation์„ ์ž„ํฌํŠธ ํ•ด์™€์„œ 12ms
2๋ฒˆ์€ ์ˆ˜์‹์œผ๋กœ๋งŒ ํ•ด๊ฒฐํ•ด์„œ 8ms๊ฐ€ ๋‚˜์˜จ๋“ฏ!

3. ์ถœ๋ ฅ ๋ถ€๋ถ„

์ฐธ๊ณ  ์‚ฌ์ดํŠธ
Optional ๊ณต์‹ ๋ฌธ์„œ
์ถœ๋ ฅ์—์„œ๋„ print(result.values.max())๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด Optional(1)์ด ๋‚˜์˜ค๊ฒŒ ๋˜์„œ ํ‹€๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Optional์ด๋ž€?

์ด ๋ณ€์ˆ˜์—๋Š” ๊ฐ’์ด ๋“ค์–ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ์•ˆ๋“ค์–ด ์žˆ์„ ์ˆ˜(nil)๋„ ์žˆ์Œ!!!
nil์ด ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ค๋ฅธ์  : pointer๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฆ„ value๊ฐ€ ์—†๋Š” ๊ฒƒ์„ ์˜๋ฏธ
Wrapping๋˜์–ด ์žˆ์–ด ๋ชจ๋ฅด๋Š” ์ƒํƒœ๋ผ์„œ Optional(1)์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ์ด ๋จ

๊ทธ๋ž˜์„œ

  1. result.values.max()!
    nil๊ฐ’์ด ๋“ค์–ด์˜ฌ ์ˆ˜ ์—†๋‹ค๊ณ  ์„ ์–ธํ•ด์ฃผ๊ธฐ

  2. result.values.max() ?? 0
    nil์ด ๋“ค์–ด์˜ค๋ฉด 0์œผ๋กœ ๋ฐ”๊พธ๊ธฐ

์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@tgyuuAn tgyuuAn removed request for H0ngJu and SeongHoonC June 20, 2024 15:50
Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ—‰ swift๋‹ค .....

๋ฌธ์ œ ๋˜๊ฒŒ ์ด์ง€๋น„์ง€ ํ–ˆ์Šต๋‹ˆ๋‹ค ํžˆํžˆ

from collections import defaultdict
import sys

def input(): return sys.stdin.readline().rstrip()

dic = defaultdict(int)

for num in input():
    num = int(num)
    dic[num] += 1

answer = 0
for num in range(9):
    if num == 6: answer = max(answer, (dic[num] + dic[9] +1)//2)
    else: answer = max(answer, dic[num])

print(answer)

@alstjr7437 alstjr7437 merged commit be3690a into main Jun 30, 2024
@alstjr7437 alstjr7437 deleted the 30-alstjr7437 branch June 30, 2024 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants