-
Notifications
You must be signed in to change notification settings - Fork 0
2087_MinimumCostHomecomingofaRobotinaGrid
a920604a edited this page Apr 14, 2023
·
1 revision
title: 2087. Minimum Cost Homecoming of a Robot in a Grid
tags:
- Greedy
categories: leetcode
comments: false
class Solution {
public:
int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {
int ret = 0;
int i = startPos[0], j = startPos[1], x = homePos[0], y = homePos[1];
if(i<x){
for(int c = i+1 ; c<=x;++c) ret+=rowCosts[c];
}
else if(i>x){
for(int c = i-1;c>=x;--c) ret+=rowCosts[c];
}
if(j<y){
for(int c = j+1;c<=y;++c) ret+=colCosts[c];
}
else if(j>y){
for(int c = j-1;c>=y;--c) ret+=colCosts[c];
}
return ret;
}
};
- time complexity
O(nm)
- space complexity
O(1)
footer