Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • 263 Ugly Number I
  • 264 Ugly number II
  • 313 Super ugly number

Was this helpful?

  1. 2. Array and Numbers

Ugly Number

263 Ugly Number I

Check if a number is ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. 1 is usually consider as an ugly number.

public boolean isUgly(int num) {
    int[] ugly = {2, 3, 5};
    for (int prime : ugly){
        while(num % prime == 0 && num > 0){
            num /= prime;
        }
    }
    return num == 1;
}

264 Ugly number II

Write a program to find the n-th ugly number.

  • All the ugly numbers are in format 2^a x 3^b x 5^c, ugly numbers are subsequence factor of ugly numbers.

    1. Time O(n^2), Space O(1). Loop increasing number, check if it is a ugly number.

    2. Time O(n), Space O(n). Dynamic programming, Record the subsequence of prime factors and find the smallest.

public int nthUglyNumber(int n) {
    int index_2 = 0, index_3 = 0, index_5 = 0; // the index on array
    int factor_2 = 1, factor_3 = 1, factor_5 = 1; // start with 1
    int[] arr = new int[n];
    int min = 1;

    for( int i = 0; i < n; i++){
        arr[i] = min;
        if (factor_2 == min) factor_2 = arr[index_2++] * 2;
        if (factor_3 == min) factor_3 = arr[index_3++] * 3;
        if (factor_5 == min) factor_5 = arr[index_5++] * 5;

        min = Math.min(factor_2, Math.min(factor_3, factor_5));
    }
    return arr[n-1];
}

313 Super ugly number

Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k.

public int nthSuperUglyNumber(int n, int[] primes) {
    int[] primes_idx = new int[primes.length];
    int[] primes_val = new int[primes.length];
    Arrays.fill(primes_val, 1);
    int[] ugly = new int[n];
    int min = 1;

    for (int i = 0; i < n; i++){
        ugly[i] = min;

        min = Integer.MAX_VALUE;
        for (int j = 0; j < primes.length; j++){

            if (primes_val[j] == ugly[i]) primes_val[j] = primes[j] * ugly[primes_idx[j]++];
            min = Math.min(min, primes_val[j]);
        }
    }
    return ugly[n-1];
}
PreviousAnagramNextTwoPointer

Last updated 5 years ago

Was this helpful?