Skip to content

Commit 7dba468

Browse files
authored
Added tasks 410, 412, 413, 414.
1 parent edf006f commit 7dba468

File tree

13 files changed

+344
-0
lines changed

13 files changed

+344
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
112112

113113
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
114114
|-|-|-|-|-|-
115+
| 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices/Solution.kt)| Medium | Array, Dynamic_Programming | 156 | 100.00
115116
| 0091 |[Decode Ways](src.save/main/kotlin/g0001_0100/s0091_decode_ways/Solution.kt)| Medium | Top_Interview_Questions, String, Dynamic_Programming | 237 | 76.88
116117

117118
#### Day 11
@@ -757,6 +758,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
757758

758759
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
759760
|-|-|-|-|-|-
761+
| 0412 |[Fizz Buzz](src/main/kotlin/g0401_0500/s0412_fizz_buzz/Solution.kt)| Easy | Top_Interview_Questions, String, Math, Simulation | 307 | 71.81
760762
| 0136 |[Single Number](src.save/main/kotlin/g0101_0200/s0136_single_number/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation | 344 | 83.63
761763
| 0007 |[Reverse Integer](src.save/main/kotlin/g0001_0100/s0007_reverse_integer/Solution.kt)| Medium | Top_Interview_Questions, Math | 245 | 60.32
762764
| 0009 |[Palindrome Number](src.save/main/kotlin/g0001_0100/s0009_palindrome_number/Solution.kt)| Easy | Math | 238 | 96.24
@@ -1374,6 +1376,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
13741376
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13751377
|-|-|-|-|-|-
13761378
| 0005 |[Longest Palindromic Substring](src.save/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming | 323 | 75.48
1379+
| 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices/Solution.kt)| Medium | Array, Dynamic_Programming | 156 | 100.00
13771380

13781381
#### Day 15 Dynamic Programming
13791382

@@ -1621,6 +1624,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.7'
16211624
| 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
16221625
| 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
16231626
| 0416 |[Partition Equal Subset Sum](src/main/kotlin/g0401_0500/s0416_partition_equal_subset_sum/Solution.kt)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming | 509 | 57.56
1627+
| 0414 |[Third Maximum Number](src/main/kotlin/g0401_0500/s0414_third_maximum_number/Solution.kt)| Easy | Array, Sorting | 317 | 73.00
1628+
| 0413 |[Arithmetic Slices](src/main/kotlin/g0401_0500/s0413_arithmetic_slices/Solution.kt)| Medium | Array, Dynamic_Programming, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_10 | 156 | 100.00
1629+
| 0412 |[Fizz Buzz](src/main/kotlin/g0401_0500/s0412_fizz_buzz/Solution.kt)| Easy | Top_Interview_Questions, String, Math, Simulation, Udemy_Integers | 307 | 71.81
1630+
| 0410 |[Split Array Largest Sum](src/main/kotlin/g0401_0500/s0410_split_array_largest_sum/Solution.kt)| Hard | Array, Dynamic_Programming, Greedy, Binary_Search | 165 | 100.00
16241631
| 0409 |[Longest Palindrome](src/main/kotlin/g0401_0500/s0409_longest_palindrome/Solution.kt)| Easy | String, Hash_Table, Greedy, Data_Structure_II_Day_6_String, Level_1_Day_5_Greedy | 259 | 60.71
16251632
| 0407 |[Trapping Rain Water II](src/main/kotlin/g0401_0500/s0407_trapping_rain_water_ii/Solution.kt)| Hard | Array, Breadth_First_Search, Matrix, Heap_Priority_Queue | 500 | 100.00
16261633
| 0406 |[Queue Reconstruction by Height](src/main/kotlin/g0401_0500/s0406_queue_reconstruction_by_height/Solution.kt)| Medium | Array, Sorting, Greedy, Segment_Tree, Binary_Indexed_Tree | 306 | 100.00
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0401_0500.s0410_split_array_largest_sum
2+
3+
// #Hard #Array #Dynamic_Programming #Greedy #Binary_Search
4+
// #2022_12_03_Time_165_ms_(100.00%)_Space_33.9_MB_(100.00%)
5+
6+
class Solution {
7+
fun splitArray(nums: IntArray, m: Int): Int {
8+
var maxVal = 0
9+
var minVal = nums[0]
10+
for (num in nums) {
11+
maxVal += num
12+
minVal = Math.max(minVal, num)
13+
}
14+
while (minVal < maxVal) {
15+
val midVal = minVal + (maxVal - minVal) / 2
16+
// if we can split, try to reduce the midVal so decrease maxVal
17+
if (canSplit(midVal, nums, m)) {
18+
maxVal = midVal
19+
} else {
20+
// if we can't split, then try to increase midVal by increasing minVal
21+
minVal = midVal + 1
22+
}
23+
}
24+
return minVal
25+
}
26+
27+
private fun canSplit(maxSubArrSum: Int, nums: IntArray, m: Int): Boolean {
28+
var currSum = 0
29+
var currSplits = 1
30+
for (num in nums) {
31+
currSum += num
32+
if (currSum > maxSubArrSum) {
33+
currSum = num
34+
currSplits++
35+
// if maxSubArrSum was TOO high that we can split the array into more that 'm' parts
36+
// then its not ideal
37+
if (currSplits > m) {
38+
return false
39+
}
40+
}
41+
}
42+
return true
43+
}
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
410\. Split Array Largest Sum
2+
3+
Hard
4+
5+
Given an integer array `nums` and an integer `k`, split `nums` into `k` non-empty subarrays such that the largest sum of any subarray is **minimized**.
6+
7+
Return _the minimized largest sum of the split_.
8+
9+
A **subarray** is a contiguous part of the array.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [7,2,5,10,8], k = 2
14+
15+
**Output:** 18
16+
17+
**Explanation:** There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,2,3,4,5], k = 2
22+
23+
**Output:** 9
24+
25+
**Explanation:** There are four ways to split nums into two subarrays. The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
26+
27+
**Constraints:**
28+
29+
* `1 <= nums.length <= 1000`
30+
* <code>0 <= nums[i] <= 10<sup>6</sup></code>
31+
* `1 <= k <= min(50, nums.length)`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0401_0500.s0412_fizz_buzz
2+
3+
// #Easy #Top_Interview_Questions #String #Math #Simulation #Udemy_Integers
4+
// #2022_12_03_Time_307_ms_(71.81%)_Space_41.9_MB_(71.97%)
5+
6+
class Solution {
7+
fun fizzBuzz(n: Int): Array<String> = Array<String>(n) { index ->
8+
val value = index + 1
9+
when {
10+
(value % 3 == 0 && value % 5 == 0) -> "FizzBuzz"
11+
(value % 3 == 0) -> "Fizz"
12+
(value % 5 == 0) -> "Buzz"
13+
else -> "$value"
14+
}
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
412\. Fizz Buzz
2+
3+
Easy
4+
5+
Given an integer `n`, return _a string array_ `answer` _(**1-indexed**) where_:
6+
7+
* `answer[i] == "FizzBuzz"` if `i` is divisible by `3` and `5`.
8+
* `answer[i] == "Fizz"` if `i` is divisible by `3`.
9+
* `answer[i] == "Buzz"` if `i` is divisible by `5`.
10+
* `answer[i] == i` (as a string) if none of the above conditions are true.
11+
12+
**Example 1:**
13+
14+
**Input:** n = 3
15+
16+
**Output:** ["1","2","Fizz"]
17+
18+
**Example 2:**
19+
20+
**Input:** n = 5
21+
22+
**Output:** ["1","2","Fizz","4","Buzz"]
23+
24+
**Example 3:**
25+
26+
**Input:** n = 15
27+
28+
**Output:** ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
29+
30+
**Constraints:**
31+
32+
* <code>1 <= n <= 10<sup>4</sup></code>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0401_0500.s0413_arithmetic_slices
2+
3+
// #Medium #Array #Dynamic_Programming #Algorithm_II_Day_14_Dynamic_Programming
4+
// #Dynamic_Programming_I_Day_10 #2022_12_03_Time_156_ms_(100.00%)_Space_36_MB_(41.03%)
5+
6+
class Solution {
7+
fun numberOfArithmeticSlices(nums: IntArray): Int {
8+
var sum = 0
9+
var curr = 0
10+
for (i in 2 until nums.size) {
11+
if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
12+
curr++
13+
sum += curr
14+
} else {
15+
curr = 0
16+
}
17+
}
18+
return sum
19+
}
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
413\. Arithmetic Slices
2+
3+
Medium
4+
5+
An integer array is called arithmetic if it consists of **at least three elements** and if the difference between any two consecutive elements is the same.
6+
7+
* For example, `[1,3,5,7,9]`, `[7,7,7,7]`, and `[3,-1,-5,-9]` are arithmetic sequences.
8+
9+
Given an integer array `nums`, return _the number of arithmetic **subarrays** of_ `nums`.
10+
11+
A **subarray** is a contiguous subsequence of the array.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,4]
16+
17+
**Output:** 3
18+
19+
**Explanation:** We have 3 arithmetic slices in nums: [1, 2, 3], [2, 3, 4] and [1,2,3,4] itself.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1]
24+
25+
**Output:** 0
26+
27+
**Constraints:**
28+
29+
* `1 <= nums.length <= 5000`
30+
* `-1000 <= nums[i] <= 1000`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g0401_0500.s0414_third_maximum_number
2+
3+
// #Easy #Array #Sorting #2022_12_06_Time_317_ms_(73.00%)_Space_36.9_MB_(94.00%)
4+
5+
class Solution {
6+
fun thirdMax(nums: IntArray): Int {
7+
var max1 = Long.MIN_VALUE
8+
var max2 = Long.MIN_VALUE
9+
var max3 = Long.MIN_VALUE
10+
for (i in nums) {
11+
max1 = Math.max(max1, i.toLong())
12+
}
13+
for (i in nums) {
14+
if (i.toLong() == max1) {
15+
continue
16+
}
17+
max2 = Math.max(max2, i.toLong())
18+
}
19+
for (i in nums) {
20+
if (i.toLong() == max1 || i.toLong() == max2) {
21+
continue
22+
}
23+
max3 = Math.max(max3, i.toLong())
24+
}
25+
return (if (max3 == Long.MIN_VALUE) max1 else max3).toInt()
26+
}
27+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
414\. Third Maximum Number
2+
3+
Easy
4+
5+
Given an integer array `nums`, return _the **third distinct maximum** number in this array. If the third maximum does not exist, return the **maximum** number_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [3,2,1]
10+
11+
**Output:** 1
12+
13+
**Explanation:** The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1.
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [1,2]
18+
19+
**Output:** 2
20+
21+
**Explanation:** The first distinct maximum is 2. The second distinct maximum is 1. The third distinct maximum does not exist, so the maximum (2) is returned instead.
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [2,2,3,1]
26+
27+
**Output:** 1
28+
29+
**Explanation:** The first distinct maximum is 3. The second distinct maximum is 2 (both 2's are counted together since they have the same value). The third distinct maximum is 1.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
35+
36+
**Follow up:** Can you find an `O(n)` solution?
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0401_0500.s0410_split_array_largest_sum
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 splitArray() {
10+
assertThat(Solution().splitArray(intArrayOf(7, 2, 5, 10, 8), 2), equalTo(18))
11+
}
12+
13+
@Test
14+
fun splitArray2() {
15+
assertThat(Solution().splitArray(intArrayOf(1, 2, 3, 4, 5), 2), equalTo(9))
16+
}
17+
18+
@Test
19+
fun splitArray3() {
20+
assertThat(Solution().splitArray(intArrayOf(1, 4, 4), 3), equalTo(4))
21+
}
22+
}

0 commit comments

Comments
 (0)