Skip to content

Commit 175a210

Browse files
checking in notes (but no solution) to 1717 in case it's useful to anyone...
ran out of time to work on this; i'll come back to it tomorrow!
1 parent 5dfaa51 commit 175a210

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

problems/1717/jeremymanning.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
# [Problem 1717: Maximum Score From Removing Substrings](https://leetcode.com/problems/maximum-score-from-removing-substrings/description/?envType=daily-question)
22

33
## Initial thoughts (stream-of-consciousness)
4+
- I think all of the examples could be solved using something like:
5+
- prioritize `'ab'` if `x > y` and `'ba'` otherwise
6+
- remove the higher-priority pattern (tracking points) until there are none left
7+
- then remove the lower-priority pattern (tracking points) until there are none left
8+
- For trickier inputs, it's possible that new instances of the higher-priority pattern could emerge only after removing the lower-priority patterns. In fact, maybe this could happen even after just a single lower-priority pattern removal. So maybe we need to do something like:
9+
- loop until nothing else can be done:
10+
- remove higher priority patterns until there are none left
11+
- remove a single lower priority pattern
12+
- if none found, return the current score
13+
- return the current score
14+
- This is very inefficient: finding substrings requires looping through all of `s` in $O(n)$ time, where $n$ is the length of `s`, every time we search for another instance. In the worst case (e.g., something like `abababababab...ababab` it'll take $O(n^2)$ time, which may be too long given that `s` can be up to $10^5$ characters long
15+
- One potential way to optimize would be to break the string into segments bounded by non-a/b characters. Then we could compute the max score for each of these parts and sum them together.
16+
- Another thing we can do to optimize would be to replace every consecutive sequence of non-a/b characters with a single non-a/b character (e.g., "x"). That might substantially shorten the total length of the string.
17+
- A hypothetical edge case that I'm not sure can happen (but if so, we'll need to take it into account) is if there were some case where we could get *two* matches of the lower-priority string by sacrificing *one* instance of the higher-priority string. Actually: a simple case might be: `s = 'abab'`, where `x < y`. But if `x > y/2` then our best move *isn't* to remove the middle "ba" sequence-- it's to remove each "ab" sequence in turn to cash in two instances of the lower point value to get a higher total. So those solutions above won't quite work; we need to take this into account. I wonder if we might even encounter an example where sacrificing one higher priority string could lead to *three* (or more) matches of lower-priority strings.
18+
- Let's try to think through some potential cases of what can happen with a given sequence of a/b characters:
19+
- If the length is less than or equal to two, there's nothing to search (either we have a match of one of the patterns, or not)
20+
- If the length is equal to three, we can only remove at most one substring (so if both are available we just return `max(x, y)` and otherwise we return the value for whichever string is found in the sequence, if any, or 0 otherwise).
21+
- If the length is equal to four, let's see what the possibilities are:
22+
- aaaa
23+
- aaab
24+
- aaba
25+
- aabb
26+
- abaa
27+
- abab
28+
- abba
29+
- abbb
30+
- baaa
31+
- baab
32+
- baba
33+
- babb
34+
- bbaa
35+
- bbab
36+
- bbba
37+
- bbbb
38+
- Aside: this is like counting in binary...maybe there's some shortcut we could take based on that?
39+
- I'm out of time for tonight...I'm going to have to come back to this tomorrow!
440

541
## Refining the problem, round 2 thoughts
642

0 commit comments

Comments
 (0)