classSolution { public: intmaxDistance(vector<int>& c){ unordered_map<int, int> mp; int n = c.size(); int ans = 0; for (int i = 0; i < n; i ++ ) { int col = c[i]; for (auto& [cc, idx] : mp) { if (cc != col) ans = max(ans, i - idx); } if (mp.count(col) == 0) mp[col] = i; } return ans; } };
classSolution { public: intwateringPlants(vector<int>& nums, int cap){ int n = nums.size(); // all : 当前剩余 int ret = 0, all = cap; for (int i = 0; i < n; i ++ ) { int cur = nums[i]; if (all >= cur) { all -= cur; ret += 1; } else { all = cap; ret += i + i + 1; all -= cur; } } return ret; } };
classRangeFreqQuery { public: unordered_map<int, vector<int>> mp; RangeFreqQuery(vector<int>& arr) { int n = arr.size(); for (int i = 0; i < n; i ++ ) mp[arr[i]].push_back(i); } intquery(int l, int r, int val){ auto L = lower_bound(mp[val].begin(), mp[val].end(), l) - mp[val].begin(); auto R = upper_bound(mp[val].begin(), mp[val].end(), r) - mp[val].begin(); return R - L; } };
/** * Your RangeFreqQuery object will be instantiated and called as such: * RangeFreqQuery* obj = new RangeFreqQuery(arr); * int param_1 = obj->query(left,right,value); */
constint M = 30; using LL = longlong; vector<vector<LL>> nums;
classSolution { public: voiddfs(int len, int cur, string& num){ int R = (len + 1) / 2; if (cur == R + 1) { string ss = num; if (len & 1) { for (int idx = R - 1; idx >= 1; idx -- ) ss.push_back(num[idx - 1]); } else { for (int idx = R; idx >= 1; idx -- ) ss.push_back(num[idx - 1]); } LL val = stoll(ss); for (int k = 2; k <= 9; k ++ ) { if (nums[k].size() == M) continue; LL cv = val; string s; while (cv) { s.push_back(char(cv % k + '0')); cv /= k; } string rs = s; reverse(rs.begin(), rs.end()); if (s == rs and rs[0] != '0') nums[k].push_back(val); } return ; } for (int i = 0; i <= 9; i ++ ) { num.push_back(char(i + '0')); dfs(len, cur + 1, num); num.pop_back(); } } voidinit(){ if (nums.size()) return ; nums.resize(10); for (int len = 1; len <= 12; len ++ ) { for (int i = 1; i <= 9; i ++ ) { string s = to_string(i); dfs(len, 2, s); } } } longlongkMirror(int k, int n){ init(); LL ret = 0ll; for (int i = 0; i < n; i ++ ) ret += nums[k][i]; return ret; } };