classSubrectangleQueries { vector<vector<int>> rect; int rows, cols; public: SubrectangleQueries(vector<vector<int>>& rectangle) : rect(move(rectangle)){ rows = rect.size(); cols = rect[0].size(); } voidupdateSubrectangle(int row1, int col1, int row2, int col2, int newValue){ for (int i = row1; i <= row2; ++i) { for (int j = col1; j <= col2; ++j) { rect[i][j] = newValue; } } } intgetValue(int row, int col){ return rect[row][col]; } };
/** * Your SubrectangleQueries object will be instantiated and called as such: * SubrectangleQueries* obj = new SubrectangleQueries(rectangle); * obj->updateSubrectangle(row1,col1,row2,col2,newValue); * int param_2 = obj->getValue(row,col); */
classSolution { constint INF = 0x3f3f3f3f; public: intminSumOfLengths(vector<int>& arr, int target){ int ans = INF; constint n = arr.size(); vector<int> best_till(n, INF); unordered_map<int, int> presum; presum[0] = -1; int sum = 0; for (int i = 0; i < arr.size(); ++i) { sum += arr[i]; auto it = presum.find(sum - target); if (it != presum.end()) { int left = it->second; int length = i - left; if (left >= 0) ans = min(ans, best_till[left] + length); if (i > 0) best_till[i] = min(best_till[i-1], length); else best_till[i] = length; } else { if (i > 0) best_till[i] = best_till[i-1]; } presum[sum] = i; } return ans >= INF ? -1 : ans; } };
classSolution { constint INF = 0x3f3f3f3f; public: intminDistance(vector<int>& houses, int kk){ constint n = houses.size(); sort(houses.begin(), houses.end()); map<pair<int,int>, int> memo_range; auto range = [&](int left, int right) -> int { auto it = memo_range.find({left, right}); if (it != memo_range.end()) return it->second; else { int ans = 0; int l = left, r = right; while (l < r) { ans += houses[r] - houses[l]; ++l; --r; } return memo_range[{left, right}] = ans; } }; map<pair<int,int>, int> memo_dp; function<int(int,int)> dp = [&](int i, int k) -> int { auto it = memo_dp.find({i, k}); if (it != memo_dp.end()) return it->second; else { if (k == 1) { int ans = range(i, n - 1); // cout << "k == 1 " << i << " " << k << " " << ans << endl; return memo_dp[{i, k}] = ans; } elseif (n - i <= k) { return0; } elseif (i >= n) { return0; } else { int ans = INF; // n - ni >= k - 1 for (int ni = i + 1; ni <= n; ++ni) { // if (i == 2) { // cout << "2 :: " << ni << " "; // cout << range(i, ni - 1) << endl; // } ans = min(ans, range(i, ni - 1) + dp(ni, k - 1)); } // cout << i << " " << k << " " << ans << endl; return memo_dp[{i, k}] = ans; } } }; returndp(0, kk); } };