๋ฌธ์
You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties:
- items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item.
- The value of each item in items is unique.
Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items with value valuei.
Note: ret should be returned in ascending order by value.
Example 1:
Input: items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
Output: [[1,6],[3,9],[4,5]]
Explanation:
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.
The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.
The item with value = 4 occurs in items1 with weight = 5, total weight = 5.
Therefore, we return [[1,6],[3,9],[4,5]].
Example 2:
Input: items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
Output: [[1,4],[2,4],[3,4]]
Explanation:
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.
The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.
The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
Therefore, we return [[1,4],[2,4],[3,4]].
Example 3:
Input: items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
Output: [[1,7],[2,4],[7,1]]
Explanation:
The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7.
The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
The item with value = 7 occurs in items2 with weight = 1, total weight = 1.
Therefore, we return [[1,7],[2,4],[7,1]].
Constraints:
- 1 <= items1.length, items2.length <= 1000
- items1[i].length == items2[i].length == 2
- 1 <= valuei, weighti <= 1000
- Each valuei in items1 is unique.
- Each valuei in items2 is unique.
์ ๊ทผ๋ฐฉํฅ
๋ฐฐ์ด์ ํ๋๋ก ํฉ์น๊ณ , 0์ธ๋ฑ์ค ์๋ฆฌ ์๋ฅผ ๋น๊ตํด์ 1์ธ๋ฑ์ค ์๋ฆฌ์๋ฅผ ๋ํด์ ์๋ก์ด ๋ฐฐ์ด์ ๋ํ๊ณ ๋จ์ ๋ฐฐ์ด์ ๋ค์ ์ด์ด๋ถ์ธ๋ค - ๋ผ๊ณ ์๊ฐํ๋ฉฐ ์ฝ๋๋ฅผ ์ผ๋ค.
์ฝ๋
- ๋ฐํ์ ์ค๋ฒ ์คํจ์ฝ๋
์ฒ์ ์๊ฐํ๋ ์ ๊ทผ ๋ฐฉํฅ์ผ๋ก ๊ตฌ๊ตฌ์ ์ ํ๊ฒ ์ ์ด๋ด๋ ค๊ฐ๋ ์ฝ๋. ๋ฐํ์ ์ด๊ณผํ๋ค -
var mergeSimilarItems = function(items1, items2) {
const result = []
const newTemp = [...items1, ...items2]
for(let i=0; i<newTemp.length; i++){
for(let j=i+1; j<newTemp.length; j++){
if(newTemp[i][0] == newTemp[j][0]){
let sum = newTemp[i][1] + newTemp[j][1]
result.push([newTemp[i][0], sum])
newTemp.splice(j,1)
newTemp.splice(i,1)
i--
j--
}
}
}
return [...result, ...newTemp].sort((a,b) => a[0] - b[0])
}
-ํด๊ฒฐ์ฝ๋
๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋๋ฅผ ๋ณด๋ฉด์ ์ ์ด๋ด๋ ค๊ฐ ์ฝ๋.
var mergeSimilarItems = function(items1, items2) {
let map = {}
let items = [...items1, ...items2]
for(const [val, weight] of items) {
if(map[val]) {
map[val] += weight
} else {
map[val] = weight
}
}
return Object.entries(map)
};
์๊ณ ๋ฆฌ์ฆ ๋ฐฑ๋ ํ๋ฉด์ ๋๋ ๋ญํ๋ ์ฌ๋์ธ๊ฐ ์ถ์ ์๊ฐ์ด ๋ค์๋ ๋ฌธ์ ์๋ค.
'๊ฐ๋ฐ > ๐ฌ ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋น ์ค ํ๊ธฐ๋ฒ Big-O Notation ์ด๋? (2) | 2024.04.25 |
---|---|
[leetCode/Javascript] 1572. Matrix Diagonal Sum (0) | 2024.02.13 |
[leetCode/Javascript] 2418. Sort the People (1) | 2024.02.11 |
[LeetCode/Javascript] 216. Combination Sum III (0) | 2024.02.05 |
[leetCode/javascript] 1071. Greatest Common Divisor of Strings (0) | 2024.02.02 |