Naveen Shan
BAN USERHere I prefer to go with divide an conquer approach.
Approach:
From the 52 weeks find the biggest selling value, divide it from there and find the smallest value in front of it.
Also do the same iteration with second cropped part and return the difference of highest and smallest.
The most profit is max value for any of those part.
Now observe the question again.
Point 1 : Short selling is not allowed so while finding the smallest we need to ignore the adjacent one and splitting the array we need to start from that position. Also ignore the first two element while looking for highest value.
- This is the reason I choose divide and conquer we get much more flexibility.
Point 2 : Report if there is no profit making senerio. We after the execution we can check the profit value is greater than 0 to do this. Also check if we had more than 2 weeks otherwise there is no profit making senerio.
Code
public int minimumHops(int k, int n) {
return (k/n + k%n);
}
Example : k = 68 and n = 10
Answer : 6 (n hops : 60) + 8 (1 hops : 8) = 14hops
k/n (68/10 = 6) + k%n (68%10 = 8) = 14hops
Got issue while pasting the code, See below
- Naveen Shan February 24, 2017