Skip to content

Commit f34cdeb

Browse files
authored
Added tasks 508, 509, 511, 513.
1 parent 894f615 commit f34cdeb

File tree

13 files changed

+397
-0
lines changed

13 files changed

+397
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
372372

373373
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
374374
|-|-|-|-|-|-
375+
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i/script.sql)| Easy | LeetCode_Curated_SQL_70, Database | 790 | 45.04
375376

376377
#### Day 9 Control of Flow
377378

@@ -447,6 +448,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
447448

448449
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
449450
|-|-|-|-|-|-
451+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number/Solution.kt)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
450452
| 0070 |[Climbing Stairs](src.save/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06
451453

452454
#### Day 11 Dynamic Programming
@@ -777,6 +779,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
777779
| 0152 |[Maximum Product Subarray](src.save/main/kotlin/g0101_0200/s0152_maximum_product_subarray/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 253 | 88.42
778780
| 0198 |[House Robber](src.save/main/kotlin/g0101_0200/s0198_house_robber/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24
779781
| 0213 |[House Robber II](src.save/main/kotlin/g0201_0300/s0213_house_robber_ii/Solution.kt)| Medium | Array, Dynamic_Programming | 257 | 59.62
782+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number/Solution.kt)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
780783
| 0070 |[Climbing Stairs](src.save/main/kotlin/g0001_0100/s0070_climbing_stairs/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06
781784
| 0064 |[Minimum Path Sum](src.save/main/kotlin/g0001_0100/s0064_minimum_path_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 222 | 95.70
782785
| 0300 |[Longest Increasing Subsequence](src.save/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28
@@ -1494,6 +1497,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
14941497

14951498
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14961499
|-|-|-|-|-|-
1500+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number/Solution.kt)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
14971501

14981502
#### Day 2
14991503

@@ -1641,6 +1645,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16411645
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16421646
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16431647
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1648+
| 0513 |[Find Bottom Left Tree Value](src/main/kotlin/g0501_0600/s0513_find_bottom_left_tree_value/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 190 | 88.24
1649+
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i/script.sql)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 790 | 45.04
1650+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number/Solution.kt)| Easy | Dynamic_Programming, Math, Recursion, Memoization, Dynamic_Programming_I_Day_1, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming | 139 | 82.72
1651+
| 0508 |[Most Frequent Subtree Sum](src/main/kotlin/g0501_0600/s0508_most_frequent_subtree_sum/Solution.kt)| Medium | Hash_Table, Depth_First_Search, Tree, Binary_Tree | 246 | 80.00
16441652
| 0507 |[Perfect Number](src/main/kotlin/g0501_0600/s0507_perfect_number/Solution.kt)| Easy | Math | 172 | 81.82
16451653
| 0506 |[Relative Ranks](src/main/kotlin/g0501_0600/s0506_relative_ranks/Solution.kt)| Easy | Array, Sorting, Heap_Priority_Queue | 244 | 94.23
16461654
| 0504 |[Base 7](src/main/kotlin/g0501_0600/s0504_base_7/Solution.kt)| Easy | Math | 136 | 100.00
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g0501_0600.s0508_most_frequent_subtree_sum
2+
3+
// #Medium #Hash_Table #Depth_First_Search #Tree #Binary_Tree
4+
// #2023_01_10_Time_246_ms_(80.00%)_Space_38.4_MB_(93.33%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
private val cache = mutableMapOf<Int, Int>()
20+
21+
fun findFrequentTreeSum(root: TreeNode?): IntArray {
22+
treeSum(root)
23+
if (cache.isEmpty()) {
24+
return IntArray(0)
25+
}
26+
val max = cache.maxBy { it.value }.value
27+
return cache.filter { it.value == max }.map { it.key }.toIntArray()
28+
}
29+
30+
private fun treeSum(node: TreeNode?): Int {
31+
if (node == null) {
32+
return 0
33+
} else {
34+
val sum = node.`val` + treeSum(node.left) + treeSum(node.right)
35+
cache[sum] = cache.getOrDefault(sum, 0) + 1
36+
return sum
37+
}
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
508\. Most Frequent Subtree Sum
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return the most frequent **subtree sum**. If there is a tie, return all the values with the highest frequency in any order.
6+
7+
The **subtree sum** of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself).
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/24/freq1-tree.jpg)
12+
13+
**Input:** root = [5,2,-3]
14+
15+
**Output:** [2,-3,4]
16+
17+
**Example 2:**
18+
19+
![](https://assets.leetcode.com/uploads/2021/04/24/freq2-tree.jpg)
20+
21+
**Input:** root = [5,2,-5]
22+
23+
**Output:** [2]
24+
25+
**Constraints:**
26+
27+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
28+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0509_fibonacci_number
2+
3+
// #Easy #Dynamic_Programming #Math #Recursion #Memoization #Dynamic_Programming_I_Day_1
4+
// #Level_1_Day_10_Dynamic_Programming #Udemy_Dynamic_Programming
5+
// #2023_01_10_Time_139_ms_(82.72%)_Space_32.6_MB_(92.35%)
6+
7+
class Solution {
8+
private val memo = IntArray(31)
9+
fun fib(n: Int): Int {
10+
if (n == 0) {
11+
return 0
12+
}
13+
if (n == 1) {
14+
return 1
15+
}
16+
if (memo[n] != 0) {
17+
return memo[n]
18+
}
19+
memo[n] = fib(n - 1) + fib(n - 2)
20+
return memo[n]
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
509\. Fibonacci Number
2+
3+
Easy
4+
5+
The **Fibonacci numbers**, commonly denoted `F(n)` form a sequence, called the **Fibonacci sequence**, such that each number is the sum of the two preceding ones, starting from `0` and `1`. That is,
6+
7+
F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1.
8+
9+
Given `n`, calculate `F(n)`.
10+
11+
**Example 1:**
12+
13+
**Input:** n = 2
14+
15+
**Output:** 1
16+
17+
**Explanation:** F(2) = F(1) + F(0) = 1 + 0 = 1.
18+
19+
**Example 2:**
20+
21+
**Input:** n = 3
22+
23+
**Output:** 2
24+
25+
**Explanation:** F(3) = F(2) + F(1) = 1 + 1 = 2.
26+
27+
**Example 3:**
28+
29+
**Input:** n = 4
30+
31+
**Output:** 3
32+
33+
**Explanation:** F(4) = F(3) + F(2) = 2 + 1 = 3.
34+
35+
**Constraints:**
36+
37+
* `0 <= n <= 30`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
511\. Game Play Analysis I
2+
3+
Easy
4+
5+
SQL Schema
6+
7+
Table: `Activity`
8+
9+
+--------------+---------+
10+
| Column Name | Type |
11+
+--------------+---------+
12+
| player_id | int |
13+
| device_id | int |
14+
| event_date | date |
15+
| games_played | int |
16+
+--------------+---------+
17+
(player_id, event_date) is the primary key of this table.
18+
This table shows the activity of players of some games.
19+
Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device.
20+
21+
Write an SQL query to report the **first login date** for each player.
22+
23+
Return the result table in **any order**.
24+
25+
The query result format is in the following example.
26+
27+
**Example 1:**
28+
29+
**Input:**
30+
31+
Activity table:
32+
+-----------+-----------+------------+--------------+
33+
| player_id | device_id | event_date | games_played |
34+
+-----------+-----------+------------+--------------+
35+
| 1 | 2 | 2016-03-01 | 5 |
36+
| 1 | 2 | 2016-05-02 | 6 |
37+
| 2 | 3 | 2017-06-25 | 1 |
38+
| 3 | 1 | 2016-03-02 | 0 |
39+
| 3 | 4 | 2018-07-03 | 5 |
40+
+-----------+-----------+------------+--------------+
41+
42+
**Output:**
43+
44+
+-----------+-------------+
45+
| player_id | first_login |
46+
+-----------+-------------+
47+
| 1 | 2016-03-01 |
48+
| 2 | 2017-06-25 |
49+
| 3 | 2016-03-02 |
50+
+-----------+-------------+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Write your MySQL query statement below
2+
# #Easy #LeetCode_Curated_SQL_70 #Database #SQL_I_Day_8_Function
3+
# #2023_01_10_Time_790_ms_(45.04%)_Space_0B_(100.00%)
4+
select distinct(player_id), min(event_date) as first_login
5+
from activity
6+
group by player_id
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g0501_0600.s0513_find_bottom_left_tree_value
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #2023_01_10_Time_190_ms_(88.24%)_Space_37.4_MB_(76.47%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
private fun func(root: TreeNode?, level: Int): IntArray? {
20+
if (root!!.left == null && root.right == null) {
21+
val a = IntArray(2)
22+
a[0] = root.`val`
23+
a[1] = level
24+
return a
25+
}
26+
var a: IntArray? = null
27+
var b: IntArray? = null
28+
if (root.left != null) {
29+
a = func(root.left, level + 1)
30+
}
31+
if (root.right != null) {
32+
b = func(root.right, level + 1)
33+
}
34+
return if (a == null) {
35+
b
36+
} else if (b == null) {
37+
a
38+
} else {
39+
if (a[1] >= b[1]) {
40+
a
41+
} else {
42+
b
43+
}
44+
}
45+
}
46+
47+
fun findBottomLeftValue(root: TreeNode?): Int {
48+
val a = func(root, 0)
49+
return if (a != null && a.size > 0) {
50+
a[0]
51+
} else -1
52+
}
53+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
513\. Find Bottom Left Tree Value
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return the leftmost value in the last row of the tree.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg)
10+
11+
**Input:** root = [2,1,3]
12+
13+
**Output:** 1
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg)
18+
19+
**Input:** root = [1,2,3,4,null,5,6,null,null,7]
20+
21+
**Output:** 7
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.
26+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0508_most_frequent_subtree_sum
2+
3+
import com_github_leetcode.TreeNode
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun findFrequentTreeSum() {
11+
val treeNode: TreeNode = TreeNode.create(listOf(5, 2, -3))!!
12+
val expected = intArrayOf(2, -3, 4)
13+
assertThat(Solution().findFrequentTreeSum(treeNode), equalTo(expected))
14+
}
15+
16+
@Test
17+
fun findFrequentTreeSum2() {
18+
val treeNode: TreeNode = TreeNode.create(listOf(5, 2, -5))!!
19+
val expected = intArrayOf(2)
20+
assertThat(Solution().findFrequentTreeSum(treeNode), equalTo(expected))
21+
}
22+
}

0 commit comments

Comments
 (0)