Skip to content

Commit 6e4a282

Browse files
authored
Added tasks 441, 442, 443, 445.
1 parent 457081d commit 6e4a282

File tree

13 files changed

+396
-0
lines changed

13 files changed

+396
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
355355
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
356356
|-|-|-|-|-|-
357357
| 0002 |[Add Two Numbers](src.save/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Math, Linked_List, Recursion | 417 | 50.44
358+
| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii/Solution.kt)| Medium | Math, Stack, Linked_List | 240 | 82.61
358359

359360
#### Day 16
360361

@@ -809,6 +810,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
809810
| 0075 |[Sort Colors](src.save/main/kotlin/g0001_0100/s0075_sort_colors/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers | 198 | 85.66
810811
| 0066 |[Plus One](src.save/main/kotlin/g0001_0100/s0066_plus_one/Solution.kt)| Easy | Top_Interview_Questions, Array, Math | 303 | 35.18
811812
| 0238 |[Product of Array Except Self](src.save/main/kotlin/g0201_0300/s0238_product_of_array_except_self/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 669 | 48.96
813+
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table | 480 | 73.81
812814
| 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
813815
| 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
814816

@@ -850,6 +852,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
850852
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
851853
|-|-|-|-|-|-
852854
| 0114 |[Flatten Binary Tree to Linked List](src.save/main/kotlin/g0101_0200/s0114_flatten_binary_tree_to_linked_list/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List | 191 | 93.10
855+
| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii/Solution.kt)| Medium | Math, Stack, Linked_List | 240 | 82.61
853856
| 0328 |[Odd Even Linked List](src.save/main/kotlin/g0301_0400/s0328_odd_even_linked_list/Solution.kt)| Medium | Top_Interview_Questions, Linked_List | 216 | 86.96
854857
| 0061 |[Rotate List](src.save/main/kotlin/g0001_0100/s0061_rotate_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 193 | 92.16
855858
| 0024 |[Swap Nodes in Pairs](src.save/main/kotlin/g0001_0100/s0024_swap_nodes_in_pairs/Solution.kt)| Medium | Top_100_Liked_Questions, Linked_List, Recursion | 149 | 99.39
@@ -1469,6 +1472,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
14691472

14701473
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14711474
|-|-|-|-|-|-
1475+
| 0441 |[Arranging Coins](src/main/kotlin/g0401_0500/s0441_arranging_coins/Solution.kt)| Easy | Math, Binary_Search | 150 | 84.21
14721476

14731477
#### Day 7
14741478

@@ -1630,6 +1634,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16301634
| 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
16311635
| 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
16321636
| 0494 |[Target Sum](src/main/kotlin/g0401_0500/s0494_target_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Backtracking | 308 | 89.61
1637+
| 0445 |[Add Two Numbers II](src/main/kotlin/g0401_0500/s0445_add_two_numbers_ii/Solution.kt)| Medium | Math, Stack, Linked_List, Programming_Skills_II_Day_15, Udemy_Linked_List | 240 | 82.61
1638+
| 0443 |[String Compression](src/main/kotlin/g0401_0500/s0443_string_compression/Solution.kt)| Medium | String, Two_Pointers | 198 | 92.68
1639+
| 0442 |[Find All Duplicates in an Array](src/main/kotlin/g0401_0500/s0442_find_all_duplicates_in_an_array/Solution.kt)| Medium | Array, Hash_Table, Udemy_Arrays | 480 | 73.81
1640+
| 0441 |[Arranging Coins](src/main/kotlin/g0401_0500/s0441_arranging_coins/Solution.kt)| Easy | Math, Binary_Search, Binary_Search_I_Day_6 | 150 | 84.21
16331641
| 0440 |[K-th Smallest in Lexicographical Order](src/main/kotlin/g0401_0500/s0440_k_th_smallest_in_lexicographical_order/Solution.kt)| Hard | Trie | 149 | 100.00
16341642
| 0438 |[Find All Anagrams in a String](src/main/kotlin/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.kt)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer | 561 | 54.68
16351643
| 0437 |[Path Sum III](src/main/kotlin/g0401_0500/s0437_path_sum_iii/Solution.kt)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree | 403 | 54.12
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0401_0500.s0441_arranging_coins
2+
3+
// #Easy #Math #Binary_Search #Binary_Search_I_Day_6
4+
// #2022_12_23_Time_150_ms_(84.21%)_Space_33.4_MB_(100.00%)
5+
6+
class Solution {
7+
fun arrangeCoins(n: Int): Int {
8+
var i = 1
9+
var x = n
10+
while (x > 0) {
11+
x -= ++i
12+
}
13+
return i - 1
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
441\. Arranging Coins
2+
3+
Easy
4+
5+
You have `n` coins and you want to build a staircase with these coins. The staircase consists of `k` rows where the <code>i<sup>th</sup></code> row has exactly `i` coins. The last row of the staircase **may be** incomplete.
6+
7+
Given the integer `n`, return _the number of **complete rows** of the staircase you will build_.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins1-grid.jpg)
12+
13+
**Input:** n = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** Because the 3<sup>rd</sup> row is incomplete, we return 2.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/04/09/arrangecoins2-grid.jpg)
22+
23+
**Input:** n = 8
24+
25+
**Output:** 3
26+
27+
**Explanation:** Because the 4<sup>th</sup> row is incomplete, we return 3.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= n <= 2<sup>31</sup> - 1</code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0442_find_all_duplicates_in_an_array
2+
3+
// #Medium #Array #Hash_Table #Udemy_Arrays #2022_12_23_Time_480_ms_(73.81%)_Space_45.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun findDuplicates(nums: IntArray): List<Int> {
7+
val setToCheck: MutableSet<Int> = mutableSetOf()
8+
val duplicates: MutableList<Int> = mutableListOf()
9+
10+
nums.forEach { element ->
11+
if (!setToCheck.add(element)) {
12+
duplicates.add(element)
13+
}
14+
}
15+
16+
return duplicates
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
442\. Find All Duplicates in an Array
2+
3+
Medium
4+
5+
Given an integer array `nums` of length `n` where all the integers of `nums` are in the range `[1, n]` and each integer appears **once** or **twice**, return _an array of all the integers that appears **twice**_.
6+
7+
You must write an algorithm that runs in `O(n) `time and uses only constant extra space.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [4,3,2,7,8,2,3,1]
12+
13+
**Output:** [2,3]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,1,2]
18+
19+
**Output:** [1]
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [1]
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* `n == nums.length`
30+
* <code>1 <= n <= 10<sup>5</sup></code>
31+
* `1 <= nums[i] <= n`
32+
* Each element in `nums` appears **once** or **twice**.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0401_0500.s0443_string_compression
2+
3+
// #Medium #String #Two_Pointers #2022_12_23_Time_198_ms_(92.68%)_Space_35.4_MB_(95.12%)
4+
5+
class Solution {
6+
/* This is breaking the rules, it's not in-place. */
7+
fun compress(chars: CharArray): Int {
8+
if (chars.size == 0) {
9+
return 0
10+
}
11+
val sb = StringBuilder()
12+
var count = 1
13+
var prev = chars[0]
14+
for (i in 1 until chars.size) {
15+
if (chars[i] == prev) {
16+
count++
17+
} else {
18+
if (count > 1) {
19+
sb.append(prev)
20+
sb.append(count)
21+
} else if (count == 1) {
22+
sb.append(prev)
23+
}
24+
prev = chars[i]
25+
count = 1
26+
}
27+
}
28+
sb.append(prev)
29+
if (count > 1) {
30+
sb.append(count)
31+
}
32+
var i = 0
33+
for (c in sb.toString().toCharArray()) {
34+
chars[i++] = c
35+
}
36+
return sb.length
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
443\. String Compression
2+
3+
Medium
4+
5+
Given an array of characters `chars`, compress it using the following algorithm:
6+
7+
Begin with an empty string `s`. For each group of **consecutive repeating characters** in `chars`:
8+
9+
* If the group's length is `1`, append the character to `s`.
10+
* Otherwise, append the character followed by the group's length.
11+
12+
The compressed string `s` **should not be returned separately**, but instead, be stored **in the input character array `chars`**. Note that group lengths that are `10` or longer will be split into multiple characters in `chars`.
13+
14+
After you are done **modifying the input array,** return _the new length of the array_.
15+
16+
You must write an algorithm that uses only constant extra space.
17+
18+
**Example 1:**
19+
20+
**Input:** chars = ["a","a","b","b","c","c","c"]
21+
22+
**Output:** Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
23+
24+
**Explanation:** The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
25+
26+
**Example 2:**
27+
28+
**Input:** chars = ["a"]
29+
30+
**Output:** Return 1, and the first character of the input array should be: ["a"]
31+
32+
**Explanation:** The only group is "a", which remains uncompressed since it's a single character.
33+
34+
**Example 3:**
35+
36+
**Input:** chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
37+
38+
**Output:** Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
39+
40+
**Explanation:** The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
41+
42+
**Constraints:**
43+
44+
* `1 <= chars.length <= 2000`
45+
* `chars[i]` is a lowercase English letter, uppercase English letter, digit, or symbol.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g0401_0500.s0445_add_two_numbers_ii
2+
3+
// #Medium #Math #Stack #Linked_List #Programming_Skills_II_Day_15 #Udemy_Linked_List
4+
// #2022_12_23_Time_240_ms_(82.61%)_Space_42.6_MB_(73.91%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
/*
9+
* Example:
10+
* var li = ListNode(5)
11+
* var v = li.`val`
12+
* Definition for singly-linked list.
13+
* class ListNode(var `val`: Int) {
14+
* var next: ListNode? = null
15+
* }
16+
*/
17+
@Suppress("NAME_SHADOWING")
18+
class Solution {
19+
private fun reverse(head: ListNode?): ListNode? {
20+
if (head == null || head.next == null) {
21+
return head
22+
}
23+
var prev: ListNode? = null
24+
var curr: ListNode = head
25+
var next = head.next
26+
while (next != null) {
27+
curr.next = prev
28+
prev = curr
29+
curr = next
30+
next = next.next
31+
}
32+
curr.next = prev
33+
return curr
34+
}
35+
36+
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
37+
var l1 = l1
38+
var l2 = l2
39+
l1 = reverse(l1)
40+
l2 = reverse(l2)
41+
var res: ListNode? = ListNode()
42+
val head = res
43+
var carry = 0
44+
while (l1 != null || l2 != null) {
45+
var val1: Int
46+
var val2: Int
47+
if (l1 == null) {
48+
val1 = 0
49+
} else {
50+
val1 = l1.`val`
51+
l1 = l1.next
52+
}
53+
if (l2 == null) {
54+
val2 = 0
55+
} else {
56+
val2 = l2.`val`
57+
l2 = l2.next
58+
}
59+
var data = val1 + val2 + carry
60+
if (data > 9) {
61+
carry = data / 10
62+
data = data % 10
63+
} else {
64+
carry = 0
65+
}
66+
res!!.next = ListNode(data)
67+
res = res.next
68+
}
69+
if (carry != 0) {
70+
res!!.next = ListNode(carry)
71+
}
72+
return reverse(head!!.next)
73+
}
74+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
445\. Add Two Numbers II
2+
3+
Medium
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/04/09/sumii-linked-list.jpg)
12+
13+
**Input:** l1 = [7,2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,8,0,7]
16+
17+
**Example 2:**
18+
19+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
20+
21+
**Output:** [8,0,7]
22+
23+
**Example 3:**
24+
25+
**Input:** l1 = [0], l2 = [0]
26+
27+
**Output:** [0]
28+
29+
**Constraints:**
30+
31+
* The number of nodes in each linked list is in the range `[1, 100]`.
32+
* `0 <= Node.val <= 9`
33+
* It is guaranteed that the list represents a number that does not have leading zeros.
34+
35+
**Follow up:** Could you solve it without reversing the input lists?
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0401_0500.s0441_arranging_coins
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 findKthNumber() {
10+
assertThat(Solution().arrangeCoins(5), equalTo(2))
11+
}
12+
13+
@Test
14+
fun arrangeCoins2() {
15+
assertThat(Solution().arrangeCoins(8), equalTo(3))
16+
}
17+
}

0 commit comments

Comments
 (0)