|
1 |
| -# Binary Tree Tilt |
2 |
| -Given the root of a binary tree, return the sum of every tree node's tilt. |
| 1 | +# Pairs of Songs With Total Durations Divisible by 60 |
| 2 | +You are given a list of songs where the ith song has a duration of time[i] seconds. |
3 | 3 |
|
4 |
| -The tilt of a tree node is the absolute difference between the sum of all left subtree node values and all right subtree node values. If a node does not have a left child, then the sum of the left subtree node values is treated as 0. The rule is similar if there the node does not have a right child. |
| 4 | +Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i, j such that i < j with (time[i] + time[j]) % 60 == 0. |
5 | 5 |
|
6 | 6 |
|
7 | 7 |
|
8 | 8 | Example 1:
|
9 | 9 |
|
10 |
| - |
11 |
| -Input: root = [1,2,3] |
12 |
| -Output: 1 |
13 |
| -Explanation: |
14 |
| -Tilt of node 2 : |0-0| = 0 (no children) |
15 |
| -Tilt of node 3 : |0-0| = 0 (no children) |
16 |
| -Tile of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3) |
17 |
| -Sum of every tilt : 0 + 0 + 1 = 1 |
| 10 | +Input: time = [30,20,150,100,40] |
| 11 | +Output: 3 |
| 12 | +Explanation: Three pairs have a total duration divisible by 60: |
| 13 | +(time[0] = 30, time[2] = 150): total duration 180 |
| 14 | +(time[1] = 20, time[3] = 100): total duration 120 |
| 15 | +(time[1] = 20, time[4] = 40): total duration 60 |
18 | 16 | Example 2:
|
19 | 17 |
|
20 |
| - |
21 |
| -Input: root = [4,2,9,3,5,null,7] |
22 |
| -Output: 15 |
23 |
| -Explanation: |
24 |
| -Tilt of node 3 : |0-0| = 0 (no children) |
25 |
| -Tilt of node 5 : |0-0| = 0 (no children) |
26 |
| -Tilt of node 7 : |0-0| = 0 (no children) |
27 |
| -Tilt of node 2 : |3-5| = 2 (left subtree is just left child, so sum is 3; right subtree is just right child, so sum is 5) |
28 |
| -Tilt of node 9 : |0-7| = 7 (no left child, so sum is 0; right subtree is just right child, so sum is 7) |
29 |
| -Tilt of node 4 : |(3+5+2)-(9+7)| = |10-16| = 6 (left subtree values are 3, 5, and 2, which sums to 10; right subtree values are 9 and 7, which sums to 16) |
30 |
| -Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15 |
31 |
| -Example 3: |
32 |
| - |
33 |
| - |
34 |
| -Input: root = [21,7,14,1,1,2,2,3,3] |
35 |
| -Output: 9 |
| 18 | +Input: time = [60,60,60] |
| 19 | +Output: 3 |
| 20 | +Explanation: All three pairs have a total duration of 120, which is divisible by 60. |
36 | 21 |
|
37 | 22 |
|
38 | 23 | Constraints:
|
39 | 24 |
|
40 |
| -The number of nodes in the tree is in the range [0, 104]. |
41 |
| --1000 <= Node.val <= 1000 <br> |
| 25 | +1 <= time.length <= 6 * 104 |
| 26 | +1 <= time[i] <= 500 <br> |
42 | 27 |
|
43 | 28 | ## Idea
|
44 | 29 |
|
|
0 commit comments