开关灯 动态规划

Problem Statement
  Elly has a row of N lamps, conveniently numbered from 0 to N-1. Some of them are initially lit and the rest are not. She can control the lamps using a controller. On the controller there is a row of N buttons. Clicking the button with index i changes the state of lamp i: it goes off, if it was on, and it goes on if it was off.

  Unfortunately the controller has some defects. Each of the buttons correctly changes the state of the corresponding lamp. However, it is possible that some of the buttons also change the state of one or both adjacent lamps as well. This means that pressing the button with index i has the following effects:
The state of lamp i changes.
If there is a lamp with index i-1, its state might change.
If there is a lamp with index i+1, its state might change.
The girl does not initially know which lamps change their state when each of the buttons is pressed. She knows, however, that every time she clicks a particular button, the same set of lamps will change their states.


  Elly wants to reach a configuration in which the number of lamps that are turned on is minimized. Help her determine how many of them will remain lit in the worst possible case. (That is, for the worst possible way how the buttons change the state of the lamps.) She can use each of the buttons as many times as she likes, in any order she likes.


  You will be given a String lamps, which gives information about the initial state of the lamps. The i-th character of lamps will be 'Y' if the i-th lamp is lit, and 'N', if it is not. Return the minimal number of lit lamps the girl can get in the worst possible case.
 
Definition
   
Class: EllysLamps
Method: getMin
Parameters: String
Returns: int
Method signature: int getMin(String lamps)
(be sure your method is public)
    
 
Notes
- The defects in the switches might not be symmetric. This means that if a lamp with index i happens to change the state of lamp i+1, this does not necessarily mean that the using the switch of lamp i+1 changes the state of lamp i.
 
Constraints
- lamps will contain between 1 and 50 elements, inclusive.
- Each element of lamps will be either 'Y' or 'N'.
 
Examples
0)
   
"YNNYN"
Returns: 2
In this case Elly will only make things worse (or the same) by pressing buttons. For example, suppose that:
buttons 0 and 1 each change the state of both lamps 0 and 1
buttons 2 and 3 each change the state of both lamps 2 and 3
button 4 only changes the state of lamp 4
In this situation, Elly cannot reach any configuration with fewer than two lit lamps.
1)
   
"NNN"
Returns: 0
Obviously, with no initially lit lamps, Elly can just sit and relax.
2)
   
"YY"
Returns: 0
If one of the lamps influences the other one, then Elly can use it and turn both off with one button press. Otherwise, each button changes the state of its lamp only, thus Elly can turn them both off by pressing both buttons.
3)
   
"YNYYYNNNY"
Returns: 3
4)
   
"YNYYYYNYNNYYNNNNNNYNYNYNYNNYNYYYNY"

Returns: 13


---------------------------------------------------

 f[i,j,k]表示前i个灯,第i个按j次,第i-1个按k次的最坏情况 
 会影响到i,但是有j和k你其实可以知道最坏情况下i是不是亮的
状态是f[i, j, k]对应1..i-1的最坏
然后f[i+1, t, j] 就去判断第i个灯的最坏 
 第i个灯的最坏和t, j, k的情况有关,肯定可以枚举出来最坏是不是亮的
一般都是亮的。。如果t和k有一个是1的话。。 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值