Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 14: Find 2D pattern in matrix #9

Merged
merged 4 commits into from
May 4, 2017
Merged

Day 14: Find 2D pattern in matrix #9

merged 4 commits into from
May 4, 2017

Conversation

thatguyintech
Copy link
Owner

plz comment with thoughts, criticisms, anything! :)

@thatguyintech
Copy link
Owner Author

atm the code is not finished, today was a long day and I couldn't bring myself to finish the code. But the thougth process is all there and it's not too complex.


there are only 4 possible starting locations: `(0, 0), (0, 1), (1,0), (1,1)`.

I don't think it's possible to do this problem and faster. So it's not
Copy link

@jjwon0 jjwon0 Apr 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I haven't seen or done this problem before, so this might be totally wrong.

This problem reminds me a lot of fast string matching, which means that there's probably a faster way to do it. In KMP and other linear string search algorithms, a key observation is that you might be able to skip steps. For example,

needle = 'abcd'
haystack = 'abcfffffabcdffffff'

When we compare by eye, we notice that the first three characters of needle and haystack match, but the fourth doesn't. Subsequently, we skip ahead when looking, because we know that the fourth character was an f, but needle[0] != 'f', so we can jump a few characters ahead. Specifically, we can start searching again from haystack[4:].

I suspect something similar can work in this problem, which would give it ultimately a runtime of O(nm), where n and m are the dimensions of the big matrix.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn uR RIGHT

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done thorough research yet, but it seems like this one also generalizes well to two-dimensional patern matching https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm

@thatguyintech thatguyintech merged commit 96f413a into master May 4, 2017
@thatguyintech thatguyintech deleted the day14 branch May 4, 2017 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants