Skip to content

Commit e53233e

Browse files
author
kkarpyshev
committed
Merge remote-tracking branch 'origin/master'
2 parents b6597af + d0e65f1 commit e53233e

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

src/easy/942. DI String Match .kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package easy
2+
3+
import ArraysTopic
4+
import GreedyTopic
5+
import MathTopic
6+
import StringTopic
7+
import TwoPointersTopic
8+
9+
/**
10+
* 942. DI String Match
11+
* https://leetcode.com/problems/length-of-last-word/
12+
*
13+
* Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string.
14+
* A word is a maximal substring consisting of non-space characters only.
15+
*/
16+
17+
class Easy942 : ArraysTopic, TwoPointersTopic, StringTopic, GreedyTopic, MathTopic {
18+
19+
fun diStringMatch(s: String): IntArray {
20+
val result = IntArray(s.length + 1)
21+
var top = 0
22+
var bottom = 0
23+
for (i in result.lastIndex - 1 downTo 0) {
24+
if (s[i] == 'I') {
25+
bottom--
26+
result[i] = bottom
27+
} else {
28+
top++
29+
result[i] = top
30+
}
31+
}
32+
return result.also { for (i in it.indices) it[i] -= bottom }
33+
}
34+
}
35+
36+
fun main() {
37+
println(Easy942().diStringMatch("IDID").toList())
38+
println(Easy942().diStringMatch("III").toList())
39+
println(Easy942().diStringMatch("DDI").toList())
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package medium
2+
3+
import ArraysTopic
4+
import GreedyTopic
5+
import SortingTopic
6+
import TwoPointersTopic
7+
8+
/**
9+
* 881. Boats to Save People
10+
* https://leetcode.com/problems/boats-to-save-people/
11+
*
12+
You are given an array people where people[i] is the weight of the ith person,
13+
and an infinite number of boats where each boat can carry a maximum weight of limit.
14+
Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.
15+
Return the minimum number of boats to carry every given person.
16+
*/
17+
18+
class Medium881 : ArraysTopic, TwoPointersTopic, GreedyTopic, SortingTopic {
19+
20+
fun numRescueBoats(people: IntArray, limit: Int): Int {
21+
people.sort()
22+
var result = 0
23+
for (i in people.indices) {
24+
if (people[i] == 0) continue
25+
for (j in people.lastIndex downTo i + 1) {
26+
if (people[j] == 0) continue
27+
if (people[j] + people[i] > limit) continue
28+
people[j] = 0
29+
break
30+
}
31+
people[i] = 0
32+
result++
33+
}
34+
return result
35+
}
36+
}
37+
38+
fun main() {
39+
println(Medium881().numRescueBoats(intArrayOf(1, 2), 3))
40+
println(Medium881().numRescueBoats(intArrayOf(3, 2, 2, 1), 3))
41+
println(Medium881().numRescueBoats(intArrayOf(3, 5, 3, 4), 5))
42+
println(Medium881().numRescueBoats(intArrayOf(5, 1, 4, 2), 6))
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package medium
2+
3+
import GreedyTopic
4+
import MathTopic
5+
6+
7+
/**
8+
* 991. Broken Calculator
9+
* https://leetcode.com/problems/broken-calculator/
10+
*
11+
There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:
12+
multiply the number on display by 2, or
13+
subtract 1 from the number on display.
14+
Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator.
15+
BULLSHIT
16+
*/
17+
18+
class Medium991 : MathTopic, GreedyTopic {
19+
20+
fun brokenCalc(startValue: Int, target: Int): Int {
21+
var value = target
22+
var ans = 0
23+
while (value > startValue) {
24+
ans++
25+
value = if (value % 2 == 1) value + 1 else value / 2
26+
}
27+
return ans + startValue - value
28+
}
29+
}
30+
31+
fun main() {
32+
println(Medium991().brokenCalc(2, 3))
33+
println(Medium991().brokenCalc(5, 8))
34+
println(Medium991().brokenCalc(3, 10))
35+
}

0 commit comments

Comments
 (0)