Ranking: 1197 / 15151 πππ

# Q1. 1518. Water Bottles

Given numBottles full water bottles, you can exchange numExchange empty water bottles for one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Return the maximum number of water bottles you can drink

1 | class Solution: |

# Q2. 1519. Number of Nodes in the Sub-Tree With the Same Label

See details in link

## Note

similar to

437. Path Sum III

834. Sum of Distances in Tree

1 | class Solution: |

# Q3. 1520. Maximum Number of Non-Overlapping Substrings FAILED

See details in link

## Notes

- So hardβ¦.
- I thought we just need to deal with the edges
`[l, r]`

. But it turns out that we have to scan

all possible substring since within a substring, it may contains other characters which may extend the size of current substring. So it is different to merge ranges. - @votrubacβs post) provides an idea which is
- Append current possible substring
- Replace the previous substring if we find a better one to replace it

This idea might be helpful when dealing with other greedy algorithm.

1 | class Solution: |

# Q4. 1521. Find a Value of a Mysterious Function Closest to Target FAILED

See details in link

## Brute force

1 | class Solution: |

# Summary

- When dealing with tree/graph problem, a pattern
`subtree = total - parent`

can be very useful. e.g.

437. Path Sum III

834. Sum of Distances in Tree - A useful pattern in greedy algo.
- Add current possible value
- Replace previous value if we find a better

- Too sadβ¦