Skip to content

[Algorithm] 카펫 #200

Closed
Closed
@hwangJi-dev

Description

@hwangJi-dev

💬 문제

https://school.programmers.co.kr/learn/courses/30/lessons/42842


💬 Idea

  • 노란색을 직사각형으로 채울 수 있는 가로, 세로를 찾기 위해 반복문을 돌면서 나머지가 0인 경우를 찾는다.
  • 만약 (yx * 2) + (i * 2) + 4 가 브라운의 값과 같다면 target이므로 break한다.

💬 풀이

import Foundation

func solution(_ brown:Int, _ yellow:Int) -> [Int] {
    var res: [Int] = []
    
    if yellow == 1 {
        return [3, 3]
    }
    
    for i in 1...yellow / 2 + 1 {
        if yellow % i == 0 {
            let yx = yellow / i
            let ans = (yx * 2) + (i * 2) + 4
            
            if ans == brown {
                res = [yx + 2, i + 2]
                break
            }
        }
    }
    
    return res
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions