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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0912 |[Sort an Array](src/main/kotlin/g0901_1000/s0912_sort_an_array)| Medium | Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Merge_Sort, Bucket_Sort, Counting_Sort, Radix_Sort | 606 | 98.48

#### Udemy 2D Arrays/Matrix

Expand Down Expand Up @@ -1241,6 +1242,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0911 |[Online Election](src/main/kotlin/g0901_1000/s0911_online_election)| Medium | Array, Hash_Table, Binary_Search, Design | 766 | 83.33

### Dynamic Programming I

Expand Down Expand Up @@ -1277,6 +1279,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0053 |[Maximum Subarray](src/main/kotlin/g0001_0100/s0053_maximum_subarray)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer | 662 | 82.48
| 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue | 339 | 86.96

#### Day 6

Expand Down Expand Up @@ -1472,6 +1475,7 @@

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array)| Easy | Array | 576 | 90.91
| 0028 |[Find the Index of the First Occurrence in a String](src/main/kotlin/g0001_0100/s0028_implement_strstr)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 257 | 32.35

#### Day 2
Expand Down Expand Up @@ -1550,6 +1554,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0304 |[Range Sum Query 2D - Immutable](src/main/kotlin/g0301_0400/s0304_range_sum_query_2d_immutable)| Medium | Array, Matrix, Design, Prefix_Sum | 1373 | 85.71
| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii)| Medium | Array, Math, Sorting, Greedy | 234 | 100.00

#### Day 14

