Skip to content

Commit fd9aeca

Browse files
finally-- solved 1717!
1 parent ca4afe6 commit fd9aeca

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

problems/1717/jeremymanning.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,57 @@
3737
- bbbb
3838
- Aside: this is like counting in binary...maybe there's some shortcut we could take based on that?
3939
- after taking a pause....
40-
- Ok, in re-thinking the "abab" example above, it's not correct as I described it. If we remove "ba" first, then we're still left with an instance of "ab" which we can use to get those points. So actually, prioritizing the higher-scoring substring *does* seem to make sense. The real issue is with efficiency. Ok...*now* I'm going to come back to this tomorrow 😄.
41-
- Quick idea to explore tomorrow: these substrings can be described using context-free grammars, which means we can solve this using a pushdown automata...which means implementing a stack might be useful.
42-
- 😴
40+
- Ok, in re-thinking the "abab" example above, it's not correct as I described it. If we remove "ba" first, then we're still left with an instance of "ab" which we can use to get those points. So actually, prioritizing the higher-scoring substring *does* seem to make sense. The real issue is with efficiency.
41+
- another pause...
42+
- let's try a "stack" implementation to avoid having to loop through the same string a gajillion times:
43+
- to detect (with priority) the two-character substring `sub` with point value `p`:
44+
- loop through each character, `c` of `s`:
45+
- if the stack is not empty, and if the top of the stack + `c` matches `sub`:
46+
- increment total points by `p`
47+
- pop the top of the stack ("removes" the substring)
48+
- otherwise push `c` to the stack
49+
- return the total points, along with `''.join(stack)` (i.e., the updated string, with matches removed)
50+
- run this first for the higher-priority substring, and then for the lower-priority substring, and then return the sum of the resulting point totals
4351

4452
## Refining the problem, round 2 thoughts
53+
- thinking through edge cases:
54+
- can we have an empty input string? no, we're given that `1 <= s.length <= 10^5`
55+
- can the "points values" (`x` or `y`) ever be negative? no, we're given that `1 <= x, y <= 10^4`
56+
- let's try this!
4557

4658
## Attempted solution(s)
4759
```python
48-
class Solution: # paste your code here!
49-
...
60+
class Solution:
61+
def maximumGain(self, s: str, x: int, y: int) -> int:
62+
def helper(s, pattern, p):
63+
stack = []
64+
points = 0
65+
for c in s:
66+
if len(stack) > 0 and stack[-1] + c == pattern:
67+
stack.pop()
68+
points += p
69+
else:
70+
stack.append(c)
71+
return ''.join(stack), points
72+
73+
if x > y:
74+
high, low, p1, p2 = 'ab', 'ba', x, y
75+
else:
76+
high, low, p1, p2 = 'ba', 'ab', y, x
77+
78+
first_pass, total1 = helper(s, high, p1)
79+
_, total2 = helper(first_pass, low, p2)
80+
81+
return total1 + total2
5082
```
83+
- given test cases: pass
84+
- other tests:
85+
- 's = "ababababab"`, `x = 5`, `y = `10`: pass
86+
- 's = "ababababab"`, `x = 10`, `y = `5`: pass
87+
- `s = "abacbabcabacbabc"`, `x = 1`, `y = 1`: pass
88+
- ok, looks reasonable...submitting...
89+
90+
![Screenshot 2024-07-12 at 9 05 26 AM](https://github.com/user-attachments/assets/0010d7ff-d2e1-46ac-9c8f-43a428c6405c)
91+
92+
done!
93+

0 commit comments

Comments
 (0)