Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- 탐욕법
- 현대
- 스마트팩토리
- 그리디
- re_lunchu
- heapq
- Algorithm
- 현대자동차
- 알고리즘
- softeer
- GAN
- Python
- 자바스크립트
- 백준
- 파이썬
- Baekjoon
- cim
- cs공부
- programmers
- alogorithm
- 소프티어
- JavaScript
- 토이프로젝트
- MES
- Java
- 자바
- 비전공자
- 힙큐
- 딥러닝
Archives
- Today
- Total
eaz_coding
[Programmers] 방금그곡 (Python, Java) 본문
문제
요약
노래의 일부만 듣고 방금 들은 곡인지 확인하고 싶다.
"시작시간,끝시간,제목,코드" 가 들어있는 배열이 주어지고, 코드는 C, C#, D, D#, E, F, F#, G, G#, A, A#, B가 주어진다.
(문제 조건에 B#이 없는데 테케에 있음.)
노래는 처음부터 시작하고 재생시간이 코드보다 길 경우 처음부터 다시 반복된다.
문제
https://school.programmers.co.kr/learn/courses/30/lessons/17683
풀이
Python
def solution(m, musicinfos):
answer, at = "", 0
codes = {"C#":"1","D#":"2","F#":"3","G#":"4", "A#":"5", "B#":"6"}
for i, v in codes.items():
m = m.replace(i, v)
for info in musicinfos:
start, end, name, code = info.split(",")
sh, sm = map(int, start.split(":"))
eh, em = map(int, end.split(":"))
time = (eh-sh)*60 + em-sm
for i, v in codes.items():
code = code.replace(i, v)
if time > len(code):
tmp = code * (time//len(code)) + code[:time%len(code)]
else:
tmp = code[:time]
if m in tmp:
if at < time:
answer = name
at = time
return answer if len(answer) else "(None)"
Java
새로 배운 것
1. 다른 메소드에서 변수에 접근이 가능하게 하기 위해서는 메소드 바깥에 전역변수로 선언한 뒤, 값을 메소드 내에서 할당해준다.
2. 문자열을 여러번 쓸 때, 파이썬은 *를 사용하지만, 자바는 repeat를 사용한다.
자바의 Map 쓰는 법 잊지말기!
import java.util.*;
class Solution {
static Map<String, String> dict = new HashMap<String, String>();
public String change(String str){
for (String k : dict.keySet()){
str = str.replace(k, dict.get(k));
}
return str;
}
public int getTime(String time) {
Integer[] HM = Arrays.stream(time.split(":"))
.map(Integer::parseInt)
.toArray(Integer[]::new);
return HM[0]*60 + HM[1];
}
public String solution(String m, String[] musicinfos) {
String answer = "";
int at = 0;
dict.put("C#", "1");
dict.put("D#", "2");
dict.put("F#", "3");
dict.put("G#", "4");
dict.put("A#", "5");
dict.put("B#", "6");
m = change(m);
for (String info : musicinfos) {
String[] lst = info.split(",");
int time = getTime(lst[1]) - getTime(lst[0]);
String code = change(lst[3]);
String tmp;
int l = code.length();
if (time > l){
tmp = code.repeat(time/l) + code.substring(0, time%l);
} else {
tmp = code.substring(0, time);
}
if (tmp.contains(m)){
if (time > at) {
answer = lst[2];
at = time;
}
}
}
if (answer.length() > 0){
return answer;
} else {
return "(None)";
}
}
}
'eaz_algorithm' 카테고리의 다른 글
[Programmers] 줄 서는 방법(JavaScript) (0) | 2024.06.05 |
---|---|
[Programmers] 무인도 여행(Python, Java, JavaScript) (0) | 2024.06.05 |
[Programmers] 배달(Python, Java) (0) | 2024.06.03 |
[Programmers] 시소 짝꿍(Python, Java) (0) | 2024.05.30 |
[Programmers] 숫자 카드 나누기(Python, JavaScript) (0) | 2024.05.29 |