ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프로그래머스 / 바탕화면 정리
    코딩 테스트/Level 1 2023. 3. 2. 18:26
    반응형

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

    파이썬

    def solution(wallpaper):
        x_min = y_min = float('inf')
        x_max = y_max = 0
        for x, row in enumerate(wallpaper):
            for y, each in enumerate(row):
                if each == '#':
                    x_min = min(x, x_min)
                    x_max = max(x, x_max)
                    y_min = min(y, y_min)
                    y_max = max(y, y_max)
        return [x_min, y_min, x_max + 1, y_max + 1]

    코틀린

    import kotlin.math.*
    
    class Solution {
        fun solution(wallpaper: Array<String>): IntArray {
            var (xMin, yMin) = (50 to 50)
            var (xMax, yMax) = (0 to 0)
            (wallpaper.indices).forEach { x ->
                (wallpaper[0].indices).forEach { y ->
                    if (wallpaper[x][y] == '#') {
                        xMin = min(x, xMin)
                        xMax = max(x, xMax)
                        yMin = min(y, yMin)
                        yMax = max(y, yMax)
                    }
                }
            }
            return intArrayOf(xMin, yMin, xMax + 1, yMax + 1)
        }
    }

    golang

    func solution(wallpaper []string) []int {
    	var max_x, max_y = 0, 0
    	var min_x, min_y = 50, 50
    	for x, row := range wallpaper {
    		for y, each := range row {
    			if each == '#' {
    				if x < min_x {
    					min_x = x
    				}
    				if x > max_x {
    					max_x = x
    				}
    				if y < min_y {
    					min_y = y
    				}
    				if y > max_y {
    					max_y = y
    				}
    			}
    		}
    	}
    	return []int{min_x, min_y, max_x + 1, max_y + 1}
    }
    반응형
Designed by Tistory.