Skip to content

Commit 6aaf06b

Browse files
committed
Added tasks 481-513.
1 parent f07ed57 commit 6aaf06b

File tree

27 files changed

+1709
-29
lines changed

27 files changed

+1709
-29
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
4747
|-|-|-|-|-|-
48+
| 0496 |[Next Greater Element I](src/main/kotlin/g0401_0500/s0496_next_greater_element_i)| Easy | Array, Hash_Table, Stack, Monotonic_Stack | 171 | 100.00
4849

4950
#### Day 6 Array
5051

@@ -152,6 +153,7 @@
152153

153154
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
154155
|-|-|-|-|-|-
156+
| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii)| Medium | Array, Stack, Monotonic_Stack | 331 | 92.68
155157

156158
#### Day 11
157159

@@ -338,6 +340,7 @@
338340

339341
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
340342
|-|-|-|-|-|-
343+
| 0511 |[Game Play Analysis I](src/main/kotlin/g0501_0600/s0511_game_play_analysis_i)| Easy | LeetCode_Curated_SQL_70, Database | 790 | 45.04
341344

342345
#### Day 9 Control of Flow
343346

@@ -413,6 +416,7 @@
413416

414417
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
415418
|-|-|-|-|-|-
419+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
416420
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06
417421

418422
#### Day 11 Dynamic Programming
@@ -534,7 +538,7 @@
534538

