Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 6 Array

Expand Down Expand Up @@ -152,6 +153,7 @@

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

#### Day 11

Expand Down Expand Up @@ -338,6 +340,7 @@

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

#### Day 9 Control of Flow

Expand Down Expand Up @@ -413,6 +416,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
| 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

#### Day 11 Dynamic Programming
Expand Down Expand Up @@ -534,7 +538,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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

#### Day 14 Sliding Window/Two Pointer
Expand Down Expand Up @@ -743,6 +747,7 @@
| 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
| 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
| 0213 |[House Robber II](src/main/kotlin/g0201_0300/s0213_house_robber_ii)| Medium | Array, Dynamic_Programming | 257 | 59.62
| 0509 |[Fibonacci Number](src/main/kotlin/g0501_0600/s0509_fibonacci_number)| Easy | Dynamic_Programming, Math, Recursion, Memoization | 139 | 82.72
| 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
| 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
| 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
Expand Down Expand Up @@ -1460,6 +1465,7 @@

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

#### Day 2

Expand Down Expand Up @@ -1607,7 +1613,31 @@
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
| 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
| 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
| 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
| 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
| 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
| 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
| 0507 |[Perfect Number](src/main/kotlin/g0501_0600/s0507_perfect_number)| Easy | Math | 172 | 81.82
| 0506 |[Relative Ranks](src/main/kotlin/g0501_0600/s0506_relative_ranks)| Easy | Array, Sorting, Heap_Priority_Queue | 244 | 94.23
| 0504 |[Base 7](src/main/kotlin/g0501_0600/s0504_base_7)| Easy | Math | 136 | 100.00
| 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
| 0502 |[IPO](src/main/kotlin/g0501_0600/s0502_ipo)| Hard | Array, Sorting, Greedy, Heap_Priority_Queue | 799 | 54.55
| 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
| 0500 |[Keyboard Row](src/main/kotlin/g0401_0500/s0500_keyboard_row)| Easy | Array, String, Hash_Table | 170 | 85.19
| 0498 |[Diagonal Traverse](src/main/kotlin/g0401_0500/s0498_diagonal_traverse)| Medium | Array, Matrix, Simulation | 430 | 61.54
| 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
| 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
| 0495 |[Teemo Attacking](src/main/kotlin/g0401_0500/s0495_teemo_attacking)| Easy | Array, Simulation | 283 | 100.00
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
| 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
| 0492 |[Construct the Rectangle](src/main/kotlin/g0401_0500/s0492_construct_the_rectangle)| Easy | Math | 202 | 88.89
| 0491 |[Non-decreasing Subsequences](src/main/kotlin/g0401_0500/s0491_increasing_subsequences)| Medium | Array, Hash_Table, Bit_Manipulation, Backtracking | 499 | 100.00
| 0488 |[Zuma Game](src/main/kotlin/g0401_0500/s0488_zuma_game)| Hard | String, Dynamic_Programming, Breadth_First_Search, Memoization | 1727 | 100.00
| 0486 |[Predict the Winner](src/main/kotlin/g0401_0500/s0486_predict_the_winner)| Medium | Array, Dynamic_Programming, Math, Recursion, Game_Theory | 158 | 84.62
| 0485 |[Max Consecutive Ones](src/main/kotlin/g0401_0500/s0485_max_consecutive_ones)| Easy | Array | 272 | 88.46
| 0483 |[Smallest Good Base](src/main/kotlin/g0401_0500/s0483_smallest_good_base)| Hard | Math, Binary_Search | 164 | 100.00
| 0482 |[License Key Formatting](src/main/kotlin/g0401_0500/s0482_license_key_formatting)| Easy | String | 234 | 87.18
| 0481 |[Magical String](src/main/kotlin/g0401_0500/s0481_magical_string)| Medium | String, Two_Pointers | 124 | 100.00
| 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
| 0479 |[Largest Palindrome Product](src/main/kotlin/g0401_0500/s0479_largest_palindrome_product)| Hard | Math | 147 | 100.00
| 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
Expand Down Expand Up @@ -1661,7 +1691,7 @@
| 0420 |[Strong Password Checker](src/main/kotlin/g0401_0500/s0420_strong_password_checker)| Hard | String, Greedy, Heap_Priority_Queue | 157 | 80.00
| 0419 |[Battleships in a Board](src/main/kotlin/g0401_0500/s0419_battleships_in_a_board)| Medium | Array, Depth_First_Search, Matrix | 273 | 76.92
| 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
| 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
| 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
| 0415 |[Add Strings](src/main/kotlin/g0401_0500/s0415_add_strings)| Easy | String, Math, Simulation, Data_Structure_II_Day_6_String | 296 | 76.00
| 0414 |[Third Maximum Number](src/main/kotlin/g0401_0500/s0414_third_maximum_number)| Easy | Array, Sorting | 317 | 73.00
| 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,22 @@ Given a **non-empty** array `nums` containing **only positive integers**, find i
```kotlin
class Solution {
fun canPartition(nums: IntArray): Boolean {
var sum = 0
var sums = 0
for (num in nums) {
sum += num
sums += num
}
if (sum % 2 != 0) {
if (sums % 2 == 1) {
return false
}
sum /= 2
// if use primitive boolean array will make default value to false
// we need the default value "null" to help us to do the memo
val dp = arrayOfNulls<Boolean>(sum + 1)
return sumTo(nums, sum, 0, dp)
}

private fun sumTo(nums: IntArray, sum: Int, index: Int, dp: Array<Boolean?>): Boolean {
if (sum == 0) {
return true
}
if (sum < 0) {
return false
}
if (index == nums.size) {
return false
}
if (dp[sum] != null) {
return dp[sum]!!
sums /= 2
val dp = BooleanArray(sums + 1)
dp[0] = true
for (num in nums) {
for (sum in sums downTo num) {
dp[sum] = dp[sum] || dp[sum - num]
}
}
// use the number or not use the number
dp[sum] = sumTo(nums, sum - nums[index], index + 1, dp) || sumTo(nums, sum, index + 1, dp)
return dp[sum]!!
return dp[sums]
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ Merging the serialization of each level and removing trailing nulls we obtain:
## Solution

```kotlin
import com_github_leetcode.prev_next.Node

