Leetcode(135)——分发糖果

Browsed some problems and want to add some different thoughts on this specific one (skip descriptions).

To make it as short as possible.

1.  Clearly there exists only 3 distinct patterns (-, /, \), each may occur 0 or more times.

2.  There exists one turning point between any two neighboring different patterns and consider this turning point belonging to both patterns (or two groups for easy understanding).

3.  Find out all occurrence of the 3 distinct patterns and all turning points in between.

4.  For each occurrence of a given pattern (in other words, for each group), assign a value to each member in the group INDEPENDENTLY.

5.  For any turning point belonging to two neighboring groups, choose ONE values (logically correct one) from the two values which are obtained independently in the two neighboring groups.

6.  Add up all values one by one as final answer.

 

Let us see simple examples,

 

(1,2,1) >>> two groups {1,2} (pattern /) and {2,1} (pattern \).  For first group, P1=1 and P2=2.  For second group, P2=2 and P3=1.  For Turing point P2, choose 2 (because both values are the same).  The answer is 1+2+1=4.

 

To extend the above a little bit,

(1,2,3,1)  >>> two groups {1,2,3} (/) and {3,1) (\).  So for first group, P1=1 and P2=2 and P3=3. For second group, P3=2 and P4=1.  For turning point P3, choose 3 (must have P3>P2 and P3>P4).  The answer is 1+2+3+1=7.

 

Now see some other examples,

(1,2,2) >>> two groups {1,2} (/) and {2,2} (-).  For first group, P1=1 and P2=2.  For second group, P2=1 and P3=1.  For turning point P2, choose 2 (must have P2>P1 and no constraint between P2 and P3).  The answer is 1+2+1=4.

 

(1,0,2) >>> two groups {1,0} (pattern \) and {0,2} (patter /).  For first group, P1=2 and P2=1.  For second group, P2=1 and P3=2.  For turning point P2, choose 1 (for patter combination of \ / it is obvious that the turning point value shoud be 1, for / \ it should be the larger one, etc).  The answer for this example is then 2+1+2=5.

 

The rules for choosing all turning points values are so obvious that one may practice more examples manually to find it out before coding. I believe in all cases any given turning point should choose the larger one between two possible values (unless all members in patter - groups are required to have the same value).

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值