Skip to content

Commit 04fde26

Browse files
authored
Added tasks 532, 535, 537, 538.
1 parent eb96b83 commit 04fde26

File tree

13 files changed

+350
-1
lines changed

13 files changed

+350
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Maven Central](https://img.shields.io/maven-central/v/com.github.javadev/leetcode-in-kotlin.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.github.javadev%22%20AND%20a%3A%22leetcode-in-kotlin%22)
44
[![MIT License](http://img.shields.io/badge/license-MIT-green.svg) ](https://github.com/javadev/leetcode-in-kotlin/blob/main/LICENSE)
5-
[![Java CI with Maven](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/gradle.yml/badge.svg)](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/gradle.yml)
5+
[![Java CI with Maven](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/maven.yml/badge.svg)](https://github.com/javadev/LeetCode-in-Kotlin/actions/workflows/maven.yml)
66
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=javadev_LeetCode-in-Kotlin&metric=sqale_rating)](https://sonarcloud.io/summary/overall?id=javadev_LeetCode-in-Kotlin)
77
[![javadoc](https://javadoc.io/badge2/com.github.javadev/leetcode-in-kotlin/javadoc.svg)](https://javadoc.io/doc/com.github.javadev/leetcode-in-kotlin)
88
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
@@ -677,6 +677,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
677677
| 0448 |[Find All Numbers Disappeared in an Array](src.save/main/kotlin/g0401_0500/s0448_find_all_numbers_disappeared_in_an_array/Solution.kt)| Easy | Array, Hash_Table | 394 | 100.00
678678
| 0442 |[Find All Duplicates in an Array](src.save/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table | 480 | 73.81
679679
| 0041 |[First Missing Positive](src.save/main/kotlin/g0001_0100/s0041_first_missing_positive/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table | 345 | 100.00
680+
| 0532 |[K-diff Pairs in an Array](src/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers | 230 | 84.62
680681
| 0456 |[132 Pattern](src.save/main/kotlin/g0401_0500/s0456_132_pattern/Solution.kt)| Medium | Array, Binary_Search, Stack, Ordered_Set, Monotonic_Stack | 434 | 100.00
681682
| 0239 |[Sliding Window Maximum](src.save/main/kotlin/g0201_0300/s0239_sliding_window_maximum/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue | 1059 | 86.14
682683

@@ -1648,6 +1649,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.8'
16481649
| 0647 |[Palindromic Substrings](src/main/kotlin/g0601_0700/s0647_palindromic_substrings/Solution.kt)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming | 266 | 67.83
16491650
| 0560 |[Subarray Sum Equals K](src/main/kotlin/g0501_0600/s0560_subarray_sum_equals_k/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 692 | 53.27
16501651
| 0543 |[Diameter of Binary Tree](src/main/kotlin/g0501_0600/s0543_diameter_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 307 | 43.93
1652+
| 0538 |[Convert BST to Greater Tree](src/main/kotlin/g0501_0600/s0538_convert_bst_to_greater_tree/Solution.kt)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 252 | 77.78
1653+
| 0537 |[Complex Number Multiplication](src/main/kotlin/g0501_0600/s0537_complex_number_multiplication/Solution.kt)| Medium | String, Math, Simulation | 171 | 75.00
1654+
| 0535 |[Encode and Decode TinyURL](src/main/kotlin/g0501_0600/s0535_encode_and_decode_tinyurl/Codec.kt)| Medium | String, Hash_Table, Design, Hash_Function | 183 | 81.25
1655+
| 0532 |[K-diff Pairs in an Array](src/main/kotlin/g0501_0600/s0532_k_diff_pairs_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers, Udemy_Arrays | 230 | 84.62
16511656
| 0530 |[Minimum Absolute Difference in BST](src/main/kotlin/g0501_0600/s0530_minimum_absolute_difference_in_bst/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 209 | 86.96
16521657
| 0529 |[Minesweeper](src/main/kotlin/g0501_0600/s0529_minesweeper/Solution.kt)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 243 | 87.50
16531658
| 0528 |[Random Pick with Weight](src/main/kotlin/g0501_0600/s0528_random_pick_with_weight/Solution.kt)| Medium | Math, Binary_Search, Prefix_Sum, Randomized, Binary_Search_II_Day_13 | 393 | 91.38
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g0501_0600.s0532_k_diff_pairs_in_an_array
2+
3+
// #Medium #Array #Hash_Table #Sorting #Binary_Search #Two_Pointers #Udemy_Arrays
4+
// #2023_01_15_Time_230_ms_(84.62%)_Space_37.3_MB_(92.31%)
5+
6+
class Solution {
7+
fun findPairs(nums: IntArray, k: Int): Int {
8+
var res = 0
9+
val set: HashSet<Int> = HashSet()
10+
val twice: HashSet<Int> = HashSet()
11+
for (n in nums) {
12+
if (set.contains(n)) {
13+
if (k == 0 && !twice.contains(n)) {
14+
res++
15+
twice.add(n)
16+
} else {
17+
continue
18+
}
19+
} else {
20+
if (set.contains(n - k)) {
21+
res++
22+
}
23+
if (set.contains(n + k)) {
24+
res++
25+
}
26+
}
27+
set.add(n)
28+
}
29+
return res
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
532\. K-diff Pairs in an Array
2+
3+
Medium
4+
5+
Given an array of integers `nums` and an integer `k`, return _the number of **unique** k-diff pairs in the array_.
6+
7+
A **k-diff** pair is an integer pair `(nums[i], nums[j])`, where the following are true:
8+
9+
* `0 <= i, j < nums.length`
10+
* `i != j`
11+
* `nums[i] - nums[j] == k`
12+
13+
**Notice** that `|val|` denotes the absolute value of `val`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [3,1,4,1,5], k = 2
18+
19+
**Output:** 2
20+
21+
**Explanation:** There are two 2-diff pairs in the array, (1, 3) and (3, 5). Although we have two 1s in the input, we should only return the number of **unique** pairs.
22+
23+
**Example 2:**
24+
25+
**Input:** nums = [1,2,3,4,5], k = 1
26+
27+
**Output:** 4
28+
29+
**Explanation:** There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,3,1,5,4], k = 0
34+
35+
**Output:** 1
36+
37+
**Explanation:** There is one 0-diff pair in the array, (1, 1).
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
42+
* <code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code>
43+
* <code>0 <= k <= 10<sup>7</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g0501_0600.s0535_encode_and_decode_tinyurl
2+
3+
// #Medium #String #Hash_Table #Design #Hash_Function
4+
// #2023_01_15_Time_183_ms_(81.25%)_Space_35.8_MB_(68.75%)
5+
6+
class Codec {
7+
private val map: MutableMap<String, String> = HashMap()
8+
private var n = 0
9+
10+
// Encodes a URL to a shortened URL.
11+
fun encode(longUrl: String): String {
12+
n++
13+
var ans = "http://tinyurl.com/"
14+
ans += n.toString()
15+
map[ans] = longUrl
16+
return ans
17+
}
18+
19+
// Decodes a shortened URL to its original URL.
20+
fun decode(shortUrl: String): String? {
21+
return map[shortUrl]
22+
}
23+
}
24+
25+
/*
26+
* Your Codec object will be instantiated and called as such:
27+
* var obj = Codec()
28+
* var url = obj.encode(longUrl)
29+
* var ans = obj.decode(url)
30+
*/
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
535\. Encode and Decode TinyURL
2+
3+
Medium
4+
5+
> Note: This is a companion problem to the [System Design](https://leetcode.com/discuss/interview-question/system-design/) problem: [Design TinyURL](https://leetcode.com/discuss/interview-question/124658/Design-a-URL-Shortener-(-TinyURL-)-System/).
6+
7+
TinyURL is a URL shortening service where you enter a URL such as `https://leetcode.com/problems/design-tinyurl` and it returns a short URL such as `http://tinyurl.com/4e9iAk`. Design a class to encode a URL and decode a tiny URL.
8+
9+
There is no restriction on how your encode/decode algorithm should work. You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.
10+
11+
Implement the `Solution` class:
12+
13+
* `Solution()` Initializes the object of the system.
14+
* `String encode(String longUrl)` Returns a tiny URL for the given `longUrl`.
15+
* `String decode(String shortUrl)` Returns the original long URL for the given `shortUrl`. It is guaranteed that the given `shortUrl` was encoded by the same object.
16+
17+
**Example 1:**
18+
19+
**Input:** url = "https://leetcode.com/problems/design-tinyurl"
20+
21+
**Output:** "https://leetcode.com/problems/design-tinyurl"
22+
23+
**Explanation:**
24+
25+
Solution obj = new Solution();
26+
string tiny = obj.encode(url); // returns the encoded tiny url.
27+
string ans = obj.decode(tiny); // returns the original url after deconding it.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= url.length <= 10<sup>4</sup></code>
32+
* `url` is guranteed to be a valid URL.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g0501_0600.s0537_complex_number_multiplication
2+
3+
// #Medium #String #Math #Simulation #2023_01_16_Time_171_ms_(75.00%)_Space_35.2_MB_(100.00%)
4+
5+
class Solution {
6+
fun complexNumberMultiply(num1: String, num2: String): String {
7+
val countReal: Int
8+
val countImagine: Int
9+
val arr1 = IntArray(2)
10+
val arr2 = IntArray(2)
11+
arr1[0] = num1.substring(0, num1.indexOf("+")).toInt()
12+
arr1[1] = num1.substring(num1.indexOf("+") + 1, num1.length - 1).toInt()
13+
arr2[0] = num2.substring(0, num2.indexOf("+")).toInt()
14+
arr2[1] = num2.substring(num2.indexOf("+") + 1, num2.length - 1).toInt()
15+
countReal = arr1[0] * arr2[0] - arr1[1] * arr2[1]
16+
countImagine = arr1[0] * arr2[1] + arr1[1] * arr2[0]
17+
return countReal.toString() + "+" + countImagine + "i"
18+
}
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
537\. Complex Number Multiplication
2+
3+
Medium
4+
5+
A [complex number](https://en.wikipedia.org/wiki/Complex_number) can be represented as a string on the form <code>"**real**+**imaginary**i"</code> where:
6+
7+
* `real` is the real part and is an integer in the range `[-100, 100]`.
8+
* `imaginary` is the imaginary part and is an integer in the range `[-100, 100]`.
9+
* <code>i<sup>2</sup> == -1</code>.
10+
11+
Given two complex numbers `num1` and `num2` as strings, return _a string of the complex number that represents their multiplications_.
12+
13+
**Example 1:**
14+
15+
**Input:** num1 = "1+1i", num2 = "1+1i"
16+
17+
**Output:** "0+2i"
18+
19+
**Explanation:** (1 + i) \* (1 + i) = 1 + i2 + 2 \* i = 2i, and you need convert it to the form of 0+2i.
20+
21+
**Example 2:**
22+
23+
**Input:** num1 = "1+-1i", num2 = "1+-1i"
24+
25+
**Output:** "0+-2i"
26+
27+
**Explanation:** (1 - i) \* (1 - i) = 1 + i2 - 2 \* i = -2i, and you need convert it to the form of 0+-2i.
28+
29+
**Constraints:**
30+
31+
* `num1` and `num2` are valid complex numbers.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0501_0600.s0538_convert_bst_to_greater_tree
2+
3+
// #Medium #Depth_First_Search #Tree #Binary_Tree #Binary_Search_Tree
4+
// #2023_01_16_Time_252_ms_(77.78%)_Space_36_MB_(100.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 convertBST(root: TreeNode?): TreeNode? {
20+
if (root != null) {
21+
postOrder(root, 0)
22+
}
23+
return root
24+
}
25+
26+
private fun postOrder(node: TreeNode, `val`: Int): Int {
27+
var newVal = 0
28+
if (node.right != null) {
29+
newVal += postOrder(node.right!!, `val`)
30+
}
31+
newVal += if (newVal == 0) `val` + node.`val` else node.`val`
32+
node.`val` = newVal
33+
if (node.left != null) {
34+
newVal = postOrder(node.left!!, newVal)
35+
}
36+
return newVal
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
538\. Convert BST to Greater Tree
2+
3+
Medium
4+
5+
Given the `root` of a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST.
6+
7+
As a reminder, a _binary search tree_ is a tree that satisfies these constraints:
8+
9+
* The left subtree of a node contains only nodes with keys **less than** the node's key.
10+
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
11+
* Both the left and right subtrees must also be binary search trees.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2019/05/02/tree.png)
16+
17+
**Input:** root = [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
18+
19+
**Output:** [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
20+
21+
**Example 2:**
22+
23+
**Input:** root = [0,null,1]
24+
25+
**Output:** [1,null,1]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.
30+
* <code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code>
31+
* All the values in the tree are **unique**.
32+
* `root` is guaranteed to be a valid binary search tree.
33+
34+
**Note:** This question is the same as 1038: [https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0501_0600.s0532_k_diff_pairs_in_an_array
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun findPairs() {
10+
assertThat(Solution().findPairs(intArrayOf(3, 1, 4, 1, 5), 2), equalTo(2))
11+
}
12+
13+
@Test
14+
fun findPairs2() {
15+
assertThat(Solution().findPairs(intArrayOf(1, 2, 3, 4, 5), 1), equalTo(4))
16+
}
17+
18+
@Test
19+
fun findPairs3() {
20+
assertThat(Solution().findPairs(intArrayOf(1, 3, 1, 5, 4), 0), equalTo(1))
21+
}
22+
}

0 commit comments

Comments
 (0)