Skip to content

Commit d0ade85

Browse files
authored
Added tasks 292-299.
1 parent 3cd1e51 commit d0ade85

File tree

5 files changed

+254
-1
lines changed

5 files changed

+254
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,7 @@
663663
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
664664
|-|-|-|-|-|-
665665
| 0001 |[Two Sum](src/main/kotlin/g0001_0100/s0001_two_sum)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 234 | 92.75
666+
| 0299 |[Bulls and Cows](src/main/kotlin/g0201_0300/s0299_bulls_and_cows)| Medium | String, Hash_Table, Counting | 254 | 84.82
666667

667668
#### Day 14 Stack
668669

@@ -930,6 +931,7 @@
930931
| 0111 |[Minimum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0111_minimum_depth_of_binary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 525 | 90.51
931932
| 0104 |[Maximum Depth of Binary Tree](src/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
932933
| 0110 |[Balanced Binary Tree](src/main/kotlin/g0101_0200/s0110_balanced_binary_tree)| Easy | Depth_First_Search, Tree, Binary_Tree | 310 | 63.63
934+
| 0297 |[Serialize and Deserialize Binary Tree](src/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design | 475 | 78.85
933935
| 0124 |[Binary Tree Maximum Path Sum](src/main/kotlin/g0101_0200/s0124_binary_tree_maximum_path_sum)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree | 331 | 74.42
934936
| 0098 |[Validate Binary Search Tree](src/main/kotlin/g0001_0100/s0098_validate_binary_search_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 330 | 41.38
935937
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree | 386 | 45.21
@@ -1214,6 +1216,7 @@
12141216
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12151217
|-|-|-|-|-|-
12161218
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/kotlin/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Tree, Binary_Tree | 386 | 45.21
1219+
| 0297 |[Serialize and Deserialize Binary Tree](src/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design | 475 | 78.85
12171220

12181221
#### Day 19 Graph
12191222

@@ -1558,7 +1561,10 @@
15581561
| 0338 |[Counting Bits](src/main/kotlin/g0301_0400/s0338_counting_bits)| Easy | Top_100_Liked_Questions, Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation | 186 | 99.26
15591562
| 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, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming | 332 | 50.68
15601563
| 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, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming | 318 | 82.28
1564+
| 0299 |[Bulls and Cows](src/main/kotlin/g0201_0300/s0299_bulls_and_cows)| Medium | String, Hash_Table, Counting, Level_1_Day_13_Hashmap | 254 | 84.82
1565+
| 0297 |[Serialize and Deserialize Binary Tree](src/main/kotlin/g0201_0300/s0297_serialize_and_deserialize_binary_tree)| Hard | Top_Interview_Questions, String, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Design, Data_Structure_II_Day_18_Tree, Udemy_Tree_Stack_Queue | 475 | 78.85
15611566
| 0295 |[Find Median from Data Stream](src/main/kotlin/g0201_0300/s0295_find_median_from_data_stream)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream | 2289 | 33.60
1567+
| 0292 |[Nim Game](src/main/kotlin/g0201_0300/s0292_nim_game)| Easy | Math, Game_Theory, Brainteaser | 129 | 94.29
15621568
| 0290 |[Word Pattern](src/main/kotlin/g0201_0300/s0290_word_pattern)| Easy | String, Hash_Table, Data_Structure_II_Day_7_String | 177 | 89.55
15631569
| 0289 |[Game of Life](src/main/kotlin/g0201_0300/s0289_game_of_life)| Medium | Top_Interview_Questions, Array, Matrix, Simulation | 174 | 96.97
15641570
| 0287 |[Find the Duplicate Number](src/main/kotlin/g0201_0300/s0287_find_the_duplicate_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5 | 656 | 66.21

src/main/kotlin/g0101_0200/s0177_nth_highest_salary/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ DECLARE M INT;
7272
SET M=N-1;
7373
RETURN (
7474
# Write your MySQL query statement below.
75-
# #Medium #Database #2022_10_18_Time_338_ms_(86.68%)_Space_0B_(100.00%)
7675
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT M, 1
7776
);
7877
END
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
## 292\. Nim Game
5+
6+
Easy
7+
8+
You are playing the following Nim Game with your friend:
9+
10+
* Initially, there is a heap of stones on the table.
11+
* You and your friend will alternate taking turns, and **you go first**.
12+
* On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
13+
* The one who removes the last stone is the winner.
14+
15+
Given `n`, the number of stones in the heap, return `true` _if you can win the game assuming both you and your friend play optimally, otherwise return_ `false`.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 4
20+
21+
**Output:** false
22+
23+
**Explanation:** These are the possible outcomes: 1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins. 2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins. 3. You remove 3 stones. Your friend removes the last stone. Your friend wins. In all outcomes, your friend wins.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 1
28+
29+
**Output:** true
30+
31+
**Example 3:**
32+
33+
**Input:** n = 2
34+
35+
**Output:** true
36+
37+
**Constraints:**
38+
39+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
40+
41+
## Solution
42+
43+
```kotlin
44+
class Solution {
45+
fun canWinNim(n: Int): Boolean {
46+
if (n % 4 == 0) {
47+
return false
48+
}
49+
return true
50+
}
51+
}
52+
```
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
## 297\. Serialize and Deserialize Binary Tree
5+
6+
Hard
7+
8+
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
9+
10+
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
11+
12+
**Clarification:** The input/output format is the same as [how LeetCode serializes a binary tree](https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation-). You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2020/09/15/serdeser.jpg)
17+
18+
**Input:** root = [1,2,3,null,null,4,5]
19+
20+
**Output:** [1,2,3,null,null,4,5]
21+
22+
**Example 2:**
23+
24+
**Input:** root = []
25+
26+
**Output:** []
27+
28+
**Constraints:**
29+
30+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
31+
* `-1000 <= Node.val <= 1000`
32+
33+
## Solution
34+
35+
```kotlin
36+
import com_github_leetcode.TreeNode
37+
38+
/*
39+
* Definition for a binary tree node.
40+
* class TreeNode(var `val`: Int) {
41+
* var left: TreeNode? = null
42+
* var right: TreeNode? = null
43+
* }
44+
*/
45+
46+
class Codec {
47+
private var offset = 0
48+
49+
// Encodes a tree to a single string.
50+
fun serialize(root: TreeNode?): String {
51+
val sb = StringBuilder()
52+
offset = 0
53+
serialize(root, sb)
54+
return sb.toString()
55+
}
56+
57+
fun serialize(root: TreeNode?, sb: StringBuilder) {
58+
// all nodes fit into 4 bits.
59+
// IFF we offset at 0. So encode(val) = val + min(default - 1000)
60+
if (root == null) {
61+
sb.append(DELIM)
62+
return
63+
}
64+
val s = Integer.toHexString(root.`val` + BASE_OFFSET)
65+
val sb2 = StringBuilder()
66+
for (i in 0 until 3 - s.length) {
67+
sb2.append('0')
68+
}
69+
sb2.append(s)
70+
sb.append(sb2)
71+
serialize(root.left, sb)
72+
serialize(root.right, sb)
73+
}
74+
75+
// Decodes your encoded data to tree.
76+
fun deserialize(data: String): TreeNode? {
77+
if (data[offset] == '*') {
78+
offset++
79+
return null
80+
}
81+
val root = TreeNode(
82+
data.substring(offset, offset + 3).toInt(16) - BASE_OFFSET
83+
)
84+
offset += 3
85+
root.left = deserialize(data)
86+
root.right = deserialize(data)
87+
return root
88+
}
89+
90+
companion object {
91+
private const val BASE_OFFSET = 1000
92+
private const val DELIM = "*"
93+
}
94+
}
95+
96+
/*
97+
* Your Codec object will be instantiated and called as such:
98+
* var ser = Codec()
99+
* var deser = Codec()
100+
* var data = ser.serialize(longUrl)
101+
* var ans = deser.deserialize(data)
102+
*/
103+
```
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
## 299\. Bulls and Cows
5+
6+
Medium
7+
8+
You are playing the **[Bulls and Cows](https://en.wikipedia.org/wiki/Bulls_and_Cows)** game with your friend.
9+
10+
You write down a secret number and ask your friend to guess what the number is. When your friend makes a guess, you provide a hint with the following info:
11+
12+
* The number of "bulls", which are digits in the guess that are in the correct position.
13+
* The number of "cows", which are digits in the guess that are in your secret number but are located in the wrong position. Specifically, the non-bull digits in the guess that could be rearranged such that they become bulls.
14+
15+
Given the secret number `secret` and your friend's guess `guess`, return _the hint for your friend's guess_.
16+
17+
The hint should be formatted as `"xAyB"`, where `x` is the number of bulls and `y` is the number of cows. Note that both `secret` and `guess` may contain duplicate digits.
18+
19+
**Example 1:**
20+
21+
**Input:** secret = "1807", guess = "7810"
22+
23+
**Output:** "1A3B"
24+
25+
**Explanation:**
26+
27+
Bulls are connected with a '|' and cows are underlined:
28+
"1807"
29+
|
30+
"7810"
31+
32+
**Example 2:**
33+
34+
**Input:** secret = "1123", guess = "0111"
35+
36+
**Output:** "1A1B"
37+
38+
**Explanation:**
39+
40+
Bulls are connected with a '|' and cows are underlined:
41+
"1123" "1123"
42+
| or |
43+
"0111" "0111"
44+
Note that only one of the two unmatched 1s is counted as a cow since the non-bull digits can only be rearranged to allow one 1 to be a bull.
45+
46+
**Example 3:**
47+
48+
**Input:** secret = "1", guess = "0"
49+
50+
**Output:** "0A0B"
51+
52+
**Example 4:**
53+
54+
**Input:** secret = "1", guess = "1"
55+
56+
**Output:** "1A0B"
57+
58+
**Constraints:**
59+
60+
* `1 <= secret.length, guess.length <= 1000`
61+
* `secret.length == guess.length`
62+
* `secret` and `guess` consist of digits only.
63+
64+
## Solution
65+
66+
```kotlin
67+
class Solution {
68+
fun getHint(secret: String, guess: String): String {
69+
val ans = IntArray(10)
70+
var bulls = 0
71+
var cows = 0
72+
for (i in 0 until secret.length) {
73+
val s = Character.getNumericValue(secret[i])
74+
val g = Character.getNumericValue(guess[i])
75+
if (s == g) {
76+
bulls++
77+
} else {
78+
// digit s was already seen in guess, is being seen again in secret
79+
if (ans[s] < 0) {
80+
cows++
81+
}
82+
// digit was already seen in secret, now being seen again in guess
83+
if (ans[g] > 0) {
84+
cows++
85+
}
86+
ans[s]++
87+
ans[g]--
88+
}
89+
}
90+
return bulls.toString() + "A" + cows + "B"
91+
}
92+
}
93+
```

0 commit comments

Comments
 (0)