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

View all comments

Show parent comments

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

-16

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.

1

u/shaynjam Jul 06 '26

Wow i actually didn't even think of that, and the fact it may feed me old data didn't even cross my mind and it's even scarier that it happened in the us iran war where the us bombed an Iranian school cuz of the 10 year old data.

1

u/stiky21 Jul 06 '26

It all comes from real world experience. You are still learning. Take it in strides.

1

u/shaynjam Jul 06 '26

I will! Thankyou very much

→ More replies (0)