-
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 'The K Weakest Rows in a Matrix' challenge
- Loading branch information
Showing
3 changed files
with
125 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
64 changes: 64 additions & 0 deletions
64
LeetCode/LeetCode/Challenges/TheKWeakestRowsInAMatrix.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,64 @@ | ||
// | ||
// TheKWeakestRowsInAMatrix.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/ | ||
struct TheKWeakestRowsInAMatrix { | ||
|
||
//Time: O(n log m) where n is the number of rows in `mat` | ||
// where m is the number of columns in `mat` | ||
//Space: O(n) | ||
//array | ||
//matrix | ||
//binary search | ||
//sorting | ||
// | ||
//Solution Description: | ||
//Using binary we calculate the number of solider in each row and use that as the score for that row. We then sort the scores | ||
//in ascending order and return the first `k` elements. | ||
func kWeakestRows(_ mat: [[Int]], _ k: Int) -> [Int] { | ||
var scores = [(Int, Int)]() //[row, score] | ||
for i in 0..<mat.count { | ||
let row = mat[i] | ||
let score = binarySearch(row) | ||
scores.append((i, score)) | ||
} | ||
|
||
scores.sort { $0.1 < $1.1 } | ||
|
||
let rows = scores.map { $0.0 } | ||
|
||
return Array(rows[0..<k]) | ||
} | ||
|
||
//1 = solider, 0 = civilian | ||
private func binarySearch(_ values: [Int]) -> Int { | ||
let target = 1 | ||
|
||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result = -1 | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] >= target { | ||
left = mid + 1 | ||
|
||
if values[mid] == target { | ||
result = mid | ||
} | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
return result + 1 //return the number of solider in values | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
LeetCode/LeetCodeTests/Tests/TheKWeakestRowsInAMatrixTests.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,53 @@ | ||
// | ||
// TheKWeakestRowsInAMatrixTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 25/02/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class TheKWeakestRowsInAMatrixTests: XCTestCase { | ||
|
||
//MARK: - Tests | ||
|
||
func test_A() { | ||
let mat = [[1,1,0,0,0], | ||
[1,1,1,1,0], | ||
[1,0,0,0,0], | ||
[1,1,0,0,0], | ||
[1,1,1,1,1]] | ||
|
||
let k = 3 | ||
|
||
let result = TheKWeakestRowsInAMatrix().kWeakestRows(mat, k) | ||
|
||
XCTAssertEqual(result, [2,0,3]) | ||
} | ||
|
||
func test_B() { | ||
let mat = [[1,0,0,0], | ||
[1,1,1,1], | ||
[1,0,0,0], | ||
[1,0,0,0]] | ||
let k = 2 | ||
|
||
let result = TheKWeakestRowsInAMatrix().kWeakestRows(mat, k) | ||
|
||
XCTAssertEqual(result, [0,2]) | ||
} | ||
|
||
func test_C() { | ||
let mat = [[1,0,0,0], | ||
[1,1,1,1], | ||
[0,0,0,0], | ||
[1,0,0,0]] | ||
let k = 2 | ||
|
||
let result = TheKWeakestRowsInAMatrix().kWeakestRows(mat, k) | ||
|
||
XCTAssertEqual(result, [2,0]) | ||
} | ||
} |