일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- swift 알고리즘
- swift algorithm
- swift 2xn 타일링 백준
- swift codility
- rxswift
- MVVM
- swift 프로그래머스
- 연속된 부분 수열의 합 swift
- swift dfs
- swift ac
- swift ac 문제풀이
- 백준 2xn 타일링
- iOS Charts
- 백준 2xn 타일링 풀이
- 1 2 3 더하기 풀이
- ac swift 풀이
- swift ac 풀이
- swift 백준 9095
- swift 2xn 타일링 풀이
- ac 구현 풀이
- 연속된 부분 수열의 합 투포인터
- swift gRPC
- ios
- swift 2xn 타일링
- swift 연속된 부분 수열의 합
- ac 투포인터
- swift 9095 풀이
- swift
- 123 더하기 풀이
- swift 연속된 부분 수열의 합 풀이
- Today
- Total
boraBong
[Swift] MissingInteger 문제 풀이 [Codility - Lesson 4. Counting Elements] 본문
[Swift] MissingInteger 문제 풀이 [Codility - Lesson 4. Counting Elements]
보라봉_ 2023. 3. 30. 20:32💬 문제
https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/
MissingInteger coding task - Learn to Code - Codility
Find the smallest positive integer that does not occur in a given sequence.
app.codility.com
💬 Idea
- N 정수의 배열 A가 주어지면 A에서 발생하지 않는 가장 작은 양의 정수(0보다 큼)를 반환해야 하므로
우선 A를 정렬한 뒤 A에서 1보다 작은 수를 모두 필터링해주었다. - 이후 결과가 빈 배열이라면 1을 리턴하여 빨리 결과값을 도출해주었고,
- 그렇지 않다면 for문을 돌며 preInteger(이전 정수 값)와 정렬된 원소의 값을 비교하며 그 차이가 1 초과인 경우에 가장 작은 양의 정수를 도출하기 위해 preInteger + 1 을 리턴해주었다.
- 만약 for문을 다 돌고도 가장 작은 정수를 찾지 못했다면 1 이상의 모든 원소가 채워져있는 것이므로
Set(A)의 개수 + 1을 해서 그 다음으로 작은 수를 리턴해주었다.
💬 풀이
import Foundation
public func solution(A : inout [Int]) -> Int {
A = A.sorted()
A = A.filter({ $0 >= 1 })
if A.isEmpty { return 1 }
var preInteger = 0
for i in A {
if i - preInteger > 1 {
return preInteger + 1
} else {
preInteger = i
}
}
return Set(A).count + 1
}
시간 복잡도 : O(N) or O(N * log(N))
https://app.codility.com/demo/results/trainingK7RBSR-4DF/
Test results - Codility
This is a demo task. Write a function: public func solution(_ A : inout [Int]) -> Int that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the func
app.codility.com
GitHub - hwangJi-dev/swiftAlgorithm: Algorithm Practice with Swift
Algorithm Practice with Swift. Contribute to hwangJi-dev/swiftAlgorithm development by creating an account on GitHub.
github.com
'iOS > Algorithm' 카테고리의 다른 글
[Swift] 2178번: 미로 탐색 문제 풀이 [BOJ - Silver1] (2) | 2023.04.01 |
---|---|
[Swift] [3차] 압축 문제 풀이 [Programmers - Level2 (2018 KAKAO BLIND RECRUITMENT)] (1) | 2023.03.30 |
[Swift] StrSymmetryPoint 문제 풀이 [Codility - Exercise 4. Algorithmic Skills] (0) | 2023.03.30 |
[Swift] LongestPassword 문제 풀이 [Codility - Exercise 1. 2015 Contest] (0) | 2023.03.30 |
[Swift] 타겟 넘버 문제 풀이 [Programmers - Level2 (DFS/BFS)] (0) | 2023.03.30 |