Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf30f2f

Browse files
authored
Sri Hari: Batch-5/Neetcode-ALL/Added-articles (neetcode-gh#3831)
* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles* Batch-5/Neetcode-ALL/Added-articles
1 parente4fc2bc commitaf30f2f

File tree

27 files changed

+12580
-2
lines changed

27 files changed

+12580
-2
lines changed

‎articles/132-pattern.md

Lines changed: 420 additions & 0 deletions
Large diffs are not rendered by default.

‎articles/check-if-move-is-legal.md

Lines changed: 298 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,298 @@
1+
##1. Iteration - I
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defcheckMove(self,board: List[List[str]],rMove:int,cMove:int,color:str) ->bool:
8+
ROWS,COLS=len(board),len(board[0])
9+
direction= [[1,0], [-1,0], [0,1], [0,-1],
10+
[1,1], [-1,-1], [1,-1], [-1,1]]
11+
12+
board[rMove][cMove]= color
13+
14+
deflegal(row,col,color,direc):
15+
dr, dc= direc
16+
row, col= row+ dr, col+ dc
17+
length=1
18+
19+
while0<= row<ROWSand0<= col<COLS:
20+
length+=1
21+
if board[row][col]==".":
22+
returnFalse
23+
if board[row][col]== color:
24+
return length>=3
25+
row, col= row+ dr, col+ dc
26+
returnFalse
27+
28+
for din direction:
29+
if legal(rMove, cMove, color, d):
30+
returnTrue
31+
returnFalse
32+
```
33+
34+
```java
35+
publicclassSolution {
36+
publicbooleancheckMove(char[][]board,intrMove,intcMove,charcolor) {
37+
intROWS= board.length,COLS= board[0].length;
38+
int[][] direction= {{1,0}, {-1,0}, {0,1}, {0,-1},
39+
{1,1}, {-1,-1}, {1,-1}, {-1,1}};
40+
41+
board[rMove][cMove]= color;
42+
43+
for (int[] d: direction) {
44+
if (legal(board, rMove, cMove, color, d)) {
45+
returntrue;
46+
}
47+
}
48+
returnfalse;
49+
}
50+
51+
privatebooleanlegal(char[][]board,introw,intcol,charcolor,int[]direc) {
52+
intROWS= board.length,COLS= board[0].length;
53+
int dr= direc[0], dc= direc[1];
54+
row+= dr;
55+
col+= dc;
56+
int length=1;
57+
58+
while (row>=0&& row<ROWS&& col>=0&& col<COLS) {
59+
length++;
60+
if (board[row][col]=='.') {
61+
returnfalse;
62+
}
63+
if (board[row][col]== color) {
64+
return length>=3;
65+
}
66+
row+= dr;
67+
col+= dc;
68+
}
69+
returnfalse;
70+
}
71+
}
72+
```
73+
74+
```cpp
75+
classSolution {
76+
public:
77+
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
78+
int ROWS = board.size(), COLS = board[0].size();
79+
vector<vector<int>> direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1},
80+
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
81+
82+
board[rMove][cMove] = color;
83+
84+
for (auto& d : direction) {
85+
if (legal(board, rMove, cMove, color, d)) {
86+
return true;
87+
}
88+
}
89+
returnfalse;
90+
}
91+
92+
private:
93+
boollegal(vector<vector<char>>& board, int row, int col, char color, vector<int>& direc) {
94+
int ROWS = board.size(), COLS = board[0].size();
95+
int dr = direc[0], dc = direc[1];
96+
row += dr;
97+
col += dc;
98+
int length = 1;
99+
100+
while (row >= 0 && row < ROWS && col >= 0 && col < COLS) {
101+
length++;
102+
if (board[row][col] == '.') {
103+
return false;
104+
}
105+
if (board[row][col] == color) {
106+
return length >= 3;
107+
}
108+
row += dr;
109+
col += dc;
110+
}
111+
return false;
112+
}
113+
};
114+
```
115+
116+
```javascript
117+
class Solution {
118+
/**
119+
* @param {character[][]} board
120+
* @param {number} rMove
121+
* @param {number} cMove
122+
* @param {character} color
123+
* @return {boolean}
124+
*/
125+
checkMove(board, rMove, cMove, color) {
126+
const ROWS = board.length, COLS = board[0].length;
127+
const direction = [[1, 0], [-1, 0], [0, 1], [0, -1],
128+
[1, 1], [-1, -1], [1, -1], [-1, 1]];
129+
130+
board[rMove][cMove] = color;
131+
132+
const legal = (row, col, color, [dr, dc]) => {
133+
row += dr;
134+
col += dc;
135+
let length = 1;
136+
137+
while (row >= 0 && row < ROWS && col >= 0 && col < COLS) {
138+
length++;
139+
if (board[row][col] === ".") {
140+
return false;
141+
}
142+
if (board[row][col] === color) {
143+
return length >= 3;
144+
}
145+
row += dr;
146+
col += dc;
147+
}
148+
return false;
149+
};
150+
151+
for (let d of direction) {
152+
if (legal(rMove, cMove, color, d)) {
153+
return true;
154+
}
155+
}
156+
return false;
157+
}
158+
}
159+
```
160+
161+
::tabs-end
162+
163+
###Time & Space Complexity
164+
165+
* Time complexity: $O(1)$
166+
* Space complexity: $O(1)$
167+
168+
---
169+
170+
##2. Iteration - II
171+
172+
::tabs-start
173+
174+
```python
175+
classSolution:
176+
defcheckMove(self,board: List[List[str]],rMove:int,cMove:int,color:str) ->bool:
177+
ROWS,COLS=len(board),len(board[0])
178+
direction= [0,1,0,-1,0,1,1,-1,-1,1]
179+
180+
board[rMove][cMove]= color
181+
182+
for dinrange(9):
183+
length=1
184+
row, col= rMove, cMove
185+
whileTrue:
186+
row+= direction[d]
187+
col+= direction[d+1]
188+
189+
if row<0or col<0or row>=ROWSor col>=COLSor board[row][col]==".":
190+
break
191+
if board[row][col]== color:
192+
if length>1:
193+
returnTrue
194+
break
195+
length+=1
196+
197+
returnFalse
198+
```
199+
200+
```java
201+
publicclassSolution {
202+
publicbooleancheckMove(char[][]board,intrMove,intcMove,charcolor) {
203+
intROWS= board.length,COLS= board[0].length;
204+
int[] direction= {0,1,0,-1,0,1,1,-1,-1,1};
205+
206+
board[rMove][cMove]= color;
207+
208+
for (int d=0; d<9; d++) {
209+
int row= rMove, col= cMove;
210+
for (int length=1; ;++length) {
211+
row+= direction[d];
212+
col+= direction[d+1];
213+
214+
if (row<0|| col<0|| row>=ROWS|| col>=COLS|| board[row][col]=='.')
215+
break;
216+
if (board[row][col]== color) {
217+
if (length>1)
218+
returntrue;
219+
break;
220+
}
221+
}
222+
}
223+
returnfalse;
224+
}
225+
}
226+
227+
```
228+
229+
```cpp
230+
classSolution {
231+
public:
232+
bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
233+
int ROWS = board.size(), COLS = board[0].size();
234+
int direction[10] = {0, 1, 0, -1, 0, 1, 1, -1, -1, 1};
235+
236+
board[rMove][cMove] = color;
237+
238+
for (int d = 0; d < 9; ++d) {
239+
int row = rMove, col = cMove;
240+
for (int length = 1; ; ++length) {
241+
row += direction[d];
242+
col += direction[d + 1];
243+
244+
if (row < 0 || col < 0 || row >= ROWS || col >= COLS || board[row][col] == '.')
245+
break;
246+
if (board[row][col] == color) {
247+
if (length > 1)
248+
return true;
249+
break;
250+
}
251+
}
252+
}
253+
returnfalse;
254+
}
255+
};
256+
```
257+
258+
```javascript
259+
classSolution {
260+
/**
261+
*@param{character[][]}board
262+
*@param{number}rMove
263+
*@param{number}cMove
264+
*@param{character}color
265+
*@return{boolean}
266+
*/
267+
checkMove(board,rMove,cMove,color) {
268+
constROWS=board.length,COLS= board[0].length;
269+
constdirection= [0,1,0,-1,0,1,1,-1,-1,1];
270+
271+
board[rMove][cMove]= color;
272+
273+
for (let d=0; d<9; d++) {
274+
let row= rMove, col= cMove;
275+
for (let length=1; ;++length) {
276+
row+= direction[d];
277+
col+= direction[d+1];
278+
279+
if (row<0|| col<0|| row>=ROWS|| col>=COLS|| board[row][col]==='.')
280+
break;
281+
if (board[row][col]=== color) {
282+
if (length>1)
283+
returntrue;
284+
break;
285+
}
286+
}
287+
}
288+
returnfalse;
289+
}
290+
}
291+
```
292+
293+
::tabs-end
294+
295+
###Time & Space Complexity
296+
297+
* Time complexity: $O(1)$
298+
* Space complexity: $O(1)$

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp