417. Pacific Atlantic Water Flow
·
Algorithm/LeetCode
문제그래프 탐색 문제이다. 2차원 배열의 그래프가 주어진다. 그래프는 섬을 의미한다. 각 노드의 value는 섬의 높이다. 인근 노드와의 높이 차이로 물이 흐른다. 각 노드를 시작으로 물이 흐를 때 Pacific Ocean(태평양)과 Atlantic Ocean(대서양) 까지 닿는 노드들을 찾는 문제이다.링크: https://leetcode.com/problems/pacific-atlantic-water-flow/description/Solutionclass Solution { final int[][] d = {{-1, 0},{1, 0},{0, -1},{0, 1}}; public List> pacificAtlantic(int[][] heights) { int m = heights.l..
Template Method Pattern
·
Programming/디자인 패턴
템플릿이란?템플릿 == 문자형 틀예를 들어, 색연필로 색칠할 때 색상은 바뀌어도 문자형은 유지되는 것처럼, 상위 클래스의 틀을 통해 기본적인 로직은 동일하게 유지템플릿 메서드 패턴이란?상위 클래스에서 처리의 뼈대를 결정하고 하위 클래스에서 그 구체적인 내용을 결정하는 패턴이다.구성(추상 클래스 + 구현 클래스)추상 클래스추상 클래스는 하위 클래스에서 구현할 것으로 기대하는 추상 메서드와 로직의 뼈대가 되는 템플릿 메서드로 구성된다.템플릿 메서드는 구체적인 로직의 순서를 정의한 일반 메서드로, 추상 메서드의 조합을 통해 알고리즘을 구성한다.템플릿 메서드 == 알고리즘Public abstract class AbstractDisplay {// 추상화 메서드(== 뼈대) public abstract vo..
207. Course Schedule
·
Algorithm/LeetCode
문제There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.Return true if you can finish all courses. Otherwise, return fal..
133. Clone Graph
·
Algorithm/LeetCode
문제원본 노드를 깊은 복사하는 문제이다. bfs를 사용했다.링크: https://leetcode.com/problems/clone-graph/Solution/*// Definition for a Node.class Node { public int val; public List neighbors; public Node() { val = 0; neighbors = new ArrayList(); } public Node(int _val) { val = _val; neighbors = new ArrayList(); } public Node(int _val, ArrayList _neighbors) { val = _..
11. Container With Most Water
·
Algorithm/LeetCode
문제각 위치의 높이를 나타내는 정수 배열이 주어진다. 두 위치 간 높이에 따른 최대 부피를 계산하는 문제이다.Constraints:n == height.length2 0 링크https://leetcode.com/problems/container-with-most-water/Solution 1class Solution { public int maxArea(int[] height) { int max = 0; int n = height.length; for(int i=0;i문제를 읽고 직관적으로 생각한 풀이 방식이다.시간 복잡도: O(n^2)타임 초과 발생!Solution 2class Solution { public int maxArea(int[] height..
추상 클래스
·
Programming/Java
목차1. 추상 클래스 특징2. 인터페이스와 차이점3. 추상 클래스의 목적 "추상 클래스는 공통 기능을 제공하면서 하위 클래스가 필수적으로 구현해야 할 규격을 정의하여 일관성과 다형성을 지원하는 클래스 설계 도구이다." 1. 추상 클래스 특징- 일부 메서드를 구현하지 않고 서브 클래스에서 구체화하도록 강제하는 클래스- 인스턴스화 할 수 없다- 세부 구현은 자식 클래스가 맡는다.public abstract class Animal { public String kind; public void breath(){ System.out.println("숨 쉰다."); } //추상메서드 public abstract void sound();//구체적인 구현부는 없음!} 2. 인터페이..
15. 3Sum
·
Algorithm/LeetCode
문제 링크https://leetcode.com/problems/3sum/description/ 해결 1.class Solution { public List> threeSum(int[] nums) { int n= nums.length; List> answer=new ArrayList(); //중복을 방지하고자 임시로 사용할 Set을 선언하였다. Set> temp=new HashSet(); // 배열을 먼저 정렬하여 중복 및 순서 문제를 해결 Arrays.sort(nums); for(int i=0;i set = new HashSet(); for(int j=i+1;j triplet = Arrays..
MapStruct & Lombok 적용
·
Programming/Spring
MapStruct - Dto를 Entity로 매핑(반대도 가능)해주는 라이브러리다. - 리플렉션이 아닌 직접 메소드를 호출하는 방식으로 동작하여 속도가 빠르다. Set up ... 11 1.3.1.Final 1.18.12 ... org.mapstruct mapstruct ${org.mapstruct.version} org.projectlombok lombok ${org.projectlombok.version} provided ... org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 3.5.1 11 11 org.mapstruct mapstruct-processor ${org.mapst..
@JsonProperty, @JsonNaming
·
Programming/Spring
REST API 방식으로 서버와 클라이언트가 데이터를 통신할 때 JSON 형식을 주로 사용한다. 서버는 카멜 방식(Camel Case), 클라이언트는 스네이크 방식(Snake Case)을 사용한다. 카멜 방식 - 첫 글자는 소문자, 중간 글자들은 대문자 표기법 ex)phoneNumber 스네이크 방식 - 언더바가 포함된 표현 방식 ex) phone_number 서버와 클라이언트의 JSON 표현의 방식의 차이로 데이터의 key가 달라지는 상황이 발생한다. 이러한 문제를 @JsonProperty, @JsonNaming을 사용하여 해결할 수 있다. 예제 UserRequest package org.juhyun.kotlinspringboot.model data class UserRequest ( var name:..
변경 감지와 병합
·
Programming/JPA
준영속 엔티티란? - 영속성 컨텍스트가 더는 관리하지 않는 엔티티를 말한다. - DB에 한번 저장 되어서 식별자가 존재한다. - 임의로 만들어낸 엔티티도 기존 식별자(id)를 가지고 있으면 준영속 엔티티로 볼 수 있다. 준영속 엔티티를 수정하는 방법 2가지 - 변경 감지(Dirty Checking) 기능 사용 - 병합(merge) 사용 변경 감지 기능 사용 @Transactional void update(Item itemParam) { //itemParam: 파리미터로 넘어온 준영속 상태의 엔티티 Item findItem = em.find(Item.class, itemParam.getId()); //같은 엔티티를 조회한다. findItem.setPrice(itemParam.getPrice()); //데이..