leetcode

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

commit 2714655c4c0d7d17f4324320836901b51f1ebfe8
parent 2fc6ad699580a8e52b5b46eafa3816ecf4391536
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat, 31 Aug 2024 10:28:27 +0200

1 Random Problem

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

diff --git a/Problems/2087.cpp b/Problems/2087.cpp @@ -0,0 +1,21 @@ +class Solution { + public: + int minCost(const vector<int> &startPos, const vector<int> &homePos, const vector<int> &rowCosts, + const vector<int> &colCosts) const { + int i = startPos[0], j = startPos[1]; + int x = homePos[0], y = homePos[1]; + int res = 0; + + while (i != x) { + i += i < x ? 1 : -1; + res += rowCosts[i]; + } + + while (j != y) { + j += j < y ? 1 : -1; + res += colCosts[j]; + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -1081,6 +1081,7 @@ for solving problems. | 2074 | Medium | [Reverse Nodes in Even Length Groups](Problems/2074.cpp) | | 2079 | Medium | [Watering Plants](Problems/2079.cpp) | | 2085 | Easy | [Count Common Words With One Occurrence](Problems/2085.cpp) | +| 2087 | Medium | [Minimum Cost Homecoming of a Robot in a Grid](Problems/2087.cpp) | | 2090 | Medium | [K Radius Subarray Averages](Problems/2090.cpp) | | 2091 | Medium | [Removing Minimum and Maximum From Array](Problems/2091.cpp) | | 2092 | Hard | [Find All People With Secret](Problems/2092.cpp) |