티스토리 뷰
728x90
문제
https://programmers.co.kr/learn/courses/30/lessons/12977
주어진 숫자 중 3개의 수를 더했을 때 소수가 되는 경우의 개수를 구하려고 합니다. 숫자들이 들어있는 배열 nums가 매개변수로 주어질 때, nums에 있는 숫자들 중 서로 다른 3개를 골라 더했을 때 소수가 되는 경우의 개수를 return 하도록 solution 함수를 완성해주세요.
접근방법
3개의 수를 뽑아야 하니깐 조합으로 3개의 수를 뽑고
그 수의 합이 소수이면 count += 1
return count
뽑아야 할 수가 적다면, 단순하게 for문을 뽑을 갯수만큼 만들어 주면 조합이 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let nums = [1,2,3,4,5,6,7,8] | |
for i in nums.indices { | |
for j in i+1..<nums.count { | |
for k in j+1..<nums.count { | |
print("\(nums[i])\(nums[j])\(nums[k])") | |
} | |
} | |
} |
요런식으로.. 3개를 뽑는건 for문 3개..
단 많을경우 dfs를 통해 조합을 만들어 주면 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func solution(_ nums:[Int]) -> Int { | |
var count = 0 | |
let list = Array(repeating: 0, count: nums.count) | |
// data: 데이터 리스트, list: 뽑을 데이터의 idx 정보, r: 뽑을 갯수 | |
dfs(data: nums, list: list, r: 3, idx: 0, depth: 0, count: &count) | |
return count | |
} | |
func dfs(data: [Int], list: [Int], r: Int, idx: Int, depth: Int, count: inout Int) { | |
if r == 0 { | |
var sum = 0 | |
for i in 0 ..< idx { | |
sum += data[list[i]] // data[list[i]]가 뽑은 item | |
} | |
if check(sum) { | |
count += 1 | |
} | |
} else if depth >= data.count { return } | |
else { | |
var list = list | |
list[idx] = depth | |
dfs(data: data, list: list, r: r-1, idx: idx+1, depth: depth+1, count: &count) | |
dfs(data: data, list: list, r: r, idx: idx, depth: depth+1, count: &count) | |
} | |
} |
풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func solution(_ nums:[Int]) -> Int { | |
var count = 0 | |
let list = Array(repeating: 0, count: nums.count) | |
// data: 데이터 리스트, list: 뽑을 데이터의 idx 정보, r: 뽑을 갯수 | |
dfs(data: nums, list: list, r: 3, idx: 0, depth: 0, count: &count) | |
return count | |
} | |
func dfs(data: [Int], list: [Int], r: Int, idx: Int, depth: Int, count: inout Int) { | |
if r == 0 { | |
var sum = 0 | |
for i in 0 ..< idx { | |
sum += data[list[i]] // data[list[i]]가 뽑은 item | |
} | |
if check(sum) { | |
count += 1 | |
} | |
} else if depth >= data.count { return } | |
else { | |
var list = list | |
list[idx] = depth | |
dfs(data: data, list: list, r: r-1, idx: idx+1, depth: depth+1, count: &count) | |
dfs(data: data, list: list, r: r, idx: idx, depth: depth+1, count: &count) | |
} | |
} | |
func check(_ n: Int) -> Bool { | |
if n < 3 { return false} | |
if n == 3 { return true } | |
else { | |
for i in 2 ..< n { | |
if n%i == 0 { return false} | |
} | |
} | |
return true | |
} | |
let a = [1,2,3,4] | |
print(solution(a)) |
728x90
'개발 블로그 > 알고리즘' 카테고리의 다른 글
[Swift] 프로그래머스 - 예상 대진표 (0) | 2020.10.23 |
---|---|
[Swift] 프로그래머스 - 영어 끝말잇기 (0) | 2020.10.22 |
[Swift] 프로그래머스 - 짝지어 제거하기 (0) | 2020.10.21 |
[Swift] 프로그래머스 - 수식 최대화 (0) | 2020.10.21 |
[Swift] 프로그래머스 - N개의 최소공배수 (0) | 2020.10.12 |
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- VIPER 패턴
- ReactorKit
- RxDataSource
- 카카오블라인드2018
- BaseViewController
- 백준 1946
- 1차 뉴스 클러스터링
- UIModalPresentationStyle
- 프로그래머스 추석트래픽
- 자기PR
- 프로그래머스 캐시
- TransitionStyle
- 백준 신입사원
- today extension
- Widget
- BaseTableViewController
- ios
- Stack
- 알고리즘
- RxSwift
- 괄호연산
- Github Search
- presentStyle
- 카카오 블라인드 2018
- 카카오 블라인드2018
- Swift
- Level 3
- 아키택처
- 프로그래머스 오픈채팅방
- 위젯
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함