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 | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | nor | 150 |
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 |
---|