会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Jetz
不信书,信运气。
博客园
首页
新随笔
管理
google编程挑战赛Round1的前两道题
差了一点,看来的确不适合这项运动了。
public
int
chooseSeat(String row)
{
int
[] min
=
new
int
[row.Length];
for
(
int
i
=
0
;i
<
row.Length
-
1
;i
++
)
{
min[i]
=
IsOk(row,i);
}
int
j
=-
1
;
int
mine
=-
1
;
for
(
int
i
=
0
;i
<
row.Length
-
1
;i
++
)
{
if
(min[i]
>
mine)
{
j
=
i;
mine
=
min[i];
}
}
return
j;
}
public
int
IsOk(
string
row,
int
i)
{
if
(row[i]
!=
'
X
'
&&
row[i
+
1
]
!=
'
X
'
)
{
int
l1
=
i;
int
l2
=
row.Length
-
i
-
1
;
return
l1
<
l2
?
l1:l2;
}
else
return
-
1
;
}
public
int
cover(
int
[] heights)
{
if
(heights.Length
==
1
)
return
1
;
int
imax
=-
1
;
int
max
=-
1
;
for
(
int
i
=
0
;i
<
heights.Length;i
++
)
{
if
(heights[i]
>
max)
{
imax
=
i;
max
=
heights[i];
}
}
if
(lbstate(heights,imax)
==-
1
)
{
heights[imax]
--
;
return
cover(heights);
}
if
(lbstate(heights,imax)
==
1
)
{
heights[imax]
--
;
return
cover(heights)
+
1
;
}
if
(lbstate(heights,imax)
==
0
)
{
int
[] h2
=
new
int
[heights.Length
-
1
];
int
j
=
0
;
for
(
int
i
=
0
;i
<
heights.Length;i
++
)
{
if
(i
!=
imax
+
1
)
{
h2[j]
=
heights[i];
j
++
;
}
}
return
cover(h2);
}
return
0
;
}
public
int
lbstate(
int
[] h,
int
idx)
{
if
(idx
-
1
>=
0
)
{
if
(h[idx
-
1
]
==
h[idx])
return
0
;
}
if
(idx
+
1
<
h.Length)
{
if
(h[idx
+
1
]
==
h[idx])
return
0
;
}
if
(idx
-
1
>=
0
)
{
if
(h[idx
-
1
]
==
h[idx]
-
1
)
return
1
;
}
if
(idx
+
1
<
h.Length)
{
if
(h[idx
+
1
]
==
h[idx]
-
1
)
return
1
;
}
return
-
1
;
}
posted @
2005-12-19 23:51
jetz
阅读(
318
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告