diff --git a/.gitattributes b/.gitattributes index 1e26cd1..2ce9131 100644 --- a/.gitattributes +++ b/.gitattributes @@ -18,10 +18,11 @@ *.css filter=lfs diff=lfs merge=lfs -text *.cpp filter=lfs diff=lfs merge=lfs -text *.jpg filter=lfs diff=lfs merge=lfs -text +*.base filter=lfs diff=lfs merge=lfs -text +*.drawio filter=lfs diff=lfs merge=lfs -text .yml !text !filter !merge !diff *.txt !text !filter !merge !diff *.json !text !filter !merge !diff * !text !filter !merge !diff *.jpeg filter=lfs diff=lfs merge=lfs -text *.bkp filter=lfs diff=lfs merge=lfs -text -*.drawio filter=lfs diff=lfs merge=lfs -text diff --git a/content/Leetcode Questions/Leetcode Questions Base.base b/content/Leetcode Questions/Leetcode Questions Base.base new file mode 100644 index 0000000..42d749e --- /dev/null +++ b/content/Leetcode Questions/Leetcode Questions Base.base @@ -0,0 +1,19 @@ +filters: + and: + - file.inFolder("Leetcode Questions") + - file.ext == "md" +views: + - type: table + name: Table + order: + - file.name + - title + - date + - file.tags + - file.mtime + sort: + - property: date + direction: DESC + columnSize: + note.title: 162 + file.tags: 256 diff --git a/content/Leetcode Questions/Pyramid Transition Matrix.md b/content/Leetcode Questions/Pyramid Transition Matrix.md index 764b59c..205f0d6 100644 --- a/content/Leetcode Questions/Pyramid Transition Matrix.md +++ b/content/Leetcode Questions/Pyramid Transition Matrix.md @@ -176,6 +176,55 @@ If cache hit, it means such **sub question has no solution** and we should **not > > In other word, if any sub question is valid in our definition, it must also indicate the original question solution is also being found. +## More About Cache + +At the beginning, I'm actually thinking about another way of recording cache. That is, when we **find a valid value for position `[i][j]`,** then if we **use this position as pyramid top to consider the sub-pyramid**, then **all the layer should be able to considered a valid sequence** as a sub question. + +![[251231_another_way_of_cache.png]] + +Use the image above as an example, if during the searching procedure, we **found that `D` as a valid color for position `[2][1]`,** then we should be able to ensure the **layers of the sub-pyramid are all valid sequences as sub-question**, that is: + +``` +seq1 = C, D +seq2 = E, F, A +seq3 = ... +``` + +**All these sequences should be consider valid**, since when any of them is considered as the bottom, we **could found at least a valid pyramid that satisfies the constrain of the question** simply based on the sub-pyramid *(the green and blue part in the image)* we found. + +> [!note] +> +> In this case, if we **can not find any valid value for position `[1][2]`,** we **can NOT say** conversely that **any layer for the sub-pyramid is not valid**. +> +> This is easy to understand, actually we could ensure `C, D` is not a valid pyramid bottom, however, that doesn't mean `E, F, A` is not valid, since it may have other available sequence other than `C, D` as the upper layer of `E, F, A` + +----- + +I tried writing solution code using this thought, triggering cache but it turns out I got `TLE`. The basic code logic I implement is like below: + +```python +# if we should trigger cache for current position for now? +def should_trigger_cache(curR, curC) -> bool: + # only trigger cache when a position is being searched + # at the first time in current sub solution space + if currChar != '0': + return False + + # first search, but no valid value for current position + # then do not cache + if next_possible_char_for_this_pos() == '1': + return False + + return True + +# perform caching for all sequences of sub pyramid +def set_cache_for_sub_pyramid(curR, curC): + for string in get_sub_pyramid_layers_as_string(): + cache.add(seq=string, value=True) +``` + +Personally I guess the cost of caching sub-pyramid for every position every time it's first searched in a sub solution space is too high, but I can't figure out a better approach lol + # Code Implementation ```cpp @@ -550,4 +599,4 @@ public: return found; } }; -``` \ No newline at end of file +``` diff --git a/content/Leetcode Questions/assets/251231_another_way_of_cache.png b/content/Leetcode Questions/assets/251231_another_way_of_cache.png new file mode 100644 index 0000000..a96e003 --- /dev/null +++ b/content/Leetcode Questions/assets/251231_another_way_of_cache.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3d18335cdb17c634194d15a26c2c32e71db5d8302f25fe75a282fb1468e5a508 +size 23927 diff --git a/content/assets/Pyramid Transfer Matrix.drawio b/content/assets/Pyramid Transfer Matrix.drawio new file mode 100644 index 0000000..5a2ad5e --- /dev/null +++ b/content/assets/Pyramid Transfer Matrix.drawio @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:41b5e6d96ae85acb9d26b25fe3e2a6eb644002356a5910910337367cc8580069 +size 50949