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

3๏ธโƒฃ Swift/Problem Solving110

[Swift Algorithm] ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ Programmers(Lv.2) ๋ฌธ์ œ ์„ค๋ช… ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” F(0) = 0, F(1) = 1์ผ ๋•Œ, 1 ์ด์ƒ์˜ n์— ๋Œ€ํ•˜์—ฌ F(n) = F(n-1) + F(n-2) ๊ฐ€ ์ ์šฉ๋˜๋Š” ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด F(2) = F(0) + F(1) = 0 + 1 = 1 F(3) = F(1) + F(2) = 1 + 1 = 2 F(4) = F(2) + F(3) = 1 + 2 = 3 F(5) = F(3) + F(4) = 2 + 3 = 5 ์™€ ๊ฐ™์ด ์ด์–ด์ง‘๋‹ˆ๋‹ค. 2 ์ด์ƒ์˜ n์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ, n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ 1234567์œผ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด * n์€ 1์ด์ƒ, 100000์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ n return 3 2 5 5 ๋‚ด ๋ฌธ์ œ ํ’€์ด func solution(_ n:Int) -> Int.. 2021. 7. 8.
[Swift Algorithm] ํ–‰๋ ฌ์˜ ๊ณฑ์…ˆ Programmers(Lv.2) ๋ฌธ์ œ ์„ค๋ช… 2์ฐจ์› ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, arr1์— arr2๋ฅผ ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด ํ–‰๋ ฌ arr1, arr2์˜ ํ–‰๊ณผ ์—ด์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค. ํ–‰๋ ฌ arr1, arr2์˜ ์›์†Œ๋Š” -10 ์ด์ƒ 20 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ณฑํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด๋งŒ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr1 arr2 return [[1, 4], [3, 2], [4, 1]] [[3, 3], [3, 3]] [[15, 15], [15, 15], [15, 15]] [[2, 3, 2], [4, 2, 4], [3, 1, 4]] [[5, 4, 3], [2, 4, 1], [3, 1, 1]] [[22, 22, 11], [36, 28, 18], [29, 20, 14]] ๋‚ด ๋ฌธ์ œ ํ’€์ด.. 2021. 7. 8.
[Swift Algorithm] JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ Programmers(Lv.2) ๋ฌธ์ œ ์„ค๋ช… JadenCase๋ž€ ๋ชจ๋“  ๋‹จ์–ด์˜ ์ฒซ ๋ฌธ์ž๊ฐ€ ๋Œ€๋ฌธ์ž์ด๊ณ , ๊ทธ ์™ธ์˜ ์•ŒํŒŒ๋ฒณ์€ ์†Œ๋ฌธ์ž์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, s๋ฅผ JadenCase๋กœ ๋ฐ”๊พผ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด s๋Š” ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. s๋Š” ์•ŒํŒŒ๋ฒณ๊ณผ ๊ณต๋ฐฑ๋ฌธ์ž(" ")๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฌธ์ž๊ฐ€ ์˜๋ฌธ์ด ์•„๋‹๋•Œ์—๋Š” ์ด์–ด์ง€๋Š” ์˜๋ฌธ์€ ์†Œ๋ฌธ์ž๋กœ ์”๋‹ˆ๋‹ค. ( ์ฒซ๋ฒˆ์งธ ์ž…์ถœ๋ ฅ ์˜ˆ ์ฐธ๊ณ  ) ์ž…์ถœ๋ ฅ ์˜ˆ s return "3people unFollowed me" "3people Unfollowed Me" "for the last week" "For The Last Week" ๋‚ด ๋ฌธ์ œ ํ’€์ด func solution(_ s:String) -> String { let upperString =.. 2021. 7. 8.
[Swift Algorithm] ์ตœ์†Ÿ๊ฐ’ ๋งŒ๋“ค๊ธฐ Programmers(Lv.2) ๋ฌธ์ œ ์„ค๋ช… ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด A, B ๋‘๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฐฐ์—ด์€ ์ž์—ฐ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด A, B์—์„œ ๊ฐ๊ฐ ํ•œ ๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„ ๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ, ๋‘ ์ˆ˜๋ฅผ ๊ณฑํ•œ ๊ฐ’์„ ๋ˆ„์ ํ•˜์—ฌ ๋”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์ตœ์ข…์ ์œผ๋กœ ๋ˆ„์ ๋œ ๊ฐ’์ด ์ตœ์†Œ๊ฐ€ ๋˜๋„๋ก ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค. (๋‹จ, ๊ฐ ๋ฐฐ์—ด์—์„œ k๋ฒˆ์งธ ์ˆซ์ž๋ฅผ ๋ฝ‘์•˜๋‹ค๋ฉด ๋‹ค์Œ์— k๋ฒˆ์งธ ์ˆซ์ž๋Š” ๋‹ค์‹œ ๋ฝ‘์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.) ์˜ˆ๋ฅผ ๋“ค์–ด A = [1, 4, 2] , B = [5, 4, 4] ๋ผ๋ฉด A์—์„œ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž์ธ 1, B์—์„œ ์ฒซ๋ฒˆ์งธ ์ˆซ์ž์ธ 5๋ฅผ ๋ฝ‘์•„ ๊ณฑํ•˜์—ฌ ๋”ํ•ฉ๋‹ˆ๋‹ค. (๋ˆ„์ ๋œ ๊ฐ’ : 0 + 5(1x5) = 5) A์—์„œ ๋‘๋ฒˆ์งธ ์ˆซ์ž์ธ 4, B์—์„œ ์„ธ๋ฒˆ์งธ ์ˆซ์ž์ธ 4๋ฅผ ๋ฝ‘์•„ ๊ณฑํ•˜์—ฌ ๋”ํ•ฉ๋‹ˆ๋‹ค. (๋ˆ„์ ๋œ ๊ฐ’ : 5 + 16(4x4) = 21.. 2021. 6. 24.
[Swift Algorithm] N๊ฐœ์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ Programmers(Lv.2) ๋ฌธ์ œ ์„ค๋ช… ๋‘ ์ˆ˜์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜(Least Common Multiple)๋ž€ ์ž…๋ ฅ๋œ ๋‘ ์ˆ˜์˜ ๋ฐฐ์ˆ˜ ์ค‘ ๊ณตํ†ต์ด ๋˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 2์™€ 7์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋Š” 14๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ •์˜๋ฅผ ํ™•์žฅํ•ด์„œ, n๊ฐœ์˜ ์ˆ˜์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋Š” n ๊ฐœ์˜ ์ˆ˜๋“ค์˜ ๋ฐฐ์ˆ˜ ์ค‘ ๊ณตํ†ต์ด ๋˜๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆซ์ž๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. n๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด arr์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ ์ด ์ˆ˜๋“ค์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”. ์ œํ•œ ์กฐ๊ฑด arr์€ ๊ธธ์ด 1์ด์ƒ, 15์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. arr์˜ ์›์†Œ๋Š” 100 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ arr result [2,6,8,14] 168 [1,2,3] 6 ๋‚ด ๋ฌธ์ œ ํ’€์ด import Foundation func solution(_ arr:[Int]) -> Int { re.. 2021. 6. 18.