Expand Down Expand Up @@ -1693,6 +1698,38 @@
|------|----------------|-------------|-------------|----------|--------
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_17_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming | 307 | 38.36
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges)| Medium | Array, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Level_2_Day_10_Graph/BFS/DFS | 308 | 57.93
| 0923 |[3Sum With Multiplicity](src/main/kotlin/g0901_1000/s0923_3sum_with_multiplicity)| Medium | Array, Hash_Table, Sorting, Two_Pointers, Counting | 190 | 100.00
| 0922 |[Sort Array By Parity II](src/main/kotlin/g0901_1000/s0922_sort_array_by_parity_ii)| Easy | Array, Sorting, Two_Pointers | 257 | 87.50
| 0921 |[Minimum Add to Make Parentheses Valid](src/main/kotlin/g0901_1000/s0921_minimum_add_to_make_parentheses_valid)| Medium | String, Greedy, Stack | 131 | 92.59
| 0920 |[Number of Music Playlists](src/main/kotlin/g0901_1000/s0920_number_of_music_playlists)| Hard | Dynamic_Programming, Math, Combinatorics | 136 | 100.00
| 0919 |[Complete Binary Tree Inserter](src/main/kotlin/g0901_1000/s0919_complete_binary_tree_inserter)| Medium | Breadth_First_Search, Tree, Binary_Tree, Design | 225 | 100.00
| 0918 |[Maximum Sum Circular Subarray](src/main/kotlin/g0901_1000/s0918_maximum_sum_circular_subarray)| Medium | Array, Dynamic_Programming, Divide_and_Conquer, Queue, Monotonic_Queue, Dynamic_Programming_I_Day_5 | 339 | 86.96
| 0917 |[Reverse Only Letters](src/main/kotlin/g0901_1000/s0917_reverse_only_letters)| Easy | String, Two_Pointers | 126 | 100.00
| 0916 |[Word Subsets](src/main/kotlin/g0901_1000/s0916_word_subsets)| Medium | Array, String, Hash_Table | 397 | 88.89
| 0915 |[Partition Array into Disjoint Intervals](src/main/kotlin/g0901_1000/s0915_partition_array_into_disjoint_intervals)| Medium | Array | 510 | 76.92
| 0914 |[X of a Kind in a Deck of Cards](src/main/kotlin/g0901_1000/s0914_x_of_a_kind_in_a_deck_of_cards)| Easy | Array, Hash_Table, Math, Counting, Number_Theory | 238 | 70.00
| 0913 |[Cat and Mouse](src/main/kotlin/g0901_1000/s0913_cat_and_mouse)| Hard | Dynamic_Programming, Math, Graph, Memoization, Topological_Sort, Game_Theory | 211 | 100.00
| 0912 |[Sort an Array](src/main/kotlin/g0901_1000/s0912_sort_an_array)| Medium | Array, Sorting, Heap_Priority_Queue, Divide_and_Conquer, Merge_Sort, Bucket_Sort, Counting_Sort, Radix_Sort, Udemy_Sorting_Algorithms | 606 | 98.48
| 0911 |[Online Election](src/main/kotlin/g0901_1000/s0911_online_election)| Medium | Array, Hash_Table, Binary_Search, Design, Binary_Search_II_Day_20 | 766 | 83.33
| 0910 |[Smallest Range II](src/main/kotlin/g0901_1000/s0910_smallest_range_ii)| Medium | Array, Math, Sorting, Greedy, Programming_Skills_II_Day_13 | 234 | 100.00
| 0909 |[Snakes and Ladders](src/main/kotlin/g0901_1000/s0909_snakes_and_ladders)| Medium | Array, Breadth_First_Search, Matrix | 203 | 100.00
| 0908 |[Smallest Range I](src/main/kotlin/g0901_1000/s0908_smallest_range_i)| Easy | Array, Math | 202 | 87.50
| 0907 |[Sum of Subarray Minimums](src/main/kotlin/g0901_1000/s0907_sum_of_subarray_minimums)| Medium | Array, Dynamic_Programming, Stack, Monotonic_Stack | 341 | 100.00
| 0906 |[Super Palindromes](src/main/kotlin/g0901_1000/s0906_super_palindromes)| Hard | Math, Enumeration | 153 | 100.00
| 0905 |[Sort Array By Parity](src/main/kotlin/g0901_1000/s0905_sort_array_by_parity)| Easy | Array, Sorting, Two_Pointers | 219 | 75.00
| 0904 |[Fruit Into Baskets](src/main/kotlin/g0901_1000/s0904_fruit_into_baskets)| Medium | Array, Hash_Table, Sliding_Window | 371 | 100.00
| 0903 |[Valid Permutations for DI Sequence](src/main/kotlin/g0901_1000/s0903_valid_permutations_for_di_sequence)| Hard | Dynamic_Programming | 140 | 100.00
| 0902 |[Numbers At Most N Given Digit Set](src/main/kotlin/g0901_1000/s0902_numbers_at_most_n_given_digit_set)| Hard | Array, Dynamic_Programming, Math, Binary_Search | 138 | 100.00
| 0901 |[Online Stock Span](src/main/kotlin/g0901_1000/s0901_online_stock_span)| Medium | Stack, Design, Monotonic_Stack, Data_Stream | 641 | 75.00
| 0900 |[RLE Iterator](src/main/kotlin/g0801_0900/s0900_rle_iterator)| Medium | Array, Design, Counting, Iterator | 175 | 83.33
| 0899 |[Orderly Queue](src/main/kotlin/g0801_0900/s0899_orderly_queue)| Hard | String, Math, Sorting | 148 | 100.00
| 0898 |[Bitwise ORs of Subarrays](src/main/kotlin/g0801_0900/s0898_bitwise_ors_of_subarrays)| Medium | Array, Dynamic_Programming, Bit_Manipulation | 812 | 100.00
| 0897 |[Increasing Order Search Tree](src/main/kotlin/g0801_0900/s0897_increasing_order_search_tree)| Easy | Depth_First_Search, Tree, Binary_Tree, Stack, Binary_Search_Tree | 128 | 85.71
| 0896 |[Monotonic Array](src/main/kotlin/g0801_0900/s0896_monotonic_array)| Easy | Array, Programming_Skills_II_Day_1 | 576 | 90.91
| 0895 |[Maximum Frequency Stack](src/main/kotlin/g0801_0900/s0895_maximum_frequency_stack)| Hard | Hash_Table, Stack, Design, Ordered_Set | 617 | 100.00
| 0894 |[All Possible Full Binary Trees](src/main/kotlin/g0801_0900/s0894_all_possible_full_binary_trees)| Medium | Dynamic_Programming, Tree, Binary_Tree, Recursion, Memoization | 257 | 100.00
| 0893 |[Groups of Special-Equivalent Strings](src/main/kotlin/g0801_0900/s0893_groups_of_special_equivalent_strings)| Medium | Array, String, Hash_Table | 141 | 100.00
| 0892 |[Surface Area of 3D Shapes](src/main/kotlin/g0801_0900/s0892_surface_area_of_3d_shapes)| Easy | Array, Math, Matrix, Geometry | 180 | 100.00
| 0891 |[Sum of Subsequence Widths](src/main/kotlin/g0801_0900/s0891_sum_of_subsequence_widths)| Hard | Array, Math, Sorting | 481 | 100.00
| 0890 |[Find and Replace Pattern](src/main/kotlin/g0801_0900/s0890_find_and_replace_pattern)| Medium | Array, String, Hash_Table | 150 | 100.00
| 0889 |[Construct Binary Tree from Preorder and Postorder Traversal](src/main/kotlin/g0801_0900/s0889_construct_binary_tree_from_preorder_and_postorder_traversal)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 168 | 100.00
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/g0801_0900/s0815_bus_routes/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ Return _the least number of buses you must take to travel from_ `source` _to_ `t
```kotlin
import java.util.LinkedList
import java.util.Queue
import kotlin.collections.ArrayList
import kotlin.collections.HashSet
import kotlin.collections.MutableSet

