河的两边有相同数量的城市。从一侧的城市到另一侧的城市的桥梁由1#y3
代表,其中下方的城市1在上侧具有通往城市3的桥梁。我们必须找到非重叠桥的最大数量。因此,对于输入1#y2, 2#y4, 3#y1, 4#y5, 5#y3, 6#y6
,4
将1#y2, 2#y4, 4#y5, 6#y6
与public static int maxNonOverlappingBridges(String input1[]) {
int result = 0;
for (int i = 0; i < input1.length; i++) {
int total = 1;
int notCrossing = Integer.parseInt(input1[i].substring(input1[i].length() - 1));
for (int j = 0; j < input1.length; j++) {
if (j < i) {
if (Integer.parseInt(input1[j].substring(input1[j].length() - 1)) < notCrossing) {
total += 1;
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
} else if (j > i) {
if (Integer.parseInt(input1[j].substring(input1[j].length() - 1)) > notCrossing) {
total += 1;
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
} else {
notCrossing = Integer.parseInt(input1[j].substring(input1[j].length() - 1));
}
}
if (total > result) result = total;
}
return result;
}
不重叠。
这是我的代码 -
SELECT emp_name, position, absent,
(select sum(absent) from employee) as sum,
(absent*100)/(select sum(absent) from employee) AS 'percentage'
FROM employee
是否有更优化的算法?
答案 0 :(得分:1)
这在 O(nLog(n))中运行。
return {"project":o}
希望这有帮助。
答案 1 :(得分:1)
O(nLog(n)) ...
的简单解决方案openjdk\hotspot\src\share\vm\runtime\jniHandles.cpp
答案 2 :(得分:0)
public static int bridge_count(String[] input1,int input2)
{
int[][] track=new int[input2][input2];
int count=0;
for(int i=0;i<input1.length;i++)
{
String cityCon=input1[i];
String cityArray[]=cityCon.split("#");
int fst=Integer.parseInt(cityArray[0]);
int sec=Integer.parseInt(cityArray[1]);
track[fst-1][sec-1]=-1;
}
int found=0;
int maxCol=-1;
for(int c=0;c<input2;c++)
{
found=-1;
for(int j=0;j<input2;j++)
{
if(track[c][j]==-1)
{
found=j;
break;
}
}
if(found!=-1 && maxCol<=found)
{
count++;
maxCol=found;
}
}
return count;
}