Skip to content

Commit

Permalink
Implemented 'Merge Two 2D Arrays by Summing Values' challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
wibosco committed Jun 9, 2024
1 parent 82a8898 commit 58dd248
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions LeetCode/LeetCode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,8 @@
43D3C1B22B29E9B7000DFBFC /* BinaryTreeColoringGameTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D3C1B12B29E9B7000DFBFC /* BinaryTreeColoringGameTests.swift */; };
43D3C1B42B29FB6A000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnother.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D3C1B32B29FB6A000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnother.swift */; };
43D3C1B62B29FBAB000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnotherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D3C1B52B29FBAB000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnotherTests.swift */; };
43D842F92C1641CD002C5220 /* MergeTwo2DArraysBySummingValues.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D842F82C1641CD002C5220 /* MergeTwo2DArraysBySummingValues.swift */; };
43D842FB2C164214002C5220 /* MergeTwo2DArraysBySummingValuesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D842FA2C164214002C5220 /* MergeTwo2DArraysBySummingValuesTests.swift */; };
43D8B8022A629C0000B3F197 /* FindTheDifferenceOfTwoArrays.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8B8012A629C0000B3F197 /* FindTheDifferenceOfTwoArrays.swift */; };
43D8B8042A629C3700B3F197 /* FindTheDifferenceOfTwoArraysTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43D8B8032A629C3700B3F197 /* FindTheDifferenceOfTwoArraysTests.swift */; };
43E175ED2A701BB200798BE4 /* FruitIntoBaskets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43E175EC2A701BB200798BE4 /* FruitIntoBaskets.swift */; };
Expand Down Expand Up @@ -1901,6 +1903,8 @@
43D3C1B12B29E9B7000DFBFC /* BinaryTreeColoringGameTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BinaryTreeColoringGameTests.swift; sourceTree = "<group>"; };
43D3C1B32B29FB6A000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnother.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepByStepDirectionsFromABinaryTreeNodeToAnother.swift; sourceTree = "<group>"; };
43D3C1B52B29FBAB000DFBFC /* StepByStepDirectionsFromABinaryTreeNodeToAnotherTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepByStepDirectionsFromABinaryTreeNodeToAnotherTests.swift; sourceTree = "<group>"; };
43D842F82C1641CD002C5220 /* MergeTwo2DArraysBySummingValues.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeTwo2DArraysBySummingValues.swift; sourceTree = "<group>"; };
43D842FA2C164214002C5220 /* MergeTwo2DArraysBySummingValuesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MergeTwo2DArraysBySummingValuesTests.swift; sourceTree = "<group>"; };
43D8B8012A629C0000B3F197 /* FindTheDifferenceOfTwoArrays.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindTheDifferenceOfTwoArrays.swift; sourceTree = "<group>"; };
43D8B8032A629C3700B3F197 /* FindTheDifferenceOfTwoArraysTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindTheDifferenceOfTwoArraysTests.swift; sourceTree = "<group>"; };
43E175EC2A701BB200798BE4 /* FruitIntoBaskets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitIntoBaskets.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2294,6 +2298,7 @@
3D5C90CB27A7F7620035C399 /* MergeSortedArray.swift */,
43C42B7F2A65296A00D5881B /* MergeStringsAlternately.swift */,
43EE0BD52A54762F003D23E0 /* MergeTripletsToFormTargetTriplet.swift */,
43D842F82C1641CD002C5220 /* MergeTwo2DArraysBySummingValues.swift */,
3DA8E5142804604400A23F3C /* MergeTwoBinaryTrees.swift */,
3D5C90AC27A7F7620035C399 /* MergeTwoSortedLists.swift */,
3D5C90A227A7F7620035C399 /* MiddleOfLinkedList.swift */,
Expand Down Expand Up @@ -2778,6 +2783,7 @@
3D5C923127A7F76B0035C399 /* MergeSortedArrayTests.swift */,
43C42B812A6529DA00D5881B /* MergeStringsAlternatelyTests.swift */,
43EE0BD72A54766A003D23E0 /* MergeTripletsToFormTargetTripletTests.swift */,
43D842FA2C164214002C5220 /* MergeTwo2DArraysBySummingValuesTests.swift */,
3DA8E5162804615800A23F3C /* MergeTwoBinaryTreesTests.swift */,
3D5C922227A7F76B0035C399 /* MergeTwoSortedListsTests.swift */,
3D5C924F27A7F76C0035C399 /* MiddleOfLinkedListTests.swift */,
Expand Down Expand Up @@ -3140,6 +3146,7 @@
3D7F318927F7915700200775 /* UniquePathsII.swift in Sources */,
4366348C2AFE255D0087F13F /* RestoreTheArrayFromAdjacentPairs.swift in Sources */,
43FB7C502B90E44A002FD664 /* FairCandySwap.swift in Sources */,
43D842F92C1641CD002C5220 /* MergeTwo2DArraysBySummingValues.swift in Sources */,
4354347B2AF50335002B996E /* MinimumNumberOfVerticesToReachAllNodes.swift in Sources */,
3D2A96B3289D683A003B954F /* FindLeavesOfBinaryTree.swift in Sources */,
3D5C914B27A7F7630035C399 /* ListNodeRandom.swift in Sources */,
Expand Down Expand Up @@ -3618,6 +3625,7 @@
3D5C932727A7F76C0035C399 /* BinaryTreePostorderTraversalTests.swift in Sources */,
3D5C933E27A7F76C0035C399 /* SymmetricTreeTests.swift in Sources */,
3D5C92CB27A7F76C0035C399 /* MinCostToConnectAllPointsTests.swift in Sources */,
43D842FB2C164214002C5220 /* MergeTwo2DArraysBySummingValuesTests.swift in Sources */,
3D5C92EC27A7F76C0035C399 /* GuessNumberHigherOrLowerTests.swift in Sources */,
4331E4552B91255A003C2E95 /* SpecialArrayWithXElementsGreaterThanOrEqualXTests.swift in Sources */,
43ED237A2A716BE900C80E11 /* MaximumNumberOfVowelsInASubstringOfGivenLengthTests.swift in Sources */,
Expand Down
57 changes: 57 additions & 0 deletions LeetCode/LeetCode/Challenges/MergeTwo2DArraysBySummingValues.swift
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
}
}
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]])
}
}

0 comments on commit 58dd248

Please sign in to comment.