Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Kth Largest Element/kthLargest.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//: Playground - noun: a place where people can play

func kthLargest(a: [Int], k: Int) -> Int? {
func kthLargest(_ a: [Int], _ k: Int) -> Int? {
let len = a.count
if k > 0 && k <= len {
let sorted = a.sort()
let sorted = a.sorted()
return sorted[len - k]
} else {
return nil
Expand All @@ -12,23 +12,23 @@ func kthLargest(a: [Int], k: Int) -> Int? {

let a = [5, 1, 3, 2, 7, 6, 4]

kthLargest(a, k: 0)
kthLargest(a, k: 1)
kthLargest(a, k: 2)
kthLargest(a, k: 3)
kthLargest(a, k: 4)
kthLargest(a, k: 5)
kthLargest(a, k: 6)
kthLargest(a, k: 7)
kthLargest(a, k: 8)
kthLargest(a, 0)
kthLargest(a, 1)
kthLargest(a, 2)
kthLargest(a, 3)
kthLargest(a, 4)
kthLargest(a, 5)
kthLargest(a, 6)
kthLargest(a, 7)
kthLargest(a, 8)




import Foundation

/* Returns a random integer in the range min...max, inclusive. */
public func random(min min: Int, max: Int) -> Int {
public func random( min: Int, max: Int) -> Int {
assert(min < max)
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
}
Expand All @@ -37,22 +37,22 @@ public func random(min min: Int, max: Int) -> Int {
Swift's swap() doesn't like it if the items you're trying to swap refer to
the same memory location. This little wrapper simply ignores such swaps.
*/
public func swap<T>(inout a: [T], _ i: Int, _ j: Int) {
public func swap<T>(_ a:inout [T], _ i: Int, _ j: Int) {
if i != j {
swap(&a[i], &a[j])
}
}

public func randomizedSelect<T: Comparable>(array: [T], order k: Int) -> T {
public func randomizedSelect<T: Comparable>(_ array: [T], order k: Int) -> T {
var a = array

func randomPivot<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> T {
func randomPivot<T: Comparable>(_ a: inout[T], _ low: Int, _ high: Int) -> T {
let pivotIndex = random(min: low, max: high)
swap(&a, pivotIndex, high)
return a[high]
}

func randomizedPartition<T: Comparable>(inout a: [T], _ low: Int, _ high: Int) -> Int {
func randomizedPartition<T: Comparable>(_ a: inout[T], _ low: Int, _ high: Int) -> Int {
let pivot = randomPivot(&a, low, high)
var i = low
for j in low..<high {
Expand All @@ -65,7 +65,7 @@ public func randomizedSelect<T: Comparable>(array: [T], order k: Int) -> T {
return i
}

func randomizedSelect<T: Comparable>(inout a: [T], _ low: Int, _ high: Int, _ k: Int) -> T {
func randomizedSelect<T: Comparable>(_ a: inout [T], _ low: Int, _ high: Int, _ k: Int) -> T {
if low < high {
let p = randomizedPartition(&a, low, high)
if k == p {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "CF309AABC690F91A443043D5C69EECB0069B5411",
"DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : {

},
"DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : {
"CF309AABC690F91A443043D5C69EECB0069B5411" : 9223372036854775807,
"FA0506A44181383605977F2A9C8020B861F7CE04" : 9223372036854775807
},
"DVTSourceControlWorkspaceBlueprintIdentifierKey" : "6CCD70DA-EE44-4E31-98F0-6DA8B083772A",
"DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : {
"CF309AABC690F91A443043D5C69EECB0069B5411" : "swift-algorithm-club\/",
"FA0506A44181383605977F2A9C8020B861F7CE04" : ""
},
"DVTSourceControlWorkspaceBlueprintNameKey" : "kthLargest",
"DVTSourceControlWorkspaceBlueprintVersion" : 204,
"DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Kth Largest Element\/kthLargest.playground",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Deeer\/swift-algorithm-club.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "CF309AABC690F91A443043D5C69EECB0069B5411"
},
{
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/Deeer\/CuteSticker.git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git",
"DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "FA0506A44181383605977F2A9C8020B861F7CE04"
}
]
}
6 changes: 0 additions & 6 deletions Kth Largest Element/kthLargest.playground/timeline.xctimeline

This file was deleted.