byvoid

2.7万

l_y_f

carson
benno0812
sp
Shmilysw
lijiale
@_55
xkkl

ailu
THYLZ
zhangxc

charley123
Mr.Lonely

byvoid
1小时前
class Solution {
public:
int removeCoveredIntervals(vector<vector<int>>& arr) {
sort(arr.begin(),arr.end(),[](auto &a,auto &b){
if (a[0]!=b[0]) return a[0]<b[0];
else return a[1]>b[1];
});
int ans=0,maxv=0;
for (auto &ar:arr){
if (ar[1]>maxv) ans++,maxv=ar[1];
}
return ans;
}
};


byvoid
15小时前
//这里填你的代码^^
//注意代码要放在两组三个点之间，才可以正确显示代码高亮哦~

class Solution {
public:
bool check(vector<int>& arr,int num){
int n=arr.size(),l=0,r=n-1;
while (l<r){
int m=l+r>>1;
if (arr[m]>=num) r=m;
else l=m+1;
}
int left=l;
l=0,r=n-1;
while (l<r){
int m=l+r+1>>1;
if (arr[m]<=num) l=m;
else r=m-1;
}
return l-left+1>n/4;
}
int findSpecialInteger(vector<int>& arr) {
int n=arr.size(),m=(n+3)/4;
for (int i=0;i<4;i++)
if (check(arr,arr[i*m])) return arr[i*m];
return -1;
}
};


byvoid
1天前
class CombinationIterator {
public:
string s;
int cur=0,n,m;
CombinationIterator(string characters, int combinationLength) {
s=characters;
n=s.size();m=combinationLength;
for (int i=0;i<m;i++) cur|=1<<i;
}

string next() {
string ans;
for (int i=0;i<n;i++)
if (cur>>i&1) ans+=s[i];
int k=n-1;
while (k>=0 && (cur>>k&1)) k--;
if (n-1-k==m) cur=-1;
else{
int cnt=n-1-k;
for (int i=k+1;i<n;i++) cur-=1<<i;
while (k>=0 && !(cur>>k&1)) k--;
cur-=1<<k;
for (int i=k+1,j=0;j<cnt+1;i++,j++) cur+=1<<i;
}
return ans;
}

bool hasNext() {
return cur!=-1;
}
};


byvoid
3天前
class Solution {
public:
vector<vector<int>> g;
int n,m;
int dx[5]={0,1,0,-1,0},dy[5]={1,0,-1,0,0};
void turn(int x,int y){
for (int i=0;i<5;i++){
int nx=x+dx[i],ny=y+dy[i];
if (nx>=0 && nx<n && ny>=0 && ny<m){
if (g[nx][ny]==1) g[nx][ny]=0;
else g[nx][ny]=1;
}
}
}
int minFlips(vector<vector<int>>& mat) {
int ans=1e9;
n=mat.size();
m=mat[0].size();
for (int op=0;op<1<<m;op++){
g=mat;
int cur=0;
for (int i=0;i<m;i++)
if (op>>i&1){
turn(0,i);
cur++;
}
for (int i=0;i<n-1;i++)
for (int j=0;j<m;j++)
if (g[i][j]==1){
turn(i+1,j);
cur++;
}
bool flag=true;
for (int i=0;i<m;i++)
if (g[n-1][i]==1) flag=false;
if (flag) ans=min(ans,cur);
}
if (ans==1e9) return -1;
return ans;
}
};


byvoid
4天前
class Solution {
public:
int smallestDivisor(vector<int>& nums, int threshold) {
int l=1,r=1e9;
while (l<r){
int m=l+r>>1;
int ans=0;
for (auto &num:nums) ans+=(num+m-1)/m;
if (ans<=threshold) r=m;
else l=m+1;
}
return l;
}
};


byvoid
4天前
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
unordered_map<int,vector<vector<int>>> ans;
for (int i=0;i<groupSizes.size();i++){
auto& t=ans[groupSizes[i]];
if (t.size()==0) t.push_back(vector<int>());
if (t.back().size()==groupSizes[i]) t.push_back(vector<int>());
t[t.size()-1].push_back(i);
}
vector<vector<int>> ret;
for (auto &[a,b]:ans){
for (auto &bb:b) ret.push_back(bb);
}
return ret;
}
};


byvoid
4天前
class Solution {
public:
int subtractProductAndSum(int n) {
int sum=0,mul=1;
while (n){
sum+=n%10;
mul*=n%10;
n/=10;
}
return mul-sum;
}
};


byvoid
4天前
class Solution {
public:
int palindromePartition(string s, int k) {
int n=s.size();
vector<vector<int>> costs(n,vector<int>(n));
for (int len=2;len<=n;len++)
for (int i=0;i+len-1<n;i++){
int j=i+len-1;
if (s[i]==s[j]) costs[i][j]=costs[i+1][j-1];
else costs[i][j]=costs[i+1][j-1]+1;
}
vector<vector<int>> f(n,vector<int>(k+1,1e9));
for (int i=0;i<n;i++){
f[i][1]=costs[0][i];
for (int j=2;j<=k;j++)
for (int t=i-1;t>=0;t--){
f[i][j]=min(f[i][j],f[t][j-1]+costs[t+1][i]);
}
}
return f[n-1][k];
}
};


byvoid
5天前
class Solution {
public:
int countSquares(vector<vector<int>>& arr) {
int n=arr.size(),m=arr[0].size();
vector<vector<int>> f(n+1,vector<int>(m+1));
int ans=0;
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
if (arr[i][j]){
f[i+1][j+1]=1+min({f[i][j+1],f[i+1][j],f[i][j]});
ans+=f[i+1][j+1];
}
return ans;
}
};


byvoid
5天前
class Solution {
public:
vector<int> numOfBurgers(int a, int b) {
//4x+2y=a;x+y=b;
//2x=a-2*b;
//x=a/2-b; y=b*2-a/2;
if (a%2) return {};
if (a/2-b<0 || b*2-a/2<0) return {};
return {a/2-b,b*2-a/2};
}
};