Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
4c64b18deb
65
src/main/java/leetcode/editor/cn/MaximumProductSubarray.java
Normal file
65
src/main/java/leetcode/editor/cn/MaximumProductSubarray.java
Normal file
@ -0,0 +1,65 @@
|
||||
//<p>给你一个整数数组 <code>nums</code> ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。</p>
|
||||
//
|
||||
//<p>测试用例的答案是一个 <strong>32-位</strong> 整数。</p>
|
||||
//
|
||||
//<p><strong>子数组</strong> 是数组的连续子序列。</p>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>示例 1:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong> nums = [2,3,-2,4]
|
||||
//<strong>输出:</strong> <span><code>6</code></span>
|
||||
//<strong>解释:</strong> 子数组 [2,3] 有最大乘积 6。
|
||||
//</pre>
|
||||
//
|
||||
//<p><strong>示例 2:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong> nums = [-2,0,-1]
|
||||
//<strong>输出:</strong> 0
|
||||
//<strong>解释:</strong> 结果不能为 2, 因为 [-2,-1] 不是子数组。</pre>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
// <li><code>-10 <= nums[i] <= 10</code></li>
|
||||
// <li><code>nums</code> 的任何前缀或后缀的乘积都 <strong>保证</strong> 是一个 <strong>32-位</strong> 整数</li>
|
||||
//</ul>
|
||||
//
|
||||
//<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 1973</li><li>👎 0</li></div>
|
||||
package leetcode.editor.cn;
|
||||
|
||||
// 152:乘积最大子数组
|
||||
public class MaximumProductSubarray {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new MaximumProductSubarray().new Solution();
|
||||
// TO TEST
|
||||
System.out.println(solution.maxProduct(new int[]{2,-5,-2,-4,3}));
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxProduct(int[] nums) {
|
||||
int max = nums[0];
|
||||
int[] arrMax = new int[nums.length];
|
||||
int[] arrMin = new int[nums.length];
|
||||
arrMax[0] = nums[0];
|
||||
arrMin[0] = nums[0];
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
arrMax[i] = Math.max(nums[i],nums[i]*arrMax[i-1]);
|
||||
arrMax[i] = Math.max(arrMax[i],nums[i]*arrMin[i-1]);
|
||||
max = Math.max(max,arrMax[i]);
|
||||
arrMin[i] = Math.min(nums[i],nums[i]*arrMin[i-1]);
|
||||
arrMin[i] = Math.min(arrMin[i],nums[i]*arrMax[i-1]);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
92
src/main/java/leetcode/editor/cn/RussianDollEnvelopes.java
Normal file
92
src/main/java/leetcode/editor/cn/RussianDollEnvelopes.java
Normal file
@ -0,0 +1,92 @@
|
||||
//<p>给你一个二维整数数组 <code>envelopes</code> ,其中 <code>envelopes[i] = [w<sub>i</sub>, h<sub>i</sub>]</code> ,表示第 <code>i</code> 个信封的宽度和高度。</p>
|
||||
//
|
||||
//<p>当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。</p>
|
||||
//
|
||||
//<p>请计算 <strong>最多能有多少个</strong> 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。</p>
|
||||
//
|
||||
//<p><strong>注意</strong>:不允许旋转信封。</p>
|
||||
//
|
||||
//<p><strong>示例 1:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong>envelopes = [[5,4],[6,4],[6,7],[2,3]]
|
||||
//<strong>输出:</strong>3
|
||||
//<strong>解释:</strong>最多信封的个数为 <span><code>3, 组合为: </code></span>[2,3] => [5,4] => [6,7]。</pre>
|
||||
//
|
||||
//<p><strong>示例 2:</strong></p>
|
||||
//
|
||||
//<pre>
|
||||
//<strong>输入:</strong>envelopes = [[1,1],[1,1],[1,1]]
|
||||
//<strong>输出:</strong>1
|
||||
//</pre>
|
||||
//
|
||||
//<p> </p>
|
||||
//
|
||||
//<p><strong>提示:</strong></p>
|
||||
//
|
||||
//<ul>
|
||||
// <li><code>1 <= envelopes.length <= 10<sup>5</sup></code></li>
|
||||
// <li><code>envelopes[i].length == 2</code></li>
|
||||
// <li><code>1 <= w<sub>i</sub>, h<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
//</ul>
|
||||
//
|
||||
//<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>动态规划</li><li>排序</li></div></div><br><div><li>👍 881</li><li>👎 0</li></div>
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
// 354:俄罗斯套娃信封问题
|
||||
public class RussianDollEnvelopes {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new RussianDollEnvelopes().new Solution();
|
||||
// TO TEST
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxEnvelopes(int[][] envelopes) {
|
||||
if (envelopes.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int n = envelopes.length;
|
||||
Arrays.sort(envelopes, new Comparator<int[]>() {
|
||||
public int compare(int[] e1, int[] e2) {
|
||||
if (e1[0] != e2[0]) {
|
||||
return e1[0] - e2[0];
|
||||
} else {
|
||||
return e2[1] - e1[1];
|
||||
}
|
||||
}
|
||||
});
|
||||
List<Integer> f = new ArrayList<Integer>();
|
||||
f.add(envelopes[0][1]);
|
||||
for (int i = 1; i < n; ++i) {
|
||||
int num = envelopes[i][1];
|
||||
if (num > f.get(f.size() - 1)) {
|
||||
f.add(num);
|
||||
} else {
|
||||
int index = binarySearch(f, num);
|
||||
f.set(index, num);
|
||||
}
|
||||
}
|
||||
return f.size();
|
||||
}
|
||||
public int binarySearch(List<Integer> f, int target) {
|
||||
int low = 0, high = f.size() - 1;
|
||||
while (low < high) {
|
||||
int mid = (high - low) / 2 + low;
|
||||
if (f.get(mid) < target) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid;
|
||||
}
|
||||
}
|
||||
return low;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。</p>
|
||||
|
||||
<p>测试用例的答案是一个 <strong>32-位</strong> 整数。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组的连续子序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [2,3,-2,4]
|
||||
<strong>输出:</strong> <span><code>6</code></span>
|
||||
<strong>解释:</strong> 子数组 [2,3] 有最大乘积 6。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> nums = [-2,0,-1]
|
||||
<strong>输出:</strong> 0
|
||||
<strong>解释:</strong> 结果不能为 2, 因为 [-2,-1] 不是子数组。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li><code>nums</code> 的任何前缀或后缀的乘积都 <strong>保证</strong> 是一个 <strong>32-位</strong> 整数</li>
|
||||
</ul>
|
||||
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 1973</li><li>👎 0</li></div>
|
@ -0,0 +1,33 @@
|
||||
<p>给你一个二维整数数组 <code>envelopes</code> ,其中 <code>envelopes[i] = [w<sub>i</sub>, h<sub>i</sub>]</code> ,表示第 <code>i</code> 个信封的宽度和高度。</p>
|
||||
|
||||
<p>当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。</p>
|
||||
|
||||
<p>请计算 <strong>最多能有多少个</strong> 信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。</p>
|
||||
|
||||
<p><strong>注意</strong>:不允许旋转信封。</p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>envelopes = [[5,4],[6,4],[6,7],[2,3]]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>最多信封的个数为 <span><code>3, 组合为: </code></span>[2,3] => [5,4] => [6,7]。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>envelopes = [[1,1],[1,1],[1,1]]
|
||||
<strong>输出:</strong>1
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= envelopes.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>envelopes[i].length == 2</code></li>
|
||||
<li><code>1 <= w<sub>i</sub>, h<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>动态规划</li><li>排序</li></div></div><br><div><li>👍 881</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user