-
-
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 Binary Search Solution
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
...tions/004 - Median of Two Sorted Arrays/MedianOfTwoSortedArraysBinarySearchSolution.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,48 @@ | ||
// | ||
// MedianOfTwoSortedArraysBinarySearchSolution.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 20/07/2024. | ||
// | ||
|
||
import Core | ||
import Foundation | ||
import Problems | ||
|
||
final class MedianOfTwoSortedArraysBinarySearchSolution: MedianOfTwoSortedArraysDefinition { | ||
|
||
func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double { | ||
let a = nums1.count < nums2.count ? nums1 : nums2 | ||
let b = nums1.count < nums2.count ? nums2 : nums1 | ||
let m = a.count | ||
let n = b.count | ||
|
||
var low = 0 | ||
var high = m | ||
|
||
while low <= high { | ||
let i = (low + high) / 2 | ||
let j = (m + n + 1) / 2 - i | ||
|
||
let maxLeftA = (i == 0) ? Int.min : a[i - 1] | ||
let minRightA = (i == m) ? Int.max : a[i] | ||
|
||
let maxLeftB = (j == 0) ? Int.min : b[j - 1] | ||
let minRightB = (j == n) ? Int.max : b[j] | ||
|
||
if maxLeftA <= minRightB && maxLeftB <= minRightA { | ||
if (m + n) % 2 == 0 { | ||
return Double(max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2.0 | ||
} else { | ||
return Double(max(maxLeftA, maxLeftB)) | ||
} | ||
} else if maxLeftA > minRightB { | ||
high = i - 1 | ||
} else { | ||
low = i + 1 | ||
} | ||
} | ||
|
||
return 0.0 // Invalid input case | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../004 - Median of Two Sorted Arrays/MedianOfTwoSortedArraysBinarySearchSolutionTests.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 @@ | ||
// | ||
// MedianOfTwoSortedArraysBinarySearchSolutionTests.swift | ||
// LeetSwift | ||
// | ||
// Created by Jobert Sá on 20/07/2024. | ||
// | ||
|
||
import XCTest | ||
@testable import Problems | ||
@testable import Solutions | ||
@testable import TestSupport | ||
|
||
final class MedianOfTwoSortedArraysBinarySearchSolutionTests: XCTestCase { | ||
|
||
let solution: MedianOfTwoSortedArraysDefinition = MedianOfTwoSortedArraysBinarySearchSolution() | ||
|
||
func testSolution() { | ||
for testData in data { | ||
let input = testData.input | ||
|
||
let output = solution.findMedianSortedArrays(input.nums1, input.nums2) | ||
|
||
XCTAssertEqual(output, testData.expectedOutput) | ||
} | ||
} | ||
} | ||
|
||
extension MedianOfTwoSortedArraysBinarySearchSolutionTests: MedianOfTwoSortedArraysTestCaseProvider { } |