Skip to content

Instantly share code, notes, and snippets.

@liggitt
Created November 23, 2021 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liggitt/1e5f40f92235d4440724af24f803f17e to your computer and use it in GitHub Desktop.
Save liggitt/1e5f40f92235d4440724af24f803f17e to your computer and use it in GitHub Desktop.
LHS | RHS | Result
----|-------|-------
A | Bpre | A
B | B | Bpre
C | Bpost | B
D | Hpre | C
E | H | D
F | Hpost | F
G | Epre | G
H | E | Bpost
I | Epost | Hpre
J | L | H
K | M | I
| | J
| | Hpost
| | Epre
| | E
| | Epost
| | L
| | M
Start with LHS and RHS
```
LHS | RHS
----|-------
A | Bpre
B | B
C | Bpost
D | Hpre
E | H
F | Hpost
G | Epre
H | E
I | Epost
J | L
K | M
```
For every item in LHS, determine the index of the item in RHS (-1 means not present in RHS)
```
LHS | RHS
-----|------
A rindex=-1 | Bpre
B rindex=1 | B
C rindex=-1 | Bpost
D rindex=-1 | Hpre
E rindex=7 | H
F rindex=-1 | Hpost
G rindex=-1 | Epre
H rindex=4 | E
I rindex=-1 | Epost
J rindex=-1 | L
K rindex=-1 | M
```
Shift shared items in LHS with rindex >= 0 to ensure rindex is monotonically increasing
```
LHS | RHS
-----|------
A rindex=-1 | Bpre
B rindex=1 | B
C rindex=-1 | Bpost
D rindex=-1 | Hpre
F rindex=-1 | H
G rindex=-1 | Hpost
H rindex=4 | Epre
E rindex=7 | E
I rindex=-1 | Epost
J rindex=-1 | L
K rindex=-1 | M
```
Merge LHS and RHS (iterator vars li and ri):
- If LHS is not empty and LHS[li].rindex == -1, take LHS[li] and increment li
- If LHS is not empty and LHS[li].rindex == ri, take RHS[ri] and increment li and ri
- If RHS is not empty, take RHS[ri] and increment ri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment