[프로그래머스]시소 짝꿍(Swift) - dp

brick·2023년 3월 6일
0

코테

목록 보기
38/53
  • 시간 초과
func solution(_ weights:[Int]) -> Int64 {
    let distances: [Double] = [1, 2, 3/2, 4/3]
    var count: Int = 0
    let weights: [Int] = weights.sorted()

    for (i, w) in weights.enumerated() {
    loop: for j in i+1..<weights.count {
            for d in distances {
                if d * Double(w) == Double(weights[j]) {
                    count += 1
                    continue loop
                }
            }
        }
    }
    return Int64(count)
}

func solution(_ weights:[Int]) -> Int64 {
    var position: [(Int, Int)] = [(1, 1), (2, 3), (2, 4), (3, 2), (3, 4), (4, 2), (4, 3)]
    var answer: Int = 0
    var dict: [Int: Int] = [:]
    
    for w in weights {
        for p in position {
            var n = w * p.0
            guard n % p.1 == 0 else { continue }
            n /= p.1
            guard dict[n] != nil else { continue }
            answer += dict[n]!
        }
        
        if dict[w] == nil {
            dict[w] = 1
        } else {
            dict[w]! += 1
        }
    }
    
    return Int64(answer)
}

0개의 댓글