-
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 'Find Target Indices After Sorting Array' challenge
- Loading branch information
Showing
5 changed files
with
179 additions
and
1 deletion.
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
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
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
81 changes: 81 additions & 0 deletions
81
LeetCode/LeetCode/Challenges/FindTargetIndicesAfterSortingArray.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,81 @@ | ||
// | ||
// FindTargetIndicesAfterSortingArray.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/find-target-indices-after-sorting-array/ | ||
struct FindTargetIndicesAfterSortingArray { | ||
|
||
//Time: O(n log n) where n is the number of elements in `nums` | ||
//Space: O(n) | ||
//array | ||
//sorting | ||
//binary search | ||
// | ||
//Solution Description: | ||
//With a sorted array of `nums` we can perform two binary searches to find the first occurrence of `target` and the last | ||
//occurrence. With these two indexes we can construct the result array from that range. | ||
// | ||
//N.B. Both `leftMostIndex` and `rightMostIndex` might have the same index value or they both might be nil but one can't | ||
//be nil while the other is non-nil. | ||
func targetIndices(_ nums: [Int], _ target: Int) -> [Int] { | ||
let sortedNums = nums.sorted(by: <) | ||
|
||
let leftMostIndex = firstOccurrence(sortedNums, target) | ||
let rightMostIndex = lastOccurrence(sortedNums, target) | ||
|
||
guard let leftMostIndex = leftMostIndex, let rightMostIndex = rightMostIndex else { | ||
return [Int]() | ||
} | ||
|
||
return Array(leftMostIndex...rightMostIndex) | ||
} | ||
|
||
private func firstOccurrence(_ values: [Int], _ target: Int) -> Int? { | ||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result: Int? | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] == target { | ||
result = mid | ||
right = mid - 1 | ||
} else if values[mid] > target { | ||
right = mid - 1 | ||
} else { | ||
left = mid + 1 | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private func lastOccurrence(_ values: [Int], _ target: Int) -> Int? { | ||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result: Int? | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] == target { | ||
result = mid | ||
left = mid + 1 | ||
} else if values[mid] < target { | ||
left = mid + 1 | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
LeetCode/LeetCodeTests/Tests/FindTargetIndicesAfterSortingArrayTests.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,69 @@ | ||
// | ||
// FindTargetIndicesAfterSortingArrayTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class FindTargetIndicesAfterSortingArrayTests: XCTestCase { | ||
|
||
//MARK: - Tests | ||
|
||
func test_A() { | ||
let nums = [1,2,5,2,3] | ||
let target = 2 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [1,2]) | ||
} | ||
|
||
func test_B() { | ||
let nums = [1,2,5,2,3] | ||
let target = 3 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [3]) | ||
} | ||
|
||
func test_C() { | ||
let nums = [1,2,5,2,3] | ||
let target = 5 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [4]) | ||
} | ||
|
||
func test_D() { | ||
let nums = [1,2,5,2,3] | ||
let target = 7 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [Int]()) | ||
} | ||
|
||
func test_E() { | ||
let nums = [48,90,9,21,31,35,19,69,29,52,100,54,21,86,6,45,42,5,62,77,15,38] | ||
let target = 6 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [1]) | ||
} | ||
|
||
func test_F() { | ||
let nums = [19,35,87,45,93,10,79,41,57,75,66,56,74,25,59,71,19,18,84,28,32,63,73,97,53] | ||
let target = 8 | ||
|
||
let result = FindTargetIndicesAfterSortingArray().targetIndices(nums, target) | ||
|
||
XCTAssertEqual(result, [Int]()) | ||
} | ||
} |