Sort Vector
You're given a vector V containing a permutation of the integers 0 to n-1. For example {0,2,1}. You're also given an integer N which is greater than or equal to 2 and less than V.size(). You can reverse N contiguous items in V at a time. For example V = {0,2,1} and N = 2 you can reverse the ints located in V[0] and V[1] to give V = {2,0,1}. You can also reverse the ints in V[1] and V[2] to give V = {0,1,2}. Write a function that takes V and N as parameters and returns the minimum number of reversals needed to sort the vector increasing. If it's not possible return -1.
For example V = {0,2,1} N = 2 returns 1. (reverse V[1] and V[2])
Read more
RYB Street
You need to paint n houses. You have red, yellow, and blue paint. The integer cost to paint the ith house red is given by Costs[i][0], yellow by Costs[i][1], and blue by Costs[i][2]. Houses that are next to each other must be different colors.
Write a function "int getCost(int[][] Costs)" that returns the minimum cost to paint all the houses.
The 0th house and the n-1st house can be the same color (the houses are in a row, not a circle).
Read more
Permutations and Derangements
A derangement is a permutation p of {1...n} such that no item is in its proper position, ie. pi != i for all 1 <= i <= n.
Write an efficient program that constructs all the derangements of n items.
Read more
Index == Value
Suppose that you are given a sorted sequence of distinct integers {a1,a2,a3...an}.
Give an O(lg N) algorithm to determine whether there exists an i index such that ai = i.
For example, in {-10, -3, 3, 6, 7}, a3 is 3.
In {2,3,4,5,6,7} there is no such i.
Read more