Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g4001_4100.s4015_weighted_sum_of_a_tree;

// #Medium #Array #Tree #Senior #Weekly_Contest_514 #Depth_First_Search
// #2026_11_19_Time_5_ms_(100.00%)_Space_107.29_MB_(94.38%)

public class Solution {
public long weightedSum(int[] parent, int[] nums) {
int n = parent.length;
int[] depth = new int[n];
depth[0] = 1;
for (int i = 1; i < n; i++) {
if (depth[i] != 0) {
continue;
}
int currDepth = getDepth(parent, depth, i);
int curr = i;
while (depth[curr] == 0) {
depth[curr] = currDepth;
curr = parent[curr];
currDepth -= 1;
}
}
long res = 0;
int height = 0;
for (int i = 0; i < n; i++) {
height = Math.max(height, depth[i]);
}
for (int i = 0; i < n; i++) {
res += (long) (height - depth[i] + 1) * nums[i];
}
return res;
}

private int getDepth(int[] parent, int[] currDepth, int node) {
int add = 0;
while (currDepth[node] == 0) {
node = parent[node];
add += 1;
}
return currDepth[node] + add;
}
}
70 changes: 70 additions & 0 deletions src/main/java/g4001_4100/s4015_weighted_sum_of_a_tree/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
4015\. Weighted Sum of a Tree

Medium

You are given an integer array `parent` of length `n` representing a rooted tree with nodes labeled from 0 to `n - 1`.

The tree is **rooted** at node 0, so `parent[0] = -1`. For each node `i` where `1 <= i <= n - 1`, `parent[i]` denotes the parent of node `i`.

You are also given an integer array `nums` of length `n`, where `nums[i]` denotes the value of node `i`.

The weight of a node `i` at depth `d` is `nums[i] * (h - d + 1)`, where `h` is the height of the tree.

Return the **sum** of the weights of all nodes in the tree.

The **depth** of a node is the number of nodes on the path from the root to that node, inclusive, with the root having depth 1.

The **height** of the tree is the maximum depth among all nodes in the tree.

**Example 1:**

