Skip to content

Commit deb6ac2

Browse files
committed
2 parents 1ad29e0 + 638415b commit deb6ac2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
fun canSeePersonsCount(heights: IntArray): IntArray {
3+
val size = heights.size
4+
val res = IntArray(size)
5+
val st = LinkedList<Int>()
6+
7+
for (i in size - 1 downTo 0) {
8+
val curPerson = heights[i]
9+
10+
while (st.isNotEmpty()) {
11+
val nextPerson = st.peek()
12+
13+
res[i]++
14+
if (nextPerson > curPerson) break
15+
else st.pop()
16+
}
17+
st.push(curPerson)
18+
}
19+
return res
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
fun lengthOfLIS(nums: IntArray): Int {
3+
val sub = mutableListOf<Int>()
4+
nums.forEach { num ->
5+
if (sub.isEmpty() || sub.last() < num) {
6+
sub.add(num)
7+
} else {
8+
val bin = sub.binarySearch(num)
9+
val i = if (bin >= 0) bin else -bin - 1
10+
sub[i] = num
11+
}
12+
}
13+
return sub.size
14+
} //TC: O(nlogn) SC: O(n)
15+
}

0 commit comments

Comments
 (0)