[LeetCode#264]Ugly Number II
2015-09-01 07:45
363 查看
Problem:
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
Note that
Analysis:
Solution:
Write a program to find the
n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5. For example,
1, 2, 3, 4, 5, 6, 8, 9, 10, 12is the sequence of the first
10ugly numbers.
Note that
1is typically treated as an ugly number.
Analysis:
This problem could be solved through a magic! The naive way to solve this problem is to count each ugly number we have scanned from 1. However, when the n gets larger and larger, it's more and more difficult to achieve such goal. We must find a more efficient way to solve this problem. Let us think in this way. Suppose we have already known the ugly sequenece(in ascending order). 1 2 3 4 5 6... At each position (number in the sequence), we have three choices to continue the sequence: multiple by 2, 3 or 5. Thus actually, the ugly sequence is actually the combiation of following sequences: sequence 2: 1*2 2*2 3*2 4*2 5*2 6*2 sequence 3: 1*3 2*3 3*3 4*3 5*3 6*3 sequence 5: 1*5 2*5 3*5 4*5 5*5 6*5 The ugly sequence is actually come from the merge of three sequence. Thus, we could use the skill of mering sorted list!!! Key: always pick the smallest one amongst those three lists. min = Math.min(ugly5, Math.min(ugly2, ugly3)); a[i] = min; The problem? Whether we need to write those three sequences first? No need, and we can not!!! Cause the ulgy sequence is the target we want to compute, we could no know them first!!! And we can always compute the a[i] from "a[ptr2] * 2", "a[ptr3] * 3" and "a[ptr5] * 5". And we know ptr2, ptr3, ptr5 must '<' than i. int ptr2 = 0, ptr3 = 0, ptr5 = 0; for (int i = 1; i < n; i++) 'i' start from 1, ptr start from 0. we calculate the a[i] based on a[j] (j<i). This is inherently a dynamic programming problem. Right? Since sequence 2, sequence 3, and sequence 5 only differ in the multiplier, we have no need to record them. Just pretend they ar exist. ugly2 = a[ptr2] * 2; ugly3 = a[ptr3] * 3; ugly5 = a[ptr5] * 5; Acoording to the pattern of merging lists, we just need to update the current pointer for each list is enough!!!
Solution:
public class Solution { public int nthUglyNumber(int n) { int[] a = new int ; a[0] = 1; int ptr2 = 0, ptr3 = 0, ptr5 = 0; int ugly2, ugly3, ugly5, min; for (int i = 1; i < n; i++) { ugly2 = a[ptr2] * 2; ugly3 = a[ptr3] * 3; ugly5 = a[ptr5] * 5; min = Math.min(ugly5, Math.min(ugly2, ugly3)); a[i] = min; if (min == ugly2) ptr2++; if (min == ugly3) ptr3++; if (min == ugly5) ptr5++; } return a[n-1]; } }
相关文章推荐
- Gradle--第一章 引言
- TCP-IP 相关基础知识备忘
- 大家好
- Objective-C 【@property 的参数问题】
- Objective-C 【@property 的参数问题】
- *Flatten Binary Tree to Linked List
- dired显示文件大小时使用"human readable"的格式显示
- Activity生命周期的保存,其中onSaveInstanceState和onRestoreInstanceState触发的时机
- [LeetCode]Kth Largest Element in an Array
- *Convert Sorted Array to Binary Search Tree
- js-AMD-CMD-依赖区别
- [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线
- 项目开发流程
- 在线服务之socket编程科普
- [android]service中再启service
- 基础知识 -- (编程语言)
- matrix设置原始位置
- 正弦函数如何变成声音
- android Matrix.setRotate 和 postRotate的区别
- [Unity5.1]同步动画UNET