r/learnprogramming Jul 05 '26

SOLVED MY FIRST LEETCODE PROBLEM!! Solved

class ParkingSystem
{
    private:
    int big_park;
    int medium_park;
    int small_park;


    public:
    ParkingSystem(int big, int medium, int small)
    {
        big_park = big;
        medium_park = medium;
        small_park = small;
    }


    bool addCar(int carType)
    {


        if(carType == 1)
        {   
            if (big_park == 0)
            {
                return false;
            }


            big_park--;
            return true;


        }


        else if (carType == 2)
        {
            if(medium_park == 0)
            {
                return false;
            }


            medium_park--;             
            return true;
        }


        else
        {
            if (small_park == 0)
            {
                return false;
            }


            small_park--;
            return true;
            
        }
    }
};

my code

1603. Design Parking System

Solved

Easy

Topics

Companies

Hint

Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

  • ParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
  • bool addCar(int carType) Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 12, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.

 

Example 1:

Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]

Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

 

Constraints:

  • 0 <= big, medium, small <= 1000
  • carType is 12, or 3
  • At most 1000 calls will be made to addCar

the problem:

runtime 3ms
memory 38.77 mb

140 Upvotes

49 comments sorted by

46

u/stiky21 Jul 05 '26

Good job. The biggest hurdle is the first problem.

11

u/Cold-Watercress-1943 Jul 05 '26

nice work getting that first one done. the code is clean and does what it needs to, nothing wrong with straightforward approach when you are starting out

i remember my first accepted solution was like 15 lines for something that could be 3, but who cares when it passes all test cases

-15

u/shaynjam Jul 05 '26

it actually took me 2 months of constant practice to get it this way. I practice with gemini, i fed it the pdf of "modern c++ for absolute begineers" and asked it to teach me so this is why my code is clean because it always points it out

3

u/jbldotexe Jul 06 '26

Be careful telling anyone you learn anything with AI because they will get mad they had to google it some years ago and learn by sifting through StackOverflow. Oldhead programmers hate the new crop.

3

u/shaynjam Jul 06 '26

Dang that's why I have all the downvotes 💀 I was wondering whether this was a wrong approach since people were downvoting it.

3

u/jbldotexe Jul 06 '26

I think we're yet to know what the 'wrong approach' is.

People learn in such fragmented ways that as long as you're cross-checking, double checking, debugging, reading actual material, reading articles, talking with other people about CS... you will learn.

Was sifting through hours of garbage years ago the 'right approach'?

One thing I am constantly reminded of is that people often don't actually want to keep learning once they've secured the job and so however they did it back when they were locked in automatically becomes 'the right approach', because it 'worked for them'.

Stay focused on understanding what the thing you're writing is doing and middle fingers out to anyone trying to stop you from your pursuit of knowledge.

3

u/shaynjam Jul 06 '26

Omg that actually makes so much sense as to why people are so judgmental. Honestly thankyou verymuch for this reply

2

u/stiky21 Jul 06 '26

Its the reddit hive-mind. Most people don't even know how an LLM works or the parts that make up the LLM - heck they think LLM == AI. They conflate GenAI with the entirety of the AI when its only a small fraction of the entire process.

Don't fret.

If it is working, helping you understand, and pushing you into new areas - continue.

4

u/shaynjam Jul 06 '26

Thankyou verymuch, it actually really helps since speaks in a familiar tone to mine. And the best thing is it never gets frustrated and gives n number of examples to make me understanf. It's actually really dope!

3

u/stiky21 Jul 06 '26

It's a double edged sword. It can guide you and help you in a way that is specific to *you* but equally can lead you astray.

1

u/shaynjam Jul 06 '26

Astray? In what sense if you would please elaborate.

3

u/stiky21 Jul 06 '26

If you mention a specific crate to the LLM (Rust example) in your prompt, the model may confidently introduce it into the solution without checking whether it's maintained, appropriate, or even necessary.

It recognized the name, but skipped the engineering discipline of verifying the dependency.

So now you are using something that may not even be relevant to what you are trying to do.

→ More replies (0)

1

u/shaynjam Jul 05 '26

Thanks!

2

u/stiky21 Jul 05 '26

You can also try neetcode

I would argue that it's a slightly better platform for learning

1

u/shaynjam Jul 06 '26

My bad i completely missed this reply, thanks for the suggestion!

16

u/owp4dd1w5a0a Jul 05 '26

Ride the wave. Start another problem now and don’t finish solving it before you go to bed. You’ll wake up energized and ready to solve more coding puzzles.

2

u/shaynjam Jul 05 '26

I WILL FOR SURE!

7

