2024 March 2 Problems

131 views
Skip to first unread message

daryl...@gmail.com

unread,
Mar 2, 2024, 12:41:30 PMMar 2
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.


2551Put Marbles in Bags66.8%Hard

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,
Mar 2, 2024, 1:16:15 PMMar 2
to leetcode-meetup
Time: O(n lg(n)) Memory: O(n)
class Solution {
public:
int splitNum(int num) {

vector<int> withoutZeros{};

while (num > 0)
{
int digit = num % 10;
if (digit != 0)
{
withoutZeros.push_back(digit);
}
num = num/10;
}

sort(withoutZeros.begin(), withoutZeros.end());

int num1 = 0;
int num2 = 0;

int ii=0;
while (ii < withoutZeros.size())
{
num1 = (num1 * 10) + withoutZeros[ii];

++ii;
if (ii < withoutZeros.size())
{
num2 = (num2 * 10) + withoutZeros[ii];
}
++ii;
}

return num1 + num2;
}
};

Flocela Maldonado

unread,
Mar 2, 2024, 1:29:08 PMMar 2
to leetcode-meetup
Time: O(N Lg(N)) Memory: O(n)
class Solution {
public:
int maximizeGreatness(vector<int>& nums) {
vector<int> copy = nums;

sort(copy.begin(), copy.end());
sort(nums.begin(), nums.end());

// number of greater numbers
int count = 0;

// pointer for copy
int jj = 0;

for(int ii=0; ii<nums.size(); ++ii)
{
int cur = nums[ii];
while (jj < copy.size())
{
if (copy[jj] > cur)
{
++count;
++jj;
break;
}
++jj;
}
}

return count;

}
};

Vivek H

unread,
Mar 2, 2024, 1:44:13 PMMar 2
to leetcod...@googlegroups.com


*******************************************************************************
*******************************************************************************

class Solution {
public:
    int maximizeGreatness(vector<int>& nums) {

        vector<int> sortedNum = nums;
        sort(sortedNum.begin(), sortedNum.end());
        sort(nums.begin(), nums.end());

        int i=0; int count = 0; int j = i;
        while(i< nums.size()) {
           
            while(j < sortedNum.size()) {
                if(sortedNum[j] > sortedNum[i]) {
                    count++; j++;
                    break;
                }
                j++;
            }
            i++;
        }
       
       
        return count;      
    }
};

--
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/bdac9126-a34b-4804-b5db-3829e7074e2dn%40googlegroups.com.

Ellee Ch

unread,
Mar 2, 2024, 1:49:57 PMMar 2
to leetcode-meetup

class Solution:

    def maximizeGreatness(self, nums: List[int]) -> int:

        nums.sort()


        res = 0

        i, j = 0, 0

        while j < len(nums):

            if nums[j] > nums[i]:

                res += 1

                i += 1

            j += 1


        return res

Carlos Green

unread,
Mar 2, 2024, 2:22:03 PMMar 2
to leetcode-meetup
/**
* @param {number} num
* @return {number}

Time O(n log n) for sorting Space O(n) for holding a string of num's characters

Convert num into a string and then split the characters Sort them and add every character to num1 or num2
*/
var splitNum = function(num) {
const nums = String(num).split('').sort()
let num1 = ''
let num2 = ''

while(nums.length > 0) {
let chr = nums.shift()
if (chr === undefined) break
num1 += chr

chr = nums.shift()
if (chr === undefined) break
num2 += chr
}

return +num1 + +num2
};

Luan Nguyen

unread,
Mar 2, 2024, 2:51:25 PMMar 2
to leetcode-meetup
2578. Split With Minimum Sum

Time: O(nlogn), Space: O(n)

    def splitNum(self, num: int) -> int:

        digits = []
        while num > 0:
            digits.append(num % 10)
            num = num // 10
       
        digits = sorted(digits)
        num1, num2 = [], []

        for i in range(len(digits)):
            if i % 2 == 0:
                num1.append(digits[i])
            else:
                num2.append(digits[i])

        num1 = [str(n) for n in num1]
        num1 =int("".join(num1))
        num2 = [str(n) for n in num2]
        num2 = int("".join(num2))
        return num1 + num2

Luan Nguyen

unread,
Mar 2, 2024, 2:52:45 PMMar 2
to leetcode-meetup
2592. Maximize Greatness of an Array

Time: O(nlogn)   Space: O(n)
    def maximizeGreatness(self, nums: List[int]) -> int:
        count, i, j = 0, 0, 0
        nums = sorted(nums)
        perm = nums

        while j < len(perm):
            if nums[i] == perm[j]:
                j += 1
            else:
                count += 1
                i += 1
                j += 1

        return count

