Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
XILINN
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm
/
src
/
class048
/
Code02_LargestOneBorderedSquare.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
55 lines (49 loc) · 1.73 KB
main
Breadcrumbs
algorithm
/
src
/
class048
/
Code02_LargestOneBorderedSquare.java
Copy path
Top
File metadata and controls
Code
Blame
55 lines (49 loc) · 1.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
class048
;
// 边框为1的最大正方形
// 给你一个由若干 0 和 1 组成的二维网格 grid
// 请你找出边界全部由 1 组成的最大 正方形 子网格
// 并返回该子网格中的元素数量。如果不存在,则返回 0。
// 测试链接 : https://leetcode.cn/problems/largest-1-bordered-square/
public
class
Code02_LargestOneBorderedSquare
{
// 打败比例不高,但完全是常数时间的问题
// 时间复杂度O(n * m * min(n,m)),额外空间复杂度O(1)
// 复杂度指标上绝对是最优解
public
static
int
largest1BorderedSquare
(
int
[][]
g
) {
int
n
=
g
.
length
;
int
m
=
g
[
0
].
length
;
build
(
n
,
m
,
g
);
if
(
sum
(
g
,
0
,
0
,
n
-
1
,
m
-
1
) ==
0
) {
return
0
;
}
// 找到的最大合法正方形的边长
int
ans
=
1
;
for
(
int
a
=
0
;
a
<
n
;
a
++) {
for
(
int
b
=
0
;
b
<
m
;
b
++) {
// (a,b)所有左上角点
// (c,d)更大边长的右下角点,k是当前尝试的边长
for
(
int
c
=
a
+
ans
,
d
=
b
+
ans
,
k
=
ans
+
1
;
c
<
n
&&
d
<
m
;
c
++,
d
++,
k
++) {
if
(
sum
(
g
,
a
,
b
,
c
,
d
) -
sum
(
g
,
a
+
1
,
b
+
1
,
c
-
1
,
d
-
1
) == (
k
-
1
) <<
2
) {
ans
=
k
;
}
}
}
}
return
ans
*
ans
;
}
// g : 原始二维数组
// 把g变成原始二维数组的前缀和数组sum,复用自己
// 不能补0行,0列,都是0
public
static
void
build
(
int
n
,
int
m
,
int
[][]
g
) {
for
(
int
i
=
0
;
i
<
n
;
i
++) {
for
(
int
j
=
0
;
j
<
m
;
j
++) {
g
[
i
][
j
] +=
get
(
g
,
i
,
j
-
1
) +
get
(
g
,
i
-
1
,
j
) -
get
(
g
,
i
-
1
,
j
-
1
);
}
}
}
public
static
int
sum
(
int
[][]
g
,
int
a
,
int
b
,
int
c
,
int
d
) {
return
a
>
c
?
0
: (
g
[
c
][
d
] -
get
(
g
,
c
,
b
-
1
) -
get
(
g
,
a
-
1
,
d
) +
get
(
g
,
a
-
1
,
b
-
1
));
}
public
static
int
get
(
int
[][]
g
,
int
i
,
int
j
) {
return
(
i
<
0
||
j
<
0
) ?
0
:
g
[
i
][
j
];
}
}
You can’t perform that action at this time.