LeetCode, GeeksForGeeks Problem of the day solution
前往频道在 Telegram
Complete daily challenges from LeetCode, GeeksForGeeks and redeem their rewards Channel link : https://t.me/leetcode_gfg_potd
显示更多1 250
订阅者
+224 小时
+147 天
+2930 天
帖子存档
class Solution {
public:
int dp[2000][2000];
bool f(int ind, int k, unordered_map &mp, int target, vector &stones){
if(ind==target) return true;
if(dp[ind][k]!=-1) return dp[ind][k];
bool res = false;
if(k>1 && mp[stones[ind]+k-1]){
res |= f(mp[stones[ind]+k-1],k-1,mp,target,stones);
}
if(k && mp[stones[ind]+k]){
res |= f(mp[stones[ind]+k],k,mp,target,stones);
}
if(mp[stones[ind]+k+1]){
res |= f(mp[stones[ind]+k+1],k+1,mp,target,stones);
}
return dp[ind][k] = res;
}
bool canCross(vector& stones) {
unordered_map mp;
memset(dp,-1,sizeof(dp));
for(int i=0; i
class Solution
{
public:
string reverseWord(string str)
{
int s = 0;
int e = str.size()-1;
while(s
class Solution {
public:
static bool comp(vector&v1,vector&v2)
{
return v1[1]>& pairs) {
sort(pairs.begin(),pairs.end(),comp);
int sm=pairs[0][1];
int ct=1;
for(int i=1;i
class Solution{
public:
int longestKSubstr(string s, int k) {
int maxi = -1e9;
unordered_map mp;
int n = s.size();
int i=0;
for(int j=0;jk){
while(mp.size()>k){
mp[s[i]]--;
if(mp[s[i]]==0){
mp.erase(s[i]);
}
i++;
}
}
}
if(maxi==-1e9){
return -1;
}
return maxi;
}
};
class Solution {
public:
bool check(string &s1,string &s2,string &s3,int i,int j,int k,vector>&dp){
if(dp[i][j]!=-1)return dp[i][j];
if(i==s1.length() and j==s2.length() and k==s3.length())
return true;
bool res=false;
if(i!=s1.length())
if(s1[i]==s3[k])
res|=check(s1,s2,s3,i+1,j,k+1,dp);
if(j!=s2.length())
if(s2[j]==s3[k])
res|=check(s1,s2,s3,i,j+1,k+1,dp);
return dp[i][j]=res;
}
bool isInterleave(string s1, string s2, string s3) {
int n=s1.length(),m=s2.length();
vector> dp(n+1,vector(m+1,-1));
bool ans=check(s1,s2,s3,0,0,0,dp);
return ans;
}
};
class Solution{
public:
int isPalindrome(string S)
{
string S2 = S;
reverse(S.begin(), S.end());
return S==S2;
}
};
class Solution {
public:
vector fullJustify(vector& words, int maxWidth) {
int n = words.size();
vector ans;
int i=0;
while(i0){
line+=" ";
extra--;
}
line+=words[k];
}
}
ans.push_back(line);
i=j;
}
return ans;
}
};
class Solution{
public:
/*You are required to complete below function */
string multiplyStrings(string s1, string s2) {
if (s1 == "0" || s2 == "0")
return "0";
int sign = 1;
if (s1[0] == '-') {
sign *= -1;
s1 = s1.substr(1);
}
if (s2[0] == '-') {
sign *= -1;
s2 = s2.substr(1);
}
string ans(s1.length() + s2.length(), '0');
for (int i = s2.length() - 1; i >= 0; i--) {
int carry = 0;
for (int j = s1.length() - 1; j >= 0; j--) {
int product = (s1[j] - '0') * (s2[i] - '0') + (ans[i + j + 1] - '0') + carry;
carry = product / 10;
ans[i + j + 1] = (product % 10) + '0';
}
ans[i] += carry;
}
int start = 0;
while (start < ans.length() - 1 && ans[start] == '0') {
start++;
}
string result = ans.substr(start);
if (sign == -1 && result != "0") {
result = "-" + result;
}
return result;
}
};
class Solution {
public:
string reorganizeString(string s) {
priority_queue<pair<int,char>>q;
int dp[26] = {0};
for(int i=0;i<s.size();i++){
dp[s[i]-'a']++;
}
for(int i=0;i<26;i++){
char t = (char)('a'+i);
if(dp[i]>0){
//cout<<dp[i]<<endl;
q.push({dp[i],t});
}
}
string ans = "";
while(q.size()>1){
auto t1 = q.top();
q.pop();
auto t2 = q.top();
q.pop();
ans += t1.second;
ans += t2.second;
if(t1.first>1){
q.push({t1.first-1,t1.second});
}
if(t2.first>1){
q.push({t2.first-1,t2.second});
}
}
if(q.size()==1){
auto t = q.top();
q.pop();
if(t.first>1){
return "";
}
if(ans.size()==0){
ans += t.second;
}
else if(ans[ans.size()-1]!=t.second){
ans += t.second;
}
else if(ans[0]!=t.second){
ans = t.second + ans;
}
else{
return "";
}
}
return ans;
}
};
class Solution {
public:
bool check(vector> &grid,int dr[],int dc[],int &n,int &m,int x,int y, string &word,int &k){
if(grid[x][y]!=word[0])return false;
for(int p=0;p<8;p++){
int f=1;
for(int i=1;i=0 && ny>=0 && nx>searchWord(vector>grid, string word){
// Code here
int n=grid.size(),m=grid[0].size(),k=word.size();
vector> ans;
int dr[]={0,1,0,-1,1,1,-1,-1};
int dc[]={1,0,-1,0,1,-1,1,-1};
for(int i=0;i
