Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0894ed5

Browse filesBrowse files
authored
Added tasks 875, 876, 877, 878
1 parent 30481e5 commit 0894ed5
Copy full SHA for 0894ed5

File tree

13 files changed

+351
-0
lines changed
Filter options

13 files changed

+351
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
140140

141141
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
142142
|-|-|-|-|-|-
143+
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List | 136 | 76.52
143144
| 0142 |[Linked List Cycle II](src.save/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii/Solution.kt)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39
144145

145146
#### Day 5 Greedy
@@ -467,6 +468,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
467468
| 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
468469
| 0061 |[Rotate List](src.save/main/kotlin/g0001_0100/s0061_rotate_list/Solution.kt)| Medium | Two_Pointers, Linked_List | 193 | 92.16
469470
| 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
471+
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List | 136 | 76.52
470472
| 0142 |[Linked List Cycle II](src.save/main/kotlin/g0101_0200/s0142_linked_list_cycle_ii/Solution.kt)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List | 192 | 63.39
471473
| 0141 |[Linked List Cycle](src.save/main/kotlin/g0101_0200/s0141_linked_list_cycle/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List | 223 | 91.85
472474
| 0206 |[Reverse Linked List](src.save/main/kotlin/g0201_0300/s0206_reverse_linked_list/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion | 279 | 45.78
@@ -856,6 +858,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
856858

857859
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
858860
|-|-|-|-|-|-
861+
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List | 136 | 76.52
859862
| 0019 |[Remove Nth Node From End of List](src.save/main/kotlin/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List | 180 | 91.58
860863

861864
#### Day 6 Sliding Window
@@ -1178,6 +1181,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
11781181

11791182
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11801183
|-|-|-|-|-|-
1184+
| 0875 |[Koko Eating Bananas](src/main/kotlin/g0801_0900/s0875_koko_eating_bananas/Solution.kt)| Medium | Array, Binary_Search | 267 | 93.85
11811185

11821186
#### Day 5
11831187

@@ -1476,6 +1480,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
14761480

14771481
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14781482
|-|-|-|-|-|-
1483+
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List | 136 | 76.52
14791484
| 0104 |[Maximum Depth of Binary Tree](src.save/main/kotlin/g0101_0200/s0104_maximum_depth_of_binary_tree/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 236 | 83.39
14801485
| 0404 |[Sum of Left Leaves](src.save/main/kotlin/g0401_0500/s0404_sum_of_left_leaves/Solution.kt)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 173 | 86.05
14811486

@@ -1719,6 +1724,10 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.11'
17191724
|------|----------------|-------------|-------------|----------|---------
17201725
| 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
17211726
| 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
1727+
| 0878 |[Nth Magical Number](src/main/kotlin/g0801_0900/s0878_nth_magical_number/Solution.kt)| Hard | Math, Binary_Search | 124 | 100.00
1728+
| 0877 |[Stone Game](src/main/kotlin/g0801_0900/s0877_stone_game/Solution.kt)| Medium | Array, Dynamic_Programming, Math, Game_Theory | 136 | 88.24
1729+
| 0876 |[Middle of the Linked List](src/main/kotlin/g0801_0900/s0876_middle_of_the_linked_list/Solution.kt)| Easy | Two_Pointers, Linked_List, Algorithm_I_Day_5_Two_Pointers, Programming_Skills_I_Day_10_Linked_List_and_Tree, Level_1_Day_4_Linked_List, Udemy_Linked_List | 136 | 76.52
1730+
| 0875 |[Koko Eating Bananas](src/main/kotlin/g0801_0900/s0875_koko_eating_bananas/Solution.kt)| Medium | Array, Binary_Search, Binary_Search_II_Day_4 | 267 | 93.85
17221731
| 0874 |[Walking Robot Simulation](src/main/kotlin/g0801_0900/s0874_walking_robot_simulation/Solution.kt)| Medium | Array, Simulation | 274 | 100.00
17231732
| 0873 |[Length of Longest Fibonacci Subsequence](src/main/kotlin/g0801_0900/s0873_length_of_longest_fibonacci_subsequence/Solution.kt)| Medium | Array, Hash_Table, Dynamic_Programming | 341 | 90.00
17241733
| 0872 |[Leaf-Similar Trees](src/main/kotlin/g0801_0900/s0872_leaf_similar_trees/Solution.kt)| Easy | Depth_First_Search, Tree, Binary_Tree | 140 | 100.00
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0801_0900.s0875_koko_eating_bananas
2+
3+
// #Medium #Array #Binary_Search #Binary_Search_II_Day_4
4+
// #2023_04_08_Time_267_ms_(93.85%)_Space_37.7_MB_(96.62%)
5+
6+
class Solution {
7+
fun minEatingSpeed(piles: IntArray, h: Int): Int {
8+
var maxP = piles[0]
9+
var sumP = 0L
10+
for (pile in piles) {
11+
maxP = maxP.coerceAtLeast(pile)
12+
sumP += pile.toLong()
13+
}
14+
// binary search
15+
var low = ((sumP - 1) / h + 1).toInt()
16+
var high = maxP
17+
while (low < high) {
18+
val mid = low + (high - low) / 2
19+
if (isPossible(piles, mid, h)) {
20+
high = mid
21+
} else {
22+
low = mid + 1
23+
}
24+
}
25+
return low
26+
}
27+
28+
private fun isPossible(piles: IntArray, k: Int, h: Int): Boolean {
29+
var sum = 0
30+
for (pile in piles) {
31+
sum += (pile - 1) / k + 1
32+
}
33+
return sum <= h
34+
}
35+
}
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
875\. Koko Eating Bananas
2+
3+
Medium
4+
5+
Koko loves to eat bananas. There are `n` piles of bananas, the <code>i<sup>th</sup></code> pile has `piles[i]` bananas. The guards have gone and will come back in `h` hours.
6+
7+
Koko can decide her bananas-per-hour eating speed of `k`. Each hour, she chooses some pile of bananas and eats `k` bananas from that pile. If the pile has less than `k` bananas, she eats all of them instead and will not eat any more bananas during this hour.
8+
9+
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
10+
11+
Return _the minimum integer_ `k` _such that she can eat all the bananas within_ `h` _hours_.
12+
13+
**Example 1:**
14+
15+
**Input:** piles = [3,6,7,11], h = 8
16+
17+
**Output:** 4
18+
19+
**Example 2:**
20+
21+
**Input:** piles = [30,11,23,4,20], h = 5
22+
23+
**Output:** 30
24+
25+
**Example 3:**
26+
27+
**Input:** piles = [30,11,23,4,20], h = 6
28+
29+
**Output:** 23
30+
31+
**Constraints:**
32+
33+
* <code>1 <= piles.length <= 10<sup>4</sup></code>
34+
* <code>piles.length <= h <= 10<sup>9</sup></code>
35+
* <code>1 <= piles[i] <= 10<sup>9</sup></code>
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g0801_0900.s0876_middle_of_the_linked_list
2+
3+
// #Easy #Two_Pointers #Linked_List #Algorithm_I_Day_5_Two_Pointers
4+
// #Programming_Skills_I_Day_10_Linked_List_and_Tree #Level_1_Day_4_Linked_List #Udemy_Linked_List
5+
// #2023_04_08_Time_136_ms_(76.52%)_Space_34_MB_(11.02%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Example:
11+
* var li = ListNode(5)
12+
* var v = li.`val`
13+
* Definition for singly-linked list.
14+
* class ListNode(var `val`: Int) {
15+
* var next: ListNode? = null
16+
* }
17+
*/
18+
class Solution {
19+
fun middleNode(head: ListNode?): ListNode? {
20+
var fast = head
21+
var slow = head
22+
while (fast?.next != null) {
23+
fast = fast.next!!.next
24+
slow = slow!!.next
25+
}
26+
return slow
27+
}
28+
}
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
876\. Middle of the Linked List
2+
3+
Easy
4+
5+
Given the `head` of a singly linked list, return _the middle node of the linked list_.
6+
7+
If there are two middle nodes, return **the second middle** node.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/07/23/lc-midlist1.jpg)
12+
13+
**Input:** head = [1,2,3,4,5]
14+
15+
**Output:** [3,4,5]
16+
17+
**Explanation:** The middle node of the list is node 3.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2021/07/23/lc-midlist2.jpg)
22+
23+
**Input:** head = [1,2,3,4,5,6]
24+
25+
**Output:** [4,5,6]
26+
27+
**Explanation:** Since the list has two middle nodes with values 3 and 4, we return the second one.
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the list is in the range `[1, 100]`.
32+
* `1 <= Node.val <= 100`
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g0801_0900.s0877_stone_game
2+
3+
// #Medium #Array #Dynamic_Programming #Math #Game_Theory
4+
// #2023_04_08_Time_136_ms_(88.24%)_Space_33.9_MB_(52.94%)
5+
6+
class Solution {
7+
fun stoneGame(piles: IntArray): Boolean {
8+
var low = 0
9+
var high = piles.size - 1
10+
var alice = 0
11+
var bob = 0
12+
while (low < high) {
13+
alice += piles[low].coerceAtLeast(piles[high])
14+
bob += piles[low].coerceAtMost(piles[high])
15+
low++
16+
high--
17+
}
18+
return alice > bob
19+
}
20+
}
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
877\. Stone Game
2+
3+
Medium
4+
5+
Alice and Bob play a game with piles of stones. There are an **even** number of piles arranged in a row, and each pile has a **positive** integer number of stones `piles[i]`.
6+
7+
The objective of the game is to end with the most stones. The **total** number of stones across all the piles is **odd**, so there are no ties.
8+
9+
Alice and Bob take turns, with **Alice starting first**. Each turn, a player takes the entire pile of stones either from the **beginning** or from the **end** of the row. This continues until there are no more piles left, at which point the person with the **most stones wins**.
10+
11+
Assuming Alice and Bob play optimally, return `true` _if Alice wins the game, or_ `false` _if Bob wins_.
12+
13+
**Example 1:**
14+
15+
**Input:** piles = [5,3,4,5]
16+
17+
**Output:** true
18+
19+
**Explanation:**
20+
21+
Alice starts first, and can only take the first 5 or the last 5.
22+
23+
Say she takes the first 5, so that the row becomes [3, 4, 5].
24+
25+
If Bob takes 3, then the board is [4, 5], and Alice takes 5 to win with 10 points.
26+
27+
If Bob takes the last 5, then the board is [3, 4], and Alice takes 4 to win with 9 points.
28+
29+
This demonstrated that taking the first 5 was a winning move for Alice, so we return true.
30+
31+
**Example 2:**
32+
33+
**Input:** piles = [3,7,2,3]
34+
35+
**Output:** true
36+
37+
**Constraints:**
38+
39+
* `2 <= piles.length <= 500`
40+
* `piles.length` is **even**.
41+
* `1 <= piles[i] <= 500`
42+
* `sum(piles[i])` is **odd**.
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g0801_0900.s0878_nth_magical_number
2+
3+
// #Hard #Math #Binary_Search #2023_04_08_Time_124_ms_(100.00%)_Space_32.9_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun nthMagicalNumber(n: Int, a: Int, b: Int): Int {
8+
val c = lcm(a.toLong(), b.toLong())
9+
var l: Long = 2
10+
var r = n * c
11+
var ans = r
12+
while (l <= r) {
13+
val mid = l + (r - l) / 2
14+
val cnt = mid / a + mid / b - mid / c
15+
if (cnt < n) {
16+
l = mid + 1
17+
} else {
18+
ans = mid
19+
r = mid - 1
20+
}
21+
}
22+
return (ans % MOD).toInt()
23+
}
24+
25+
private fun lcm(a: Long, b: Long): Long {
26+
return a * b / gcd(a, b)
27+
}
28+
29+
private fun gcd(a: Long, b: Long): Long {
30+
var a = a
31+
var b = b
32+
var t: Long
33+
while (b != 0L) {
34+
t = b
35+
b = a % b
36+
a = t
37+
}
38+
return a
39+
}
40+
41+
companion object {
42+
private const val MOD = 1000000007
43+
}
44+
}
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
878\. Nth Magical Number
2+
3+
Hard
4+
5+
A positive integer is _magical_ if it is divisible by either `a` or `b`.
6+
7+
Given the three integers `n`, `a`, and `b`, return the <code>n<sup>th</sup></code> magical number. Since the answer may be very large, **return it modulo** <code>10<sup>9</sup> + 7</code>.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 1, a = 2, b = 3
12+
13+
**Output:** 2
14+
15+
**Example 2:**
16+
17+
**Input:** n = 4, a = 2, b = 3
18+
19+
**Output:** 6
20+
21+
**Constraints:**
22+
23+
* <code>1 <= n <= 10<sup>9</sup></code>
24+
* <code>2 <= a, b <= 4 * 10<sup>4</sup></code>
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g0801_0900.s0875_koko_eating_bananas
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 minEatingSpeed() {
10+
assertThat(Solution().minEatingSpeed(intArrayOf(3, 6, 7, 11), 8), equalTo(4))
11+
}
12+
13+
@Test
14+
fun minEatingSpeed2() {
15+
assertThat(Solution().minEatingSpeed(intArrayOf(30, 11, 23, 4, 20), 5), equalTo(30))
16+
}
17+
18+
@Test
19+
fun minEatingSpeed3() {
20+
assertThat(Solution().minEatingSpeed(intArrayOf(30, 11, 23, 4, 20), 6), equalTo(23))
21+
}
22+
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g0801_0900.s0876_middle_of_the_linked_list
2+
3+
import com_github_leetcode.LinkedListUtils.createSinglyLinkedList
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun middleNode() {
11+
val head = createSinglyLinkedList(listOf(1, 2, 3, 4, 5))
12+
assertThat(
13+
Solution().middleNode(head).toString(),
14+
equalTo(createSinglyLinkedList(listOf(3, 4, 5)).toString())
15+
)
16+
}
17+
18+
@Test
19+
fun middleNode2() {
20+
val head = createSinglyLinkedList(listOf(1, 2, 3, 4, 5, 6))
21+
assertThat(
22+
Solution().middleNode(head).toString(),
23+
equalTo(createSinglyLinkedList(listOf(4, 5, 6)).toString())
24+
)
25+
}
26+
}
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g0801_0900.s0877_stone_game
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 stoneGame() {
10+
assertThat(Solution().stoneGame(intArrayOf(5, 3, 4, 5)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun stoneGame2() {
15+
assertThat(Solution().stoneGame(intArrayOf(3, 7, 2, 3)), equalTo(true))
16+
}
17+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.