We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Absolute Permutation
Absolute Permutation
Sort by
recency
|
522 Discussions
|
Please Login in order to post a comment
I think there is wrong answer in test case n = 69660 and k = 1620 this shouldn't be -1, why test case #12 shows it -1 ?
this my code in c
Swift solution: There are only 2 cases:
k == 0
n%(2*k) == 0
Exp
n=12
k=3
:4 5 6 1 2 3 10 11 12 7 8 9
It will loop in 2 sub:
[4 5 6 1 2 3] & [10 11 12 7 8 9]
O(n) greedy approach that dont need to consider corner cases. We always take the (i-k) choice if possible, if not try (i+k), if that fails return -1.
Here is my code which pass all tests, but I saw a lot of check about n%(2*k)==0 and don't really understand why and I'm curious if someone would care to explain