Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
de21dcd4d2
64
src/main/java/leetcode/editor/cn/ArrayPartitionI.java
Normal file
64
src/main/java/leetcode/editor/cn/ArrayPartitionI.java
Normal file
@ -0,0 +1,64 @@
|
||||
//给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得
|
||||
//从 1 到 n 的 min(ai, bi) 总和最大。
|
||||
//
|
||||
// 返回该 最大总和 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:nums = [1,4,3,2]
|
||||
//输出:4
|
||||
//解释:所有可能的分法(忽略元素顺序)为:
|
||||
//1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
|
||||
//2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
|
||||
//3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
|
||||
//所以最大总和为 4
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:nums = [6,2,6,5,1,2]
|
||||
//输出:9
|
||||
//解释:最优的分法为 (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 +
|
||||
//6 = 9
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= n <= 10⁴
|
||||
// nums.length == 2 * n
|
||||
// -10⁴ <= nums[i] <= 10⁴
|
||||
//
|
||||
// Related Topics 贪心 数组 计数排序 排序 👍 260 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
//561:数组拆分 I
|
||||
class ArrayPartitionI {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new ArrayPartitionI().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int arrayPairSum(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
int sum = 0;
|
||||
for (int i = 0; i < nums.length; i = i + 2) {
|
||||
sum += nums[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
//给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。
|
||||
//
|
||||
// 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
|
||||
//
|
||||
// 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入: prices = [7,1,5,3,6,4]
|
||||
//输出: 7
|
||||
//解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
|
||||
// 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入: prices = [1,2,3,4,5]
|
||||
//输出: 4
|
||||
//解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
|
||||
// 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入: prices = [7,6,4,3,1]
|
||||
//输出: 0
|
||||
//解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= prices.length <= 3 * 10⁴
|
||||
// 0 <= prices[i] <= 10⁴
|
||||
//
|
||||
// Related Topics 贪心 数组 动态规划 👍 1353 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//122:买卖股票的最佳时机 II
|
||||
class BestTimeToBuyAndSellStockIi {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new BestTimeToBuyAndSellStockIi().new Solution();
|
||||
System.out.println(solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maxProfit(int[] prices) {
|
||||
int result = 0;
|
||||
for (int i = 1; i < prices.length; i++) {
|
||||
result += Math.max(0, prices[i] - prices[i - 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
67
src/main/java/leetcode/editor/cn/FizzBuzz.java
Normal file
67
src/main/java/leetcode/editor/cn/FizzBuzz.java
Normal file
@ -0,0 +1,67 @@
|
||||
//写一个程序,输出从 1 到 n 数字的字符串表示。
|
||||
//
|
||||
// 1. 如果 n 是3的倍数,输出“Fizz”;
|
||||
//
|
||||
// 2. 如果 n 是5的倍数,输出“Buzz”;
|
||||
//
|
||||
// 3.如果 n 同时是3和5的倍数,输出 “FizzBuzz”。
|
||||
//
|
||||
// 示例:
|
||||
//
|
||||
// n = 15,
|
||||
//
|
||||
//返回:
|
||||
//[
|
||||
// "1",
|
||||
// "2",
|
||||
// "Fizz",
|
||||
// "4",
|
||||
// "Buzz",
|
||||
// "Fizz",
|
||||
// "7",
|
||||
// "8",
|
||||
// "Fizz",
|
||||
// "Buzz",
|
||||
// "11",
|
||||
// "Fizz",
|
||||
// "13",
|
||||
// "14",
|
||||
// "FizzBuzz"
|
||||
//]
|
||||
//
|
||||
// Related Topics 数学 字符串 模拟 👍 103 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//412:Fizz Buzz
|
||||
class FizzBuzz {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new FizzBuzz().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public List<String> fizzBuzz(int n) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 1; i <= n; i++) {
|
||||
if (i % 15 == 0) {
|
||||
list.add("FizzBuzz");
|
||||
} else if (i % 5 == 0) {
|
||||
list.add("Buzz");
|
||||
} else if (i % 3 == 0) {
|
||||
list.add("Fizz");
|
||||
} else {
|
||||
list.add("" + i);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
72
src/main/java/leetcode/editor/cn/IsSubsequence.java
Normal file
72
src/main/java/leetcode/editor/cn/IsSubsequence.java
Normal file
@ -0,0 +1,72 @@
|
||||
//给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
|
||||
//
|
||||
// 字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而
|
||||
//"aec"不是)。
|
||||
//
|
||||
// 进阶:
|
||||
//
|
||||
// 如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代
|
||||
//码?
|
||||
//
|
||||
// 致谢:
|
||||
//
|
||||
// 特别感谢 @pbrother 添加此问题并且创建所有测试用例。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:s = "abc", t = "ahbgdc"
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:s = "axc", t = "ahbgdc"
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 0 <= s.length <= 100
|
||||
// 0 <= t.length <= 10^4
|
||||
// 两个字符串都只由小写字符组成。
|
||||
//
|
||||
// Related Topics 双指针 字符串 动态规划 👍 504 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//392:判断子序列
|
||||
class IsSubsequence {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new IsSubsequence().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isSubsequence(String s, String t) {
|
||||
if (s.length() == 0) {
|
||||
return true;
|
||||
}
|
||||
if (t.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
int index = 0;
|
||||
for (int i = 0; i < t.length() && index < s.length(); i++) {
|
||||
if (t.charAt(i) == s.charAt(index)) {
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return index == s.length();
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
79
src/main/java/leetcode/editor/cn/MaximumUnitsOnATruck.java
Normal file
79
src/main/java/leetcode/editor/cn/MaximumUnitsOnATruck.java
Normal file
@ -0,0 +1,79 @@
|
||||
//请你将一些箱子装在 一辆卡车 上。给你一个二维数组 boxTypes ,其中 boxTypes[i] = [numberOfBoxesi,
|
||||
//numberOfUnitsPerBoxi] :
|
||||
//
|
||||
//
|
||||
// numberOfBoxesi 是类型 i 的箱子的数量。
|
||||
// numberOfUnitsPerBoxi 是类型 i 每个箱子可以装载的单元数量。
|
||||
//
|
||||
//
|
||||
// 整数 truckSize 表示卡车上可以装载 箱子 的 最大数量 。只要箱子数量不超过 truckSize ,你就可以选择任意箱子装到卡车上。
|
||||
//
|
||||
// 返回卡车可以装载 单元 的 最大 总数。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
//输出:8
|
||||
//解释:箱子的情况如下:
|
||||
//- 1 个第一类的箱子,里面含 3 个单元。
|
||||
//- 2 个第二类的箱子,每个里面含 2 个单元。
|
||||
//- 3 个第三类的箱子,每个里面含 1 个单元。
|
||||
//可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。
|
||||
//单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
//输出:91
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= boxTypes.length <= 1000
|
||||
// 1 <= numberOfBoxesi, numberOfUnitsPerBoxi <= 1000
|
||||
// 1 <= truckSize <= 10⁶
|
||||
//
|
||||
// Related Topics 贪心 数组 排序 👍 21 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
//1710:卡车上的最大单元数
|
||||
class MaximumUnitsOnATruck {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MaximumUnitsOnATruck().new Solution();
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int maximumUnits(int[][] boxTypes, int truckSize) {
|
||||
Arrays.sort(boxTypes, Comparator.comparingInt(o -> o[1]));
|
||||
int sum = 0;
|
||||
int index = boxTypes.length - 1;
|
||||
while (truckSize > 0 && index >= 0) {
|
||||
if (boxTypes[index][0] < truckSize) {
|
||||
sum += boxTypes[index][0] * boxTypes[index][1];
|
||||
truckSize -= boxTypes[index][0];
|
||||
index--;
|
||||
} else {
|
||||
sum += truckSize * boxTypes[index][1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
75
src/main/java/leetcode/editor/cn/MergeIntervals.java
Normal file
75
src/main/java/leetcode/editor/cn/MergeIntervals.java
Normal file
@ -0,0 +1,75 @@
|
||||
//以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返
|
||||
//回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
|
||||
//输出:[[1,6],[8,10],[15,18]]
|
||||
//解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:intervals = [[1,4],[4,5]]
|
||||
//输出:[[1,5]]
|
||||
//解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= intervals.length <= 10⁴
|
||||
// intervals[i].length == 2
|
||||
// 0 <= starti <= endi <= 10⁴
|
||||
//
|
||||
// Related Topics 数组 排序 👍 1092 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import com.code.leet.entiy.TwoArray;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
//56:合并区间
|
||||
class MergeIntervals{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new MergeIntervals().new Solution();
|
||||
// TwoArray twoArray = new TwoArray("[[1,4],[2,3]]",true);
|
||||
TwoArray twoArray = new TwoArray("[[1,3],[2,6],[8,10],[15,18]]",true);
|
||||
solution.merge(twoArray.getArr());
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int[][] merge(int[][] intervals) {
|
||||
Arrays.sort(intervals, (o1, o2) -> o1[0]==o2[0]?o1[1]-o2[1]:o1[0]-o2[0]);
|
||||
List<int[]> list = new ArrayList<>();
|
||||
int[] bef = intervals[0];
|
||||
for (int[] arr:intervals) {
|
||||
if(arr[0]<=bef[1]){
|
||||
bef[1] = Math.max(arr[1],bef[1]);
|
||||
}else{
|
||||
list.add(bef);
|
||||
bef = arr;
|
||||
}
|
||||
}
|
||||
list.add(bef);
|
||||
int[][] result = new int[list.size()][2];
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
result[i] = list.get(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -71,81 +71,36 @@ class SearchSuggestionsSystem {
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
List<String> getL;
|
||||
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
|
||||
Arrays.sort(products);
|
||||
Map<Character, Node> map = new HashMap<>();
|
||||
for (String product : products) {
|
||||
char ch = product.charAt(0);
|
||||
Node root = map.getOrDefault(ch, new Node(ch));
|
||||
Node temp = root;
|
||||
for (int j = 1; j < product.length(); j++) {
|
||||
if (temp.child[product.charAt(j) - 'a'] == null) {
|
||||
temp.child[product.charAt(j) - 'a'] = new Node(product.charAt(j));
|
||||
}
|
||||
temp = temp.child[product.charAt(j) - 'a'];
|
||||
if (j == product.length() - 1) {
|
||||
temp.isEnd = true;
|
||||
temp.str = product;
|
||||
}
|
||||
}
|
||||
map.put(ch, root);
|
||||
}
|
||||
int index = 0;
|
||||
String str = searchWord.substring(0, index + 1);
|
||||
List<List<String>> result = new ArrayList<>();
|
||||
Node node;
|
||||
if (!map.containsKey(searchWord.charAt(0))) {
|
||||
for (int i = 0; i < searchWord.length(); i++) {
|
||||
for (int i = 0; i < products.length; i++) {
|
||||
if (products[i].startsWith(str)) {
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int j = i; j < Math.min(i + 3, products.length); j++) {
|
||||
if (products[j].startsWith(str)) {
|
||||
list.add(products[j]);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
result.add(new ArrayList<>(list));
|
||||
if (index == searchWord.length()) {
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
str = searchWord.substring(0, index + 1);
|
||||
}
|
||||
}
|
||||
if (index < searchWord.length()) {
|
||||
int size = result.size();
|
||||
for (int i = 0; i < searchWord.length() - size; i++) {
|
||||
result.add(new ArrayList<>());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
node = map.get(searchWord.charAt(0));
|
||||
getL = new ArrayList<>();
|
||||
getStr(node);
|
||||
result.add(new ArrayList<>(getL));
|
||||
for (int i = 1; i < searchWord.length(); i++) {
|
||||
char ch = searchWord.charAt(i);
|
||||
getL = new ArrayList<>();
|
||||
if (node.child[ch - 'a'] == null) {
|
||||
break;
|
||||
}
|
||||
getStr(node.child[ch - 'a']);
|
||||
node = node.child[ch - 'a'];
|
||||
result.add(new ArrayList<>(getL));
|
||||
}
|
||||
int size = result.size();
|
||||
for (int i = 0; i < searchWord.length() - size; i++) {
|
||||
result.add(new ArrayList<>());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void getStr(Node root) {
|
||||
if (root.isEnd) {
|
||||
getL.add(root.str);
|
||||
}
|
||||
for (int i = 0; i < 26; i++) {
|
||||
if (getL.size() == 3) {
|
||||
break;
|
||||
}
|
||||
if (root.child[i] != null) {
|
||||
getStr(root.child[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Node {
|
||||
char ch;
|
||||
boolean isEnd;
|
||||
Node[] child;
|
||||
String str;
|
||||
|
||||
public Node(char ch) {
|
||||
this.ch = ch;
|
||||
child = new Node[26];
|
||||
isEnd = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,35 @@
|
||||
<p>给定长度为 <code>2n</code><strong> </strong>的整数数组 <code>nums</code> ,你的任务是将这些数分成 <code>n</code><strong> </strong>对, 例如 <code>(a<sub>1</sub>, b<sub>1</sub>), (a<sub>2</sub>, b<sub>2</sub>), ..., (a<sub>n</sub>, b<sub>n</sub>)</code> ,使得从 <code>1</code> 到 <code>n</code> 的 <code>min(a<sub>i</sub>, b<sub>i</sub>)</code> 总和最大。</p>
|
||||
|
||||
<p>返回该 <strong>最大总和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,4,3,2]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>所有可能的分法(忽略元素顺序)为:
|
||||
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
|
||||
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
|
||||
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
|
||||
所以最大总和为 4</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [6,2,6,5,1,2]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>最优的分法为 (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>nums.length == 2 * n</code></li>
|
||||
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>计数排序</li><li>排序</li></div></div><br><div><li>👍 260</li><li>👎 0</li></div>
|
@ -0,0 +1,42 @@
|
||||
<p>给定一个数组 <code>prices</code> ,其中 <code>prices[i]</code> 是一支给定股票第 <code>i</code> 天的价格。</p>
|
||||
|
||||
<p>设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。</p>
|
||||
|
||||
<p><strong>注意:</strong>你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> prices = [7,1,5,3,6,4]
|
||||
<strong>输出:</strong> 7
|
||||
<strong>解释:</strong> 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
|
||||
随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> prices = [1,2,3,4,5]
|
||||
<strong>输出:</strong> 4
|
||||
<strong>解释:</strong> 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
|
||||
注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong> prices = [7,6,4,3,1]
|
||||
<strong>输出:</strong> 0
|
||||
<strong>解释:</strong> 在这种情况下, 没有交易完成, 所以最大利润为 0。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prices.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= prices[i] <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>动态规划</li></div></div><br><div><li>👍 1353</li><li>👎 0</li></div>
|
@ -0,0 +1,38 @@
|
||||
<p>给定字符串 <strong>s</strong> 和 <strong>t</strong> ,判断 <strong>s</strong> 是否为 <strong>t</strong> 的子序列。</p>
|
||||
|
||||
<p>字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,<code>"ace"</code>是<code>"abcde"</code>的一个子序列,而<code>"aec"</code>不是)。</p>
|
||||
|
||||
<p><strong>进阶:</strong></p>
|
||||
|
||||
<p>如果有大量输入的 S,称作 S1, S2, ... , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?</p>
|
||||
|
||||
<p><strong>致谢:</strong></p>
|
||||
|
||||
<p>特别感谢<strong> </strong><a href="https://leetcode.com/pbrother/">@pbrother </a>添加此问题并且创建所有测试用例。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abc", t = "ahbgdc"
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "axc", t = "ahbgdc"
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= s.length <= 100</code></li>
|
||||
<li><code>0 <= t.length <= 10^4</code></li>
|
||||
<li>两个字符串都只由小写字符组成。</li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li><li>动态规划</li></div></div><br><div><li>👍 504</li><li>👎 0</li></div>
|
@ -0,0 +1,42 @@
|
||||
<p>请你将一些箱子装在 <strong>一辆卡车</strong> 上。给你一个二维数组 <code>boxTypes</code> ,其中 <code>boxTypes[i] = [numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub>]</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numberOfBoxes<sub>i</sub></code> 是类型 <code>i</code> 的箱子的数量。</li>
|
||||
<li><code>numberOfUnitsPerBox<sub>i</sub></code><sub> </sub>是类型 <code>i</code> 每个箱子可以装载的单元数量。</li>
|
||||
</ul>
|
||||
|
||||
<p>整数 <code>truckSize</code> 表示卡车上可以装载 <strong>箱子</strong> 的 <strong>最大数量</strong> 。只要箱子数量不超过 <code>truckSize</code> ,你就可以选择任意箱子装到卡车上。</p>
|
||||
|
||||
<p>返回卡车可以装载 <strong>单元</strong> 的 <strong>最大</strong> 总数<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[1,3],[2,2],[3,1]], truckSize = 4
|
||||
<strong>输出:</strong>8
|
||||
<strong>解释:</strong>箱子的情况如下:
|
||||
- 1 个第一类的箱子,里面含 3 个单元。
|
||||
- 2 个第二类的箱子,每个里面含 2 个单元。
|
||||
- 3 个第三类的箱子,每个里面含 1 个单元。
|
||||
可以选择第一类和第二类的所有箱子,以及第三类的一个箱子。
|
||||
单元总数 = (1 * 3) + (2 * 2) + (1 * 1) = 8</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>boxTypes = [[5,10],[2,5],[4,7],[3,9]], truckSize = 10
|
||||
<strong>输出:</strong>91
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= boxTypes.length <= 1000</code></li>
|
||||
<li><code>1 <= numberOfBoxes<sub>i</sub>, numberOfUnitsPerBox<sub>i</sub> <= 1000</code></li>
|
||||
<li><code>1 <= truckSize <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>贪心</li><li>数组</li><li>排序</li></div></div><br><div><li>👍 21</li><li>👎 0</li></div>
|
@ -0,0 +1,29 @@
|
||||
<p>以数组 <code>intervals</code> 表示若干个区间的集合,其中单个区间为 <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> 。请你合并所有重叠的区间,并返回一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,3],[2,6],[8,10],[15,18]]
|
||||
<strong>输出:</strong>[[1,6],[8,10],[15,18]]
|
||||
<strong>解释:</strong>区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>intervals = [[1,4],[4,5]]
|
||||
<strong>输出:</strong>[[1,5]]
|
||||
<strong>解释:</strong>区间 [1,4] 和 [4,5] 可被视为重叠区间。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= intervals.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>intervals[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>排序</li></div></div><br><div><li>👍 1092</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user