Skip to content

Commit 62b0432

Browse files
authored
Added tasks 892, 893, 894, 895
1 parent 256aff9 commit 62b0432

File tree

13 files changed

+467
-0
lines changed

13 files changed

+467
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17251725
|------|----------------|-------------|-------------|----------|---------
17261726
| 1143 |[Longest Common Subsequence](src/main/kotlin/g1101_1200/s1143_longest_common_subsequence/Solution.kt)| 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
17271727
| 0994 |[Rotting Oranges](src/main/kotlin/g0901_1000/s0994_rotting_oranges/Solution.kt)| 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
1728+
| 0895 |[Maximum Frequency Stack](src/main/kotlin/g0801_0900/s0895_maximum_frequency_stack/FreqStack.kt)| Hard | Hash_Table, Stack, Design, Ordered_Set | 617 | 100.00
1729+
| 0894 |[All Possible Full Binary Trees](src/main/kotlin/g0801_0900/s0894_all_possible_full_binary_trees/Solution.kt)| Medium | Dynamic_Programming, Tree, Binary_Tree, Recursion, Memoization | 257 | 100.00
1730+
| 0893 |[Groups of Special-Equivalent Strings](src/main/kotlin/g0801_0900/s0893_groups_of_special_equivalent_strings/Solution.kt)| Medium | Array, String, Hash_Table | 141 | 100.00
1731+
| 0892 |[Surface Area of 3D Shapes](src/main/kotlin/g0801_0900/s0892_surface_area_of_3d_shapes/Solution.kt)| Easy | Array, Math, Matrix, Geometry | 180 | 100.00
17281732
| 0891 |[Sum of Subsequence Widths](src/main/kotlin/g0801_0900/s0891_sum_of_subsequence_widths/Solution.kt)| Hard | Array, Math, Sorting | 481 | 100.00
17291733
| 0890 |[Find and Replace Pattern](src/main/kotlin/g0801_0900/s0890_find_and_replace_pattern/Solution.kt)| Medium | Array, String, Hash_Table | 150 | 100.00
17301734
| 0889 |[Construct Binary Tree from Preorder and Postorder Traversal](src/main/kotlin/g0801_0900/s0889_construct_binary_tree_from_preorder_and_postorder_traversal/Solution.kt)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 168 | 100.00
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g0801_0900.s0892_surface_area_of_3d_shapes
2+
3+
// #Easy #Array #Math #Matrix #Geometry #2023_04_11_Time_180_ms_(100.00%)_Space_35.8_MB_(100.00%)
4+
5+
class Solution {
6+
fun surfaceArea(grid: Array<IntArray>): Int {
7+
var surfaceArea = 0
8+
for (i in grid.indices) {
9+
for (j in grid[i].indices) {
10+
if (grid[i][j] > 0) {
11+
surfaceArea += 4 * grid[i][j] + 2
12+
surfaceArea -= hiddenSides(i, j, grid)
13+
}
14+
}
15+
}
16+
return surfaceArea
17+
}
18+
19+
private fun hiddenSides(i: Int, j: Int, grid: Array<IntArray>): Int {
20+
var hidden = 0
21+
val tower = grid[i][j]
22+
if (j + 1 < grid[i].size && grid[i][j + 1] > 0) {
23+
hidden += tower.coerceAtMost(grid[i][j + 1])
24+
}
25+
if (j - 1 >= 0 && grid[i][j - 1] > 0) {
26+
hidden += tower.coerceAtMost(grid[i][j - 1])
27+
}
28+
if (i + 1 < grid.size && grid[i + 1][j] > 0) {
29+
hidden += tower.coerceAtMost(grid[i + 1][j])
30+
}
31+
if (i - 1 >= 0 && grid[i - 1][j] > 0) {
32+
hidden += tower.coerceAtMost(grid[i - 1][j])
33+
}
34+
return hidden
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
892\. Surface Area of 3D Shapes
2+
3+
Easy
4+
5+
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)`.
6+
7+
After placing these cubes, you have decided to glue any directly adjacent cubes to each other, forming several irregular 3D shapes.
8+
9+
Return _the total surface area of the resulting shapes_.
10+
11+
**Note:** The bottom face of each shape counts toward its surface area.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid2.jpg)
16+
17+
**Input:** grid = [[1,2],[3,4]]
18+
19+
**Output:** 34
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid4.jpg)
24+
25+
**Input:** grid = [[1,1,1],[1,0,1],[1,1,1]]
26+
27+
**Output:** 32
28+
29+
**Example 3:**
30+
31+
![](https://assets.leetcode.com/uploads/2021/01/08/tmp-grid5.jpg)
32+
33+
**Input:** grid = [[2,2,2],[2,1,2],[2,2,2]]
34+
35+
**Output:** 46
36+
37+
**Constraints:**
38+
39+
* `n == grid.length == grid[i].length`
40+
* `1 <= n <= 50`
41+
* `0 <= grid[i][j] <= 50`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0801_0900.s0893_groups_of_special_equivalent_strings
2+
3+
// #Medium #Array #String #Hash_Table #2023_04_11_Time_141_ms_(100.00%)_Space_34.7_MB_(100.00%)
4+
5+
class Solution {
6+
fun numSpecialEquivGroups(words: Array<String>): Int {
7+
val set: HashSet<String> = HashSet()
8+
var result = 0
9+
for (str in words) {
10+
if (set.add(getHashBySwap(str.toCharArray()))) {
11+
result++
12+
}
13+
}
14+
return result
15+
}
16+
17+
private fun getHashBySwap(chars: CharArray): String {
18+
for (i in chars.indices) {
19+
var j = i + 2
20+
while (j < chars.size) {
21+
if (chars[i] > chars[j]) {
22+
val temp = chars[j]
23+
chars[j] = chars[i]
24+
chars[i] = temp
25+
}
26+
j += 2
27+
}
28+
}
29+
return String(chars)
30+
}
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
893\. Groups of Special-Equivalent Strings
2+
3+
Medium
4+
5+
You are given an array of strings of the same length `words`.
6+
7+
In one **move**, you can swap any two even indexed characters or any two odd indexed characters of a string `words[i]`.
8+
9+
Two strings `words[i]` and `words[j]` are **special-equivalent** if after any number of moves, `words[i] == words[j]`.
10+
11+
* For example, `words[i] = "zzxy"` and `words[j] = "xyzz"` are **special-equivalent** because we may make the moves `"zzxy" -> "xzzy" -> "xyzz"`.
12+
13+
A **group of special-equivalent strings** from `words` is a non-empty subset of words such that:
14+
15+
* Every pair of strings in the group are special equivalent, and
16+
* 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).
17+
18+
Return _the number of **groups of special-equivalent strings** from_ `words`.
19+
20+
**Example 1:**
21+
22+
**Input:** words = ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
23+
24+
**Output:** 3
25+
26+
**Explanation:**
27+
28+
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.
29+
30+
The other two groups are ["xyzz", "zzxy"] and ["zzyx"].
31+
32+
Note that in particular, "zzxy" is not special equivalent to "zzyx".
33+
34+
**Example 2:**
35+
36+
**Input:** words = ["abc","acb","bac","bca","cab","cba"]
37+
38+
**Output:** 3
39+
40+
**Constraints:**
41+
42+
* `1 <= words.length <= 1000`
43+
* `1 <= words[i].length <= 20`
44+
* `words[i]` consist of lowercase English letters.
45+
* All the strings are of the same length.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package g0801_0900.s0894_all_possible_full_binary_trees
2+
3+
// #Medium #Dynamic_Programming #Tree #Binary_Tree #Recursion #Memoization
4+
// #2023_04_11_Time_257_ms_(100.00%)_Space_46.5_MB_(90.00%)
5+
6+
import com_github_leetcode.TreeNode
7+
8+
/*
9+
* Example:
10+
* var ti = TreeNode(5)
11+
* var v = ti.`val`
12+
* Definition for a binary tree node.
13+
* class TreeNode(var `val`: Int) {
14+
* var left: TreeNode? = null
15+
* var right: TreeNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun allPossibleFBT(n: Int): List<TreeNode> {
20+
if (n % 2 == 0) {
21+
// no complete binary tree possible
22+
return ArrayList()
23+
}
24+
val dp: Array<ArrayList<TreeNode>?> = arrayOfNulls(n + 1)
25+
// form left to right
26+
var i = 1
27+
while (i <= n) {
28+
helper(i, dp)
29+
i += 2
30+
}
31+
return dp[n]!!
32+
}
33+
34+
// Using tabulation
35+
private fun helper(n: Int, dp: Array<ArrayList<TreeNode>?>) {
36+
if (n <= 0) {
37+
return
38+
}
39+
if (n == 1) {
40+
dp[1] = ArrayList()
41+
dp[1]!!.add(TreeNode(0))
42+
return
43+
}
44+
dp[n] = ArrayList()
45+
var i = 1
46+
while (i < n) {
47+
// left
48+
for (nodeL in dp[i]!!) {
49+
// right
50+
for (nodeR in dp[n - i - 1]!!) {
51+
// 1 node used here
52+
val root = TreeNode(0)
53+
root.left = nodeL
54+
root.right = nodeR
55+
dp[n]!!.add(root)
56+
}
57+
}
58+
i += 2
59+
}
60+
}
61+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
894\. All Possible Full Binary Trees
2+
3+
Medium
4+
5+
Given an integer `n`, return _a list of all possible **full binary trees** with_ `n` _nodes_. Each node of each tree in the answer must have `Node.val == 0`.
6+
7+
Each element of the answer is the root node of one possible tree. You may return the final list of trees in **any order**.
8+
9+
A **full binary tree** is a binary tree where each node has exactly `0` or `2` children.
10+
11+
**Example 1:**
12+
13+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/22/fivetrees.png)
14+
15+
**Input:** n = 7
16+
17+
**Output:** [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
18+
19+
**Example 2:**
20+
21+
**Input:** n = 3
22+
23+
**Output:** [[0,0,0]]
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 20`
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g0801_0900.s0895_maximum_frequency_stack
2+
3+
// #Hard #Hash_Table #Stack #Design #Ordered_Set
4+
// #2023_04_11_Time_617_ms_(100.00%)_Space_62.2_MB_(95.00%)
5+
6+
class FreqStack {
7+
private class Node {
8+
var next: Node?
9+
var `val` = 0
10+
11+
constructor(`val`: Int) {
12+
this.`val` = `val`
13+
next = null
14+
}
15+
16+
constructor() {
17+
next = null
18+
}
19+
}
20+
21+
private class DLL {
22+
var head: Node = Node()
23+
var size: Int = 0
24+
25+
fun addNode(x: Int) {
26+
val node = Node(x)
27+
node.next = head.next
28+
head.next = node
29+
size++
30+
}
31+
32+
fun removeNode(): Node? {
33+
val node = head.next
34+
if (node != null) {
35+
head.next = node.next
36+
node.next = null
37+
size--
38+
}
39+
return node
40+
}
41+
}
42+
43+
private var max = 0
44+
private val freqMap: HashMap<Int, Int> = HashMap()
45+
private val freqListMap: HashMap<Int, DLL> = HashMap()
46+
47+
fun push(`val`: Int) {
48+
val count = freqMap.getOrDefault(`val`, 0) + 1
49+
max = max.coerceAtLeast(count)
50+
freqMap[`val`] = count
51+
val dll = freqListMap.getOrDefault(count, DLL())
52+
dll.addNode(`val`)
53+
freqListMap[count] = dll
54+
}
55+
56+
fun pop(): Int {
57+
val dll = freqListMap[max]
58+
val node = dll!!.removeNode()
59+
freqMap[node!!.`val`] = max - 1
60+
if (dll.size == 0) {
61+
max--
62+
}
63+
return node.`val`
64+
}
65+
}
66+
67+
/*
68+
* Your FreqStack object will be instantiated and called as such:
69+
* var obj = FreqStack()
70+
* obj.push(`val`)
71+
* var param_2 = obj.pop()
72+
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
895\. Maximum Frequency Stack
2+
3+
Hard
4+
5+
Design a stack-like data structure to push elements to the stack and pop the most frequent element from the stack.
6+
7+
Implement the `FreqStack` class:
8+
9+
* `FreqStack()` constructs an empty frequency stack.
10+
* `void push(int val)` pushes an integer `val` onto the top of the stack.
11+
* `int pop()` removes and returns the most frequent element in the stack.
12+
* If there is a tie for the most frequent element, the element closest to the stack's top is removed and returned.
13+
14+
**Example 1:**
15+
16+
**Input**
17+
18+
["FreqStack", "push", "push", "push", "push", "push", "push", "pop", "pop", "pop", "pop"]
19+
20+
[[], [5], [7], [5], [7], [4], [5], [], [], [], []]
21+
22+
**Output:** [null, null, null, null, null, null, null, 5, 7, 5, 4]
23+
24+
**Explanation:**
25+
26+
FreqStack freqStack = new FreqStack();
27+
freqStack.push(5); // The stack is [5]
28+
freqStack.push(7); // The stack is [5,7]
29+
freqStack.push(5); // The stack is [5,7,5]
30+
freqStack.push(7); // The stack is [5,7,5,7]
31+
freqStack.push(4); // The stack is [5,7,5,7,4]
32+
freqStack.push(5); // The stack is [5,7,5,7,4,5]
33+
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,5,7,4].
34+
freqStack.pop(); // return 7, as 5 and 7 is the most frequent, but 7 is closest to the top. The stack becomes [5,7,5,4].
35+
freqStack.pop(); // return 5, as 5 is the most frequent. The stack becomes [5,7,4].
36+
freqStack.pop(); // return 4, as 4, 5 and 7 is the most frequent, but 4 is closest to the top. The stack becomes [5,7].
37+
38+
**Constraints:**
39+
40+
* <code>0 <= val <= 10<sup>9</sup></code>
41+
* At most <code>2 * 10<sup>4</sup></code> calls will be made to `push` and `pop`.
42+
* It is guaranteed that there will be at least one element in the stack before calling `pop`.

0 commit comments

Comments
 (0)