![](https://assets.leetcode.com/uploads/2026/06/09/t1.png)

**Input:** parent = [-1,0,0,0,2,2], nums = [5,2,3,1,4,6]

**Output:** 37

**Explanation:**

The height of the tree is 3.

| Node | `nums[i]` | Depth (`d`) | Weight |
| --- | --- | --- | --- |
| 0 | 5 | 1 | `5 * (3 - 1 + 1) = 15` |
| 1 | 2 | 2 | `2 * (3 - 2 + 1) = 4` |
| 2 | 3 | 2 | `3 * (3 - 2 + 1) = 6` |
| 3 | 1 | 2 | `1 * (3 - 2 + 1) = 2` |
| 4 | 4 | 3 | `4 * (3 - 3 + 1) = 4` |
| 5 | 6 | 3 | `6 * (3 - 3 + 1) = 6` |

The sum of all node weights is `15 + 4 + 6 + 2 + 4 + 6 = 37`.

**Example 2:**

![](https://assets.leetcode.com/uploads/2026/06/09/t2.png)

**Input:** parent = [-1,0,1,2], nums = [1,2,3,4]

**Output:** 20

**Explanation:**

The height of the tree is 4.

| Node | `nums[i]` | Depth (`d`) | Weight |
| --- | --- | --- | --- |
| 0 | 1 | 1 | `1 * (4 - 1 + 1) = 4` |
| 1 | 2 | 2 | `2 * (4 - 2 + 1) = 6` |
| 2 | 3 | 3 | `3 * (4 - 3 + 1) = 6` |
| 3 | 4 | 4 | `4 * (4 - 4 + 1) = 4` |

The sum of all node weights is `4 + 6 + 6 + 4 = 20`.

**Constraints:**

* <code>1 <= n <= 10<sup>5</sup></code>
* `n == parent.length == nums.length`
* `parent[0] == -1`
* `0 <= parent[i] <= n - 1` for all `i` in `[1, n - 1]`
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
* The input is generated such that the array `parent` represents a valid tree rooted at node 0.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package g4001_4100.s4016_maximum_area_of_two_non_overlapping_square_submatrices;

// #Medium #Array #Dynamic_Programming #Binary_Search #Matrix #Staff #Weekly_Contest_514
// #2026_11_19_Time_12_ms_(98.63%)_Space_183.92_MB_(94.52%)

@SuppressWarnings("java:S6885")
public class Solution {
public int maxArea(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[][] dp = new int[m][n];
int[] top = new int[m];
int[] bottom = new int[m];
int[] left = new int[n];
int[] right = new int[n];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (mat[r][c] == 0) {
continue;
}
if (r == 0 || c == 0) {
dp[r][c] = 1;
} else {
dp[r][c] = 1 + Math.min(dp[r - 1][c - 1], Math.min(dp[r - 1][c], dp[r][c - 1]));
}
int k = dp[r][c];
top[r] = Math.max(top[r], k);
left[c] = Math.max(left[c], k);
bottom[r - k + 1] = Math.max(bottom[r - k + 1], k);

right[c - k + 1] = Math.max(right[c - k + 1], k);
}
}
for (int r = 1; r < m; r++) {
top[r] = Math.max(top[r], top[r - 1]);
}
for (int c = 1; c < n; c++) {
left[c] = Math.max(left[c], left[c - 1]);
}
for (int r = m - 2; r >= 0; r--) {
bottom[r] = Math.max(bottom[r], bottom[r + 1]);
}
for (int c = n - 2; c >= 0; c--) {
right[c] = Math.max(right[c], right[c + 1]);
}
int best = 0;
for (int r = 0; r < m - 1; r++) {
best = Math.max(best, Math.min(top[r], bottom[r + 1]));
}
for (int c = 0; c < n - 1; c++) {
best = Math.max(best, Math.min(left[c], right[c + 1]));
}
return best * best;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
4016\. Maximum Area of Two Non-Overlapping Square Submatrices

Medium

You are given a 2D integer matrix `mat` of size `m × n`, where:

* `mat[r][c] == 1` means the cell at row `r` and column `c` is usable.
* `mat[r][c] == 0` means it is not usable.

Your task is to find **two submatrices** that satisfy the following conditions:

* Both submatrices must be squares of the same side length `k`.
* The two submatrices must not share any cell.
* Each submatrix can only cover cells where `mat[r][c] == 1`.

Return the **maximum possible area** of each of the two squares. If it is not possible to choose two such squares, return 0.

**Example 1:**

![](https://assets.leetcode.com/uploads/2026/06/13/image.png)

**Input:** mat = [[1,1,1,0],[1,1,1,1],[0,0,1,1]]

**Output:** 4

**Explanation:**

The largest equal non-overlapping squares have side length `k = 2` with area 4.

* First square starts at top-left `(0, 0)` and covers cells `(0, 0)`, `(0, 1)`, `(1, 0)`, and `(1, 1)`.
* Second square starts at top-left `(1, 2)` and covers cells `(1, 2)`, `(1, 3)`, `(2, 2)`, and `(2, 3)`.

Thus, the answer is 4.

**Example 2:**

![](https://assets.leetcode.com/uploads/2026/06/13/screenshot-2026-06-13-at-83728pm.png)

**Input:** mat = [[0,1],[1,0]]

**Output:** 1

**Explanation:**

The largest equal non-overlapping squares have side length `k = 1` with area 1.

* First square starts at top-left `(0, 1)` and covers cell `(0, 1)`.
* Second square starts at top-left `(1, 0)` and covers cell `(1, 0)`.

Thus, the answer is 1.

**Example 3:**

![](https://assets.leetcode.com/uploads/2026/06/13/screenshot-2026-06-13-at-83751pm.png)

**Input:** mat = [[0,0],[0,1]]

**Output:** 0

**Explanation:**

There is only one usable cell, so it is impossible to choose two non-overlapping squares. Thus, the answer is 0.

**Constraints:**

* `mat.length == m`
* `mat[i].length == n`
* `1 <= m, n <= 500`
* `mat[i][j]` is either 0 or 1.
Loading
Loading