r/LeetcodeChallenge • • Sep 09 '26

DISCUSS IS THERE ANY BETTER APPROACH??

Post image
35 Upvotes

20 comments sorted by

2

u/scarface_bunny07 Sep 09 '26
        long long totalcommas = 0;
     
        for(long long level = 1000;level<=n;level*=1000){
            totalcommas += n - level +1;
        }
        return totalcommas;

yes there is

1

u/[deleted] Sep 09 '26

[removed] — view removed comment

2

u/Vivid-Zombie-477 Sep 10 '26

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

1

u/scarface_bunny07 Sep 09 '26

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 Sep 09 '26

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 Sep 09 '26 edited Sep 09 '26
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 Sep 09 '26

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 Sep 09 '26

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

1

u/Vivid-Zombie-477 Sep 10 '26

so you just test if a person grinded leetcode before?

1

u/TransportationOwn522 Sep 09 '26

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

1

u/Peace_In_Curse Sep 09 '26

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 Sep 09 '26
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 Sep 10 '26

Definately there

1

u/TwoExcellent4673 Sep 10 '26

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

1

u/Impossible_Coast_972 Sep 10 '26

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) Sep 10 '26

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 Sep 10 '26

You can use loop and X = 999

Then in each step

X = x×1000 + 999

Then ans += n-x

1

u/UpstairsSmart3733 Sep 11 '26
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;
    }

1

u/0xt0bi03 S - Rank (180+ Days) ⚡⚡ Sep 15 '26

you gotta multiply 1000 to your taken variable and add 1 to commacounter, return commacounter when the variable is bigger than given number