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:
vector> groupThePeople(vector& groupSizes) {
unordered_map> groupMap;
vector> ans;
for (int i = 0; i < groupSizes.size(); i++) {
groupMap[groupSizes[i]].push_back(i);
}
for (auto entry : groupMap) {
int i = 0;
while (i < entry.second.size()) {
vector curr;
for (int j = 0; j < entry.first; j++) {
curr.push_back(entry.second[j]);
i++;
}
ans.push_back(curr);
}
}
return ans;
}
};
class Solution{
public:
bool isLucky(int n) {
int map = n,counter = 2;
while(map>=counter){
if(map%counter==0)return 0;
map = (map/counter)*(counter-1) + map%counter;
counter++;
}
return true;
}
};
class Solution {
public:
int countOrders(int n) {
long res = 1, mod = 1e9 + 7;
for (int i = 1; i <= n; ++i)
res = res * (i * 2 - 1) * i % mod;
return res;
}
};
class Solution
{
public:
Node* insert(Node* node, int data) {
// Your code goes here
Node* temp=new Node(data);
Node* parent=NULL;
Node* curr=node;
while(curr!=NULL)
{
parent=curr;
if(curr->data>data)
{
curr=curr->left;
}
else if(curr->dataright;
}
else
return node;
}
if(parent==NULL)
{
return temp;
}
else if(parent->data>data)
{
parent->left=temp;
}
else
{
parent->right=temp;
}
return node;
}
};
class Solution {
public:
int combinationSum4(vector& nums, int target) {
vector dp(target+1, 0);
dp[0] = 1;
for(int i=1;i<=target;i++){
for(int j=0;j=0 && dp[i]< INT_MAX){
dp[i] += dp[i-nums[j]];
}
}
}
return dp[target];
}
};
class Solution
{
public:
Node* findKL(Node *root, int& K){
if(root==NULL){
return root;
}
Node* rs = findKL(root->right, K);
if(rs != NULL){
return rs;
}
K--;
if(K==0){
return root;
}
return findKL(root->left, K);
}
int kthLargest(Node *root, int K)
{
Node* ans = findKL(root, K);
return ans->data;
}
};
class Solution {
public:
vector> generate(int numRows) {
vector> pasc(numRows);
for(int i=0;i
class Solution{
public:
void inorder(Node* root, vector &v) {
if(!root) return;
inorder(root->left, v);
v.push_back(root->data);
inorder(root->right, v);
}
void toBST(Node *root, int &i, vector &v) {
if(!root) return;
toBST(root->left, i, v);
root->data = v[i++];
toBST(root->right, i, v);
}
Node *binaryTreeToBST (Node *root)
{
//Your code goes here
vector v;
inorder(root, v);
sort(v.begin(), v.end());
int i = 0;
toBST(root, i, v);
return root;
}
};
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode *dummy = new ListNode(0), *pre = dummy, *cur;
dummy -> next = head;
for (int i = 0; i < m - 1; i++) {
pre = pre -> next;
}
cur = pre -> next;
for (int i = 0; i < n - m; i++) {
ListNode* temp = pre -> next;
pre -> next = cur -> next;
cur -> next = cur -> next -> next;
pre -> next -> next = temp;
}
return dummy -> next;
}
};
class Solution {
public:
int minimumMultiplications(vector& arr, int start, int end) {
queue> pq;
pq.push({start,0});
vector dis(100000,1e9);
int m=1e5;
while(!pq.empty()){
int num=pq.front().first;
int level=pq.front().second;
pq.pop();
if(num==end)
return level;
for(auto &it:arr){
int n=((num%100000)*(it%100000))%m;
if(level+1
