Skip to content

Commit e35b5f9

Browse files
authored
Added tasks 514-525.
1 parent 1d4afe0 commit e35b5f9

File tree

13 files changed

+826
-0
lines changed

13 files changed

+826
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@
15711571
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15721572
|-|-|-|-|-|-
15731573
| 0005 |[Longest Palindromic Substring](src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
1574+
| 0516 |[Longest Palindromic Subsequence](src/main/kotlin/g0501_0600/s0516_longest_palindromic_subsequence)| Medium | String, Dynamic_Programming | 243 | 87.50
15741575

15751576
#### Day 18
15761577

@@ -1592,6 +1593,7 @@
15921593
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15931594
|-|-|-|-|-|-
15941595
| 0322 |[Coin Change](src/main/kotlin/g0301_0400/s0322_coin_change)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Breadth_First_Search | 332 | 50.68
1596+
| 0518 |[Coin Change II](src/main/kotlin/g0501_0600/s0518_coin_change_2)| Medium | Array, Dynamic_Programming | 139 | 100.00
15951597

15961598
#### Day 21
15971599

@@ -1613,6 +1615,18 @@
16131615
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16141616
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16151617
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1618+
| 0525 |[Contiguous Array](src/main/kotlin/g0501_0600/s0525_contiguous_array)| Medium | Array, Hash_Table, Prefix_Sum | 471 | 100.00
1619+
| 0524 |[Longest Word in Dictionary through Deleting](src/main/kotlin/g0501_0600/s0524_longest_word_in_dictionary_through_deleting)| Medium | Array, String, Sorting, Two_Pointers | 307 | 100.00
1620+
| 0523 |[Continuous Subarray Sum](src/main/kotlin/g0501_0600/s0523_continuous_subarray_sum)| Medium | Array, Hash_Table, Math, Prefix_Sum | 682 | 95.45
1621+
| 0522 |[Longest Uncommon Subsequence II](src/main/kotlin/g0501_0600/s0522_longest_uncommon_subsequence_ii)| Medium | Array, String, Hash_Table, Sorting, Two_Pointers | 163 | 100.00
1622+
| 0521 |[Longest Uncommon Subsequence I](src/main/kotlin/g0501_0600/s0521_longest_uncommon_subsequence_i)| Easy | String | 146 | 88.89
1623+
| 0520 |[Detect Capital](src/main/kotlin/g0501_0600/s0520_detect_capital)| Easy | String | 161 | 84.54
1624+
| 0519 |[Random Flip Matrix](src/main/kotlin/g0501_0600/s0519_random_flip_matrix)| Medium | Hash_Table, Math, Randomized, Reservoir_Sampling | 270 | 100.00
1625+
| 0518 |[Coin Change II](src/main/kotlin/g0501_0600/s0518_coin_change_2)| Medium | Array, Dynamic_Programming, Dynamic_Programming_I_Day_20 | 139 | 100.00
1626+
| 0517 |[Super Washing Machines](src/main/kotlin/g0501_0600/s0517_super_washing_machines)| Hard | Array, Greedy | 210 | 100.00
1627+
| 0516 |[Longest Palindromic Subsequence](src/main/kotlin/g0501_0600/s0516_longest_palindromic_subsequence)| Medium | String, Dynamic_Programming, Dynamic_Programming_I_Day_17 | 243 | 87.50
1628+
| 0515 |[Find Largest Value in Each Tree Row](src/main/kotlin/g0501_0600/s0515_find_largest_value_in_each_tree_row)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 238 | 73.33
1629+
| 0514 |[Freedom Trail](src/main/kotlin/g0501_0600/s0514_freedom_trail)| Hard | String, Dynamic_Programming, Depth_First_Search, Breadth_First_Search | 182 | 100.00
16161630
| 0513 |[Find Bottom Left Tree Value](src/main/kotlin/g0501_0600/s0513_find_bottom_left_tree_value)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 190 | 88.24
16171631
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i)| Easy | LeetCode_Curated_SQL_70, Database, SQL_I_Day_8_Function | 790 | 45.04
16181632
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| 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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 514\. Freedom Trail
5+
6+
Hard
7+
8+
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.
9+
10+
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_.
11+
12+
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.
13+
14+
At the stage of rotating the ring to spell the key character `key[i]`:
15+
16+
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]`.
17+
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.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2018/10/22/ring.jpg)
22+
23+
**Input:** ring = "godding", key = "gd"
24+
25+
**Output:** 4
26+
27+
**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.
28+
29+
**Example 2:**
30+
31+
**Input:** ring = "godding", key = "godding"
32+
33+
**Output:** 13
34+
35+
**Constraints:**
36+
37+
* `1 <= ring.length, key.length <= 100`
38+
* `ring` and `key` consist of only lower case English letters.
39+
* It is guaranteed that `key` could always be spelled by rotating `ring`.
40+
41+
## Solution
42+
43+
```kotlin
44+
class Solution {
45+
fun findRotateSteps(ring: String, key: String): Int {
46+
val indexs: Array<MutableList<Int>?> = arrayOfNulls<MutableList<Int>?>(26)
47+
for (i in 0 until ring.length) {
48+
val num = ring[i].code - 'a'.code
49+
if (indexs[num] == null) {
50+
indexs[num] = ArrayList()
51+
}
52+
indexs[num]!!.add(i)
53+
}
54+
val dp = Array(101) { IntArray(101) }
55+
return find(ring, 0, key, 0, dp, indexs)
56+
}
57+
58+
private fun find(
59+
ring: String,
60+
i: Int,
61+
key: String,
62+
j: Int,
63+
cache: Array<IntArray>,
64+
indexs: Array<MutableList<Int>?>
65+
): Int {
66+
if (j == key.length) {
67+
return 0
68+
}
69+
if (cache[i][j] != 0) {
70+
return cache[i][j]
71+
}
72+
var ans = Int.MAX_VALUE
73+
val targets: List<Int>? = indexs[key[j].code - 'a'.code]
74+
for (t in targets!!) {
75+
var delta = Math.abs(i - t)
76+
delta = Math.min(delta, Math.abs(ring.length - delta))
77+
ans = Math.min(ans, 1 + delta + find(ring, t, key, j + 1, cache, indexs))
78+
}
79+
cache[i][j] = ans
80+
return ans
81+
}
82+
}
83+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 515\. Find Largest Value in Each Tree Row
5+
6+
Medium
7+
8+
Given the `root` of a binary tree, return _an array of the largest value in each row_ of the tree **(0-indexed)**.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/08/21/largest_e1.jpg)
13+
14+
**Input:** root = [1,3,2,5,3,null,9]
15+
16+
**Output:** [1,3,9]
17+
18+
**Example 2:**
19+
20+
**Input:** root = [1,2,3]
21+
22+
**Output:** [1,3]
23+
24+
**Constraints:**
25+
26+
* The number of nodes in the tree will be in the range <code>[0, 10<sup>4</sup>]</code>.
27+
* <code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code>
28+
29+
## Solution
30+
31+
```kotlin
32+
import com_github_leetcode.TreeNode
33+
import java.util.LinkedList
34+
import java.util.Queue
35+
36+
/*
37+
* Example:
38+
* var ti = TreeNode(5)
39+
* var v = ti.`val`
40+
* Definition for a binary tree node.
41+
* class TreeNode(var `val`: Int) {
42+
* var left: TreeNode? = null
43+
* var right: TreeNode? = null
44+
* }
45+
*/
46+
class Solution {
47+
fun largestValues(root: TreeNode?): List<Int> {
48+
val list: MutableList<Int> = ArrayList()
49+
val queue: Queue<TreeNode?> = LinkedList()
50+
if (root != null) {
51+
queue.offer(root)
52+
while (!queue.isEmpty()) {
53+
var max = Int.MIN_VALUE
54+
val size = queue.size
55+
for (i in 0 until size) {
56+
val curr = queue.poll()
57+
max = Math.max(max, curr!!.`val`)
58+
if (curr.left != null) {
59+
queue.offer(curr.left)
60+
}
61+
if (curr.right != null) {
62+
queue.offer(curr.right)
63+
}
64+
}
65+
list.add(max)
66+
}
67+
}
68+
return list
69+
}
70+
}
71+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 516\. Longest Palindromic Subsequence
5+
6+
Medium
7+
8+
Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "bbbab"
15+
16+
**Output:** 4
17+
18+
**Explanation:** One possible longest palindromic subsequence is "bbbb".
19+
20+
**Example 2:**
21+
22+
**Input:** s = "cbbd"
23+
24+
**Output:** 2
25+
26+
**Explanation:** One possible longest palindromic subsequence is "bb".
27+
28+
**Constraints:**
29+
30+
* `1 <= s.length <= 1000`
31+
* `s` consists only of lowercase English letters.
32+
33+
## Solution
34+
35+
```kotlin
36+
class Solution {
37+
fun longestPalindromeSubseq(s: String): Int {
38+
if (s.isEmpty()) {
39+
return 0
40+
}
41+
val dp = Array(s.length) { IntArray(s.length) }
42+
for (jidiff in 0 until s.length) {
43+
for (i in 0 until s.length) {
44+
val j = i + jidiff
45+
if (j >= s.length) {
46+
continue
47+
}
48+
dp[i][j] = when (j) {
49+
i -> 1
50+
i + 1 -> if (s[i] == s[j]) 2 else 1
51+
else -> if (s[i] == s[j]) 2 + dp[i + 1][j - 1] else Math.max(dp[i + 1][j], dp[i][j - 1])
52+
}
53+
}
54+
}
55+
return dp[0][s.length - 1]
56+
}
57+
}
58+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 517\. Super Washing Machines
5+
6+
Hard
7+
8+
You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
9+
10+
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.
11+
12+
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`.
13+
14+
**Example 1:**
15+
16+
**Input:** machines = [1,0,5]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
1st move: 1 0 <-- 5 => 1 1 4
23+
24+
2nd move: 1 <-- 1 <-- 4 => 2 1 3
25+
26+
3rd move: 2 1 <-- 3 => 2 2 2
27+
28+
**Example 2:**
29+
30+
**Input:** machines = [0,3,0]
31+
32+
**Output:** 2
33+
34+
**Explanation:**
35+
36+
1st move: 0 <-- 3 0 => 1 2 0
37+
38+
2nd move: 1 2 --> 0 => 1 1 1
39+
40+
**Example 3:**
41+
42+
**Input:** machines = [0,2,0]
43+
44+
**Output:** -1
45+
46+
**Explanation:** It's impossible to make all three washing machines have the same number of dresses.
47+
48+
**Constraints:**
49+
50+
* `n == machines.length`
51+
* <code>1 <= n <= 10<sup>4</sup></code>
52+
* <code>0 <= machines[i] <= 10<sup>5</sup></code>
53+
54+
## Solution
55+
56+
```kotlin
57+
class Solution {
58+
fun findMinMoves(machines: IntArray): Int {
59+
var total = 0
60+
for (i in machines) {
61+
total += i
62+
}
63+
if (total % machines.size != 0) {
64+
return -1
65+
}
66+
val avg = total / machines.size
67+
var cnt = 0
68+
var max = 0
69+
for (load in machines) {
70+
cnt += load - avg
71+
max = Math.max(Math.max(max, Math.abs(cnt)), load - avg)
72+
}
73+
return max
74+
}
75+
}
76+
```

0 commit comments

Comments
 (0)