I will pass an exam in the Data Structures class soon. To prepare, I look at some problems based on an algorithm that I found on the Internet, and run one that, as I can, seems to fail.
You go into the room and see a row of n cards. Each of them has a number xi written on it, where I range from 1 to n. However, initially all cards are face down. Your goal is to find a local minimum: that is, a card whose number is less than or equal to the number of its neighbors, xi-1> = xi <= xi + 1. The first and last cards can also be local minima, and they only have one neighbor compared to. It is clear that there can be many local minima, but you are only responsible for finding one of them.
The only solution I can come up with basically minimizes them and finds any local lows. However, the challenge is to do this only by flipping the O (logn) cards.
Essentially, if you see the card "7", this is a local minimum if the card on the left is "10" and the card on the right is "9". How easy is it to do during login?
Any thanks, thanks.
source share