Skip to content

Commit 3df27d9

Browse files
committed
Java solution 264 && 295
1 parent 7c1365c commit 3df27d9

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

java/_264UglyNumberII.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Write a program to find the n-th ugly number.
3+
* Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9,
4+
* 10, 12 is the sequence of the first 10 ugly numbers.
5+
* <p>
6+
* Note that 1 is typically treated as an ugly number, and n does not exceed 1690.
7+
* <p>
8+
* Credits:
9+
* Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
10+
* <p>
11+
* Created by drfish on 12/06/2017.
12+
*/
13+
public class _264UglyNumberII {
14+
public int nthUglyNumber(int n) {
15+
int index2 = 0, index3 = 0, index5 = 0;
16+
int[] dp = new int[n];
17+
dp[0] = 1;
18+
for (int i = 1; i < n; i++) {
19+
dp[i] = Math.min(dp[index2] * 2, Math.min(dp[index3] * 3, dp[index5] * 5));
20+
if (dp[i] == dp[index2] * 2) {
21+
index2++;
22+
}
23+
if (dp[i] == dp[index3] * 3) {
24+
index3++;
25+
}
26+
if (dp[i] == dp[index5] * 5) {
27+
index5++;
28+
}
29+
}
30+
return dp[n - 1];
31+
}
32+
33+
public static void main(String[] args) {
34+
_264UglyNumberII solution = new _264UglyNumberII();
35+
assert 1 == solution.nthUglyNumber(1);
36+
assert 12 == solution.nthUglyNumber(10);
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.util.PriorityQueue;
2+
3+
/**
4+
* Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value.
5+
* So the median is the mean of the two middle value.
6+
* <p>
7+
* Examples:
8+
* [2,3,4] , the median is 3
9+
* [2,3], the median is (2 + 3) / 2 = 2.5
10+
* <p>
11+
* Design a data structure that supports the following two operations:
12+
* void addNum(int num) - Add a integer number from the data stream to the data structure.
13+
* double findMedian() - Return the median of all elements so far.
14+
* <p>
15+
* For example:
16+
* addNum(1)
17+
* addNum(2)
18+
* findMedian() -> 1.5
19+
* addNum(3)
20+
* findMedian() -> 2
21+
* <p>
22+
* Credits:
23+
* Special thanks to @Louis1992 for adding this problem and creating all test cases.
24+
* <p>
25+
* Created by drfish on 13/06/2017.
26+
*/
27+
public class _295FindMedianFromDataStream {
28+
public class MedianFinder {
29+
private PriorityQueue<Integer> minHeap;
30+
private PriorityQueue<Integer> maxHeap;
31+
private boolean isEven = true;
32+
33+
/**
34+
* initialize your data structure here.
35+
*/
36+
public MedianFinder() {
37+
minHeap = new PriorityQueue<>();
38+
maxHeap = new PriorityQueue<>((n1, n2) -> n2 - n1);
39+
}
40+
41+
public void addNum(int num) {
42+
if (isEven) {
43+
minHeap.offer(num);
44+
maxHeap.offer(minHeap.poll());
45+
} else {
46+
maxHeap.offer(num);
47+
minHeap.offer(maxHeap.poll());
48+
}
49+
isEven = !isEven;
50+
}
51+
52+
public double findMedian() {
53+
if (isEven) {
54+
return (maxHeap.peek() + minHeap.peek()) / 2.0;
55+
} else {
56+
return maxHeap.peek();
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)