leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit d6d35d89f594a418c43e6453792331236d015bcf
parent 648abcc77ab3d40dbf5a0052389bbf91b57223ae
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 20 Jul 2023 15:30:14 +0200

Random Problem

Diffstat:
AProblems/0289.cpp | 22++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/Problems/0289.cpp b/Problems/0289.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + void gameOfLife(vector<vector<int>> &board) { + int n = board.size(), m = board[0].size(); + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + int add = (board[i][j] & 1) << 1; + for (int k = max(i - 1, 0); k <= min(n - 1, i + 1); k++) { + for (int l = max(j - 1, 0); l <= min(m - 1, j + 1); l++) { + board[k][l] += add; + } + } + } + } + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + board[i][j] = board[i][j] == 7 || board[i][j] == 9 || board[i][j] == 6; + } + } + } +}; diff --git a/README.md b/README.md @@ -222,6 +222,7 @@ for solving problems. | 0279 | Medium | [Perfect Squares](Problems/0279.cpp) | | 0283 | Easy | [Move Zeroes](Problems/0283.cpp) | | 0287 | Medium | [Find the Duplicate Number](Problems/0287.cpp) | +| 0289 | Medium | [Game of Life](Problems/0289.cpp) | | 0290 | Easy | [Word Pattern](Problems/0290.cpp) | | 0292 | Easy | [Nim Game](Problems/0292.cpp) | | 0295 | Hard | [Find Median from Data Stream](Problems/0295.cpp) |