535539
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
536540
|-|-|-|-|-|-
537-
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming | 509 | 57.56
541+
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming | 204 | 98.82
538542
| 0152 |[Maximum Product Subarray](src/main/kotlin/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 253 | 88.42
539543

540544
#### Day 14 Sliding Window/Two Pointer
@@ -743,6 +747,7 @@
743747
| 0152 |[Maximum Product Subarray](src/main/kotlin/g0101_0200/s0152_maximum_product_subarray)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 253 | 88.42
744748
| 0198 |[House Robber](src/main/kotlin/g0101_0200/s0198_house_robber)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming | 156 | 92.24
745749
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii)| Medium | Array, Dynamic_Programming | 257 | 59.62
750+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
746751
| 0070 |[Climbing Stairs](src/main/kotlin/g0001_0100/s0070_climbing_stairs)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization | 127 | 97.06
747752
| 0064 |[Minimum Path Sum](src/main/kotlin/g0001_0100/s0064_minimum_path_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix | 222 | 95.70
748753
| 0300 |[Longest Increasing Subsequence](src/main/kotlin/g0201_0300/s0300_longest_increasing_subsequence)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Binary_Search | 318 | 82.28
@@ -1460,6 +1465,7 @@
14601465

14611466
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14621467
|-|-|-|-|-|-
1468+
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
14631469

14641470
#### Day 2
14651471

@@ -1607,7 +1613,31 @@
16071613
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16081614
| 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
16091615
| 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
1616+
| 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
1617+
| 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
1618+
| 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
1619+
| 0508 |[Most Frequent Subtree Sum](src/main/kotlin/g0501_0600/s0508_most_frequent_subtree_sum)| Medium | Hash_Table, Depth_First_Search, Tree, Binary_Tree | 246 | 80.00
1620+
| 0507 |[Perfect Number](src/main/kotlin/g0501_0600/s0507_perfect_number)| Easy | Math | 172 | 81.82
1621+
| 0506 |[Relative Ranks](src/main/kotlin/g0501_0600/s0506_relative_ranks)| Easy | Array, Sorting, Heap_Priority_Queue | 244 | 94.23
1622+
| 0504 |[Base 7](src/main/kotlin/g0501_0600/s0504_base_7)| Easy | Math | 136 | 100.00
1623+
| 0503 |[Next Greater Element II](src/main/kotlin/g0501_0600/s0503_next_greater_element_ii)| Medium | Array, Stack, Monotonic_Stack, Programming_Skills_II_Day_10 | 331 | 92.68
1624+
| 0502 |[IPO](src/main/kotlin/g0501_0600/s0502_ipo)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 799 | 54.55
1625+
| 0501 |[Find Mode in Binary Search Tree](src/main/kotlin/g0501_0600/s0501_find_mode_in_binary_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 235 | 86.67
1626+
| 0500 |[Keyboard Row](src/main/kotlin/g0401_0500/s0500_keyboard_row)| Easy | Array, String, Hash_Table | 170 | 85.19
1627+
| 0498 |[Diagonal Traverse](src/main/kotlin/g0401_0500/s0498_diagonal_traverse)| Medium | Array, Matrix, Simulation | 430 | 61.54
1628+
| 0497 |[Random Point in Non-overlapping Rectangles](src/main/kotlin/g0401_0500/s0497_random_point_in_non_overlapping_rectangles)| Medium | Math, Binary_Search, Prefix_Sum, Ordered_Set, Randomized, Reservoir_Sampling | 759 | 100.00
1629+
| 0496 |[Next Greater Element I](src/main/kotlin/g0401_0500/s0496_next_greater_element_i)| Easy | Array, Hash_Table, Stack, Monotonic_Stack, Programming_Skills_I_Day_5_Function | 171 | 100.00
1630+
| 0495 |[Teemo Attacking](src/main/kotlin/g0401_0500/s0495_teemo_attacking)| Easy | Array, Simulation | 283 | 100.00
16101631
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
1632+
| 0493 |[Reverse Pairs](src/main/kotlin/g0401_0500/s0493_reverse_pairs)| Hard | Array, Binary_Search, Ordered_Set, Divide_and_Conquer, Segment_Tree, Binary_Indexed_Tree, Merge_Sort | 887 | 66.67
1633+
| 0492 |[Construct the Rectangle](src/main/kotlin/g0401_0500/s0492_construct_the_rectangle)| Easy | Math | 202 | 88.89
1634+
| 0491 |[Non-decreasing Subsequences](src/main/kotlin/g0401_0500/s0491_increasing_subsequences)| Medium | Array, Hash_Table, Bit_Manipulation, Backtracking | 499 | 100.00
1635+
| 0488 |[Zuma Game](src/main/kotlin/g0401_0500/s0488_zuma_game)| Hard | String, Dynamic_Programming, Breadth_First_Search, Memoization | 1727 | 100.00
1636+
| 0486 |[Predict the Winner](src/main/kotlin/g0401_0500/s0486_predict_the_winner)| Medium | Array, Dynamic_Programming, Math, Recursion, Game_Theory | 158 | 84.62
1637+
| 0485 |[Max Consecutive Ones](src/main/kotlin/g0401_0500/s0485_max_consecutive_ones)| Easy | Array | 272 | 88.46
1638+
| 0483 |[Smallest Good Base](src/main/kotlin/g0401_0500/s0483_smallest_good_base)| Hard | Math, Binary_Search | 164 | 100.00
1639+
| 0482 |[License Key Formatting](src/main/kotlin/g0401_0500/s0482_license_key_formatting)| Easy | String | 234 | 87.18
1640+
| 0481 |[Magical String](src/main/kotlin/g0401_0500/s0481_magical_string)| Medium | String, Two_Pointers | 124 | 100.00
16111641
| 0480 |[Sliding Window Median](src/main/kotlin/g0401_0500/s0480_sliding_window_median)| Hard | Array, Hash_Table, Heap_Priority_Queue, Sliding_Window | 409 | 100.00
16121642
| 0479 |[Largest Palindrome Product](src/main/kotlin/g0401_0500/s0479_largest_palindrome_product)| Hard | Math | 147 | 100.00
16131643
| 0478 |[Generate Random Point in a Circle](src/main/kotlin/g0401_0500/s0478_generate_random_point_in_a_circle)| Medium | Math, Geometry, Randomized, Rejection_Sampling | 862 | 100.00
@@ -1661,7 +1691,7 @@
16611691
| 0420 |[Strong Password Checker](src/main/kotlin/g0401_0500/s0420_strong_password_checker)| Hard | String, Greedy, Heap_Priority_Queue | 157 | 80.00
16621692
| 0419 |[Battleships in a Board](src/main/kotlin/g0401_0500/s0419_battleships_in_a_board)| Medium | Array, Depth_First_Search, Matrix | 273 | 76.92
16631693
| 0417 |[Pacific Atlantic Water Flow](src/main/kotlin/g0401_0500/s0417_pacific_atlantic_water_flow)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix, Graph_Theory_I_Day_4_Matrix_Related_Problems, Level_2_Day_10_Graph/BFS/DFS, Udemy_Graph | 319 | 100.00
1664-
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1694+
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 204 | 98.82
16651695
| 0415 |[Add Strings](src/main/kotlin/g0401_0500/s0415_add_strings)| Easy | String, Math, Simulation, Data_Structure_II_Day_6_String | 296 | 76.00
16661696
| 0414 |[Third Maximum Number](src/main/kotlin/g0401_0500/s0414_third_maximum_number)| Easy | Array, Sorting | 317 | 73.00
16671697
| 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 156 | 100.00

src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/readme.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,22 @@ Given a **non-empty** array `nums` containing **only positive integers**, find i
3333
```kotlin
3434
class Solution {
3535
fun canPartition(nums: IntArray): Boolean {
36-
var sum = 0
36+
var sums = 0
3737
for (num in nums) {
38-
sum += num
38+
sums += num
3939
}
40-
if (sum % 2 != 0) {
40+
if (sums % 2 == 1) {
4141
return false
4242
}
43-
sum /= 2
44-
// if use primitive boolean array will make default value to false
45-
// we need the default value "null" to help us to do the memo
46-
val dp = arrayOfNulls<Boolean>(sum + 1)
47-
return sumTo(nums, sum, 0, dp)
48-
}
49-
50-
private fun sumTo(nums: IntArray, sum: Int, index: Int, dp: Array<Boolean?>): Boolean {
51-
if (sum == 0) {
52-
return true
53-
}
54-
if (sum < 0) {
55-
return false
56-
}
57-
if (index == nums.size) {
58-
return false
59-
}
60-
if (dp[sum] != null) {
61-
return dp[sum]!!
43+
sums /= 2
44+
val dp = BooleanArray(sums + 1)
45+
dp[0] = true
46+
for (num in nums) {
47+
for (sum in sums downTo num) {
48+
dp[sum] = dp[sum] || dp[sum - num]
49+
}
6250
}
63-
// use the number or not use the number
64-
dp[sum] = sumTo(nums, sum - nums[index], index + 1, dp) || sumTo(nums, sum, index + 1, dp)
65-
return dp[sum]!!
51+
return dp[sums]
6652
}
6753
}
6854
```

src/main/kotlin/g0401_0500/s0430_flatten_a_multilevel_doubly_linked_list/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ Merging the serialization of each level and removing trailing nulls we obtain:
6565
## Solution
6666

6767
```kotlin
68-
import com_github_leetcode.prev_next.Node
69-
7068
/*
7169
* Definition for a Node.
7270
* class Node(var `val`: Int) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
## 481\. Magical String
5+
6+
Medium
7+
8+
A magical string `s` consists of only `'1'` and `'2'` and obeys the following rules:
9+
10+
* The string s is magical because concatenating the number of contiguous occurrences of characters `'1'` and `'2'` generates the string `s` itself.
11+
12+
The first few elements of `s` is `s = "1221121221221121122……"`. If we group the consecutive `1`'s and `2`'s in `s`, it will be `"1 22 11 2 1 22 1 22 11 2 11 22 ......"` and the occurrences of `1`'s or `2`'s in each group are `"1 2 2 1 1 2 1 2 2 1 2 2 ......"`. You can see that the occurrence sequence is `s` itself.
13+
14+
Given an integer `n`, return the number of `1`'s in the first `n` number in the magical string `s`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 6
19+
20+
**Output:** 3
21+
22+
**Explanation:** The first 6 elements of magical string s is "122112" and it contains three 1's, so return 3.
23+
24+
**Example 2:**
25+
26+
**Input:** n = 1
27+
28+
**Output:** 1
29+
30+
**Constraints:**
31+
32+
* <code>1 <= n <= 10<sup>5</sup></code>
33+
34+
## Solution
35+
36+
```kotlin
37+
class Solution {
38+
fun magicalString(n: Int): Int {
39+
val a = IntArray(n + 2)
40+
var fast = 1
41+
var slow = 1
42+
var num = 1
43+
while (fast <= n) {
44+
a[fast++] = num
45+
if (a[slow++] == 2) {
46+
a[fast++] = num
47+
}
48+
num = 3 - num
49+
}
50+
var count = 0
51+
for (j in 1..n) {
52+
if (a[j] == 1) {
53+
count++
54+
}
55+
}
56+
return count
57+
}
58+
}
59+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
## 482\. License Key Formatting
5+
6+
Easy
7+
8+
You are given a license key represented as a string `s` that consists of only alphanumeric characters and dashes. The string is separated into `n + 1` groups by `n` dashes. You are also given an integer `k`.
9+
10+
We want to reformat the string `s` such that each group contains exactly `k` characters, except for the first group, which could be shorter than `k` but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.
11+
12+
Return _the reformatted license key_.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "5F3Z-2e-9-w", k = 4
17+
18+
**Output:** "5F3Z-2E9W"
19+
20+
**Explanation:** The string s has been split into two parts, each part has 4 characters. Note that the two extra dashes are not needed and can be removed.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "2-5g-3-J", k = 2
25+
26+
**Output:** "2-5G-3J"
27+
28+
**Explanation:** The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= s.length <= 10<sup>5</sup></code>
33+
* `s` consists of English letters, digits, and dashes `'-'`.
34+
* <code>1 <= k <= 10<sup>4</sup></code>
35+
36+
## Solution
37+
38+
```kotlin
39+
class Solution {
40+
fun licenseKeyFormatting(s: String, k: Int): String {
41+
val sb = StringBuilder()
42+
var cnt = 0
43+
var occ = 0
44+
for (c in s.toCharArray()) {
45+
if (c == '-') {
46+
continue
47+
}
48+
cnt++
49+
}
50+
var l = cnt % k
51+
for (c in s.toCharArray()) {
52+
if (c == '-') {
53+
continue
54+
}
55+
if (occ == k) {
56+
occ = 0
57+
sb.append('-')
58+
} else if (occ == l && l != 0) {
59+
l = 0
60+
occ = 0
61+
sb.append('-')
62+
}
63+
sb.append(c.uppercaseChar())
64+
occ++
65+
}
66+
return sb.toString()
67+
}
68+
}
69+
```
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+
## 483\. Smallest Good Base
5+
6+
Hard
7+
8+
Given an integer `n` represented as a string, return _the smallest **good base** of_ `n`.
9+
10+
We call `k >= 2` a **good base** of `n`, if all digits of `n` base `k` are `1`'s.
11+
12+
**Example 1:**
13+
14+
**Input:** n = "13"
15+
16+
**Output:** "3"
17+
18+
**Explanation:** 13 base 3 is 111.
19+
20+
**Example 2:**
21+
22+
**Input:** n = "4681"
23+
24+
**Output:** "8"
25+
26+
**Explanation:** 4681 base 8 is 11111.
27+
28+
**Example 3:**
29+
30+
**Input:** n = "1000000000000000000"
31+
32+
**Output:** "999999999999999999"
33+
34+
**Explanation:** 1000000000000000000 base 999999999999999999 is 11.
35+
36+
**Constraints:**
37+
38+
* `n` is an integer in the range <code>[3, 10<sup>18</sup>]</code>.
39+
* `n` does not contain any leading zeros.
40+
41+
## Solution
42+
43+
```kotlin
44+
class Solution {
45+
fun smallestGoodBase(n: String): String {
46+
return sol1(n)
47+
}
48+
49+
private fun sol1(n: String): String {
50+
val x = n.toLong()
51+
val ans: MutableList<Long> = ArrayList()
52+
ans.add(x - 1)
53+
val y = x - 1
54+
for (i in 2..62) {
55+
val dm = Math.pow(y.toDouble(), 1.0 / i)
56+
val dml = dm.toLong()
57+
var j = 0
58+
while (j > -3 && dml + j > 1) {
59+
val d = dml + j
60+
if (y % d == 0L) {
61+
val poly = poly(d, i)
62+
if (poly == x) {
63+
ans.add(d)
64+
}
65+
}
66+
j--
67+
}
68+
}
69+
val end = ans[ans.size - 1]
70+
return end.toString()
71+
}
72+
73+
private fun poly(b: Long, n: Int): Long {
74+
var ans: Long = 1
75+
var m: Long = 1
76+
for (i in 0 until n) {
77+
m *= b
78+
ans += m
79+
}
80+
return ans
81+
}
82+
}
83+
```

0 commit comments

Comments
 (0)