package y2019.Algorithm.array.medium; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array.medium * @ClassName: Trap * @Author: xiaof * @Description: TODO 42. Trapping Rain Water * Given n non-negative integers representing an elevation map where the width of each bar is 1, * compute how much water it is able to trap after raining. * * * * * * * * * * * * * * * * * * 0 1 2 3 4 5 6 7 8 9 0 1 * * * * Input: [0,1,0,2,1,0,1,3,2,1,2,1] * Output: 6 * * @Date: 2019/7/25 8:50 * @Version: 1.0 */ public class Trap { public int solution(int[] height) { //求出每一個位置左邊最大和右邊最大的差值 int leftMax = 0, rightMax = 0, left = 0, right = height.length - 1, res = 0; while(left < right) { //當左邊小於右邊的時候 if(height[left] < height[right]) { //這裏就是說明左邊的高度比右邊的小,永遠能找到一個靠右的牆容納水 //當左邊位置小於右邊位置,那麼就獲取當前位置能容納多少水 leftMax = Math.max(leftMax, height[left]); //取出當前的左邊最大值 //只要知道了左邊的最大值,那麼就計算差值 res += leftMax - height[left]; ++left; } else { //這裏就是標識右邊的高度比左邊小,那麼就能夠計算右邊容納的水 //左邊不用考慮,確定有比這個大的,右邊只須要考慮更右邊的最大值便可 rightMax = Math.max(height[right], rightMax); res += rightMax - height[right]; --right; } } return res; } public int solution1(int[] height) { //從第一個位置開始每一個階段位置寬度都是1,而後當遇到第一個不相等的高度的時候,就能夠計算單位 //兩高夾一矮就是容水量 int res = 0, mx = 0, n = height.length; int[] dp = new int[n]; //第一遍取每一步的當前最大值 for (int i = 0; i < n; ++i) { dp[i] = mx; mx = Math.max(mx, height[i]); } mx = 0; //第二遍遍歷,每次取右邊的每一步最小值, for (int i = n - 1; i >= 0; --i) { dp[i] = Math.min(dp[i], mx); // 去最大值的之間的小值 //取當前位置的右邊最大值 mx = Math.max(mx, height[i]); //相減值大於0,計算結果差值 if (dp[i] - height[i] > 0) res += dp[i] - height[i]; } return res; } }
package y2019.Algorithm.array.medium; import java.util.HashMap; import java.util.Map; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array.medium * @ClassName: LongestConsecutive * @Author: xiaof * @Description: TODO 128. Longest Consecutive Sequence * Given an unsorted array of integers, find the length of the longest consecutive elements sequence. * Your algorithm should run in O(n) complexity. * * Input: [100, 4, 200, 1, 3, 2] * Output: 4 * Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. * * 給定一個未排序的整數數組,找出最長連續序列的長度。 * 要求算法的時間複雜度爲 O(n)。 * * @Date: 2019/7/25 9:56 * @Version: 1.0 */ public class LongestConsecutive { public int solution(int[] nums) { //未排序的數組,求最長連續的序列 //用map存放進入當前值,第二個參數是序列長度,當前值的n-left就是這個序列的開始的位置的值,當前值n+right是這個序列結束的位置 int res = 0; Map<Integer, Integer> map = new HashMap<>(); for(int n : nums) { //遍歷全部數據 if(!map.containsKey(n)) { //判斷map是否包含左邊的數據,和右邊的數據 int leftSeqLen = map.containsKey(n - 1) ? map.get(n - 1) : 0; //判斷是否包含n-1,若是不包含,那就是0 int rightSeqLen = map.containsKey(n + 1) ? map.get(n + 1) : 0; int sum = leftSeqLen + rightSeqLen + 1; //吧兩邊連起來 //放進去數據 map.put(n, sum); res = Math.max(res, sum); //擴展長度爲對應的值,按理說中間的數據也應該更新數據,可是這裏重複的數據也不會繼續執行 //那麼只須要更新兩端的數據便可 map.put(n - leftSeqLen, sum); map.put(n + rightSeqLen, sum); } else { //若是已經存在這個數了 continue; } } return res; } public static void main(String[] args) { int data[] = { 100, 4, 200, 1, 3, 2}; LongestConsecutive fuc = new LongestConsecutive(); fuc.solution(data); System.out.println(); } }