Skip to content

Commit dcdf431

Browse files
committed
[Sort] Refactor solution to Kth Largest Element in an Array
1 parent a181347 commit dcdf431

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Sort/KthLargestElementInArray.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ class KthLargestElementInArray {
2020

2121
if leftPart.count > k - 1 {
2222
return findKthLargest(leftPart, k)
23-
} else if leftPart.count == k - 1 {
24-
return pivot
23+
} else if k - leftPart.count <= middlePart.count {
24+
return findKthLargest(middlePart, k - leftPart.count)
2525
} else {
26-
if k - leftPart.count <= middlePart.count {
27-
return findKthLargest(middlePart, k - leftPart.count)
28-
} else {
29-
return findKthLargest(rightPart, k - leftPart.count - middlePart.count)
30-
}
31-
}
26+
return findKthLargest(rightPart, k - leftPart.count - middlePart.count)
27+
}
3228
}
3329
}

0 commit comments

Comments
 (0)