-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'Merge Two 2D Arrays by Summing Values' challenge
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
LeetCode/LeetCode/Challenges/MergeTwo2DArraysBySummingValues.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// MergeTwo2DArraysBySummingValues.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 09/06/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/ | ||
struct MergeTwo2DArraysBySummingValues { | ||
|
||
//Time: O(n) where n is the larger count of `nums1` or `nums2` | ||
//Space: O(n + m) where n is the number of elements in `nums1` | ||
// where m is the number of elements in `nums2` | ||
//array | ||
//two pointers | ||
// | ||
//Solution Description: | ||
//Using two pointers we iterate through both `nums1` and `nums2`, comparing the arrays found at each index. If the IDs | ||
//match then we sum their values, add that combined value to `merged` and increment both pointers; if the ID at `p1` is | ||
//smaller we add that smaller array to `merged` and only increment the `p1` pointer; if the ID at `p2` is smaller we | ||
//add that smaller array to `merged` and only increment the `p2` pointer. Once we `p1` or/and `p2` exceed the number of | ||
//elements in their arrays we attempt to add any remaining elements to `merged` and return `merged`. | ||
func mergeArrays(_ nums1: [[Int]], _ nums2: [[Int]]) -> [[Int]] { | ||
var merged = [[Int]]() | ||
|
||
var p1 = 0 | ||
var p2 = 0 | ||
|
||
while p1 < nums1.count && p2 < nums2.count { | ||
let val1 = nums1[p1] | ||
let val2 = nums2[p2] | ||
|
||
if val1[0] == val2[0] { | ||
let sum = val1[1] + val2[1] | ||
merged.append([val1[0], sum]) | ||
|
||
p1 += 1 | ||
p2 += 1 | ||
} else if val1[0] < val2[0] { | ||
merged.append(val1) | ||
|
||
p1 += 1 | ||
} else { | ||
merged.append(val2) | ||
|
||
p2 += 1 | ||
} | ||
} | ||
|
||
merged.append(contentsOf: nums1[p1...]) | ||
merged.append(contentsOf: nums2[p2...]) | ||
|
||
return merged | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
LeetCode/LeetCodeTests/Tests/MergeTwo2DArraysBySummingValuesTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// MergeTwo2DArraysBySummingValuesTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 09/06/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class MergeTwo2DArraysBySummingValuesTests: XCTestCase { | ||
|
||
// MARK: - Tests | ||
|
||
func test_A() { | ||
let nums1 = [[1,2],[2,3],[4,5]] | ||
let nums2 = [[1,4],[3,2],[4,1]] | ||
|
||
let result = MergeTwo2DArraysBySummingValues().mergeArrays(nums1, nums2) | ||
|
||
XCTAssertEqual(result, [[1,6],[2,3],[3,2],[4,6]]) | ||
} | ||
|
||
func test_B() { | ||
let nums1 = [[2,4],[3,6],[5,5]] | ||
let nums2 = [[1,3],[4,3]] | ||
|
||
let result = MergeTwo2DArraysBySummingValues().mergeArrays(nums1, nums2) | ||
|
||
XCTAssertEqual(result, [[1,3],[2,4],[3,6],[4,3],[5,5]]) | ||
} | ||
|
||
func test_C() { | ||
let nums1 = [[1,4],[2,6]] | ||
let nums2 = [[1,3],[2,3]] | ||
|
||
let result = MergeTwo2DArraysBySummingValues().mergeArrays(nums1, nums2) | ||
|
||
XCTAssertEqual(result, [[1,7],[2,9]]) | ||
} | ||
|
||
func test_D() { | ||
let nums1 = [[1,3],[4,3]] | ||
let nums2 = [[2,4],[3,6],[5,5]] | ||
|
||
let result = MergeTwo2DArraysBySummingValues().mergeArrays(nums1, nums2) | ||
|
||
XCTAssertEqual(result, [[1,3],[2,4],[3,6],[4,3],[5,5]]) | ||
} | ||
} |