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 | 3821 |
3 | Benq | 3736 |
4 | Radewoosh | 3631 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3388 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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.
Name |
---|
No