/*
* Definition for a Node.
* class Node(var `val`: Int) {
Expand Down
59 changes: 59 additions & 0 deletions src/main/kotlin/g0401_0500/s0481_magical_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 481\. Magical String

Medium

A magical string `s` consists of only `'1'` and `'2'` and obeys the following rules:

* The string s is magical because concatenating the number of contiguous occurrences of characters `'1'` and `'2'` generates the string `s` itself.

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.

Given an integer `n`, return the number of `1`'s in the first `n` number in the magical string `s`.

**Example 1:**

**Input:** n = 6

**Output:** 3

**Explanation:** The first 6 elements of magical string s is "122112" and it contains three 1's, so return 3.

**Example 2:**

**Input:** n = 1

**Output:** 1

**Constraints:**

* <code>1 <= n <= 10<sup>5</sup></code>

## Solution

```kotlin
class Solution {
fun magicalString(n: Int): Int {
val a = IntArray(n + 2)
var fast = 1
var slow = 1
var num = 1
while (fast <= n) {
a[fast++] = num
if (a[slow++] == 2) {
a[fast++] = num
}
num = 3 - num
}
var count = 0
for (j in 1..n) {
if (a[j] == 1) {
count++
}
}
return count
}
}
```
69 changes: 69 additions & 0 deletions src/main/kotlin/g0401_0500/s0482_license_key_formatting/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 482\. License Key Formatting

Easy

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`.

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.

Return _the reformatted license key_.

**Example 1:**

**Input:** s = "5F3Z-2e-9-w", k = 4

**Output:** "5F3Z-2E9W"

**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.

**Example 2:**

**Input:** s = "2-5g-3-J", k = 2

**Output:** "2-5G-3J"

**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.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of English letters, digits, and dashes `'-'`.
* <code>1 <= k <= 10<sup>4</sup></code>

## Solution

```kotlin
class Solution {
fun licenseKeyFormatting(s: String, k: Int): String {
val sb = StringBuilder()
var cnt = 0
var occ = 0
for (c in s.toCharArray()) {
if (c == '-') {
continue
}
cnt++
}
var l = cnt % k
for (c in s.toCharArray()) {
if (c == '-') {
continue
}
if (occ == k) {
occ = 0
sb.append('-')
} else if (occ == l && l != 0) {
l = 0
occ = 0
sb.append('-')
}
sb.append(c.uppercaseChar())
occ++
}
return sb.toString()
}
}
```
83 changes: 83 additions & 0 deletions src/main/kotlin/g0401_0500/s0483_smallest_good_base/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 483\. Smallest Good Base

Hard

Given an integer `n` represented as a string, return _the smallest **good base** of_ `n`.

We call `k >= 2` a **good base** of `n`, if all digits of `n` base `k` are `1`'s.

**Example 1:**

**Input:** n = "13"

**Output:** "3"

**Explanation:** 13 base 3 is 111.

**Example 2:**

**Input:** n = "4681"

**Output:** "8"

**Explanation:** 4681 base 8 is 11111.

**Example 3:**

**Input:** n = "1000000000000000000"

**Output:** "999999999999999999"

**Explanation:** 1000000000000000000 base 999999999999999999 is 11.

**Constraints:**

* `n` is an integer in the range <code>[3, 10<sup>18</sup>]</code>.
* `n` does not contain any leading zeros.

## Solution

```kotlin
class Solution {
fun smallestGoodBase(n: String): String {
return sol1(n)
}

private fun sol1(n: String): String {
val x = n.toLong()
val ans: MutableList<Long> = ArrayList()
ans.add(x - 1)
val y = x - 1
for (i in 2..62) {
val dm = Math.pow(y.toDouble(), 1.0 / i)
val dml = dm.toLong()
var j = 0
while (j > -3 && dml + j > 1) {
val d = dml + j
if (y % d == 0L) {
val poly = poly(d, i)
if (poly == x) {
ans.add(d)
}
}
j--
}
}
val end = ans[ans.size - 1]
return end.toString()
}

private fun poly(b: Long, n: Int): Long {
var ans: Long = 1
var m: Long = 1
for (i in 0 until n) {
m *= b
ans += m
}
return ans
}
}
```
Loading