stones 문자열로부터 jewels가 몇 개 포함되었는지 찾는 문제이다. 두 String을 매개변수로 받는 solve 메서드의 틀이 주어진다. Problem : public class JewelsAndStones { public int solve(String jewels, String stones) { } } 풀이는 주어진 solve 메서드에 작성하면 된다. Solution : jewels의 각 char를 중복없이 Set에 저장한다. stones의 각 char를 돌며 jewels에 해당하는 char가 있는지 센다. import java.util.HashSet; import java.util.Set; public class JewelsAndStones { // time complexity : for lo..
필요한 최소한의 회의실 수를 구하는 문제이다. 인스턴스변수 start, end를 가진 Interval 클래스와 Interval 배열을 매개변수로 받는 solve 메서드의 틀이 주어진다. Problem : class Interval { int start; int end; Interval() { start = 0; end = 0; } Interval(int s, int e) { start = s; end = e; } } public class MeetingRoom2 { public int solve(Interval[] intervals) { } } 풀이는 solve에 작성하면 된다. Solution : Interval 객체들이 담긴 intervals 배열을 시작 시간 오름차순으로 정렬한다. 종료 시간 오름차..
Interval을 중복시간 없이 통합하는 문제이다. 인스턴스변수 start, end를 가진 Interval 클래스와 Interval 리스트를 매개변수로 받는 merge 메서드의 틀이 주어진다. Problem : class Interval { int start; int end; Interval() { start = 0; end = 0; } Interval(int s, int e) { start = s; end = e; } } public class MergeInterval { public List merge(List intervals) { } } 풀이는 merge에 작성하면 된다. Solution : Interval 객체들이 담긴 intervals 리스트를 시작 시간 오름차순으로 정렬한다. interval..
각 기온이 보다 높게 갱신되는데 걸리는 일자를 구하는 문제이다. e.g. {50, 51, 48, 47, 53} → {1, 3, 2, 1, 0} ∵ 50 → 51 : 1일 소요 / 51 → 53 : 3일 소요 / … / 53 : 마지막 int배열을 매개변수로 받는 dailyTemperatures 메서드의 틀이 주어진다. Problem : public class DailyTemperature { public int[] dailyTemperatures(int[] temperatures) { } } 풀이는 dailyTemperatures에 작성하면 된다. Solution : 결과 배열을 생성 및 초기화한다. 첫 기온을 Stack에 담는다. temperatures 배열을 순서대로 돌며 지난 기온보다 현재 기온이 ..
int배열에서 두 수의 합이 target일 때, 이 두 수의 index를 배열로 반환하는 문제이다. e.g. {1, 4, 7, 8}, 11 → 4 + 7 = 11 → {1, 2} int배열과 단일 int를 매개변수로 받는 twoSum 메서드의 틀이 주어진다. Problem : public class TwoSum { public int[] twoSum(int[] nums, int target) { } } 풀이는 주어진 twoSum 메서드에 작성하면 된다. Solution1 : 매우 간단한 방법으로 이중 for문을 이용해 다음과 같이 풀 수 있다. (반목문을 통해 두 수의 합이 target과 일치하는지 확인하는 방법) public class TwoSum { // time complexity : for dou..
새로운 자료구조를 생성하지 않고, 주어진 int배열에서 0을 배열의 뒤로 모두 옮기는 문제이다. e.g. {0, 3, 4, 0, 1, 0} -> {3, 4, 1, 0, 0, 0} int 배열을 매개변수로 받는 moveZeroes 메서드의 틀이 주어진다. Problem : public class MoveZeroes { public void moveZeroes(int[] nums) { } } 풀이는 주어진 moveZeros 메서드에 작성하면 된다. Solution : 0이 아닌 숫자를 앞에서부터 채운다. 0이 아닌 숫자를 채운 마지막 index를 기억한다. 해당 index부터 배열 끝까지 0으로 채운다. public class MoveZeroes { // time complexity : loop for in..
회의실 예약 시간(interval)이 유효한지를 알아보는 문제이다. 인스턴스변수 start, end를 가진 Interval 클래스와 Interval 배열을 매개변수로 받는 solve 메서드의 틀이 주어진다. Problem : class Interval{ int start; int end; Interval(){ this.start = 0; this.end =0; } Interval(int start, int end){ this.start = start; this.end = end; } } public class MeetingRooms { public boolean solve(Interval[] intervals) { } } 풀이는 solve 메서드에 작성하면 된다. Solution : 매개변수로 들어오는 ..