-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Problem 004 Merge solution and tests
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
...es/Solutions/004 - Median of Two Sorted Arrays/MedianOfTwoSortedArraysMergeSolution.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,24 @@ | ||
// | ||
// MedianOfTwoSortedArraysMergeSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 19/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class MedianOfTwoSortedArraysMergeSolution: MedianOfTwoSortedArraysDefinition { | ||
|
||
func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double { | ||
let mergedArray = (nums1 + nums2).sorted() | ||
|
||
let mid = mergedArray.count / 2 | ||
|
||
if mergedArray.count % 2 == 1 { | ||
return Double(mergedArray[mid]) | ||
} | ||
return Double(mergedArray[mid - 1] + mergedArray[mid]) / 2.0 | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...nsTests/004 - Median of Two Sorted Arrays/MedianOfTwoSortedArraysMergeSolutionTests.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,28 @@ | ||
// | ||
// MedianOfTwoSortedArraysMergeSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 19/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class MedianOfTwoSortedArraysMergeSolutionTests: XCTestCase { | ||
|
||
let solution: MedianOfTwoSortedArraysDefinition = MedianOfTwoSortedArraysMergeSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.findMedianSortedArrays(input.nums1, input.nums2) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension MedianOfTwoSortedArraysMergeSolutionTests: MedianOfTwoSortedArraysTestCaseProvider { } |