코트카타
44) 최소직사각형
나의 풀이
class Solution {
public int solution(int[][] sizes) {
int answer = 0;
int weight = 0;
int height = 0;
int maxWeight= 0;
int maxHeight = 0;
for (int i = 0; i < sizes.length ; i++) {
weight = sizes[i][0];
height = sizes[i][1];
if (weight < height) {
sizes[i][0] = height;
sizes[i][1] = weight;
}
if (maxWeight < sizes[i][0]){
maxWeight = sizes[i][0];
}
if (maxHeight < sizes[i][1]){
maxHeight = sizes[i][1];
}
}
answer = maxWeight * maxHeight;
return answer;
}
}
if문을 계속 쓴 점이 마음에 들지 않았다.
다른 분들의 풀이
class Solution {
public int solution(int[][] sizes) {
int length = 0, height = 0;
for (int[] card : sizes) {
length = Math.max(length, Math.max(card[0], card[1]));
height = Math.max(height, Math.min(card[0], card[1]));
}
int answer = length * height;
return answer;
}
}
출처 : https://school.programmers.co.kr/learn/courses/30/lessons/86491/solution_groups?language=java
forEach문을 사용하셨다.
Math.max기능도 사용하셨다.
깔끔할 수 있다는 것이 부럽다.
일정관리 Develop 과제 해설
튜터님 답안 : https://github.com/f-api/schedule-develop
- 도메인별로 분리해서 진행
- common - 공통
Lv 1. 일정 CRUD
BaseEntity
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime createdAt;
@LastModifiedDate
@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime updatedAt;
}
@Temporal을 사용하여, DB 타입에 맞도록 매핑 가능
@Builder
- Entity 생성자에 Schedule.builder 사용
생성자를 사용하는 것보다 장점은 같은 타입일 경우에 순서를 착각할 수 있는데 그걸 방지 가능해서 좋은 기능이라고 생각한다.
DTO는 생성, update 각각 나누자
: 언제든지 달라질 수 있기 때문에 유지보수를 위해서 나누는 습관을 가지자
@Transactional 안 붙이는 게 더 좋을 수 있다?
-> 붙이는 순간 원자성을 확보하기 위해
쓸데없는 쿼리가 많이 나온다. 6개...
ex) 카카오에서 이것 때문에 문제가 되어서 custom화된 것을 사용한다.
단건조회에서는 사용하지 않는다.
트래픽이 많아지면 문제가 된다.
@Transactional 붙여야 하는 경우
영속성 컨텐스트에 의해서 save()가 없어도 update 쿼리가 나갑니다.(더티 체킹)
Lv 2. 유저 CRUD, Lv 3. 회원가입
@RequiredArgsController 선호하는 이유
Bean으로 등록되지 않는 것을 생성자로 넣어주고자 할 때, @RequiredArgsController 사용하지 못함
따라서 컨벤션으로 특별한 case인지 다른 사람들도 확인 가능
stream 부분 공부하기
@Transactional(readOnly = true)
public List<ScheduleResponseDto> findAll() {
return scheduleRepository.findAll().stream()
.map(schedule -> new ScheduleResponseDto(
schedule.getId(),
schedule.getUser().getId(),
schedule.getTitle(),
schedule.getContent(),
schedule.getCreatedAt(),
schedule.getUpdatedAt()))
.collect(Collectors.toList());
}
null safe 코드를 작성하자
Lv 4. 로그인(인증)
cookie는 Http Header에 있습니다.
쿠키는 헤더입니다. 다른 헤더와 똑같아요.
HTTP에 있기 때문에 가져오려고 하는 것
그외...
n+1 문제는 면접 자주 나옴
getOrDefault
내가 좀 다르게 생각했던 부분도 있는 것 같다.
프로젝트 하기 전에 좀 더 살펴보고 진행해야 겠다.
'CODING > 스파르타 내일배움캠프 TIL' 카테고리의 다른 글
43_시저 암호_기초 프로젝트 트러블슈팅_25.2.14(금) (0) | 2025.02.14 |
---|---|
41_크기가 작은 부분 문자열_일정관리 Develop 주석 작성(Javadoc) 및 리팩토링_25.2.12(수) (0) | 2025.02.12 |
40_삼총사_JPA, 연관관계 실시간 구현 세션_일정관리 Develop 리팩토링_25.2.11(화) (0) | 2025.02.11 |
39_이상한 문자 만들기_일정관리 Develop 개인과제 Lv 5,6,7,8 도전과제 구현완료(튜터님 피드백 필요)_25.2.10(월) (0) | 2025.02.10 |
38_3진법 뒤집기_일정관리 Develop 개인과제 Lv 2,3,4 필수과제 구현완료_25.2.7(금) (0) | 2025.02.07 |