1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-14 01:49:41 +01:00
Commit graph

168 commits

Author SHA1 Message Date
3a111bd4cf
problems(rs): add “1557. Minimum Number of Vertices to Reach All Nodes”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-05-19 00:39:55 +02:00
4bb6d712e2
problems(js): add “2636. Promise Pool”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-05-18 20:20:38 +02:00
dbf39a2ad0
problems(js): add “2637. Promise Time Limit”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-05-18 20:10:57 +02:00
d5c96c6a21
problems(js): add “2621. Sleep”
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-05-18 19:42:47 +02:00
ac8a1720ef
problems(js): add “2632. Curry”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-14 11:00:17 +02:00
dd5aeef5f6
problems(js): add “2623. Memoize”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-14 00:05:44 +02:00
f66cfc3cae
problems(cpp): add “2466. Count Ways To Build Good Strings”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-13 23:56:24 +02:00
ddc1753842
problems(js): add “2666. Allow One Function Call”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:33:13 +02:00
f7cdba6935
problems(js): add “2629. Function Composition”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:32:50 +02:00
0c23faf222
problems(js): add “2626. Array Reduce Transformation”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:32:21 +02:00
56d6a05ce4
problems(js): add “2634. Filter Elements from Array”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:31:44 +02:00
d18930ad7e
problems(js): add “2635. Apply Transform Over Each Element in Array”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:31:11 +02:00
42c87b3aca
problems(js): add “2665. Counter II”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:30:40 +02:00
84885aa0c9
problems(js): add “2620. Counter”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:30:08 +02:00
19541ce1e7
problems(js): add “2667. Create Hello World Function”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 17:29:13 +02:00
f9ecc962a9
problems(rs): add “2140. Solving Questions With Brainpower”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-12 16:33:08 +02:00
a1a9c876e6
problems(cpp): add “59. Spiral Matrix II”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-10 10:36:07 +02:00
7147f610a2
problems(cpp): add “54. Spiral Matrix”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-09 23:47:07 +02:00
e5c5a79e70
problems(cpp): add “1572. Matrix Diagonal Sum”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-08 22:09:27 +02:00
0965799a48
problems(rs): add “1964. Find the Longest Valid Obstacle Course at Each Position”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-05-08 01:08:03 +02:00
ae2082c39c
problems(rs): add “258. Add Digits”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-27 21:01:46 +02:00
0d66103a07
problems(rs): add “2336. Smallest Number in Infinite Set”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-25 14:17:31 +02:00
748624e5ee
problems(cpp): add “1768. Merge Strings Alternately”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-19 00:17:54 +02:00
d8009b9a4a
problems(rs): add “1431. Kids With the Greatest Number of Candies”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-17 18:51:10 +02:00
43d043b5e1
problems(rs): add “303. Range Sum Query - Immutable”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-16 00:05:53 +02:00
09a14a0e3d
problems(rs): add “2218. Maximum Value of K Coins From Piles”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-15 19:29:45 +02:00
01b25b0ffe
problems(rs): add “516. Longest Palindromic Subsequence”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-14 22:40:09 +02:00
2c3aa74565
problems(rs): add “946. Validate Stack Sequences”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-13 19:24:05 +02:00
551b7819cc
problems(rs): add “71. Simplify Path”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-13 08:13:07 +02:00
5a62aba9c1
problems(cpp): add “2390. Removing Stars From a String”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-11 18:47:54 +02:00
10b6b3dd69
problems(cpp): add “20. Valid Parentheses”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-10 20:03:58 +02:00
1388a820e7
problems(cpp): add “881. Boats to Save People”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-04-10 12:21:07 +02:00
ec2b3df836
problems(cpp): add „100. Same Tree“
Signed-off-by: Matej Focko <me@mfocko.xyz>
2023-01-10 23:16:12 +01:00
63dad4c93c
problems(rs): add „451. Sort Characters By Frequency“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-12-03 22:41:54 +01:00
5d51c78a08
problems(cpp): add „1657. Determine if Two Strings Are Close“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-12-02 20:13:07 +01:00
fb8f4944c8
problems(cpp): add „393. UTF-8 Validation“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-14 10:31:37 +02:00
1700370e2f
problems(rs): add „188. Best Time to Buy and Sell Stock IV“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-10 22:33:42 +02:00
dd94963a2f
problems(cpp): add „1448. Count Good Nodes in Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-09 18:59:12 +02:00
f7ebb9037d
problems(kt): add „2133. Check if Every Row and Column Contains All Numbers“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-09 18:58:35 +02:00
1873ff8016
chore: update gitignore
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-09 18:57:31 +02:00
e3888415f7
chore: fix formatting
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-09 18:56:13 +02:00
d7f8f4fd7b
problems(rs): add „1996. The Number of Weak Characters in the Game“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-09 14:02:07 +02:00
9f4f02ac24
problems(rs): add „94. Binary Tree Inorder Traversal“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-08 13:59:46 +02:00
4838f221f3
problems(kt): add „606. Construct String from Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-07 13:24:41 +02:00
20e3625bd7
problems(cs): add „814. Binary Tree Pruning“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-06 18:35:48 +02:00
23a0bb299a
problems(java): add „429. N-ary Tree Level Order Traversal“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-05 11:31:36 +02:00
0bb6581d69
problems(cpp): add „987. Vertical Order Traversal of a Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-04 23:02:44 +02:00
b9d382731b
problems(cpp): add „967. Numbers With Same Consecutive Differences“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-03 23:52:07 +02:00
227124b327
problems(cs): add „637. Average of Levels in Binary Tree“
Signed-off-by: Matej Focko <mfocko@redhat.com>

diff --git a/problems/average-of-levels-in-binary-tree.cs b/problems/average-of-levels-in-binary-tree.cs
new file mode 100644
index 0000000..f2ba5aa
--- /dev/null
+++ b/problems/average-of-levels-in-binary-tree.cs
@@ -0,0 +1,72 @@
+public class TreeNode
+{
+    public int val;
+    public TreeNode? left;
+    public TreeNode? right;
+    public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
+    {
+        this.val = val;
+        this.left = left;
+        this.right = right;
+    }
+}
+
+
+public class Solution
+{
+    private class Level
+    {
+        long sum;
+        int counter;
+
+        public Level()
+        {
+            sum = 0;
+            counter = 0;
+        }
+
+        public void Add(int x)
+        {
+            sum += x;
+            counter++;
+        }
+
+        public double Average
+        {
+            get => sum / (double)counter;
+        }
+    }
+
+    private List<Level> AverageOfLevels(List<Level> averages, TreeNode? node, int level)
+    {
+        if (node == null)
+        {
+            return averages;
+        }
+
+        if (level == averages.Count)
+        {
+            averages.Add(new Level());
+        }
+        averages[level].Add(node.val);
+
+        AverageOfLevels(averages, node.left, level + 1);
+        AverageOfLevels(averages, node.right, level + 1);
+
+        return averages;
+    }
+
+    public IList<double> AverageOfLevels(TreeNode? root)
+        => AverageOfLevels(new List<Level>(), root, 0).Select(level => level.Average).ToList();
+
+    public static void Main()
+    {
+        var s = new Solution();
+
+        foreach (var a in s.AverageOfLevels(new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)))))
+        {
+            Console.Write($"{a} ");
+        }
+        Console.WriteLine();
+    }
+}
2022-09-02 21:11:03 +02:00
280a674871
problems(rs): add „417. Pacific Atlantic Water Flow“
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-09-01 01:01:53 +02:00