2024 January 20 Problems

79 views
Skip to first unread message

daryl...@gmail.com

unread,
Jan 20, 2024, 12:39:16 PMJan 20
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.






Please download and import the following iCalendar (.ics) files to your calendar system.
Weekly: https://us04web.zoom.us/meeting/upIqc-6upj8sGt0O2fJXTw2gC4FxRMQ-iqAT/ics?icsToken=98tyKu6uqT8tHNyRthmOR7YAB4-gKO7xiCldjbcNs03jKRhndVHxFbZkKoBSIZXZ

Join Zoom Meeting
https://us04web.zoom.us/j/76747684609?pwd=HOCapO7jjK0rifPlKvV9CxWFn5lLJn.1

Meeting ID: 767 4768 4609
Passcode: 8wf3Qa

Flocela Maldonado

unread,
Jan 20, 2024, 1:22:08 PMJan 20
to leetcode-meetup
Time O(n), Memory O(1)

class Solution {
public:
int minimumCost(vector<int>& cost) {
sort(cost.begin(), cost.end());

int sum = 0;
int count = 0;
for (int ii=cost.size()-1; ii>=0; --ii)
{
if (count == 0 || count ==1)
{
sum += cost[ii];
}
count = (count==0 || count==1) ? (count+1) : 0;
}

return sum;

}
};

Andriy T

unread,
Jan 20, 2024, 1:49:03 PMJan 20
to leetcode-meetup
Time O(nlog(n)) Space O(1)
class Solution {
    public int minimumCost(int[] cost) {
        Arrays.sort(cost);
        int sum = 0;
        int cost1 = 0;
        int cost2 = 0;
        for (int i = cost.length - 1;i>=0;i--) {
            if (cost1 == 0 && cost2 == 0) {
                cost1 = cost[i];
            }
            else if (cost1 > 0 && cost2 == 0) {
                cost2 = cost[i];
            }
            else {
                if (cost[i] <= cost1 + cost2) {
                    sum += (cost1 + cost2);
                    cost1 = 0;
                    cost2 = 0;
                }
            }
        }
        sum += (cost1 + cost2);
        return sum;
    }
}

Flocela Maldonado

unread,
Jan 20, 2024, 2:06:13 PMJan 20
to leetcode-meetup
Time: O(n); Memory O(1)
class Solution {
public:
vector<long long> maximumEvenSplit(long long finalSum) {
vector<long long> ans{};
if (finalSum % 2 != 0)
{
return ans;
}

/*vector<long long> origVector{};
int ii=1;
while (ii * 2 <= finalSum)
{
origVector.push_back(ii*2);
++ii;
}*/

long long sum = 0;
int idx=1;
while (idx <= finalSum/2 + 1)
{
long long temp = sum;
long long origVal = idx*2;
temp += origVal;

if (temp == finalSum)
{
ans.push_back(origVal);
break;
}
else if (finalSum - temp > origVal)
{
ans.push_back(origVal);
sum += origVal;
}

++idx;
}

return ans;
}
};

Vivek H

unread,
Jan 21, 2024, 1:08:36 AMJan 21
to leetcod...@googlegroups.com
*****************************************************************************
*****************************************************************************
time = O(n)
space = O(1)
Took a greedy approach and started with 2,4 6,8,...

maintain two numbers : 
1) currSum : which will be increased by 2 , in every iteration
2) FinalSum = which will be decrease by currSum every iteration

For eg finalSum = 28 , the two arrays will look like below

currSum   0     2      4     6      8
finalSum  28    26   22    16    8

we need to stop the look when currSum == finalSum ( ie when both values are 8). At this point, we have added 2, 4, 6, into our array
now the only number that is left to make finalSum = 0 is16 , which is currSum + finalSum ( 8+8), so we add 16. 
this our array becomes 2,4,6,16

Code below

class Solution {
public:
    vector<long long> maximumEvenSplit(long long finalSum) {
       
        long long currSum = 0;
        long long i = 2;
        vector<long long> result;

        //if number is odd return empty vector
        if((finalSum & 1) == 1)
            return result;
        while(currSum < finalSum) {
           
           
           currSum = currSum+2;
           finalSum = finalSum-currSum;
           if(currSum < finalSum)
                result.push_back(currSum);
            else
                break;
                     
        } //while ends
        result.push_back(finalSum+currSum);
        return result;
    }
};

--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/6e743853-448f-4f28-a4c4-2425294c604en%40googlegroups.com.

Bhupathi Kakarlapudi

unread,
Jan 23, 2024, 10:53:36 PMJan 23
to leetcod...@googlegroups.com
class Solution:
def minimumCost(self, cost: List[int]) -> int:
cost.sort(reverse=True)
result:int = 0
idx:int = 0

while idx < len(cost):
result = result + sum(cost[idx:idx+2])
idx += 3
return result

--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.

Vivek H

unread,
Jan 23, 2024, 10:53:36 PMJan 23
to leetcod...@googlegroups.com
*********************************************************************************
*********************************************************************************
time = O(n)
space = O(1)

class Solution {
public:
    int minimumCost(vector<int>& cost) {

        if(cost.size() == 0)
            return 0;

        sort(cost.begin(), cost.end());
        int totalCost = 0;
        while(cost.size() > 0) {
            int cost1=0; int cost2=0;
            cost1 = cost.back();
            cost.pop_back();
            if(cost.size() > 0) {
                cost2 = cost.back();
                cost.pop_back();
            }
            //cout << "cost1:" << cost1 << "cost2:" << cost2 << endl;
            totalCost += cost1+cost2;
            if(cost.size() > 0)
                cost.pop_back();

           
        }
        return totalCost;
       
    }
};

On Sat, Jan 20, 2024 at 9:39 AM daryl...@gmail.com <daryl...@gmail.com> wrote:
--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages