Coding Recipies: TV remote
Gaurav has fractured his leg in a bicycle accident.He keeps himself busy by watching television on his Tata Sky DTH. While watching the television, one day he decided to play a small game, where
he has to identify the minimum number of clicks required to surf a given set of channels in a
sequence.
WAP with given instructions:
Instructions
There are 13 buttons on his remote: 10 buttons for the numbers (0-9), an "Up Channel" button,
a "Down Channel" button, and a "Back" button:
● The number buttons allow you to jump directly to a specific channel. (Ex: to go to
channel 63 by typing "6", "3".)
● The "Up Channel" button increments the current channel to the next higher viewable
channel, unless the current channel is the highest viewable channel, in which case it
rolls over to the lowest viewable channel.
● The "Down Channel" button decrements to the next lower viewable channel, unless the
current channel is the lowest viewable channel, in which case it rolls over to the highest
viewable channel.
● The "Back" button reverts to whatever channel was on the television before the current
channel. (Ex: If channel 1 is viewed, then channel 100, then when Back is pressed, the
television will go to channel 1.)
Gaurav can get from one channel to the next in one of the two ways:
● Clicking any combination of "Up Channel", "Down Channel", and "Back" buttons.
● Keying in the numbers to the channel. Gaurav will never combine "Back" and number
buttons when moving from one channel to the next.
Gaurav's parents have set some parental control on some channels on Gaurav's television.
These channels are not viewable, so they are skipped by the "Up Channel" and "Down
Channel" buttons.
Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked
channels, your program should return the minimum number of clicks necessary to get
through all the shows that Gaurav would like to watch.
Related: http://massivealgorithms.blogspot.com/2014/07/avid-tv-watcher-programming-interviews.html
Read full article from Coding Recipies: TV remote
Gaurav has fractured his leg in a bicycle accident.He keeps himself busy by watching television on his Tata Sky DTH. While watching the television, one day he decided to play a small game, where
he has to identify the minimum number of clicks required to surf a given set of channels in a
sequence.
WAP with given instructions:
Instructions
There are 13 buttons on his remote: 10 buttons for the numbers (0-9), an "Up Channel" button,
a "Down Channel" button, and a "Back" button:
● The number buttons allow you to jump directly to a specific channel. (Ex: to go to
channel 63 by typing "6", "3".)
● The "Up Channel" button increments the current channel to the next higher viewable
channel, unless the current channel is the highest viewable channel, in which case it
rolls over to the lowest viewable channel.
● The "Down Channel" button decrements to the next lower viewable channel, unless the
current channel is the lowest viewable channel, in which case it rolls over to the highest
viewable channel.
● The "Back" button reverts to whatever channel was on the television before the current
channel. (Ex: If channel 1 is viewed, then channel 100, then when Back is pressed, the
television will go to channel 1.)
Gaurav can get from one channel to the next in one of the two ways:
● Clicking any combination of "Up Channel", "Down Channel", and "Back" buttons.
● Keying in the numbers to the channel. Gaurav will never combine "Back" and number
buttons when moving from one channel to the next.
Gaurav's parents have set some parental control on some channels on Gaurav's television.
These channels are not viewable, so they are skipped by the "Up Channel" and "Down
Channel" buttons.
Given a list of channels to view, the lowest channel, the highest channel, and a list of blocked
channels, your program should return the minimum number of clicks necessary to get
through all the shows that Gaurav would like to watch.
Constraints
● The sequence that Gaurav must view contains between 1 and 50 elements, inclusive. All
channels in this sequence are not in the blocked list and are between lowest channel
and highest channel, inclusive.
class
TVRemote {
private
int
BackChannel =
0
;
private
List<Integer> channels;
private
int
currentIndex =
0
;
private
int
clicks =
0
;
private
int
totalChannels;
public
TVRemote(
int
low,
int
high,
int
[] blocked,
int
[] view) {
channels =
new
ArrayList<Integer>(high - low);
for
(
int
i = low; i <= high; i++)
channels.add(i);
for
(Integer o : blocked)
channels.remove(o);
totalChannels = channels.size();
}
public
int
solve(
int
low,
int
high,
int
[] view) {
for
(
int
i =
0
; i < view.length; i++) {
int
toBeViewedChannel = view[i];
int
toBeViewedIndex = getIndex(toBeViewedChannel);
int
diff = getDiff(currentIndex, toBeViewedIndex);
int
absDiff = Math.abs(diff);
int
numDigits = String.valueOf(toBeViewedChannel).length();
if
(diff ==
0
)
// same channel
continue
;
else
if
(BackChannel == toBeViewedIndex)
// when back is to be
// pressed
backPressed(toBeViewedIndex);
else
if
(absDiff > numDigits)
// when number of digits are less than
// displacement
goToChannel(toBeViewedChannel, toBeViewedIndex, numDigits);
else
// when displacement is less than number of digits
{
for
(
int
j =
0
; j < absDiff; j++) {
if
(diff <
0
)
downPressed();
else
upPressed();
}
}
}
return
clicks;
}
/**
* Get the displacement between start Index to End Index in an Array
*/
private
int
getDiff(
int
start,
int
end) {
int
diff1 = Math.abs(end - start);
int
diff2 = Math.abs(totalChannels - diff1);
if
(start < end)
return
diff1 < diff2 ? diff1 : -diff2;
else
return
diff1 < diff2 ? -diff1 : diff2;
}
/**
* @param channel
* : given the channel
* @return : returns the index of the channel
*/
private
int
getIndex(
int
channel) {
for
(
int
index =
0
; index < totalChannels; index++)
if
(channel == channels.get(index))
return
index;
return
0
;
}
private
void
goToChannel(
int
toBeViewedChannel,
int
toBeViewedIndex,
int
numDigits) {
BackChannel = currentIndex;
currentIndex = toBeViewedIndex;
for
(
int
j =
0
; j < numDigits; j++)
clicks++;
}
/**
* Up Button is pressed
*/
private
void
upPressed() {
BackChannel = currentIndex;
if
(currentIndex == totalChannels -
1
)
currentIndex =
0
;
else
currentIndex++;
clicks++;
}
/**
* Down Button is Pressed
*/
private
void
downPressed() {
BackChannel = currentIndex;
if
(currentIndex ==
0
)
currentIndex = totalChannels -
1
;
else
currentIndex--;
clicks++;
}
/**
* Back BUtton is pressed
* @param toBeViewedIndex
*/
private
void
backPressed(
int
toBeViewedIndex) {
BackChannel = currentIndex;
currentIndex = toBeViewedIndex;
clicks++;
}
}
Read full article from Coding Recipies: TV remote