leetcode

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

2059.cpp (1247B)


0 class Solution { 1 public: 2 int minimumOperations(const vector<int> &nums, int start, int goal) const { 3 bool seen[1001] = {0}; 4 queue<int> q; 5 6 seen[start] = true; 7 q.emplace(start); 8 9 for (int lvl = 1; !q.empty(); lvl++) { 10 for (int l = size(q); l > 0; l--) { 11 const int crnt = q.front(); 12 q.pop(); 13 14 for (const int n : nums) { 15 const int x = crnt ^ n; 16 if (x == goal) return lvl; 17 if (x >= 0 && x <= 1000 && !seen[x]) { 18 seen[x] = true; 19 q.emplace(x); 20 } 21 22 const int y = crnt + n; 23 if (y == goal) return lvl; 24 if (y >= 0 && y <= 1000 && !seen[y]) { 25 seen[y] = true; 26 q.emplace(y); 27 } 28 29 const int z = crnt - n; 30 if (z == goal) return lvl; 31 if (z >= 0 && z <= 1000 && !seen[z]) { 32 seen[z] = true; 33 q.emplace(z); 34 } 35 } 36 } 37 } 38 39 return -1; 40 } 41 };