README

Algorithm

  • 문제 제λͺ©μ„ ν΄λ¦­ν•˜λ©΄, GitHub μ½”λ“œλ‘œ, ✏️λ₯Ό ν΄λ¦­ν•˜λ©΄ ν‹°μŠ€ν† λ¦¬λ‘œ μ΄λ™ν•©λ‹ˆλ‹€.

  • μ½”λ”©ν…ŒμŠ€νŠΈ μ—°μŠ΅ (λ°±μ€€, ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ λ“±) μ•Œκ³ λ¦¬μ¦˜

  • μˆ˜ν•™

    • μˆœμ—΄ (nPr)

자료ꡬ쑰

μ•Œκ³ λ¦¬μ¦˜

μˆ˜ν•™

μˆœμ—΄

  • μˆœμ„œκ°€ μžˆλŠ” μž„μ˜μ˜ 집합, n개 쀑 r개λ₯Ό λ½‘λŠ” 방법

var v = [1, 2, 3]

func permutation(n: Int, r: Int, depth: Int) {
    if r == depth {
        print( v.map { String($0) }.joined(separator: " "))
        return
    }
    
    for i in depth..<n {
        v.swapAt(i, depth)
        permutation(n: n, r: r, depth: depth + 1)
        v.swapAt(i, depth)
    }
    return
}

permutation(n: 3, r: 3, depth: 0)

μ‘°ν•©

  • μˆœμ„œκ°€ μ—†λŠ” μž„μ˜μ˜ 집합, n개 쀑 r개λ₯Ό λ½‘λŠ” 방법

  1. μž¬κ·€ν•¨μˆ˜ 이용

var v = [1, 2, 3]

func permutation(n: Int, r: Int, depth: Int) {
    if r == depth {
        print( v.map { String($0) }.joined(separator: " "))
        return
    }
    
    for i in depth..<n {
        v.swapAt(i, depth)
        permutation(n: n, r: r, depth: depth + 1)
        v.swapAt(i, depth)
    }
    return
}

permutation(n: 3, r: 3, depth: 0)
  1. 반볡문 μ‚¬μš©

let n = 5, r = 3, a = [1, 2, 3, 4, 5]

// r=3μ΄λ―€λ‘œ 3쀑 forλ¬Έ -> r이 μž‘μ„ λ•Œμ—λ§Œ λ°˜λ³΅λ¬Έμ„ μ‚¬μš©ν•˜μž
for i in 0..<n {
    for j in i+1 ..< i {
        for k in j+1 ..< j {
            print("\(i), \(j), \(k)
        }
    }
}

// μˆœμ„œλ§Œ λ‹€λ₯Ό 뿐
for i in 0..<n {
    for j in 0..<i {
        for k in 0..<j {
            print("\(i), \(j), \(k)
        }
    }
}

μ΅œλŒ€κ³΅μ•½μˆ˜

  • μœ ν΄λ¦¬λ“œ ν˜Έμ œλ²• 이용

func gcd(_ a: Int, _ b: Int) -> Int {
    if b == 0 { return a }
    return gcd(b, a%b)
}

μ΅œμ†Œκ³΅λ°°μˆ˜

func gcd(_ a: Int, _ b: Int) -> Int {
    if b == 0 { return a }
    return gcd(b, a%b)
}

func lcm(_ a: Int, _ b: Int) -> Int {
    return a * b / gcd(a, b)
}

μ†Œμˆ˜ μ°ΎκΈ°

  1. μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체(n <= 1,000,000)

func primeNumbers(_ n: Int) -> [Int] {
  var numbers = [Bool](repeating: true, count: n+1)
  var result = [Int]()

  for i in 2...n {
      if numbers[i] {
          for j in stride(from: i*2, to: n, by: i) {
              numbers[j] = false
          }
      }
  }

  for i in 2...n {
      if numbers[i] { result.append(i) }
  }

  return result
}
func primeNumbers(_ n: Int) -> Set<Int> {
  var numbers = Set(2...n)

  for i in 2...n {
      if numbers.contains(i) {
          numbers.subtract(Set(stride(from: i*2, to: n, by: i)))
      }
  }
  return numbers
}
  1. 제곱근 ν™œμš©

func isPrimeNumber(_ n: Int) -> Bool {
    if n < 2 { return false }
    if n == 2 { return true }
    if n % 2 == 0 { return false }
    
    for i in 2...n {
        **if !(i * i <= n) { break }**
        if n%i == 0 { return false }
    }
    
    return true
}
**import Foundation**

func isPrimeNumber(_ n: Int) -> Bool {
    if n < 4 { return n >= 2 }
    if n % 2 == 0 { return false }
    
    for i in 2...**Int(sqrt(Double(n)))** {
        if n % i == 0 { return false }
    }
    return true
}

λˆ„μ ν•©

var cards = [0]
var prefix = [0]
let input = readLine()!.split(separator: " ").map { Int($0)! }
let (n, m) = (input[0], input[1])
cards.append(contentsOf: readLine()!.split(separator: " ").map { Int($0)! })

for i in 1...n {
    prefix.append(cards[i] + **prefix[i-1]**)
}

for _ in 1...m {
    let index = readLine()!.split(separator: " ").map { Int($0)! }
    let (a, b) = (index[0], index[1])
    print(prefix[b] - prefix[a-1])
}
  • Swift의 λ‚΄μž₯ν•¨μˆ˜

print([1, 2, 3, 4, 5, 6].prefix(3))

자료ꡬ쑰

Stack

  • Swift의 λ°°μ—΄ 자체λ₯Ό Stack처럼 μ‚¬μš©ν•˜λŠ” 것이 λ² μŠ€νŠΈμΈλ“―! (ꡳ이 λ§Œλ“€ ν•„μš” 없을 것 κ°™λ‹€.)

  • μ‚½μž… μ‚­μ œ λͺ¨λ‘ O(1)

struct Stack<T> {
    private var stack: [T] = []
    
    var count: Int {
        stack.count
    }
    
    var isEmpty: Bool {
        stack.isEmpty
    }
    
    var peek: T? {
        stack.last
    }
    
    mutating func push(_ element: T) {
        stack.append(element)
    }
    
    mutating func pop() -> T? {
        stack.popLast()
    }
}

Last updated