This commit is contained in:
kkunkka
2025-06-19 17:25:58 +08:00
parent 2e7ef68025
commit 04a11d2037
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package com.dota.stack._853;
import java.util.ArrayList;
class Car {
int position;
int speed;
double res;
public Car(int position, int speed, double res) {
this.position = position;
this.speed = speed;
this.res = res;
}
}
class Solution {
public int carFleet(int target, int[] position, int[] speed) {
var list = new ArrayList<Car>();
for (int i = 0; i < position.length; i++) {
list.add(new Car(position[i], speed[i], (double) (target - position[i]) / speed[i]));
}
var res = 1;
list.sort((a, b) -> b.position - a.position);
for (int i = 1; i < list.size(); i++) {
if (list.get(i - 1).res < list.get(i).res) {
res++;
} else {
list.get(i).res = list.get(i - 1).res;
}
}
return res;
}
}

View File

@@ -0,0 +1,24 @@
package com.dota.stack._901;
import java.util.Deque;
import java.util.LinkedList;
record Node(int k, int v) {
}
class StockSpanner {
Deque<Node> stack ;
public StockSpanner() {
stack = new LinkedList<>();
}
public int next(int price) {
int sum = 1;
while(!stack.isEmpty() && stack.peekLast().k() <= price) {
sum += stack.removeLast().v();
}
stack.addLast(new Node(price, sum));
return sum;
}
}