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
- two pointers
- database
- 코딩테스트
- 코테
- Method
- java
- implement
- geometry
- array
- Matrix
- Tree
- Binary Search
- Counting
- Class
- 구현
- 파이썬
- Stack
- Number Theory
- dynamic programming
- greedy
- bit manipulation
- string
- Math
- Binary Tree
- 자바
- simulation
- hash table
- Data Structure
- sorting
- SQL
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2363. Merge Similar Items 본문
1. Input
1) int[][] items1
2) int[][] items2
2. Output
1) item별로 총합 무게가 정리된 리스트를 반환
- items[i][0] = i번째 물건의 가치
- items[i][1] = i번째 물건의 무게
- 반환되는 리스트는 가치를 기준으로 오름차순으로 정렬해야 한다.
3. Constraint
1) 1 <= items1.length, items2.length <= 1000
2) items1[i].length == items2[i].length == 2
3) 1 <= value_i, weight_i <= 1000
4) items1과 items2 각 배열에는 중복되는 가치는 없다.
4. Example
Input: items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]] -> Output: [[1,6],[3,9],[4,5]]
- 가치가 1인 물건의 무게 = 1+5=6
- 가치가 3인 물건의 무게 = 8+1=9
- 가치가 4인 물건의 무게 = 5
5. Code
1) 첫 코드(2023/05/02)
class Solution {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0 ; i<items1.length ; i++)
map.put(items1[i][0], map.getOrDefault(items1[i][0],0)+items1[i][1]);
for(int i=0 ; i<items2.length ; i++)
map.put(items2[i][0], map.getOrDefault(items2[i][0],0)+items2[i][1]);
ArrayList<Integer> key = new ArrayList<>(map.keySet());
key.sort(Comparator.naturalOrder());
for(int k : key){
List<Integer> list = new ArrayList<>();
list.add(k); list.add(map.get(k));
ret.add(list);
}
return ret;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2373. Largest Local Values in a Matrix (0) | 2023.05.02 |
---|---|
[LeetCode/Easy] 2367. Number of Arithmetic Triplets (0) | 2023.05.02 |
[LeetCode/Easy] 2357. Make Array Zero by Subtracting Equal Amounts (0) | 2023.05.02 |
[LeetCode/Easy] 2325. Decode the Message (0) | 2023.05.02 |
[프로그래머스/Lv.0] 이차원 배열 대각선 순회하기 (0) | 2023.05.01 |