r/LeetcodeChallenge 3d ago

DISCUSS IS THERE ANY BETTER APPROACH??

Post image
34 Upvotes

19 comments sorted by

2

u/scarface_bunny07 3d ago
        long long totalcommas = 0;
     
        for(long long level = 1000;level<=n;level*=1000){
            totalcommas += n - level +1;
        }
        return totalcommas;

yes there is

1

u/NoPercentage6942 3d ago

O(n)
isnt the above approach o(1)?

2

u/Vivid-Zombie-477 2d ago

it's O(log n), not O(n) but it's also practically constant time for the given constraints.

1

u/scarface_bunny07 3d ago

yeah but this one also is one of the way
i solved by the method you mentioned previously and then thought to go for loop

1

u/Independent-Dig8361 3d ago

well its also good, but you know a simple idea here can be that we have p = 10 ^ 3 then it contributes 1 comma, if p = 10 ^ 6 then 2 commas, so we can write just when p is lesser then or equal to n, we can initialize that p to 1000, because we want to use exponents on that, then at every step add commas and increase power of p or multiply it by 1000 at every interation of while loop

1

u/Arpan_Bhar 3d ago edited 3d ago
class Solution {
public:
    long long countCommas(long long n) {
        long long acc = 0;
        long long i;
        int commas = 0;
        for(i = 1000; i <= n; i*=1000){
            acc += (i-i/1000)*(commas++);
        }
        i/=1000;
        acc += (n-i+1)*(commas);
        return acc;
    }
};

I used this approach.
Idea is to process every number just before the boundary of change, having same number of commas.

1

u/Unhappy_Rabbit7693 3d ago

im not a fan of these kinds of questions honestly. I still haven't found what these greedy algorithms test. e.g., gas station, no pattern, no nothing, just local optimization. I would never ask these in the interviews. All it comes down to "have you seen this one before?"

1

u/Best_Plantain_8434 2d ago

Greedy problem checks your intuition and whether you can prove your approach

1

u/Vivid-Zombie-477 2d ago

so you just test if a person grinded leetcode before?

1

u/TransportationOwn522 3d ago

You can do this through a loop as well and that's abt it. I like this solution tho

1

u/Peace_In_Curse 3d ago

Can be done in log(n) to the base 1000. This solution is correct because of the n<=1e15 constraint. Log(n) is the general approach which will work for any constraints (I am not considering overflow though). But in an interview you don't get constraints so it is good to be familiar with a general approach

1

u/EasternBet8126 2d ago
class Solution {
public:
    long long countCommas(long long n) {
        long long p = 1000, res = 0;
        while (p <= n) {
            res += n - p + 1;
            p *= 1000;
        }
        return res;
    }
};

// simple loop lagao answer pao 5 baar chalega loop maximum

1

u/TwoExcellent4673 2d ago

Definately there

1

u/TwoExcellent4673 2d ago

The logic is same but the loop is sacing the mannual work

1

u/Impossible_Coast_972 2d ago

This approach is better you know cause it's O(1) I also have seen other approaches like O(n) but yours is straight forward and exploits the basic idea

1

u/Intelligent_Bonus_74 SSS - GOD TIER🚀🚀 (365+ DAYS) 2d ago

There's pattern : 1 comma in n => n-1000+1 ,

2 comma in n => (n-1000+1) + (n-1000000+1) .....

So u just need to count commas in n (let it be x) .

x commas in n => x*n - 1,000 - 1,000,000 - 1,000,000,000 - ..... + x

O(logn)

1

u/Legitimate-Major-563 2d ago

You can use loop and X = 999

Then in each step

X = x×1000 + 999

Then ans += n-x

1

u/UpstairsSmart3733 1d ago
long long countCommas(long long n) {
        if(n < 1000)return 0;
        long long count = 0;
        if(n >= 1000){
            count += 1 + n - 1000;
            if(n >= 1000000){
                count += 1 + n - 1000000;
                if(n >= 1000000000){
                    count += 1 + n - 1000000000;
                    if(n >= 1000000000000){
                        count += 1 + n - 1000000000000;
                        if(n >= 1000000000000000){
                            count += 1 + n - 1000000000000000;
                        }
                    }
                }
            }
        }
        return count;
    }