Skip to content

Commit c31f0ab

Browse files
authored
Added tasks 514, 515, 516, 517.
1 parent f34cdeb commit c31f0ab

File tree

13 files changed

+352
-0
lines changed

13 files changed

+352
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16031603
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16041604
|-|-|-|-|-|-
16051605
| 0005 |[Longest Palindromic Substring](src.save/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
1606+
| 0516 |[Longest Palindromic Subsequence](src/main/kotlin/g0501_0600/s0516_longest_palindromic_subsequence/Solution.kt)| Medium | String, Dynamic_Programming | 243 | 87.50
16061607

16071608
#### Day 18
16081609

@@ -1645,6 +1646,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16451646
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16461647
| 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
16471648
| 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
1649+
| 0517 |[Super Washing Machines](src/main/kotlin/g0501_0600/s0517_super_washing_machines/Solution.kt)| Hard | Array, Greedy | 210 | 100.00
1650+
| 0516 |[Longest Palindromic Subsequence](src/main/kotlin/g0501_0600/s0516_longest_palindromic_subsequence/Solution.kt)| Medium | String, Dynamic_Programming, Dynamic_Programming_I_Day_17 | 243 | 87.50
1651+
| 0515 |[Find Largest Value in Each Tree Row](src/main/kotlin/g0501_0600/s0515_find_largest_value_in_each_tree_row/Solution.kt)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 238 | 73.33
1652+
| 0514 |[Freedom Trail](src/main/kotlin/g0501_0600/s0514_freedom_trail/Solution.kt)| Hard | String, Dynamic_Programming, Depth_First_Search, Breadth_First_Search | 182 | 100.00
16481653
| 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
16491654
| 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
16501655
| 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
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0501_0600.s0514_freedom_trail
2+
3+
// #Hard #String #Dynamic_Programming #Depth_First_Search #Breadth_First_Search
4+
// #2023_01_12_Time_182_ms_(100.00%)_Space_35.6_MB_(100.00%)
5+
6+
class Solution {
7+
fun findRotateSteps(ring: String, key: String): Int {
8+
val indexs: Array<MutableList<Int>?> = arrayOfNulls<MutableList<Int>?>(26)
9+
for (i in 0 until ring.length) {
10+
val num = ring[i].code - 'a'.code
11+
if (indexs[num] == null) {
12+
indexs[num] = ArrayList()
13+
}
14+
indexs[num]!!.add(i)
15+
}
16+
val dp = Array(101) { IntArray(101) }
17+
return find(ring, 0, key, 0, dp, indexs)
18+
}
19+
20+
private fun find(
21+
ring: String,
22+
i: Int,
23+
key: String,
24+
j: Int,
25+
cache: Array<IntArray>,
26+
indexs: Array<MutableList<Int>?>
27+
): Int {
28+
if (j == key.length) {
29+
return 0
30+
}
31+
if (cache[i][j] != 0) {
32+
return cache[i][j]
33+
}
34+
var ans = Int.MAX_VALUE
35+
val targets: List<Int>? = indexs[key[j].code - 'a'.code]
36+
for (t in targets!!) {
37+
var delta = Math.abs(i - t)
38+
delta = Math.min(delta, Math.abs(ring.length - delta))
39+
ans = Math.min(ans, 1 + delta + find(ring, t, key, j + 1, cache, indexs))
40+
}
41+
cache[i][j] = ans
42+
return ans
43+
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
514\. Freedom Trail
2+
3+
Hard
4+
5+
In the video game Fallout 4, the quest **"Road to Freedom"** requires players to reach a metal dial called the **"Freedom Trail Ring"** and use the dial to spell a specific keyword to open the door.
6+
7+
Given a string `ring` that represents the code engraved on the outer ring and another string `key` that represents the keyword that needs to be spelled, return _the minimum number of steps to spell all the characters in the keyword_.
8+
9+
Initially, the first character of the ring is aligned at the `"12:00"` direction. You should spell all the characters in `key` one by one by rotating `ring` clockwise or anticlockwise to make each character of the string key aligned at the `"12:00"` direction and then by pressing the center button.
10+
11+
At the stage of rotating the ring to spell the key character `key[i]`:
12+
13+
1. You can rotate the ring clockwise or anticlockwise by one place, which counts as **one step**. The final purpose of the rotation is to align one of `ring`'s characters at the `"12:00"` direction, where this character must equal `key[i]`.
14+
2. If the character `key[i]` has been aligned at the `"12:00"` direction, press the center button to spell, which also counts as **one step**. After the pressing, you could begin to spell the next character in the key (next stage). Otherwise, you have finished all the spelling.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2018/10/22/ring.jpg)
19+
20+
**Input:** ring = "godding", key = "gd"
21+
22+
**Output:** 4
23+
24+
**Explanation:** For the first key character 'g', since it is already in place, we just need 1 step to spell this character. For the second key character 'd', we need to rotate the ring "godding" anticlockwise by two steps to make it become "ddinggo". Also, we need 1 more step for spelling. So the final output is 4.
25+
26+
**Example 2:**
27+
28+
**Input:** ring = "godding", key = "godding"
29+
30+
**Output:** 13
31+
32+
**Constraints:**
33+
34+
* `1 <= ring.length, key.length <= 100`
35+
* `ring` and `key` consist of only lower case English letters.
36+
* It is guaranteed that `key` could always be spelled by rotating `ring`.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0501_0600.s0515_find_largest_value_in_each_tree_row
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
4+
// #2023_01_12_Time_238_ms_(73.33%)_Space_35.5_MB_(100.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
import java.util.LinkedList
8+
import java.util.Queue
9+
10+
/*
11+
* Example:
12+
* var ti = TreeNode(5)
13+
* var v = ti.`val`
14+
* Definition for a binary tree node.
15+
* class TreeNode(var `val`: Int) {
16+
* var left: TreeNode? = null
17+
* var right: TreeNode? = null
18+
* }
19+
*/
20+
class Solution {
21+
fun largestValues(root: TreeNode?): List<Int> {
22+
val list: MutableList<Int> = ArrayList()
23+
val queue: Queue<TreeNode?> = LinkedList()
24+
if (root != null) {
25+
queue.offer(root)
26+
while (!queue.isEmpty()) {
27+
var max = Int.MIN_VALUE
28+
val size = queue.size
29+
for (i in 0 until size) {
30+
val curr = queue.poll()
31+
max = Math.max(max, curr!!.`val`)
32+
if (curr.left != null) {
33+
queue.offer(curr.left)
34+
}
35+
if (curr.right != null) {
36+
queue.offer(curr.right)
37+
}
38+
}
39+
list.add(max)
40+
}
41+
}
42+
return list
43+
}
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
515\. Find Largest Value in Each Tree Row
2+
3+
Medium
4+
5+
Given the `root` of a binary tree, return _an array of the largest value in each row_ of the tree **(0-indexed)**.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg)
10+
11+
**Input:** root = [1,3,2,5,3,null,9]
12+
13+
**Output:** [1,3,9]
14+
15+
**Example 2:**
16+
17+
**Input:** root = [1,2,3]
18+
19+
**Output:** [1,3]
20+
21+
**Constraints:**
22+
23+
* The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.
24+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0501_0600.s0516_longest_palindromic_subsequence
2+
3+
// #Medium #String #Dynamic_Programming #Dynamic_Programming_I_Day_17
4+
// #2023_01_12_Time_243_ms_(87.50%)_Space_45.8_MB_(66.67%)
5+
6+
class Solution {
7+
fun longestPalindromeSubseq(s: String): Int {
8+
if (s.isEmpty()) {
9+
return 0
10+
}
11+
val dp = Array(s.length) { IntArray(s.length) }
12+
for (jidiff in 0 until s.length) {
13+
for (i in 0 until s.length) {
14+
val j = i + jidiff
15+
if (j >= s.length) {
16+
continue
17+
}
18+
dp[i][j] = when (j) {
19+
i -> 1
20+
i + 1 -> if (s[i] == s[j]) 2 else 1
21+
else -> if (s[i] == s[j]) 2 + dp[i + 1][j - 1] else Math.max(dp[i + 1][j], dp[i][j - 1])
22+
}
23+
}
24+
}
25+
return dp[0][s.length - 1]
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
516\. Longest Palindromic Subsequence
2+
3+
Medium
4+
5+
Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
6+
7+
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "bbbab"
12+
13+
**Output:** 4
14+
15+
**Explanation:** One possible longest palindromic subsequence is "bbbb".
16+
17+
**Example 2:**
18+
19+
**Input:** s = "cbbd"
20+
21+
**Output:** 2
22+
23+
**Explanation:** One possible longest palindromic subsequence is "bb".
24+
25+
**Constraints:**
26+
27+
* `1 <= s.length <= 1000`
28+
* `s` consists only of lowercase English letters.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0501_0600.s0517_super_washing_machines
2+
3+
// #Hard #Array #Greedy #2023_01_12_Time_210_ms_(100.00%)_Space_36.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun findMinMoves(machines: IntArray): Int {
7+
var total = 0
8+
for (i in machines) {
9+
total += i
10+
}
11+
if (total % machines.size != 0) {
12+
return -1
13+
}
14+
val avg = total / machines.size
15+
var cnt = 0
16+
var max = 0
17+
for (load in machines) {
18+
cnt += load - avg
19+
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg)
20+
}
21+
return max
22+
}
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
517\. Super Washing Machines
2+
3+
Hard
4+
5+
You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
6+
7+
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
8+
9+
Given an integer array `machines` representing the number of dresses in each washing machine from left to right on the line, return _the minimum number of moves to make all the washing machines have the same number of dresses_. If it is not possible to do it, return `-1`.
10+
11+
**Example 1:**
12+
13+
**Input:** machines = [1,0,5]
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
1st move: 1 0 <-- 5 => 1 1 4
20+
21+
2nd move: 1 <-- 1 <-- 4 => 2 1 3
22+
23+
3rd move: 2 1 <-- 3 => 2 2 2
24+
25+
**Example 2:**
26+
27+
**Input:** machines = [0,3,0]
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
1st move: 0 <-- 3 0 => 1 2 0
34+
35+
2nd move: 1 2 --> 0 => 1 1 1
36+
37+
**Example 3:**
38+
39+
**Input:** machines = [0,2,0]
40+
41+
**Output:** -1
42+
43+
**Explanation:** It's impossible to make all three washing machines have the same number of dresses.
44+
45+
**Constraints:**
46+
47+
* `n == machines.length`
48+
* <code>1 <= n <= 10<sup>4</sup></code>
49+
* <code>0 <= machines[i] <= 10<sup>5</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0501_0600.s0514_freedom_trail
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun findRotateSteps() {
10+
assertThat(Solution().findRotateSteps("godding", "gd"), equalTo(4))
11+
}
12+
13+
@Test
14+
fun findRotateSteps2() {
15+
assertThat(Solution().findRotateSteps("godding", "godding"), equalTo(13))
16+
}
17+
}

0 commit comments

Comments
 (0)