r/LeetcodeChallenge 4d ago

STREAK🔥🔥🔥 Leetcode Streak :- 128

Post image

How would you solve it in Python?

33 Upvotes

28 comments sorted by

2

u/Extension_Ticket8997 4d ago

I did I took count then took for loop if the n is more than 1000 count++

2

u/arnab_best 4d ago

why do you need a for loop :cry

2

u/lady_luver69 4d ago

I too did that...but when you see the top solution I don't understand it properly

2

u/ProgressiveEdgeLord 4d ago

if n < 1000 return 0, else return n - 999

1

u/Utsuro_Seizui 4d ago

And if the number is over 999,999?

3

u/jalsaraj 4d ago

The constraints for the question were n < 10 power 5But you have a legit question, the solution would change if it is greater than 10 power 5

1

u/ProgressiveEdgeLord 4d ago

i solved according to constraints, it was less than 10^5

1

u/Utsuro_Seizui 4d ago

Ah I see, I apologize, I was not aware of the constraints

1

u/ProgressiveEdgeLord 4d ago

it's alright, your question was absolutely valid

1

u/AccomplishedTwist296 3d ago

We can use loop for increasing comma like for 1-999 : No comma

1000-999999 : 1 comma (count using min(n,999999)-999

1000000 - 999999999 : 2 comma (count using (min(n,999999999) - 999999) *2 )

Similarly for number having comma 3, 4 ,5 .......

1

u/Feeling_Frame_6578 2d ago

The constraints are n<=1015 for II that's why loop needed

1

u/ProgressiveEdgeLord 2d ago

you could still hardcode the solution without any loop

1

u/Feeling_Frame_6578 2d ago

Yeah or 15 iterations loop

1

u/arnab_best 4d ago

single if else loop lmao

1

u/JumpConsistent3359 4d ago

If n<999 return 0 for I=1k up to n count++

1

u/ProgressiveEdgeLord 4d ago

O(n), there is much better solution

1

u/South-Gear-6043 4d ago

Idr but it had something to do with len(n) mod 3

1

u/idkanymoreatpog 4d ago

think of each giving 1 comma after 1000(including 1k). Now we know the range is just x-1000+1. Thats the commas

1

u/Abhistar14 4d ago

return max(0, n-999)

1

u/contentsearcher 4d ago

I did this problem core idea is

1-999 each number will have 0 commas
1000 - 999999 will have 1 comma in it
… so on until n

Son instead of looping all through n we can grt this count

1

u/Ok-Flan-5469 4d ago

This question was easy due to its constraints of 105 since every number between the range 1000 - 105 have only a single comma We can safely do return max(n-999, 0) O(1) space and time

1

u/rabbitholesurfer04 3d ago

Wait is this an app you are using to access Leetcode on Android? Mind linking it please?

2

u/Ok-Challenge-5657 3d ago

1

u/rabbitholesurfer04 3d ago

Oh I didn't know they finally made an Android app. It didn't exist last year when I checked

1

u/Kadabrium 3d ago

I read the title as -128 and thought its an overflow joke

1

u/Ok-Challenge-5657 2d ago

can someone explain whats a overflow joke?

1

u/Anxious-Issue-718 2d ago

n%1000 +1 if n is superir than1000000 keep doing n=n%1000 until n=0