Skip to content

Commit eba686d

Browse files
committed
Update
1 parent eaa8947 commit eba686d

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

Interviews/Ohters/Bai6.PNG

91.2 KB
Loading

Interviews/Ohters/Bai6.java

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import java.util.ArrayList;
2+
import java.util.Collection;
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.Random;
7+
import java.util.Scanner;
8+
9+
/**
10+
* HoangVanCong
11+
*/
12+
public class HoangVanCong {
13+
// ***************************** DS ****************************/
14+
public static final int MAX = 100;
15+
16+
public static class Range {
17+
public final int begin;
18+
public final int end;
19+
public final int size;
20+
21+
public Range(int begin, int end) {
22+
this.begin = begin;
23+
this.end = end;
24+
this.size = this.end - this.begin - 1;
25+
}
26+
27+
public boolean isValid() {
28+
return this.size > 0;
29+
}
30+
31+
public void show() {
32+
System.out.println("size: " + size);
33+
for (int i = begin + 1; i < this.end; i++) {
34+
System.out.print(i + " ");
35+
}
36+
System.out.println();
37+
}
38+
39+
public boolean isDistinct(Range other){
40+
if((this.end <= other.begin) || (this.begin >= other.end)){
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "(" + this.begin + "; " + this.end + ")";
49+
}
50+
}
51+
52+
public static class SortByBegin implements Comparator<Range> {
53+
@Override
54+
public int compare(HoangVanCong.Range o1, HoangVanCong.Range o2) {
55+
return Integer.compare(o1.begin, o2.begin);
56+
}
57+
}
58+
59+
public static class SortBySize implements Comparator<Range> {
60+
@Override
61+
public int compare(HoangVanCong.Range o1, HoangVanCong.Range o2) {
62+
if(o1.size != o2.size){
63+
return Integer.compare(o1.size, o2.size);
64+
}
65+
return Integer.compare(o1.begin, o2.begin);
66+
}
67+
}
68+
69+
// ***************************** Main Processes ****************************/
70+
// ! Task 1
71+
public static List<Range> generateRange(int N) {
72+
List<Range> list = new ArrayList<>();
73+
Random random = new Random();
74+
75+
// 30% Generate nagative range
76+
int negativeCount = (int) Math.ceil((N / 10.0) * 3);
77+
int positiveCount = N - negativeCount;
78+
// System.out.println("negativeCount: " + negativeCount + " othersCount: " + positiveCount);
79+
80+
for (int i = 0; i < N; i++) {
81+
int begin, end;
82+
if (i < negativeCount) {
83+
begin = random.nextInt(MAX) - MAX;
84+
} else {
85+
begin = random.nextInt(MAX);
86+
}
87+
end = begin + 2 + random.nextInt(MAX);
88+
list.add(new Range(begin, end));
89+
}
90+
91+
return list;
92+
}
93+
94+
// ! Task 2
95+
public static List<Range> findMaxNumDistinctRanges(List<Range> ranges) {
96+
List<Range> results = new ArrayList<>();
97+
int N = ranges.size();
98+
99+
//maxCount[i] = max number of distinct that have to include the ith range
100+
int maxCount[] = new int[N];
101+
102+
// k = previous[i] means If I have to take ith range, the previous range I should take i k
103+
int previousSelected[] = new int[N];
104+
105+
// Preprocess the range
106+
Collections.sort(ranges, new SortByBegin());
107+
108+
int finalMaxIndex = 0;
109+
int maxNumberOfRange = 0;
110+
for (int i = 0; i < ranges.size(); i++) {
111+
previousSelected[i] = -1;
112+
int curMaxCount = 1;
113+
Range curRange = ranges.get(i);
114+
// Find previous maxCount that can add currentRange
115+
for (int j = i-1; j >= 0; j--) {
116+
Range prevRange = ranges.get(j);
117+
if(curRange.isDistinct(prevRange)){
118+
if(maxCount[j] + 1 > curMaxCount){
119+
curMaxCount = maxCount[j] + 1;
120+
previousSelected[i] = j;
121+
}
122+
}
123+
}
124+
maxCount[i] = curMaxCount;
125+
if(maxCount[i] > maxNumberOfRange){
126+
maxNumberOfRange = maxCount[i];
127+
finalMaxIndex = i;
128+
}
129+
}
130+
131+
// trace back to get results
132+
int i = finalMaxIndex;
133+
while(i != -1){
134+
results.add(ranges.get(i));
135+
i = previousSelected[i];
136+
}
137+
138+
Collections.sort(results, new SortBySize());
139+
140+
return results;
141+
}
142+
143+
public static void main(String[] args) {
144+
Scanner scanner = new Scanner(System.in);
145+
System.out.print("N = ");
146+
int N = scanner.nextInt();
147+
148+
//! Task 1
149+
System.out.println("=== Task 1 ===");
150+
var generatedList = generateRange(N);
151+
System.out.println(N);
152+
for (Range range : generatedList) {
153+
System.out.println(range);
154+
}
155+
156+
//! Task 2
157+
var result = findMaxNumDistinctRanges(generatedList);
158+
System.out.println("=== Task 2 ===");
159+
for (Range range : result) {
160+
System.out.println(range);
161+
}
162+
163+
// TEST_1();
164+
// TEST_2();
165+
}
166+
167+
public static void TEST_1(){
168+
List<Range> inputs = new ArrayList<>();
169+
inputs.add(new Range(1, 4));
170+
inputs.add(new Range(-1, 10));
171+
inputs.add(new Range(-4, 0));
172+
var result = findMaxNumDistinctRanges(inputs);
173+
}
174+
175+
public static void TEST_2(){
176+
List<Range> inputs = new ArrayList<>();
177+
inputs.add(new Range(5, 7));
178+
inputs.add(new Range(3, 5));
179+
inputs.add(new Range(1, 3));
180+
var result = findMaxNumDistinctRanges(inputs);
181+
}
182+
}

0 commit comments

Comments
 (0)