-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxeightOfTriangle.java
More file actions
93 lines (81 loc) · 2.88 KB
/
MaxeightOfTriangle.java
File metadata and controls
93 lines (81 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class MaxeightOfTriangle{
static int maxHeight(int red,int blue){
// int height =0;
// if(red==1 && (blue>1 || blue==1)){
// height=1;
// }else if(red>1 && (blue==1)){
// return 1;
// }
// return height;
// int height=0;
// int rowNo=1;
// boolean usingRed=true;
// while(true){
// if(usingRed){
// if(red>=rowNo){
// red=red-rowNo;
// }else{
// break;
// }
// }
// else{
// if(blue>=rowNo){
// blue=blue-rowNo;
// }else{
// break;
// }
// } height++;
// rowNo++;
// }return height;
// if((red==1 && blue>1) || (blue==1 && (red>1))){
// height=2;
// }else if(red==1 && blue==1){
// height=1;
// }else if(red==blue){
// height=(red+blue)/2;
// }else{
// height=((red+blue)/2)+1;
// }
// return height;
int count1 =0;
int count2=0;
int r1=red,b1=blue;
int r2=red,b2=blue;
int rowNo=1;
boolean usedBlue=true;
boolean usedRed=true;
while(r1>=0 && b1>=0){
count1++;
if(usedBlue){
if(b1>=count1){
b1=b1-count1;
}else{
break;
}
}else{
if(r1>=count1){
r1=r1-count1;
}else{
break;
}
} usedBlue=!usedBlue;
}
while(r2>=0 && b2>=0){
count2++;
if(usedRed){
if(r2>=count2){
r2=r2-count2;
}else{
break;
}
}else{
if(b2>=count2){
b2=b2-count2;
}else{
break;
}
} usedRed=!usedRed;
}
return Math.max(count1-1, count2-1);
}
}