CS/알고리즘_문제풀이(파이썬)

상자 꾸미기

Jedy_Kim 2021. 5. 10. 19:16
728x90

문제

면이 6개인 상자가 있다. 이를 여러 가지 색종이를 붙여 꾸밀려고 하는데, 단 조건이 있다. 인접한 면에 같은 색의 색종이를 붙이면 안 된다는 것이다. 또한, 한 면에는 한 장의 색종이만 붙일 수 있다. 여러 가지 색의 색종이들이 주어졌을 때, 조건을 만족하여 상자의 모든 면에 붙일 수 있는지 판별하는 프로그램을 작성하시오.

 

입력

첫째 줄에 색종이의 장수 N ( 1 <= N <= 1,000 ) 이 주어진다. 둘째 줄에 각각의 색종이의 색깔을 나타내는 N개의 숫자가 주어진다. 색깔은 양의 정수로 이루어져 있고, 1부터 N까지의 범위의 수이다.

 

출력

조건을 만족하면서 상자를 꾸밀 수 있으면 “YES”, 아니면 “NO”를 출력한다.

 

 

예제 입력 

6 1 2 1 2 1 3

예제 출력

NO

 

예제 입력 

6 1 2 3 1 2 3

예제 출력

YES

 

예제 입력

7 1 1 1 2 2 3 3

예제 출력

YES

 

예제 입력

8 1 2 2 2 1 1 1 3

예제 출력

NO

 

#처음 접근법

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
distinctColor = list()
distinctColor = {}
isSuccess = True
 
for color in getColor:
  try: distinctColor[color]+= 1
  except: distinctColor[color]=1
 
 
 
if len(distinctColor) == 3:
  print(distinctColor)
  for k, v in distinctColor.items():
      if v != 2:  
        isSuccess = False
        break
elif len(distinctColor) == 4:
  pass
elif len(distinctColor) == 5:
  pass
elif len(distinctColor) > 5:
  pass
 
if isSuccess == True:
  print('YES')
else:
  print('NO')
cs

 

# 개선된 접근법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
= int(input())
getColor = list(map(int, input().split()))
distinctColor = {} 
cnt = 0
 
for color in getColor:
  try: distinctColor[color]+= 1
  except: distinctColor[color]=1
 
 
for k, v in distinctColor.items():
  if v > 2:  
      cnt += 2
  else:
      cnt += v
  if cnt >= 6:
    break
 
 
if cnt >= 6:
  print('YES')
else:
  print('NO')
cs

 

파이썬 중복제거 참조 aceatom.tistory.com/128

 

[python] 리스트 안에 있는 중복된 값 개수 구하기

리스트안의 중복된 값의 개수 w_count = {} lists = ["가나다라", "BYE", "HELLO", "123", "Asd1", "123", "HELLO", "bye", "BYE"] for lst in lists: try: w_count[lst] += 1 except: w_count[lst]=1 print w_cou..

aceatom.tistory.com

#복습

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
import sys
 
= int(sys.stdin.readline())
getInfo = list(map(int, sys.stdin.readline().split()))
getInfo_dic = {} 
 
for i in range(n):
  try: getInfo_dic[getInfo[i]] += 1
  except: getInfo_dic[getInfo[i]] = 1
 
if len(getInfo_dic) < 3:
  print('NO')
elif len(getInfo_dic) >= 3 and len(getInfo_dic) < 6:
  result = 0
  for k, v in getInfo_dic.items():
    if v > 2:
      result += 2
    else:
      result += v
  if result >= 6:
    print('YES')
  else:
    print('NO')
else:
  print('YES')
cs
반응형

'CS > 알고리즘_문제풀이(파이썬)' 카테고리의 다른 글

maxofarr  (0) 2021.05.10
GCD LCM  (0) 2021.05.10
offset  (0) 2021.05.10
백준 - 스택 수열 #1874  (0) 2021.03.30
백준 - 단어 뒤집기 #9093  (0) 2021.03.29