내일배움캠프 30일차 TIL _ 7주 1일차
2024. 5. 27. 21:34ㆍTIL Java
- 오늘 있었던 일
- 개인과제
- 알고리즘
SPRIONG
- 스프링 정리
더보기
1 to N
- one 키 가 될 항목 (예시 : 도서의 심지 )
-
@OneToMany @JoinColumn(name = "calendar_id") private List<Reply> replylist = new ArrayList<>();
- N 연동 내용이 될 항목 ( 도서의 페이지 )
-
@ManyToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name = "calendar_id" ,insertable = false, updatable = false) private Calendar calendar;
- N 넣기 ( 페이지 넣기 )
-
@Transactional public ReplyResponseDto createreply(String todo, ReplyRequestDto replydto) { Calendar calendar = calendarService.findCalendarTodo(todo); Reply reply = new Reply(replydto); calendar.getReplylist().add(reply); Reply savereply = repository.save( reply ); return new ReplyResponseDto( savereply ); }
- 내부의 이 코드를 이용하면
-
calendar.getReplylist().add(reply);
- 값이 따로 Repository 을 가져와 save를 하거나 따로 넣지않아도 연동값이 연결된다.
알고리즘 문제 풀기
● 달리기 경주
더보기
● 달리기 경주 //링크
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
class Solution {
public String[] solution(String[] players, String[] callings) {
String[] answer = new String[players.length];
Map<String,Integer> playerIndexMap = new HashMap<>();
Map<Integer,String> indexPlayerMap = new HashMap<>();
for (int i = 0; i < players.length; i++) {
playerIndexMap.put(players[i], i);
indexPlayerMap.put(i, players[i]);
}
for (String calling : callings) {
int currentIndex = playerIndexMap.get(calling);
int newIndex = currentIndex - 1;
if (newIndex >= 0) {
String swappedPlayer = indexPlayerMap.get(newIndex);
playerIndexMap.put(calling, newIndex);
playerIndexMap.put(swappedPlayer, currentIndex);
indexPlayerMap.put(newIndex, calling);
indexPlayerMap.put(currentIndex, swappedPlayer);
}
}
for (String player : players) {
answer[playerIndexMap.get(player)] = player;
}
return answer;
}
}
- 첫 시도때 태스트문도 통과했지만 후반 문제들이 시간 초과가 일어났다.
- 문제 지문에
- 2 ≤ callings의 길이 ≤ 1,000,000
- 거의 필연적으로 반복문이 3개가 넘어가면 기산 초과가 일어난다.
- 컬랙션 map을 이용해 이름대로 키값을 만들어 추월하면 + 추얼당하면 - 로 for문을 이용해 벨류값을 찾았지다. 대부분 정답을 맞췄지만 역시 = 시간초과
- 그냥 맵 2개를 만들어 for문을 치우고 값을 넣는 방식으로 만들었다.
- 문제 지문에
- 밑은 다른 사람의 풀이
-
class Solution { public String[] solution(String[] players, String[] callings) { Map<String, Integer> current = IntStream.range(0, players.length) .boxed() .collect(Collectors.toMap(i -> players[i], Function.identity())); List<String> currentPlayers = Arrays.stream(players).collect(Collectors.toList()); for (String calling : callings) { swap(currentPlayers, current, calling); } return currentPlayers.toArray(String[]::new); } private void swap(List<String> currentPlayers, Map<String, Integer> current, String calling) { int index = current.get(calling); String otherPlayer = currentPlayers.get(index - 1); Collections.swap(currentPlayers, index, index - 1); current.put(otherPlayer, index); current.put(calling, index - 1); } }
- 스트림으로 깔끔하다.
당일 회고
- 강의는 다 듣긴 들었는데 너무 양이 많아서 복습이 필요하다
'TIL Java' 카테고리의 다른 글
내일배움캠프 33일차 TIL _ 7주 4일차 (0) | 2024.05.30 |
---|---|
내일배움캠프 31일차 TIL _ 7주 2일차 (0) | 2024.05.28 |
내일배움캠프 29일차 TIL _ 6주 5일차 (0) | 2024.05.24 |
내일배움캠프 28일차 TIL _ 6주 4일차 (0) | 2024.05.23 |
내일배움캠프 27일차 TIL _ 6주 3일차 (0) | 2024.05.22 |