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??
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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??
Rev. | Язык | Кто | Когда | Δ | Комментарий | |
---|---|---|---|---|---|---|
en1 | aman_naughty | 2019-03-18 21:57:27 | 1029 | Initial revision (published) |
Название |
---|