mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 21:50:49 +08:00
Merge pull request #1476 from wzqwtt/greedy06
添加(0452.用最少数量的箭引爆气球、0435.无重叠区间、0763.划分字母区间) Scala版本
This commit is contained in:
@ -352,7 +352,27 @@ function eraseOverlapIntervals(intervals: number[][]): number {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def eraseOverlapIntervals(intervals: Array[Array[Int]]): Int = {
|
||||||
|
var result = 0
|
||||||
|
var interval = intervals.sortWith((a, b) => {
|
||||||
|
a(1) < b(1)
|
||||||
|
})
|
||||||
|
var edge = Int.MinValue
|
||||||
|
for (i <- 0 until interval.length) {
|
||||||
|
if (edge <= interval(i)(0)) {
|
||||||
|
edge = interval(i)(1)
|
||||||
|
} else {
|
||||||
|
result += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -299,5 +299,30 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
def findMinArrowShots(points: Array[Array[Int]]): Int = {
|
||||||
|
if (points.length == 0) return 0
|
||||||
|
// 排序
|
||||||
|
var point = points.sortWith((a, b) => {
|
||||||
|
a(0) < b(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
var result = 1 // points不为空就至少需要一只箭
|
||||||
|
for (i <- 1 until point.length) {
|
||||||
|
if (point(i)(0) > point(i - 1)(1)) {
|
||||||
|
result += 1
|
||||||
|
} else {
|
||||||
|
point(i)(1) = math.min(point(i - 1)(1), point(i)(1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result // 返回结果
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -317,7 +317,31 @@ function partitionLabels(s: string): number[] {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Scala
|
||||||
|
|
||||||
|
```scala
|
||||||
|
object Solution {
|
||||||
|
import scala.collection.mutable
|
||||||
|
def partitionLabels(s: String): List[Int] = {
|
||||||
|
var hash = new Array[Int](26)
|
||||||
|
for (i <- s.indices) {
|
||||||
|
hash(s(i) - 'a') = i
|
||||||
|
}
|
||||||
|
|
||||||
|
var res = mutable.ListBuffer[Int]()
|
||||||
|
var (left, right) = (0, 0)
|
||||||
|
for (i <- s.indices) {
|
||||||
|
right = math.max(hash(s(i) - 'a'), right)
|
||||||
|
if (i == right) {
|
||||||
|
res.append(right - left + 1)
|
||||||
|
left = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user