100243. 将元素分配到两个数组中 I
-
题意- 思路
直接看代码 -
代码
class Solution {
public:vector<int> resultArray(vector<int>& nums) {vector<int> arr1,arr2;arr1.push_back(nums[0]);arr2.push_back(nums[1]);for (int i = 2;i < nums.size();i ++) {if (arr1[arr1.size()-1] > arr2[arr2.size()-1]) arr1.push_back(nums[i]);else arr2.push_back(nums[i]);}vector<int> result;for (auto &a:arr1)result.push_back(a);for (auto &b:arr2) result.push_back(b);return result;}
};
100237. 元素和小于等于 k 的子矩阵的数目
-
题意
-
思路
二维前缀和 -
代码
class Solution {
public:int countSubmatrices(vector<vector<int>>& grid, int k) {vector<vector<int>> grids;int m = grid.size();int n = grid[0].size();grids.resize(m);for (int i = 0;i < m;i ++)grids[i].resize(n);grids[0][0] = grid[0][0];for (int i = 1;i < n;i ++)grids[0][i] += grids[0][i-1] + grid[0][i];for (int i = 1;i < m;i ++)grids[i][0] += grids[i-1][0] + grid[i][0];for (int i = 1;i < m;i ++) for (int j = 1;j < n;j ++) grids[i][j] = grids[i-1][j] + grids[i][j-1] + grid[i][j] - grids[i-1][j-1];int sum = 0;for (int i = 0;i < m;i ++) for (int j = 0;j < n;j ++) if (grids[i][j] <= k)sum ++;return sum;}
};
100234. 在矩阵上写出字母 Y 所需的最少操作次数
-
题意
-
思路
枚举每一种可能
-
代码
class Solution {
public:int minimumOperationsToWriteY(vector<vector<int>>& grid) {int n = grid.size();int y[3] = {0};int _y[3] = {0};for (int i = 0;i < n;i ++) for (int j = 0;j < n;j ++){if (grid[i][j] == 0) _y[0] ++;if (grid[i][j] == 1) _y[1] ++;if (grid[i][j] == 2) _y[2] ++;if (grid[i][j] == 0 && (((i<=n/2)&&(i == j||j+i==n-1))||(i>n/2&&j == n/2))) y[0]++;if (grid[i][j] == 1 && (((i<=n/2)&&(i == j||j+i==n-1))||(i>n/2&&j == n/2))) y[1]++;if (grid[i][j] == 2 && (((i<=n/2)&&(i == j||j+i==n-1))||(i>n/2&&j == n/2))) y[2]++;}_y[0] -= y[0];_y[1] -= y[1];_y[2] -= y[2];// return y[0]// return y[1];// return y[2];int ans = n*n;for (int i = 0;i < 3;i ++) for (int j = 0;j < 3;j ++) {if (i == j) continue;int tans = n+n/2-y[i];tans += (n*n-n-n/2) - _y[j];if (tans < ans) ans = tans;}return ans;}
};
100246. 将元素分配到两个数组中2
-
题意
-
思路
离散化+树状数组 -
代码
class Solution {
public:int n;vector<int> num1,bit1,bit2;vector<int> resultArray(vector<int>& nums) {n = nums.size();vector<int> num1;num1.resize(nums.size());num1 = nums;sort(num1.begin(),num1.end());map<int,int> mp,mp2;int t = 1;mp[num1[0]] = 1;mp2[1] = num1[0];for (int i = 1;i < n;i ++){if (num1[i] == num1[i-1]) {mp[num1[i]] = t;mp2[t] = num1[i];}else {t = i+1;mp[num1[i]] = t;mp2[t] = num1[i];}}for (int i = 0;i < n;i ++) num1[i] = mp[nums[i]];this->num1.resize(n+1);bit1.resize(n+1);bit2.resize(n+1);vector<int> arr1,arr2;n = num1.size();arr1.push_back(num1[0]);add1(num1[0],1);arr2.push_back(num1[1]);add2(num1[1],1);// return arr2;for (int i = 2;i < n;i ++) {if (arr1.size()-sum1(num1[i]) > arr2.size()-sum2(num1[i])) {arr1.push_back(num1[i]);add1(num1[i],1);}else if (arr1.size()-sum1(num1[i]) < arr2.size()-sum2(num1[i])) {arr2.push_back(num1[i]);add2(num1[i],1);} else if (arr1.size()-sum1(num1[i]) == arr2.size()-sum2(num1[i])) {if (arr1.size() <= arr2.size()) {arr1.push_back(num1[i]);add1(num1[i],1);}else if (arr1.size() > arr2.size()) {arr2.push_back(num1[i]);add2(num1[i],1);}}}vector<int> result;for (auto a:arr1) result.push_back(mp2[a]);for (auto b:arr2) result.push_back(mp2[b]);return result;}int lowbit(int x) {return x & -x;}void add1(int x, int v) {for (int i = x; i <= n; i += lowbit(i)) {bit1[i] += v;}}int sum1(int x) {int ret = 0;for (int i = x; i > 0; i -= lowbit(i)) {ret += bit1[i];}return ret;}void add2(int x, int v) {for (int i = x; i <= n; i += lowbit(i)) {bit2[i] += v;}}int sum2(int x) {int ret = 0;for (int i = x; i > 0; i -= lowbit(i)) {ret += bit2[i];}return ret;}
};