Created
January 14, 2018 19:42
-
-
Save geor-kasapidi/1a064b6494eb2946a337f0ee95698942 to your computer and use it in GitHub Desktop.
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
func absolutePermutation(n: Int, k: Int) -> String { | |
let r = (1...n) | |
var tmp = Set<Int>() | |
var res = "" | |
for i in r { | |
var a = i + k | |
var b = i - k | |
if a > b { | |
swap(&a, &b) | |
} | |
let c: Int | |
if r.contains(a) && !tmp.contains(a) { | |
c = a | |
} else if r.contains(b) && !tmp.contains(b) { | |
c = b | |
} else { | |
return "-1" | |
} | |
tmp.insert(c) | |
res.append(String(c)) | |
if i != n { | |
res.append(" ") | |
} | |
} | |
return res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment