diff --git a/src/main/java/com/dota/stack/_1019/Solution.java b/src/main/java/com/dota/stack/_1019/Solution.java new file mode 100644 index 0000000..ad00b75 --- /dev/null +++ b/src/main/java/com/dota/stack/_1019/Solution.java @@ -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(); + while (head != null) { + list.add(head.val); + head = head.next; + } + var queue = new LinkedList(); + 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/dota/stack/_2866/Solution.java b/src/main/java/com/dota/stack/_2866/Solution.java new file mode 100644 index 0000000..6651209 --- /dev/null +++ b/src/main/java/com/dota/stack/_2866/Solution.java @@ -0,0 +1,28 @@ +package com.dota.stack._2866; + +import java.util.ArrayDeque; +import java.util.List; + +class Solution { + public long maximumSumOfHeights(List 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(); + 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/dota/stack/_3113/Solution.java b/src/main/java/com/dota/stack/_3113/Solution.java new file mode 100644 index 0000000..8ae3c71 --- /dev/null +++ b/src/main/java/com/dota/stack/_3113/Solution.java @@ -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(); + 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; + } +} \ No newline at end of file