[AlgoExpert] Ambiguous Measurements

Ambiguous Measurements

  • Time : O(high high cups)
  • Space : O(high * high)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
using namespace std;

bool ambiguousMeasurements(vector<vector<int>> measuringCups, int low,
int high) {
vector<vector<bool>> dp(high + 1, vector<bool>(high + 1));
dp[0][0] = true;
for(int i = 0; i <= high; i++) {
for(int j = 0; j <= high; j++) {
if(!dp[i][j]) continue;
for(auto& c : measuringCups) {
int nl = i + c[0], nr = j + c[1];
if(low <= nl and nr <= high) return true;
if(nl <= high and nr <= high)
dp[nl][nr] = true;
}
}
}
return false;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/10/PS/AlgoExpert/ambiguous-measurements/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.