Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- array
- Class
- Number Theory
- Stack
- sorting
- SQL
- 코테
- Binary Search
- Data Structure
- Tree
- simulation
- 파이썬
- Matrix
- string
- greedy
- 자바
- Math
- dynamic programming
- database
- 구현
- Method
- Counting
- 코딩테스트
- geometry
- implement
- two pointers
- bit manipulation
- hash table
- java
- Binary Tree
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1200. Minimum Absolute Difference 본문
1. Input
1) int[] arr
2. Output
1) 다음 조건을 만족하는 두 수 a,b를 리스트에 담은 리스트(List<List<Integer>>)를 반환
- a, b는 arr의 요소
- a < b
- b-a는 arr의 두 요소의 차 중 가장 작아야 한다.
3. Constraint
1) 2 <= arr.length <= 10^5
2) - 10^6 <= arr[i] <= 10^6
4. Example
Input: arr = [1,3,6,10,15] -> Output: [[1,3]]
설명: arr의 두 요소를 골라 차를 구했을 때 가장 작은 차는 2이고, 이를 만족하는 두 요소는 1과 3뿐이므로 [[1,3]]을 반환한다.
5. Code
1) 첫 코드(2023/04/06)
Arrays.sort(arr);
int dif = Integer.MAX_VALUE;
for(int i=0 ; i<arr.length-1 ; i++)
if(dif>arr[i+1]-arr[i])
dif = arr[i+1]-arr[i];
List<List<Integer>> answer = new ArrayList<List<Integer>>();
for(int i=0 ; i<arr.length-1 ; i++)
if(dif==arr[i+1]-arr[i]){
List<Integer> list = new ArrayList<>();
list.add(arr[i]); list.add(arr[i+1]);
answer.add(list);
}
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1185. Day of the Week (0) | 2023.04.06 |
---|---|
[LeetCode/Easy] 1189. Maximum Number of Balloons (0) | 2023.04.06 |
[LeetCode/Easy] 1207. Unique Number of Occurrences (0) | 2023.04.06 |
[LeetCode/Easy] 1217. Minimum Cost to Move Chips to The Same Position (0) | 2023.04.06 |
[LeetCode/Easy] 1232. Check If It Is a Straight Line (0) | 2023.04.06 |