Skip to content

Commit 9bdc702

Browse files
Solutions
1 parent c113e49 commit 9bdc702

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Solution {
2+
public int CountSquares(int[][] matrix) {
3+
int m = matrix.Length;
4+
int n = matrix[0].Length;
5+
int[,] f = new int[m, n];
6+
int ans = 0;
7+
8+
for (int i = 0; i < m; i++) {
9+
for (int j = 0; j < n; j++) {
10+
if (matrix[i][j] == 0) {
11+
continue;
12+
}
13+
if (i == 0 || j == 0) {
14+
f[i, j] = 1;
15+
} else {
16+
f[i, j] = Math.Min(f[i - 1, j - 1], Math.Min(f[i - 1, j], f[i, j - 1])) + 1;
17+
}
18+
ans += f[i, j];
19+
}
20+
}
21+
22+
return ans;
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
impl Solution {
2+
pub fn count_squares(matrix: Vec<Vec<i32>>) -> i32 {
3+
let m = matrix.len();
4+
let n = matrix[0].len();
5+
let mut f = vec![vec![0; n]; m];
6+
let mut ans = 0;
7+
8+
for i in 0..m {
9+
for j in 0..n {
10+
if matrix[i][j] == 0 {
11+
continue;
12+
}
13+
if i == 0 || j == 0 {
14+
f[i][j] = 1;
15+
} else {
16+
f[i][j] =
17+
std::cmp::min(f[i - 1][j - 1], std::cmp::min(f[i - 1][j], f[i][j - 1])) + 1;
18+
}
19+
ans += f[i][j];
20+
}
21+
}
22+
23+
ans
24+
}
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} mat
3+
* @return {number}
4+
*/
5+
var numSubmat = function (mat) {
6+
const m = mat.length;
7+
const n = mat[0].length;
8+
const g = Array.from({ length: m }, () => Array(n).fill(0));
9+
10+
for (let i = 0; i < m; i++) {
11+
for (let j = 0; j < n; j++) {
12+
if (mat[i][j]) {
13+
g[i][j] = j === 0 ? 1 : 1 + g[i][j - 1];
14+
}
15+
}
16+
}
17+
18+
let ans = 0;
19+
for (let i = 0; i < m; i++) {
20+
for (let j = 0; j < n; j++) {
21+
let col = Infinity;
22+
for (let k = i; k >= 0; k--) {
23+
col = Math.min(col, g[k][j]);
24+
ans += col;
25+
}
26+
}
27+
}
28+
29+
return ans;
30+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn num_submat(mat: Vec<Vec<i32>>) -> i32 {
3+
let m = mat.len();
4+
let n = mat[0].len();
5+
let mut g = vec![vec![0; n]; m];
6+
7+
for i in 0..m {
8+
for j in 0..n {
9+
if mat[i][j] == 1 {
10+
if j == 0 {
11+
g[i][j] = 1;
12+
} else {
13+
g[i][j] = 1 + g[i][j - 1];
14+
}
15+
}
16+
}
17+
}
18+
19+
let mut ans = 0;
20+
for i in 0..m {
21+
for j in 0..n {
22+
let mut col = i32::MAX;
23+
let mut k = i as i32;
24+
while k >= 0 && col > 0 {
25+
col = col.min(g[k as usize][j]);
26+
ans += col;
27+
k -= 1;
28+
}
29+
}
30+
}
31+
ans
32+
}
33+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
---
5+
6+
<!-- problem:start -->
7+
8+
# [3656. Determine if a Simple Graph Exists 🔒](https://leetcode.com/problems/determine-if-a-simple-graph-exists)
9+
10+
[README](/solution/3600-3699/3656.Determine%20if%20a%20Simple%20Graph%20Exists/README.md)
11+
12+
## Description
13+
14+
<!-- description:start -->
15+
16+
<p>You are given an integer array <code>degrees</code>, where <code>degrees[i]</code> represents the desired degree of the <code>i<sup>th</sup></code> vertex.</p>
17+
18+
<p>Your task is to determine if there exists an <strong>undirected simple</strong> graph with <strong>exactly</strong> these vertex degrees.</p>
19+
20+
<p>A <strong>simple</strong> graph has no self-loops or parallel edges between the same pair of vertices.</p>
21+
22+
<p>Return <code>true</code> if such a graph exists, otherwise return <code>false</code>.</p>
23+
24+
<p>&nbsp;</p>
25+
<p><strong class="example">Example 1:</strong></p>
26+
27+
<div class="example-block">
28+
<p><strong>Input:</strong> <span class="example-io">degrees = [3,1,2,2]</span></p>
29+
30+
<p><strong>Output:</strong> <span class="example-io">true</span></p>
31+
32+
<p><strong>Explanation:</strong></p>
33+
34+
<p><img src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3600-3699/3656.Determine%20if%20a%20Simple%20Graph%20Exists/images/screenshot-2025-08-13-at-24347-am.png" style="width: 200px; height: 132px;" />​​​​​​​</p>
35+
36+
<p>One possible undirected simple graph is:</p>
37+
38+
<ul>
39+
<li>Edges: <code>(0, 1), (0, 2), (0, 3), (2, 3)</code></li>
40+
<li>Degrees: <code>deg(0) = 3</code>, <code>deg(1) = 1</code>, <code>deg(2) = 2</code>, <code>deg(3) = 2</code>.</li>
41+
</ul>
42+
</div>
43+
44+
<p><strong class="example">Example 2:</strong></p>
45+
46+
<div class="example-block">
47+
<p><strong>Input:</strong> <span class="example-io">degrees = [1,3,3,1]</span></p>
48+
49+
<p><strong>Output:</strong> <span class="example-io">false</span></p>
50+
51+
<p><strong>Explanation:</strong>​​​​​​​</p>
52+
53+
<ul>
54+
<li><code>degrees[1] = 3</code> and <code>degrees[2] = 3</code> means they must be connected to all other vertices.</li>
55+
<li>This requires <code>degrees[0]</code> and <code>degrees[3]</code> to be at least 2, but both are equal to 1, which contradicts the requirement.</li>
56+
<li>Thus, the answer is <code>false</code>.</li>
57+
</ul>
58+
</div>
59+
60+
<p>&nbsp;</p>
61+
<p><strong>Constraints:</strong></p>
62+
63+
<ul>
64+
<li><code>1 &lt;= n == degrees.length &lt;= 10<sup>​​​​​​​5</sup></code></li>
65+
<li><code>0 &lt;= degrees[i] &lt;= n - 1</code></li>
66+
</ul>
67+
68+
<!-- description:end -->
69+
70+
## Solutions
71+
72+
<!-- solution:start -->
73+
74+
### Solution 1
75+
76+
<!-- tabs:start -->
77+
78+
#### Python3
79+
80+
```python
81+
82+
```
83+
84+
#### Java
85+
86+
```java
87+
88+
```
89+
90+
#### C++
91+
92+
```cpp
93+
94+
```
95+
96+
#### Go
97+
98+
```go
99+
100+
```
101+
102+
<!-- tabs:end -->
103+
104+
<!-- solution:end -->
105+
106+
<!-- problem:end -->

0 commit comments

Comments
 (0)