Hi, a newbie here, did all the google work and found some ideas about solving my problem but a few good soul might help better.
How do you approach solving a problem that has too many constraints to remember? Or how do you approach to solve a problem that requires memorizing a lot variables, ranges and constraints which usually makes it harder for a newbie?
How did you overcome this? What was your approach?
Your answer will also help Robiul001, khandokersabbirsabbir and Bug_Debug. Thanks.
Can you give an example of a problem with a lot of constraints?
Hey Redux, One of my friends sent me this problem (GameRank on Kattis) to solve and I find it too much neuron work for me (I'm no brain lazy) to remember all the constrains but I feel the problem is easy.
So that problem definitely has lots of edge cases. I find it easier to deal with these types of problems through a series of steps that sound common sense, but are easy to skip.
Define your state. In this case it would be number of stars, level, and streak.
Write lots of simple functions that call each other that compute changes to state. For this problem, you'd need a win function and a lose function. I'd probably have my win function call an isBonus function.
Figure out how to thread these simple parts together.
Finally, like you mentioned below, practice! You get more comfortable with breaking down problems as you do more of them.
Great example of a question that's not algorithmically challenging, but annoying to implement.
I'll try to break it down into parts for you, so that maybe you can see how to do it for future questions (or maybe it won't help at all since I'm just repeating a lot of what the statement says. ¯\_(ツ)_/¯).
A player is playing a game that has a ranking system. There's 26 ranks, lowest being 25 going all the way up to 1, and an extra rank above 1 called Legend. The player starts at rank 25. We want to output the player's rank at the end of a sequence of matches. (Ok, so we should maintain the player's current rank).
Each rank has a maximum number of stars and that number is different for different interval of rankings (ex. 25 — 21 has 2 stars, 20 — 16 has 3 stars, etc). (So we need to remember the max number of stars at each rank, and maintain the player's current number of stars).
How are stars gained/lost?
Gain 1 star per win.
If a player has the maximum number of stars at a their current rank, and wins a game then they will be at the next higher rank with 1 or 2 stars depending on their streak.
If the rank is between 1 and 20 then 1 star is lost per loss. Otherwise no stars are lost.
If the rank is above 20, and a player is at 0 stars and loses a game, then they drop a rank and will have the max number of stars at that new rank.
If the rank is 20 and below, then the current number of stars can't drop below 0. Also if the player's current rank is legend, then they no longer lose stars on losses.
If the rank is below 5, then you get an extra star on your 3rd or greater win in a row. (So we also need to maintain the player's current winning streak.)
So we just have to maintain a player's current rank, current number of stars, and current streak and update them accordingly.
Old Solution Code from when I did this problem before...
I maintain the values rank, stars, and streak as I mentioned before. Then I update these values (using if statement...) based on if the player wins or loses their next game, and also based on their current rank, number of stars, and streak. If their rank hits legend, then I can stop there and output "Legend". Otherwise, we continue until end of input and output the player's rank.
For this problem, there's not much you can do besides take your time with it and deal with the cases one at a time while trying to not get too frustrated or confused. If it's too much to keep in your head (brain lazy), then it's ok write it out, or draw diagrams. (I personally like writing some pseudocode in my IDE to help outline my thoughts). Take your time and you'll reach a solution eventually. It might be slow at first, but you'll get faster as you practice more.
I hope that helps.
edit: I didn't see arknave's comment as I was writing mine, but I'd recommend writing your solution in the way he suggests as it sounds easier to understand and debug.
Organize your thoughts on paper (write out every case/edges cases). That way you don't have to rely on something unreliable like periphery memory.
Also, you might be surprised/see some pattern after writing some stuff on paper.