문제 링크 :
https://programmers.co.kr/learn/courses/30/lessons/17683
단순한 문제라고 생각했다. 레벨 2이기도 하고 단순히 문자열을 처리하면 된다고 생각했다.
우선 음의 길이가 1 또는 2이기 때문에, list에 넣었다.
이 때 for문을 0부터 len-1까지 돌려서 song[i]가 #인 경우, song[i+1]이 #인 경우, 나머지 경우로 분기했고
song[len-1]번째는 #이면 이미 더해졌으니 패스하고 아니라면 더하도록 하였다.
m 역시 같은 방식으로 리스트로 구성하였고, 시간은 분으로 바꾸어서 계산하였다.
그 후 for문을 len(songlist) - len(mlist)만큼 돌면서 일정 부분 같다면 노래의 지속 시간을 비교하고, 더 크다면 answer를 변경하도록 하였다.
100
결국 다 맞긴 했지만 몇 가지 문제가 있었다
너무 느림
레벨 2문제인데 짜잘하게 틀리는 부분들 때문에 거의 40분이 넘게 걸렸다. 실전이라면 말릴 게 분명하다
모든 조건을 구현하지 않음
만약 음에 맞는 노래가 없다면 None을 return해야하는데 이 부분을 구현하지 않았다.
문제를 보는 눈
문제를 읽으면서 악보에 사용되는 음을 딱 봤을 때 length가 모두 1이 아니니까 리스트로 나눠야 겠구나 string자체로 처리하기는 어렵겠구나 라는 생각이 딱 들었어야 하는데 아무 생각도 없이 string으로 구현함.
자잘한 실수
변수 이름 잘못 쓰거나, for문에서 i를 더해줘야 하는데 더해주지 않는다거나 하는 부분들이 시간을 늘렸다.
마지막으로 빨리 풀고자 하니 코드가 좀 더러워졌다.
나의 풀이처럼 리스트로 옮겨서 풀 수도 있지만,
두 글자인 C#같은 것들을 소문자c로 바꾸는 등 대치해서 문제를 풀 수 있다.
나의 오답 코드
import sys
from collections import deque
n, m = map(int, sys.stdin.readline().split())
arr = []
for i in range(n):
temp = []
string = sys.stdin.readline().strip()
for j in range(m):
if string[j] == 'R':
redplace = [i,j]
elif string[j] == 'B':
blueplace = [i, j]
elif string[j] == 'O':
holeplace = [i, j]
temp.append(string[j])
arr.append(temp)
#U D L R
move_y = [-1, 1, 0, 0]
move_x = [0, 0, -1, 1]
def check(node, dir):
y, x = node
# dir에 맞춰서 for문으로 U/L는 0이 될때까지 D/R는 n/m이 될때까지 확인해서 .이 있는 마지막 위치를 반환
count = 0
if dir == 0:
for k in range(y-1, -1, -1):
if arr[k][x] == 'O':
return 100
elif arr[k][x] == '#':
return count
else:
count += 1
if dir == 1:
for k in range(y+1, n, 1):
if arr[k][x] == 'O':
return 100
elif arr[k][x] == '#':
return count
else:
count += 1
if dir == 2:
for k in range(x-1, -1, -1):
if arr[y][k] == 'O':
return 100
elif arr[y][k] == '#':
return count
else:
count += 1
if dir == 3:
for k in range(x+1, m, 1):
if arr[y][k] == 'O':
return 100
if arr[y][k] == '#':
return count
else:
count += 1
# 최단 거리를 찾아야 하니까 .. bfs로 이동 ?
def bfs(red, blue): # 시작 좌표 (Red와 Blue의)
answer = 1
visited = []
queue = deque([])
queue.append([red, blue])
while queue:
if answer >= 11:
return -1
rednode, bluenode = queue.popleft()
if [rednode, bluenode] not in visited:
visited.append([rednode, bluenode])
flag = False
for i in range(4):
# 여기서 만약에 안막혀있으면 쭉 이동해야 : 새로운 함수에서 movecount 몇 칸 움직일지 체크
redmove = check(rednode, i)
bluemove = check(bluenode, i)
if bluemove == 100:
flag = True
elif redmove == 100:
return answer
else:
new_red_y = rednode[0] + redmove * move_y[i]
new_red_x = rednode[1] + redmove * move_x[i]
new_blue_y = bluenode[0] + bluemove * move_y[i]
new_blue_x = bluenode[1] + bluemove * move_x[i]
if new_red_y == new_blue_y and new_red_x == new_red_x:
if redmove > bluemove: # 더 작은 값은 한 칸 덜 움직여야 한다
new_red_y -= move_y[i]
new_red_x -= move_x[i]
elif redmove < bluemove:
new_blue_y -= move_y[i]
new_blue_x -= move_x[i]
queue.append([[new_red_y, new_red_x],[new_blue_y,new_blue_x]])
answer += 1
if flag == True:
return -1
print(bfs(redplace, blueplace))
나의 정답 코드
def solution(m, musicinfos):
answer = ''
songlength = 0
for music in musicinfos:
start, end, title, song = music.split(',')
starttime = int(start[:2])*60+int(start[3:])
endtime = int(end[:2])*60+int(end[3:])
if endtime > 24*60:
endtime = 24*60
duringtime = endtime - starttime
songlist = []
for i in range(len(song)-1):
if song[i] == '#':
continue
elif song[i+1] == '#':
songlist.append(song[i:i+2])
else:
songlist.append(song[i])
if song[len(song)-1] != '#':
songlist.append(song[len(song)-1])
fullsong = songlist*(duringtime//len(songlist))+songlist[:duringtime%len(songlist)+1]
mlist = []
for i in range(len(m)-1):
if m[i] == '#':
continue
elif m[i+1] == '#':
mlist.append(m[i:i+2])
else:
mlist.append(m[i])
if m[len(m)-1] != '#':
mlist.append(m[len(m)-1])
for i in range(0, len(fullsong) - len(mlist)):
if mlist == fullsong[i:len(mlist)+i]:
if songlength < len(fullsong):
answer = title
songlength = len(fullsong)
if answer == '':
return "(None)"
return answer
# 샵 처리
# i를 더해주지 않는 실수
a = 'python'
temp = list(a)
정신차려 제발 ..