kk
This commit is contained in:
51
src/main/java/com/dota/stack/_1019/Solution.java
Normal file
51
src/main/java/com/dota/stack/_1019/Solution.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package com.dota.stack._1019;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
class ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode next;
|
||||||
|
ListNode() {}
|
||||||
|
ListNode(int val) { this.val = val; }
|
||||||
|
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||||
|
}
|
||||||
|
|
||||||
|
record N(int val, int idx){}
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var n = new ListNode(2);
|
||||||
|
n.next = new ListNode(1);
|
||||||
|
n.next.next = new ListNode(5);
|
||||||
|
new Solution().nextLargerNodes(n);
|
||||||
|
}
|
||||||
|
public int[] nextLargerNodes(ListNode head) {
|
||||||
|
var list = new ArrayList<Integer>();
|
||||||
|
while (head != null) {
|
||||||
|
list.add(head.val);
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
var queue = new LinkedList<N>();
|
||||||
|
queue.add(new N(list.getFirst(), 0));
|
||||||
|
for (int j = 1; j < list.size(); j++) {
|
||||||
|
var n = list.get(j);
|
||||||
|
while(!queue.isEmpty() && n>queue.getLast().val()) {
|
||||||
|
var t = queue.removeLast();
|
||||||
|
list.set(t.idx(), n);
|
||||||
|
}
|
||||||
|
queue.addLast(new N(n, j));
|
||||||
|
}
|
||||||
|
|
||||||
|
while(!queue.isEmpty()) {
|
||||||
|
var t = queue.removeLast();
|
||||||
|
list.set(t.idx(), 0);
|
||||||
|
}
|
||||||
|
var res = new int[list.size()];
|
||||||
|
for (int j = 0; j < list.size(); j++) {
|
||||||
|
res[j] = list.get(j);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
28
src/main/java/com/dota/stack/_2866/Solution.java
Normal file
28
src/main/java/com/dota/stack/_2866/Solution.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package com.dota.stack._2866;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public long maximumSumOfHeights(List<Integer> maxHeights) {
|
||||||
|
long res = 0;
|
||||||
|
int n = maxHeights.size();
|
||||||
|
var suf = new int[n];
|
||||||
|
suf[n-1] = maxHeights.get(n-1);
|
||||||
|
long sum = suf[n-1];
|
||||||
|
var pre = new int[n];
|
||||||
|
pre[0] = maxHeights.get(0);
|
||||||
|
var stack = new ArrayDeque<Integer>();
|
||||||
|
stack.push(n-1);
|
||||||
|
for (int i = n - 2; i >= 0; i--) {
|
||||||
|
if (maxHeights.get(i) > maxHeights.get(i-1)) {
|
||||||
|
sum += maxHeights.get(i);
|
||||||
|
suf[i] = maxHeights.get(i);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
24
src/main/java/com/dota/stack/_3113/Solution.java
Normal file
24
src/main/java/com/dota/stack/_3113/Solution.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package com.dota.stack._3113;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public long numberOfSubarrays(int[] nums) {
|
||||||
|
long res = nums.length;
|
||||||
|
var deque = new ArrayDeque<int[]>();
|
||||||
|
deque.add(new int[]{Integer.MAX_VALUE, 1});
|
||||||
|
for (int num : nums) {
|
||||||
|
while(num > deque.peek()[0]) {
|
||||||
|
deque.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num == deque.peek()[0]) {
|
||||||
|
res += deque.peek()[1];
|
||||||
|
deque.peek()[1]++;
|
||||||
|
} else {
|
||||||
|
deque.push(new int[]{num, 1});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user