There are times when I want to predict the time complexity or space complexity of the algorithm and want to check whether it fits into the given constraints or not. It always looks like that the approach is correct but there are some architecture-level problems or rather concepts that I don't know very well.
Here are my doubts,
It is given that the problems need to solve in a time limit of 1 or 2 seconds and the memory limit of 256 Megabytes or 512 Megabytes. When do we need to consider those constraints while computing the complexities?
What should be the maximum size of the array that we can declare?
How to select the STL container that fits best to solve the problem? (Although it depends on the problem)
In case of double for loop. How do we need to access the matrix (generally in DP problems) to make sure that it will not exceed the time limit? How should I write for loops bigger one outside or the smaller one? Do we need to create a matrix of N x M or M x N (i.e., N = 100 & M = 10000), and the reason for it?
How writing array outside of the main helps us to reduce the time complexity?
Example CSES Problem: Coin Combinations II
TLE Code: https://cses.fi/paste/c63f02b96710bdd1164250/.
ACCEPTED CODE: https://cses.fi/paste/4098290d90c8bb29164255/.
- How to use the Modulo operator effectively to reduce the time complexity?
Example CSES Problem: Coin Combinations I
TLE Code: https://cses.fi/paste/bd61629529d99d8b163f51/.
Modulo is written inside both loops.
ACCEPTED CODE: https://cses.fi/paste/753f06b17cbe2b761640d5/
Modulo is written outside of the inner loop.
I need help to understand the exact mathematics and concept of computer architecture behind the calculation of time and space.
Thank You.