欢迎光临散文网 会员登陆 & 注册

leetcode 2086. Minimum Number of Food Buckets to Feed the Hamste

2022-12-30 12:24 作者:您是打尖儿还是住店呢  | 我要投稿

You are given a 0-indexed string hamsters where hamsters[i] is either:

  • 'H' indicating that there is a hamster at index i, or

  • '.' indicating that index i is empty.

You will add some number of food buckets at the empty indices in order to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index i can be fed if you place a food bucket at index i - 1 and/or at index i + 1.

Return the minimum number of food buckets you should place at empty indices to feed all the hamsters or -1 if it is impossible to feed all of them.

 

Example 1:


Input: hamsters = "H..H"Output: 2Explanation: We place two food buckets at indices 1 and 2. It can be shown that if we place only one food bucket, one of the hamsters will not be fed.

Example 2:


Input: hamsters = ".H.H."Output: 1Explanation: We place one food bucket at index 2.

Example 3:


Input: hamsters = ".HHH."Output: -1Explanation: If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.

 

Constraints:

  • 1 <= hamsters.length <= 105

  • hamsters[i] is either'H' or '.'.

1:考虑边界条件;

2:先将H中间有.的清出来,然后,再去遍历剩下的H即可。

就是有点花时间了;


Runtime27 ms

Beats

10.8%

Memory50.4 MB

Beats

34.88%

这速度也是没谁了,慢的快不及格了。。。


leetcode 2086. Minimum Number of Food Buckets to Feed the Hamste的评论 (共 条)

分享到微博请遵守国家法律