Problem Statement
Optimal Code
Doubt: Can anyone explain to me how this code is working practically while theoretically O(1e4*1e5) should give TLE.
Is there any mathematically reason ?
Thanks for your time.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
HELP: Why O(n*1e5) doesn't give TLE
Given start, end and an array arr of n numbers. At each step, start is multiplied with any number in the array and then mod operation with 100000 is done to get the new start.
Your task is to find the minimum steps in which end can be achieved starting from start. If it is not possible to reach end, then return -1.
n<=1e4, arr[i]<=1e4, start, end<1e5
Expected Time & Space Complexity : O(1e5)
class Solution {
public:
int minimumMultiplications(vector<int>& v, int s, int d) {
int n = v.size();
int MOD = 1e5;
queue<pair<int,int>>q; q.push({s,0});
vector<int>vis(1e5); vis[s]=1;
while(!q.empty()){
auto elem = q.front(); q.pop();
int i = elem.first; int res = elem.second;
if(i==d) return res;
for(int j=0;j<n;j++){
int val = (i*v[j])%MOD;
if(!vis[val]) {
vis[val] = 1;
q.push({val,res+1});
}
}
}
return -1;
}
};
Doubt: Can anyone explain to me how this code is working practically while theoretically O(1e4*1e5) should give TLE.
Is there any mathematically reason ?
Thanks for your time.
Rev. | Lang. | By | When | Δ | Comment | |
---|---|---|---|---|---|---|
en2 | HuTao_Oya_OyaOya | 2024-07-22 20:18:05 | 111 | Tiny change: '\n<spoiler s' -> '<spoiler s' | ||
en1 | HuTao_Oya_OyaOya | 2024-07-22 20:16:52 | 1440 | Initial revision (published) |
Name |
---|