import java.util.*;

public class Solution {

    private int findMin(int x, int y, int z){
        int temp = x;
        temp = y < temp ? y: temp;
        temp = z < temp ? z: temp;
        return temp;
    }

    public int GetUglyNumber_Solution(int index) {
        if(index < 7){
            return index;
        }
        ArrayList<Integer> res = new ArrayList<>();
        res.add(1);
        int count = 1;
        int p2 = 0, p3 = 0, p5 = 0;
        for(int i = 2; i <= index; i++){
            int newNum = findMin(res.get(p2)*2,res.get(p3)*3,res.get(p5)*5);
            res.add(newNum);
            count += 1;
            if(newNum == res.get(p2)*2){
                p2 += 1;
            }if(newNum == res.get(p3)*3){
                p3 += 1;
            }if(newNum == res.get(p5)*5){
                p5 += 1;
            }
        }
        return res.get(count-1);
    }
}