leetcode

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

commit f40d86e308ee006d1434917d4bc1bb5490a59208
parent e1f1ff2d2d34069c7df87c656f691c7c61dcded3
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Tue, 19 Dec 2023 20:28:14 +0000

Daily Problem and 1 Random Problem

Diffstat:
AProblems/0224.cpp | 23+++++++++++++++++++++++
AProblems/0661.cpp | 25+++++++++++++++++++++++++
MREADME.md | 4+++-
3 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/Problems/0224.cpp b/Problems/0224.cpp @@ -0,0 +1,23 @@ +class Solution { + public: + int calculate(const string &s) const { + int res = 0; + vector<int> signs(2, 1); + for (int i = 0; i < s.size(); i++) { + if (s[i] >= '0') { + int number = 0; + while (i < s.size() && isdigit(s[i])) { + number = 10 * number + (s[i++] - '0'); + } + res += signs.back() * number; + signs.pop_back(); + i--; + } else if (s[i] == '(' && s[i + 1] == '-') { + } else if (s[i] == ')') + signs.pop_back(); + else if (s[i] != ' ') + signs.push_back(signs.back() * (s[i] == '-' ? -1 : 1)); + } + return res; + } +}; diff --git a/Problems/0661.cpp b/Problems/0661.cpp @@ -0,0 +1,25 @@ +class Solution { + public: + vector<vector<int>> imageSmoother(vector<vector<int>> &img) const { + const int n = img.size() - 1; + const int m = img[0].size() - 1; + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= m; j++) { + int sum = 0, count = 0; + for (int k = max(i - 1, 0); k <= min(i + 1, n); k++) { + for (int l = max(j - 1, 0); l <= min(j + 1, m); l++) { + sum += img[k][l] & 0xFF; + count++; + } + } + img[i][j] |= (sum / count) << 8; + } + } + for (int i = 0; i <= n; i++) { + for (int j = 0; j <= m; j++) { + img[i][j] >>= 8; + } + } + return img; + } +}; diff --git a/README.md b/README.md @@ -222,6 +222,7 @@ for solving problems. | 0221 | Medium | [Maximal Square](Problems/0221.cpp) | | 0222 | Medium | [Count Complete Tree Nodes](Problems/0222.cpp) | | 0223 | Medium | [Rectangle Area](Problems/0223.cpp) | +| 0224 | Hard | [Basic Calculator](Problems/0224.cpp) | | 0225 | Easy | [Implement Stack using Queues](Problems/0225.cpp) | | 0226 | Easy | [Invert Binary Tree](Problems/0226.cpp) | | 0227 | Medium | [Basic Calculator II](Problems/0227.cpp) | @@ -404,6 +405,7 @@ for solving problems. | 0653 | Easy | [Two Sum IV - Input is a BST](Problems/0653.cpp) | | 0654 | Medium | [Maximum Binary Tree](Problems/0654.cpp) | | 0655 | Medium | [Print Binary Tree](Problems/0655.cpp) | +| 0661 | Easy | [Image Smoother](Problems/0661.cpp) | | 0662 | Medium | [Maximum Width of Binary Tree](Problems/0662.cpp) | | 0664 | Hard | [Strange Printer](Problems/0664.cpp) | | 0669 | Medium | [Trim a Binary Search Tree](Problems/0669.cpp) | @@ -853,7 +855,7 @@ for solving problems. | 1905 | Medium | [Count Sub Islands](Problems/1905.cpp) | | 1907 | Medium | [Count Salary Categories](Problems/1907.cpp) | | 1910 | Medium | [Remove All Occurrences of a Substring](Problems/1910.cpp) | -| 1913 | Medium | [Maximum Product Difference Between Two Pairs](Problems/1913.cpp) | +| 1913 | Easy | [Maximum Product Difference Between Two Pairs](Problems/1913.cpp) | | 1921 | Medium | [Eliminate Maximum Number of Monsters](Problems/1921.cpp) | | 1926 | Medium | [Nearest Exit from Entrance in Maze](Problems/1926.cpp) | | 1930 | Medium | [Unique Length-3 Palindromic Subsequences](Problems/1930.cpp) |