Last active
January 3, 2026 22:23
-
-
Save D-Lite/1da3cfb30911cfe5c583a39a805ea49a to your computer and use it in GitHub Desktop.
Jan 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Dynamic dp problem. I had the base case in mind, but the loop required some mathematics. | |
| Watching this page over and over again did the magic: https://www.youtube.com/watch?v=W-WvItFiWX8 | |
| impl Solution { | |
| pub fn num_of_ways(n: i32) -> i32 { | |
| let modu = 1_000_000_007i64; | |
| if n == 1 { | |
| return 12; | |
| } | |
| // base case | |
| let mut abc = 6i64; | |
| let mut aba = 6i64; | |
| for _ in 1..n { | |
| let next_aba = (3 * aba + 2 * abc) % modu; | |
| let next_abc = (2 * aba + 2 * abc) % modu; | |
| aba = next_aba; | |
| abc = next_abc; | |
| } | |
| ((aba + abc) % modu) as i32 | |
| } | |
| } | |
| I was initially hitting an error via this line: let modu = 1_000_000_007; due to integer overflow with big numbers. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment