-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50ebeff
commit f36f1b6
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution { | ||
public: | ||
int nthUglyNumber(int n) { | ||
vector<long long> arr2; | ||
vector<long long> arr3; | ||
vector<long long> arr5; | ||
vector<long long> result; | ||
if(n==1){ | ||
return 1; | ||
} | ||
else{ | ||
result.push_back(1); | ||
arr2.push_back(2); | ||
arr3.push_back(3); | ||
arr5.push_back(5); | ||
|
||
int i=0; | ||
int j=0; | ||
int k=0; | ||
|
||
while (result.size() < n) { | ||
int mini = min({arr2[i], arr3[j], arr5[k]}); | ||
result.push_back(mini); | ||
|
||
if (mini == arr2[i]) i++; | ||
if (mini == arr3[j]) j++; | ||
if (mini == arr5[k]) k++; | ||
|
||
arr2.push_back(result.back() * 2); | ||
arr3.push_back(result.back() * 3); | ||
arr5.push_back(result.back() * 5); | ||
} | ||
return result.back(); | ||
} | ||
} | ||
}; |