package top100.贪心算法;import java.util.ArrayList;
import java.util.List;public class TOP {public int maxProfit(int[] prices) {int res = 0, min = prices[0];for (int i = 1; i < prices.length; i++) {if (prices[i] < min) {min = prices[i];}res = Math.max(res, prices[i] - min);}return res;}public boolean canJump(int[] nums) {if (nums == null || nums.length == 0) {return true;}int max = 0; for (int i = 0; i < nums.length ; i++) {if (max >= i) { max = Math.max(max, nums[i] + i);}}return max >= nums.length - 1; }public int jump(int[] nums) {int res = 0;int n = nums.length;for (int i = 0; i < n - 1;) {int maxIndex = nums[i] + i; if (maxIndex >= n - 1) {res++;break;}int max = nums[i + 1] + i + 1;int index = i + 1; for (int j = i + 2; j <= maxIndex; j++) {if (nums[j] + j >= max) { max = nums[j] + j;index = j; }}i = index; res++;}return res;}public List<Integer> partitionLabels(String s) {List<Integer> res = new ArrayList<>();int[] map = new int[26];char[] chars = s.toCharArray();for (int i = 0; i < chars.length; i++) {map[chars[i] - 'a'] = i;}for (int i = 0; i < chars.length;) {int maxIndex = map[chars[i] - 'a'];for (int j = i + 1; j < maxIndex; j++) {int curMax = map[chars[j] - 'a'];maxIndex = Math.max(maxIndex, curMax);}res.add(maxIndex - i + 1);i = maxIndex + 1; }return res;}
}