๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

3๏ธโƒฃ Swift/Problem Solving110

[Swift Algorithm] ์ง์ˆ˜์™€ ํ™€์ˆ˜ Programmers(Lv.1) ๋ฌธ์ œ ์„ค๋ช… ์ •์ˆ˜ num์ด ์ง์ˆ˜์ผ ๊ฒฝ์šฐ "Even"์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ "Odd"๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด num์€ int ๋ฒ”์œ„์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. 0์€ ์ง์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ num return 3 "Odd" 4 "Even" ๋‚ด ๋ฌธ์ œ ํ’€์ด func solution(_ num:Int) -> String { return num % 2 == 0 ? "Even" : "Odd" } 2๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€์— ๋”ฐ๋ผ ๋ฆฌํ„ด ๊ฐ’์„ ์ฃผ์—ˆ๋‹ค. ๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ผ ๋ฐ”๋กœ ํ’€์–ด๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋ฌธ์ œ https://programmers.co.kr/learn/courses/30/lessons/12937 2021. 6. 7.
[Swift Algorithm] ์ œ์ผ ์ž‘์€ ์ˆ˜ ์ œ๊ฑฐํ•˜๊ธฐ Programmers(Lv.1) ๋ฌธ์ œ ์„ค๋ช… ์ •์ˆ˜๋ฅผ ์ €์žฅํ•œ ๋ฐฐ์—ด, arr ์—์„œ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•œ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ๋ฆฌํ„ดํ•˜๋ ค๋Š” ๋ฐฐ์—ด์ด ๋นˆ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ์—” ๋ฐฐ์—ด์— -1์„ ์ฑ„์›Œ ๋ฆฌํ„ดํ•˜์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด arr์ด [4,3,2,1]์ธ ๊ฒฝ์šฐ๋Š” [4,3,2]๋ฅผ ๋ฆฌํ„ด ํ•˜๊ณ , [10]๋ฉด [-1]์„ ๋ฆฌํ„ด ํ•ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์กฐ๊ฑด arr์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค i, j์— ๋Œ€ํ•ด i ≠ j์ด๋ฉด arr[i] ≠ arr[j] ์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr return [4, 3, 2, 1] [4, 3, 2] [10] [-1] ๋‚ด ๋ฌธ์ œ ํ’€์ด func solution(_ arr:[Int]) -> [Int] { let min = arr.min() let notMinArr = arr.filter{$0 != min} retu.. 2021. 6. 7.
[Swift Algorithm] ์ •์ˆ˜ ์ œ๊ณฑ๊ทผ ํŒ๋ณ„ Programmers(Lv.1) ๋ฌธ์ œ ํ’€์ด ์ž„์˜์˜ ์–‘์˜ ์ •์ˆ˜ n์— ๋Œ€ํ•ด, n์ด ์–ด๋–ค ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋‹จํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด๋ผ๋ฉด x+1์˜ ์ œ๊ณฑ์„ ๋ฆฌํ„ดํ•˜๊ณ , n์ด ์–‘์˜ ์ •์ˆ˜ x์˜ ์ œ๊ณฑ์ด ์•„๋‹ˆ๋ผ๋ฉด -1์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด n์€ 1์ด์ƒ, 50000000000000 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ n return 121 144 3 -1 ๋‚ด ๋ฌธ์ œ ํ’€์ด import Foundation func solution(_ n:Int64) -> Int64 { let x = Int64(sqrt(Double(n))) return x * x == n ? (x + 1) * (x + 1) : -1 } sqrt ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์˜ ์ œ๊ณฑ๊ทผ์„ ๊ตฌํ•˜์˜€์Œ ๊ตฌํ•œ ์ œ๊ณฑ๊ทผ์„ ๋‹ค์‹œ ๊ณฑํ–ˆ์„ ๋•Œ n์ด ์ถœ๋ ฅ๋˜๋Š”์ง€ .. 2021. 6. 7.
[Swift Algorithm] ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ Programmers(Lv.1) ๋ฌธ์ œ ์„ค๋ช… ํ•จ์ˆ˜ solution์€ ์ •์ˆ˜ n์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. n์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ƒˆ๋กœ์šด ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด n์ด 118372๋ฉด 873211์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์กฐ๊ฑด n์€ 1์ด์ƒ 8000000000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ n return 118372 873211 ๋‚ด ๋ฌธ์ œ ํ’€์ด func solution(_ n:Int64) -> Int64 { return Int64(String(String(n).sorted(by: >))) ?? 0 } sorted ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜์˜€์Œ ??๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ nil์ด ๋‚˜์˜ฌ ๊ฒฝ์šฐ 0์ด ๋ฐ˜ํ™˜๋˜๋„๋ก ํ•ด์ฃผ์—ˆ์Œ (Int64๋กœ ํ˜•๋ณ€ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ์˜ต์…”๋„ ๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ) ๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ sorted๋ฅผ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ์Œ. ์œ ์šฉํ•˜๊ฒŒ.. 2021. 6. 7.
[Swift Algorithm] ์ง์‚ฌ๊ฐํ˜• ๋ณ„์ฐ๊ธฐ Programmers(Lv.1) ๋ฌธ์ œ ์„ค๋ช… ์ด ๋ฌธ์ œ์—๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ๋‘ ๊ฐœ์˜ ์ •์ˆ˜ n๊ณผ m์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ณ„(*) ๋ฌธ์ž๋ฅผ ์ด์šฉํ•ด ๊ฐ€๋กœ์˜ ๊ธธ์ด๊ฐ€ n, ์„ธ๋กœ์˜ ๊ธธ์ด๊ฐ€ m์ธ ์ง์‚ฌ๊ฐํ˜• ํ˜•ํƒœ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด n๊ณผ m์€ ๊ฐ๊ฐ 1000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ ๋ฌธ์ œ ํ’€์ด import Foundation let n = readLine()!.components(separatedBy: [" "]).map { Int($0)! } let (a, b) = (n[0], n[1]) for i in 0.. 2021. 6. 6.