Skip to content

Commit 3398063

Browse files
committed
190423
1 parent 7fb7c92 commit 3398063

File tree

4 files changed

+273
-1
lines changed

4 files changed

+273
-1
lines changed

src/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import leetcode.editor.en.Q1014.BestSightseeingPair;
2+
import leetcode.editor.en.Q122.BestTimeToBuyAndSellStockIi;
13
import leetcode.editor.en.Q152.MaximumProductSubarray;
24
import leetcode.editor.en.Q1548.TheMostSimilarPathInAGraph;
35
import leetcode.editor.en.Q1567.MaximumLengthOfSubarrayWithPositiveProduct;
@@ -23,7 +25,7 @@
2325

2426
public class Main {
2527
public static void main(String[] args) throws IOException {
26-
System.out.println(new MaximumLengthOfSubarrayWithPositiveProduct().getMaxLen(toIntArray("[1,2,3,5,-6,4,0,10]\n")));
28+
System.out.println(new BestTimeToBuyAndSellStockIi().maxProfit(toIntArray("[7,1,5,3,6,4]")));
2729

2830
}
2931

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package leetcode.editor.en.Q1014;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
import leetcode.editor.en.Q169.MajorityElement;
7+
8+
//You are given an integer array values where values[i] represents the value of
9+
//the iᵗʰ sightseeing spot. Two sightseeing spots i and j have a distance j - i
10+
//between them.
11+
//
12+
// The score of a pair (i < j) of sightseeing spots is values[i] + values[j] +
13+
//i - j: the sum of the values of the sightseeing spots, minus the distance
14+
//between them.
15+
//
16+
// Return the maximum score of a pair of sightseeing spots.
17+
//
18+
//
19+
// Example 1:
20+
//
21+
//
22+
//Input: values = [8,1,5,2,6]
23+
//Output: 11
24+
//Explanation: i = 0, j = 2, values[i] + values[j] + i - j = 8 + 5 + 0 - 2 = 11
25+
//
26+
//
27+
// Example 2:
28+
//
29+
//
30+
//Input: values = [1,2]
31+
//Output: 2
32+
//
33+
//
34+
//
35+
// Constraints:
36+
//
37+
//
38+
// 2 <= values.length <= 5 * 10⁴
39+
// 1 <= values[i] <= 1000
40+
//
41+
//
42+
// 👍 2312 👎 52
43+
44+
45+
//leetcode submit region begin(Prohibit modification and deletion)
46+
class Solution {
47+
public int maxScoreSightseeingPair(int[] values) {
48+
LinkedList<int[]> monoStack = new LinkedList<>();
49+
monoStack.add(new int[]{values[0], 0});
50+
int max = Integer.MIN_VALUE;
51+
for (int i = 1; i < values.length; i++) {
52+
int num = values[i];
53+
int[] prev = monoStack.peekLast();
54+
int bestPairHere = (num + prev[0] + (prev[1] - i));
55+
max = Math.max(max, bestPairHere);
56+
while (!monoStack.isEmpty() && (monoStack.peekLast()[0] - (i - prev[1])) <= num) {
57+
monoStack.pollLast();
58+
}
59+
if (monoStack.isEmpty()) {
60+
monoStack.addFirst(new int[]{num, i});
61+
}
62+
}
63+
return max;
64+
65+
}
66+
}
67+
//leetcode submit region end(Prohibit modification and deletion)
68+
69+
70+
public class BestSightseeingPair extends Solution {
71+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package leetcode.editor.en.Q122;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given an integer array prices where prices[i] is the price of a given
8+
//stock on the iᵗʰ day.
9+
//
10+
// On each day, you may decide to buy and/or sell the stock. You can only hold
11+
//at most one share of the stock at any time. However, you can buy it then
12+
//immediately sell it on the same day.
13+
//
14+
// Find and return the maximum profit you can achieve.
15+
//
16+
//
17+
// Example 1:
18+
//
19+
//
20+
//Input: prices = [7,1,5,3,6,4]
21+
//Output: 7
22+
//Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit =
23+
//5-1 = 4.
24+
//Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
25+
//
26+
//Total profit is 4 + 3 = 7.
27+
//
28+
//
29+
// Example 2:
30+
//
31+
//
32+
//Input: prices = [1,2,3,4,5]
33+
//Output: 4
34+
//Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit =
35+
//5-1 = 4.
36+
//Total profit is 4.
37+
//
38+
//
39+
// Example 3:
40+
//
41+
//
42+
//Input: prices = [7,6,4,3,1]
43+
//Output: 0
44+
//Explanation: There is no way to make a positive profit, so we never buy the
45+
//stock to achieve the maximum profit of 0.
46+
//
47+
//
48+
//
49+
// Constraints:
50+
//
51+
//
52+
// 1 <= prices.length <= 3 * 10⁴
53+
// 0 <= prices[i] <= 10⁴
54+
//
55+
//
56+
// 👍 10849 👎 2523
57+
58+
59+
//leetcode submit region begin(Prohibit modification and deletion)
60+
class Solution {
61+
62+
public int maxProfit(int[] prices) {
63+
int total_profit = 0;
64+
for (int i = 0; i < prices.length - 1; i++) {
65+
int pontential_profit = prices[i + 1] - prices[i];
66+
total_profit += Math.max(pontential_profit, 0);
67+
}
68+
return total_profit;
69+
}
70+
71+
72+
}
73+
//leetcode submit region end(Prohibit modification and deletion)
74+
75+
76+
public class BestTimeToBuyAndSellStockIi extends Solution {
77+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package leetcode.editor.en.Q1372;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//You are given the root of a binary tree.
8+
//
9+
// A ZigZag path for a binary tree is defined as follow:
10+
//
11+
//
12+
// Choose any node in the binary tree and a direction (right or left).
13+
// If the current direction is right, move to the right child of the current
14+
//node; otherwise, move to the left child.
15+
// Change the direction from right to left or from left to right.
16+
// Repeat the second and third steps until you can't move in the tree.
17+
//
18+
//
19+
// Zigzag length is defined as the number of nodes visited - 1. (A single node
20+
//has a length of 0).
21+
//
22+
// Return the longest ZigZag path contained in that tree.
23+
//
24+
//
25+
// Example 1:
26+
//
27+
//
28+
//Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
29+
//Output: 3
30+
//Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
31+
//
32+
//
33+
// Example 2:
34+
//
35+
//
36+
//Input: root = [1,1,1,null,1,null,null,1,1,null,1]
37+
//Output: 4
38+
//Explanation: Longest ZigZag path in blue nodes (left -> right -> left ->
39+
//right).
40+
//
41+
//
42+
// Example 3:
43+
//
44+
//
45+
//Input: root = [1]
46+
//Output: 0
47+
//
48+
//
49+
//
50+
// Constraints:
51+
//
52+
//
53+
// The number of nodes in the tree is in the range [1, 5 * 10⁴].
54+
// 1 <= Node.val <= 100
55+
//
56+
//
57+
// 👍 1960 👎 33
58+
59+
class TreeNode {
60+
int val;
61+
TreeNode left;
62+
TreeNode right;
63+
64+
TreeNode() {
65+
}
66+
67+
TreeNode(int val) {
68+
this.val = val;
69+
}
70+
71+
TreeNode(int val, TreeNode left, TreeNode right) {
72+
this.val = val;
73+
this.left = left;
74+
this.right = right;
75+
}
76+
}
77+
//leetcode submit region begin(Prohibit modification and deletion)
78+
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode() {}
86+
* TreeNode(int val) { this.val = val; }
87+
* TreeNode(int val, TreeNode left, TreeNode right) {
88+
* this.val = val;
89+
* this.left = left;
90+
* this.right = right;
91+
* }
92+
* }
93+
*/
94+
class Solution {
95+
int max = 0;
96+
97+
public int longestZigZag(TreeNode root) {
98+
longestZigZagHelper(root);
99+
return Math.max(max - 1, 0);
100+
}
101+
102+
private int[] longestZigZagHelper(TreeNode root) {
103+
if (root == null) {
104+
return new int[]{0, 0};
105+
}
106+
int[] rightPath = longestZigZagHelper(root.right);
107+
int[] leftPath = longestZigZagHelper(root.left);
108+
int rightSum = 1 + rightPath[0];
109+
int leftSum = 1 + leftPath[1];
110+
111+
this.max = Math.max(max, rightSum);
112+
this.max = Math.max(max, leftSum);
113+
114+
return new int[]{leftSum, rightSum};
115+
116+
}
117+
}
118+
//leetcode submit region end(Prohibit modification and deletion)
119+
120+
121+
public class LongestZigzagPathInABinaryTree extends Solution {
122+
}

0 commit comments

Comments
 (0)