Skip to content

Commit c0b0737

Browse files
author
kkarpyshev
committed
Merge remote-tracking branch 'origin/master'
2 parents c6fe792 + 8c29d9b commit c0b0737

File tree

3 files changed

+121
-7
lines changed

3 files changed

+121
-7
lines changed

src/easy/141. Linked List Cycle .kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package easy
22

3+
import HashTableTopic
4+
import LinkedListTopic
5+
import TwoPointersTopic
36
import utils.ListNode
47

58
/**
@@ -12,20 +15,21 @@ import utils.ListNode
1215
* Return true if there is a cycle in the linked list. Otherwise, return false.
1316
*/
1417

15-
class Easy141 {
18+
class Easy141 : HashTableTopic, LinkedListTopic, TwoPointersTopic {
1619

1720
fun hasCycle(head: ListNode?): Boolean {
18-
if (head == null) return false
19-
if (head.next == null || head.next!!.next == null) return false
21+
head ?: return false
22+
head.next ?: return false
23+
head.next?.next ?: return false
2024
if (head === head.next) return true
2125
var t1 = head
22-
var t2 = head.next!!.next
26+
var t2 = head.next?.next
2327
while (true) {
2428
when {
2529
t1 === t2 -> return true
26-
t2!!.next != null && t2.next!!.next != null -> {
27-
t1 = t1!!.next
28-
t2 = t2.next!!.next
30+
t2?.next != null && t2.next!!.next != null -> {
31+
t1 = t1?.next
32+
t2 = t2.next?.next
2933
}
3034
else -> return false
3135
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package hard
2+
3+
import CombinatoricsTopic
4+
import DynamicProgrammingTopic
5+
import MathTopic
6+
7+
/**
8+
* 1359. Count All Valid Pickup and Delivery Options
9+
* https://leetcode.com/problems/count-all-valid-pickup-and-delivery-options/
10+
*
11+
Given n orders, each order consist in pickup and delivery services.
12+
Count all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i).
13+
Since the answer may be too large, return it modulo 10^9 + 7.
14+
15+
BULLSHIT
16+
*/
17+
18+
class Hard1359 : MathTopic, DynamicProgrammingTopic, CombinatoricsTopic {
19+
20+
private val MOD = 1000000007L
21+
private lateinit var memo: Array<LongArray>
22+
23+
private fun totalWays(unpicked: Int, undelivered: Int): Long {
24+
if (unpicked == 0 && undelivered == 0) {
25+
// We have completed all orders.
26+
return 1
27+
}
28+
if (unpicked < 0 || undelivered < 0 || undelivered < unpicked) {
29+
// We can't pick or deliver more than N items
30+
// Number of deliveries can't exceed number of pickups
31+
// as we can only deliver after a pickup.
32+
return 0
33+
}
34+
if (memo[unpicked][undelivered] != 0L) {
35+
// Return cached value, if already present.
36+
return memo[unpicked][undelivered]
37+
}
38+
var ans: Long = 0
39+
40+
// Count all choices of picking up an order.
41+
ans += unpicked * totalWays(unpicked - 1, undelivered)
42+
// Handle integer overflow.
43+
ans %= MOD
44+
45+
// Count all choices of delivering a picked order.
46+
ans += (undelivered - unpicked) * totalWays(unpicked, undelivered - 1)
47+
// Handle integer overflow.
48+
ans %= MOD
49+
return ans.also { memo[unpicked][undelivered] = it }
50+
}
51+
52+
fun countOrders(n: Int): Int {
53+
memo = Array(n + 1) { LongArray(n + 1) }
54+
return totalWays(n, n).toInt()
55+
}
56+
}
57+
58+
fun main() {
59+
println(Hard1359().countOrders(1))
60+
println(Hard1359().countOrders(2))
61+
println(Hard1359().countOrders(3))
62+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package medium
2+
3+
import DynamicProgrammingTopic
4+
5+
/**
6+
* 799. Champagne Tower
7+
* https://leetcode.com/problems/champagne-tower/
8+
*
9+
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so on until the 100th row.
10+
Each glass holds one cup of champagne.
11+
12+
Then, some champagne is poured into the first glass at the top.
13+
When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it.
14+
When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on.
15+
(A glass at the bottom row has its excess champagne fall on the floor.)
16+
17+
For example, after one cup of champagne is poured, the top most glass is full.
18+
After two cups of champagne are poured, the two glasses on the second row are half full.
19+
After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now.
20+
After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.
21+
22+
Now after pouring some non-negative integer cups of champagne, return how full the jth glass in the ith row is (both i and j are 0-indexed.)
23+
*/
24+
25+
class Medium799 : DynamicProgrammingTopic {
26+
27+
fun champagneTower(poured: Int, query_row: Int, query_glass: Int): Double {
28+
val result = ArrayList<DoubleArray>(query_row + 1)
29+
result.add(doubleArrayOf(poured.toDouble()))
30+
for (i in 1 until query_row + 1) {
31+
val row = DoubleArray(i + 1)
32+
for (j in 0 until result[i - 1].size) {
33+
val score = maxOf(0.0, result[i - 1][j] - 1) / 2
34+
row[j] += score
35+
row[j + 1] += score
36+
}
37+
result.add(i, row)
38+
}
39+
println(result.map { it.toList() })
40+
return minOf(1.0, result[query_row][query_glass])
41+
}
42+
}
43+
44+
fun main() {
45+
println(Medium799().champagneTower(1, 1, 1))
46+
println(Medium799().champagneTower(2, 1, 1))
47+
println(Medium799().champagneTower(100000009, 33, 17))
48+
}

0 commit comments

Comments
 (0)