Skip to content

Create 264. Ugly Number II #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 264. Ugly Number II
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();
}
}
};
Loading