Problem : problem
Code :
code
. I want to know the time complexity of my algorithm. Although it runs within 16 ms on leetcode but is it really O(n) time and O(1) space??
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
Estimate time complexity of randomized algorithm
Problem : problem
Code :
class Solution
{
public:
vector<int> majorityElement(vector<int>& nums) { srand(time(NULL)); int c=0,n=nums.size(); vector <int> ans; while((n-c)>(n/3)) { int i=rand()%n,f=0; int x=nums[i]; for(int j=0;j<n&&x!=INT_MAX-1;j++) { if(nums[j]==x) { nums[j]=INT_MAX-1; f++; } } //cout<<x<<" "<<f<<" "; if(f>(n/3)) ans.push_back(x); c+=f; } return ans; }
};
. I want to know the time complexity of my algorithm. Although it runs within 16 ms on leetcode but is it really O(n) time and O(1) space??
Name |
---|