CODING/스파르타 내일배움캠프 TIL

42_최소직사각형_일정관리 Develop 과제 해설_25.2.13(목)

codingTrip 2025. 2. 14. 00:08

코트카타

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

 

내가 좀 다르게 생각했던 부분도 있는 것 같다.

프로젝트 하기 전에 좀 더 살펴보고 진행해야 겠다.