본문 바로가기
python-algorithm

백준 14647 준오는 조류혐오야!!

by 무적김두칠 2022. 6. 16.

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
29
30
31
32
33
34
35
36
37
38
def cnt_max_matrix(nums):
    row_max=0
    row=0
    for i in range(len(nums)):
        tmp_max=0
        for j in nums[i]:
            tmp_max+=j.count('9')
        if row_max<tmp_max:
            row_max=tmp_max
            row=i
    return row_max
def cnt_matrix(nums):
    cnt_max=0
    for i in nums:
        for j in i:
            cnt_max+=j.count('9')
    return cnt_max
 
def transpose(matrix):
    rows = len(matrix)
    columns = len(matrix[0])
 
    matrix_T = []
    for j in range(columns):
        row = []
        for i in range(rows):
           row.append(matrix[i][j])
        matrix_T.append(row)
 
    return matrix_T
 
n,m=map(int,input().split())
nums=[]
for i in range(n):
    nums.append(list(map(str,input().split())))
row_max=cnt_max_matrix(nums)
col_max=cnt_max_matrix(transpose(nums))
print(cnt_matrix(nums)- max(row_max,col_max))
cs

 

문제는 n by m 행렬에서 특정 조건 카운팅 구현 내용인데

처음에는 Matrix Transpose 를 numpy 라이브러리에서 transpose 함수를 썼었는데
백준은 외부 라이브러리 사용이 안돼서

 Transpose 함수 자체 구현 

반응형

댓글