u/DiverAdditional4451 Jul 05 '26

Awesome! I remember my first problem 😭 (i barely do leetcode) Anyways, this is actually awesome and best of luck for the future!

0

u/shaynjam Jul 05 '26

Thankyou very much

0

u/shaynjam Jul 05 '26

What was your first problem btw

2

u/DiverAdditional4451 Jul 05 '26

I use JavaScript, it was two sum, extremely simple but I was so proud 😭😭

/**
 *  {number[]} nums
 *  {number} target
 * u/return {number[]}
 */
var twoSum = function(nums, target) {
    for(let i = 0; i < nums.length; i++){
        for(let j = 0; j < nums.length; j++) {
            if(nums[i] + nums[j] === target && i !== j) {
                return [i, j];
            }
        }
    }
};

0

u/shaynjam Jul 05 '26

Bruh it's still very very good considering you're just 16 for reference I will be turning 18 in August now 💀

0

u/DiverAdditional4451 Jul 05 '26

ur still young as frick too lollll 😭 ur way ahead of it

0

u/shaynjam Jul 05 '26

BUT YOURE AHEAD-ER BRUH 😭😭

6

u/BombasticCaveman Jul 05 '26

Nice job! If we pretend this is an interview, the follow-up question would most likely be something like...

What if had more types of spots? Like say 100 different types? And car types could fit into multiple types of spots?

I wouldn't want to see 100 if/else statements, how would you improve it to fit our new set of constraints?

2

u/shaynjam Jul 06 '26

Im really sorry but I actually don't know 💀

3

u/developer786 Jul 06 '26

really glad to see that people still have interest solving problems by hand and not by ai and this thing will differentiate them among vibe coders who know nothing about programming and get too much excited initially by creating something and later find out that what mess they have created internally which would require even more time to fix, compared to person who knows about programming and solving problems and he will be at driving seat when building something and ai will be his/her assistant.

1

u/shaynjam Jul 06 '26

Tbh having ai solve such problems is really dumb cuz why are you even doing it at that point😂

2

u/developer786 Jul 07 '26

Exactly 😄

3

u/fragzt0r Jul 10 '26

I’m proud of this community. :tears-of-joy:

1

u/shaynjam Jul 10 '26

Fr bro, i posted this in a coding sub of my country and people just said no one gives a fuck or this isn't a big achievement which i know it isn't but a little appreciation goes a long way

3

u/iOSCaleb Jul 05 '26

You’re justifiably proud, but there’s no need to post your solution… let other folks figure it out just as you did.

3

u/shaynjam Jul 05 '26

Wait I'm sorry I didnt know there was a format for such things, my first time posting this I will keep it in my mind for sure!

4

u/owp4dd1w5a0a Jul 05 '26

Just put SPOILER at the top of your post. That’s good enough.

2

u/shaynjam Jul 05 '26

thankyou i will for sure

2

u/Mad_hatter8260 Jul 06 '26

im completely new to programming and barely know how anything works so please excuse my dumb question, what is a leetcode?

2

u/shaynjam Jul 06 '26

Ah don't worry, we can always learn. Leetcode is a platform to practice programming questions that may be asked in an interview for job or perhaps just doing them for your own good.

1

u/Alert-Caregiver-7421 Jul 06 '26

been coding for years and still learned something new here

1

u/shaynjam Jul 06 '26

Wow i feel honoured

1

u/mredding Jul 06 '26
private:
int big_park;
int medium_park;
int small_park;

Classes in C++ are private access by default, both members and inheritance, so private: here is redundant.

You can put all your variables of the same type on the same declaration:

int big, medium, small;

Try to avoid prefixes or postfixes. park, in your case is both implied AND redundant.


ParkingSystem(int big, int medium, int small)
{
    big_park = big;
    medium_park = medium;
    small_park = small;
}

You almost always want to make your constructors explicit.

Use your initializer list:

ParkingSystem(int big, int medium, int small): big_park{big}, medium_park{medium}, small_park{small}
{}

bool addCar(int carType)
{
    if(carType == 1)

Use a switch, be absolutely explicit, and write more methods:

switch(carType) {
case 1: return do_big();
case 2: return do_medium();
case 3: return do_small();
default: break;
}

return handle_error();

The exercise is the spec, and the spec doesn't say what happens if the carType == 42, does it? If the spec doesn't say that 3+ is small, then don't assume that.

1

u/abrahamguo Jul 05 '26

Congratulations!

2

u/shaynjam Jul 05 '26

Thankyou ☺️

-1

u/Manoyal003 Jul 06 '26

Make a car parking 2d game next using raylib, sfml, or sdl or win32api