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
- Number Theory
- geometry
- bit manipulation
- implement
- string
- two pointers
- Method
- Math
- greedy
- Data Structure
- Matrix
- sorting
- Tree
- 코테
- simulation
- 구현
- SQL
- hash table
- java
- dynamic programming
- Stack
- database
- 코딩테스트
- Class
- Binary Tree
- Counting
- array
- 자바
- Binary Search
- 파이썬
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1122. Relative Sort Array 본문
1. Input
1) int[] arr1
2) int[] arr2
2. Output
1) arr2의 요소가 들어있는 순서에 맞춰 arr1의 요소를 정렬한 결과를 반환
- arr2에 없는 요소는 arr1의 뒤쪽에 붙인다. 이때 정렬 순서는 오름차순이다.
3. Constraint
1) 1 <= arr1.length, arr2.length <= 1000
2) 0 <= arr1[i], arr2[i] <= 1000
3) arr2의 요소에는 중복이 없다.
4) arr2의 요소는 모두 arr1에 들어있다.
4. Example
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] -> Output: [2,2,2,1,4,3,3,9,6,7,19]
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6] -> Output: [22,28,8,6,17,44]
5. Code
1) 첫 코드(2023/06/08)
class Solution {
public int[] relativeSortArray(int[] arr1, int[] arr2) {
int[] count = new int[arr2.length];
List<Integer> a2 = new ArrayList<>();
for(int i : arr2)
a2.add(i);
List<Integer> list = new ArrayList<>();
for(int i=0 ; i<arr1.length ; i++){
if(!a2.contains(arr1[i])) list.add(arr1[i]);
else count[a2.indexOf(arr1[i])]++;
}
int a = 0;
for(int i=0 ; i<count.length ; i++){
for(int j=0 ; j<count[i] ; j++)
arr1[a++] = arr2[i];
}
if(list.size()!=0){
list.sort(Comparator.naturalOrder());
for(int i=0 ; i<list.size() ; i++)
arr1[a++] = list.get(i);
}
return arr1;
}
}
- 15%, 59%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1287. Element Appearing More Than 25% In Sorted Array (0) | 2023.06.08 |
---|---|
[LeetCode/Easy] 1175. Prime Arrangements (0) | 2023.06.08 |
[LeetCode/Easy] 1046. Last Stone Weight (0) | 2023.06.07 |
[LeetCode/Easy] 1030. Matrix Cells in Distance Order (0) | 2023.06.07 |
[LeetCode/Easy] 1021. Remove Outermost Parentheses (0) | 2023.06.07 |