class Solution {
fun numBusesToDestination(routes: Array<IntArray>, source: Int, target: Int): Int {
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/g0801_0900/s0819_most_common_word/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ The words in `paragraph` are **case-insensitive** and the answer should be retur
## Solution

```kotlin
import java.util.Locale

@Suppress("NAME_SHADOWING")
class Solution {
fun mostCommonWord(paragraph: String, banned: Array<String>): String {
var paragraph = paragraph
paragraph = paragraph.replace("\\p{Punct}".toRegex(), " ").lowercase(Locale.getDefault())
paragraph = paragraph.replace("\\p{Punct}".toRegex(), " ").lowercase()
val a = paragraph.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (i in banned.indices) {
banned[i] = banned[i].lowercase(Locale.getDefault())
banned[i] = banned[i].lowercase()
}
val map: MutableMap<String, Int> = HashMap()
for (s in a) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,13 @@ Thus, the resulting masked number is "\*\*\*-\*\*\*-7890".
## Solution

```kotlin
import java.util.Locale

class Solution {
fun maskPII(s: String): String {
val masked = StringBuilder()
return if (Character.isAlphabetic(s[0].code)) {
val locationOfAtSymbol = s.indexOf("@") - 1
masked.append(s[0]).append("*****").append(s.substring(locationOfAtSymbol))
masked.toString().lowercase(Locale.getDefault())
masked.toString().lowercase()
} else {
val allDigits = StringBuilder()
var pointer = -1
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/g0801_0900/s0843_guess_the_word/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ For each test case, you have exactly `10` guesses to guess the word. At the end
## Solution

```kotlin
import java.util.Collections

/*
* // This is the Master's API interface.
* // You should not implement it, or speculate about its implementation
Expand All @@ -65,8 +63,8 @@ class Solution {
private var next = 0

fun findSecretWord(wordlist: Array<String>, master: Master) {
val list = listOf(*wordlist)
Collections.shuffle(list)
val list = wordlist.copyOf()
list.shuffle()
val test = BooleanArray(wordlist.size)
while (true) {
val num = master.guess(list[next])
Expand All @@ -77,7 +75,7 @@ class Solution {
}
}

private fun updateList(list: List<String?>, test: BooleanArray, num: Int) {
private fun updateList(list: Array<String>, test: BooleanArray, num: Int) {
val index = next
for (i in index + 1 until test.size) {
if (test[i]) {
Expand All @@ -92,10 +90,10 @@ class Solution {
}
}

private fun getSame(word1: String?, word2: String?): Int {
private fun getSame(word1: String, word2: String): Int {
var ret = 0
for (i in 0..5) {
if (word1!![i] == word2!![i]) {
if (word1[i] == word2[i]) {
ret++
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/g0801_0900/s0855_exam_room/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ Implement the `ExamRoom` class:
## Solution

```kotlin
import java.util.Objects

class ExamRoom() {
private class Node(var `val`: Int, map: MutableMap<Int?, Node>) {
var pre: Node? = null
Expand Down Expand Up @@ -107,7 +105,7 @@ class ExamRoom() {
}
return if (right > max) {
Node(n - 1, map).insert(tail.pre)
} else Node(maxAt, map).insert(Objects.requireNonNull(maxAtLeft))
} else Node(maxAt, map).insert(maxAtLeft)
}

fun leave(p: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ class Solution {
workers[i] = Worker(wage[i], quality[i])
}
workers.sortBy { it!!.ratio() }
val maxHeap = PriorityQueue { a: Int?, b: Int? ->
Integer.compare(
b!!, a!!
)
val maxHeap = PriorityQueue { a: Int, b: Int ->
b.compareTo(a)
}
var sumQuality = 0
var result = Double.MAX_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ Recall that a permutation of letters is a bijection from letters to letters: eve
## Solution

```kotlin
import java.util.Collections

class Solution {
fun findAndReplacePattern(words: Array<String>, pattern: String): List<String> {
val finalans: MutableList<String> = ArrayList()
if (pattern.length == 1) {
Collections.addAll(finalans, *words)
finalans.addAll(words)
return finalans
}
for (word in words) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
[![](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)

## 892\. Surface Area of 3D Shapes

Easy

You are given an `n x n` `grid` where you have placed some `1 x 1 x 1` cubes. Each value `v = grid[i][j]` represents a tower of `v` cubes placed on top of cell `(i, j)`.

After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.

Return _the total surface area of the resulting shapes_.

**Note:** The bottom face of each shape counts toward its surface area.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg)

**Input:** grid = \[\[1,2],[3,4]]

**Output:** 34

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg)

**Input:** grid = \[\[1,1,1],[1,0,1],[1,1,1]]

**Output:** 32

**Example 3:**

![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg)

**Input:** grid = \[\[2,2,2],[2,1,2],[2,2,2]]

**Output:** 46

**Constraints:**

* `n == grid.length == grid[i].length`
* `1 <= n <= 50`
* `0 <= grid[i][j] <= 50`

## Solution

```kotlin
class Solution {
fun surfaceArea(grid: Array<IntArray>): Int {
var surfaceArea = 0
for (i in grid.indices) {
for (j in grid[i].indices) {
if (grid[i][j] > 0) {
surfaceArea += 4 * grid[i][j] + 2
surfaceArea -= hiddenSides(i, j, grid)
}
}
}
return surfaceArea
}

private fun hiddenSides(i: Int, j: Int, grid: Array<IntArray>): Int {
var hidden = 0
val tower = grid[i][j]
if (j + 1 < grid[i].size && grid[i][j + 1] > 0) {
hidden += tower.coerceAtMost(grid[i][j + 1])
}
if (j - 1 >= 0 && grid[i][j - 1] > 0) {
hidden += tower.coerceAtMost(grid[i][j - 1])
}
if (i + 1 < grid.size && grid[i + 1][j] > 0) {
hidden += tower.coerceAtMost(grid[i + 1][j])
}
if (i - 1 >= 0 && grid[i - 1][j] > 0) {
hidden += tower.coerceAtMost(grid[i - 1][j])
}
return hidden
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](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)

## 893\. Groups of Special-Equivalent Strings

Medium

You are given an array of strings of the same length `words`.

In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.

Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.

* For example, `words[i] = "zzxy"` and `words[j] = "xyzz"` are **special-equivalent** because we may make the moves `"zzxy" -> "xzzy" -> "xyzz"`.

A **group of special-equivalent strings** from `words` is a non-empty subset of words such that:

* Every pair of strings in the group are special equivalent, and
* The group is the largest size possible (i.e., there is not a string `words[i]` not in the group such that `words[i]` is special-equivalent to every string in the group).

Return _the number of **groups of special-equivalent strings** from_ `words`.

**Example 1:**

**Input:** words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]

**Output:** 3

**Explanation:**

One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings is all pairwise special equivalent to these.

The other two groups are ["xyzz", "zzxy"] and ["zzyx"].

Note that in particular, "zzxy" is not special equivalent to "zzyx".

**Example 2:**

**Input:** words = ["abc","acb","bac","bca","cab","cba"]

**Output:** 3

**Constraints:**

* `1 <= words.length <= 1000`
* `1 <= words[i].length <= 20`
* `words[i]` consist of lowercase English letters.
* All the strings are of the same length.

## Solution

```kotlin
class Solution {
fun numSpecialEquivGroups(words: Array<String>): Int {
val set: HashSet<String> = HashSet()
var result = 0
for (str in words) {
if (set.add(getHashBySwap(str.toCharArray()))) {
result++
}
}
return result
}

private fun getHashBySwap(chars: CharArray): String {
for (i in chars.indices) {
var j = i + 2
while (j < chars.size) {
if (chars[i] > chars[j]) {
val temp = chars[j]
chars[j] = chars[i]
chars[i] = temp
}
j += 2
}
}
return String(chars)
}
}
```
Loading