@@ -3,6 +3,144 @@ using namespace std;
33
44// https://leetcode.com/problems/maximal-square/description/?envType=problem-list-v2&envId=dynamic-programming
55
6+ namespace myIdeaButUsesIdeaFromBamboozledSolutionOfSides {
7+
8+ /*
9+ Yay, this is awesome.
10+ While, my idea works, even tho its different than the other one of overlapping
11+ squares. And also, no more checking, if size zero or if valid or if valid then
12+ sum up otherwise no, bla bla bla, all the ideas that can be noticed in the other
13+ algorithm implementations of this one.
14+ */
15+ static constexpr int L = 300 ;
16+ static int box[L][L];
17+ static int top[L][L];
18+ static int l[L][L];
19+
20+ class Solution {
21+ public:
22+ template <size_t R, size_t C>
23+ void init (int (&arr)[R][C], int val = 0) {
24+ for (size_t i = 0 ; i < R; ++i)
25+ for (size_t j = 0 ; j < C; ++j) arr[i][j] = val;
26+ }
27+ int maximalSquare (vector<vector<char >>& m) {
28+ init (box);
29+ init (top);
30+ init (l);
31+ int r = 0 ;
32+ for (int i = 0 ; i < m.size (); ++i) {
33+ int v = m[i][0 ] - ' 0' ;
34+ box[i][0 ] = v;
35+ l[i][0 ] = v;
36+ top[i][0 ] = v;
37+ r = max (r, v);
38+ }
39+ for (int i = 0 ; i < m[0 ].size (); ++i) {
40+ int v = m[0 ][i] - ' 0' ;
41+ box[0 ][i] = v;
42+ top[0 ][i] = v;
43+ l[0 ][i] = v;
44+ r = max (r, v);
45+ }
46+ for (int i = 1 ; i < m.size (); ++i) {
47+ for (int j = 1 ; j < m[0 ].size (); ++j) {
48+ if (m[i][j] != ' 0' ) {
49+ int v = 1 + min ({box[i - 1 ][j - 1 ], top[i - 1 ][j], l[i][j - 1 ]});
50+ box[i][j] = v;
51+ l[i][j] = v;
52+ top[i][j] = v;
53+ r = max (r, box[i][j]);
54+ }
55+ }
56+ }
57+
58+ return r * r;
59+ }
60+ };
61+
62+ } // namespace myIdeaButUsesIdeaFromBamboozledSolutionOfSides
63+
64+ namespace bamboozledWhyDoesItWork {
65+ /*
66+ Honestly, this solution beats all reason. Somehow i keep seing a pattern where
67+ DP solutions do not consider all cases **SOMEHOW** ...
68+
69+ I asked chatgpt to discuss, and he showed me this recurrence relation
70+ And I was like... Bamboozled... Like, what the fuck?
71+
72+ dp[i][j] = 1 + min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1])
73+
74+ Observe... Why does this even work? It simply just DOES cover this case... Just
75+ look!
76+
77+ 1 1 1 1
78+ 1 1 1 1
79+ 1 1 1 1
80+
81+ 00 01 02 03
82+ 10 11 12 13
83+ 20 21 22 23
84+ 30 31 32 33
85+
86+ 00 -> 1
87+ 01 -> 1
88+ 02 -> 1
89+ 03 -> 1
90+ 10 -> 1
91+
92+ 11 = 2 -
93+ - 00 = 1
94+ - 10 = 1
95+ - 01 = 1
96+ 12 = 2 -
97+ - 11 = 2
98+ - 01 = 1
99+ - 02 = 1
100+
101+ Where, number 2 represent the side 1s
102+ Why does it work?
103+ Not combinatorial?
104+ Somehow this unintuitive numbering covers all the cases
105+
106+ */
107+ static constexpr int L = 300 ;
108+ static int dp[L][L];
109+ class Solution {
110+ public:
111+ int maximalSquare (vector<vector<char >>& m) {
112+ for (int i = 0 ; i < L; ++i) {
113+ for (int j = 0 ; j < L; ++j) {
114+ dp[i][j] = 0 ;
115+ }
116+ }
117+
118+ int r = 0 ;
119+
120+ for (int i = 0 ; i < m[0 ].size (); ++i) {
121+ dp[0 ][i] = m[0 ][i] - ' 0' ;
122+ r = max (r, dp[0 ][i]);
123+ }
124+ for (int i = 0 ; i < m.size (); ++i) {
125+ dp[i][0 ] = m[i][0 ] - ' 0' ;
126+ r = max (r, dp[i][0 ]);
127+ }
128+
129+ for (int i = 1 ; i < m.size (); ++i) {
130+ for (int j = 1 ; j < m[0 ].size (); ++j) {
131+ if (m[i][j] == ' 1' ) {
132+ dp[i][j] = 1 + min ({dp[i - 1 ][j - 1 ], dp[i - 1 ][j], dp[i][j - 1 ]});
133+ r = max (r, dp[i][j]);
134+ }
135+ }
136+ }
137+
138+ return r * r;
139+ }
140+ };
141+
142+ } // namespace bamboozledWhyDoesItWork
143+
6144/*
7145
81463 - 6
@@ -40,10 +178,9 @@ namespace topDownNMKK_TLE {
40178
41179static const int LEN = 300 ;
42180static int dp[LEN][LEN][LEN];
43- // Still not good enough because of the loop inside making the algorithm O(n*m*k*k)
44- // Gotta try again
45- // If bottom up model of this didn't work
46- // Why did i even think a top down would lol
181+ // Still not good enough because of the loop inside making the algorithm
182+ // O(n*m*k*k) Gotta try again If bottom up model of this didn't work Why did i
183+ // even think a top down would lol
47184class Solution {
48185 public:
49186 vector<vector<char >> m;
0 commit comments