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 | Benq | 3857 |
| 2 | jiangly | 3810 |
| 3 | maroonrk | 3534 |
| 4 | tourist | 3528 |
| 5 | Kevin114514 | 3510 |
| 6 | turmax | 3411 |
| 7 | Um_nik | 3387 |
| 8 | Radewoosh | 3367 |
| 9 | heuristica | 3322 |
| 10 | strapple | 3317 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | maspy | 150 |
| 3 | Um_nik | 145 |
| 4 | Errichto | 139 |
| 5 | adamant | 136 |
| 6 | maroonrk | 134 |
| 7 | nik_exists | 133 |
| 7 | DNR | 133 |
| 9 | AmShZ | 130 |
| 10 | Dominater069 | 129 |
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) |
| Название |
|---|