Andriy T

unread,
Mar 2, 2024, 7:21:23 PMMar 2
to leetcode-meetup

 Space O(n) Time n*lg(n) - sorting
class Solution {
    public int splitNum(int num) {
        //converting to array of single digits
        int n = num;
        List<Integer> digs = new ArrayList<>();
        while (n > 0) {
            int dig = n % 10;
            n = n / 10;
            digs.add(dig);
        }
        //sort
        Collections.sort(digs);

        int maxN = digs.size() / 2;
        int factor = 1;

        //extract starting from the end and calculating for 687 -> 6, 7, 8 -> 8 * 1 + 7 * 1 + 6 * 10
        int sum = 0;
        int i = 0;
        for (int j = digs.size() - 1; j >= 0; j--) {
            if (i == 2) {
                factor *=10;
                i = 0;
            }
            sum += digs.get(j) * factor;
            i++;
        }
        return sum;
    }
}

Priyank Gupta

unread,
Mar 2, 2024, 9:13:23 PMMar 2
to leetcode-meetup
# Space O(N)
# Time O(NlogN)
class Solution:
def splitNum(self, num: int) -> int:
num = sorted(str(num))
n1 = []
n2 = []
for i in range(len(num)):
if i%2 == 0:
n1.append(num[i])
else:
n2.append(num[i])
n1 = "".join(n1)
n2 = "".join(n2)
n1 = int(n1)
n2 = int(n2)
return n1+n2

vinay

unread,
Mar 3, 2024, 1:58:33 PMMar 3
to leetcod...@googlegroups.com
Sort the array and then find how many smaller elements can be paired up elements greater than them

class Solution {
    public int maximizeGreatness(int[] nums) {
        int count = 0;

        Arrays.sort(nums);

        int i = 0, j = 0;
        while(j < nums.length){
            if(nums[i] < nums[j]){
                i++;
                count++;
            }
           
            j++;
        }

        return count;
    }
}


Flocela Maldonado

unread,
Mar 8, 2024, 11:56:29 PMMar 8
to leetcode-meetup
2551Put Marbles in Bags66.8%Hard
Time: O(n * lg(n)) Memory: O(n)class Solution {
public:
long long putMarbles(vector<int>& weights, int k) {

int n = weights.size();

// pairs. Each pair's second value is the partition's index. The partition is to the right of the index.
// Each pair's first value is the sum of the two numbers to the left and right of the partition.
// So a pair would be {weights[ii] + weights[ii+1], ii}
vector<vector<int>> sumAndPIndexes{};

for(int ii=0; ii<n-1; ++ii)
{
sumAndPIndexes.push_back({weights[ii]+weights[ii+1], ii});
}

// The pairs are sorted in increasing order per each pair's first value. The first value is the partition's sum.
sort(sumAndPIndexes.begin(), sumAndPIndexes.end());

// the k-1 partitions that have the largest sums. The sum is the weight at the two indexes to the left and right of the partition.
// find these at the right end of the sorted sumAndPIndexes.
vector<int> partitionIndexesForMaximumScore{};
int idx = sumAndPIndexes.size()-1;
for (int ii=0; ii<k-1; ++ii)
{
partitionIndexesForMaximumScore.push_back(sumAndPIndexes[idx][1]);
--idx;
}

// the k-1 partitions that have the smallest sums.
// find these at the left end of the sorted sumAndPIndexes.
vector<int> partitionIndexesForMinimumScore{};
idx = 0;
for (int ii=0; ii<k-1; ++ii)
{
partitionIndexesForMinimumScore.push_back(sumAndPIndexes[idx][1]);
++idx;
}

// maxSum is the sum over the top k-1 partitions plus the weight at weight[0] and weight[n-1]
long long maxSum = weights[0];
maxSum += weights[n-1];
for(int ii=0; ii<partitionIndexesForMaximumScore.size(); ++ii)
{
int partitionIndex = partitionIndexesForMaximumScore[ii];
maxSum += weights[partitionIndex];
maxSum += weights[partitionIndex+1];
}

// minSum is the sum over the bottom k-1 partitions plus the weight at weight[0] and weight[n-1]
long long minSum = weights[0];
minSum += weights[n-1];
for(int ii=0; ii<partitionIndexesForMinimumScore.size(); ++ii)
{
int partitionIndex = partitionIndexesForMinimumScore[ii];
minSum += weights[partitionIndex];
minSum += weights[partitionIndex+1];
}

return std::abs(maxSum - minSum);

}
};
Reply all
Reply to author
Forward
0 new messages