leetcode 122,392,455,605,860,874,1005

122

   其实更简单的做法是只要是前一个数字比后一个大就相加

    public static int maxProfit(int[] prices) {
        int min = prices[0];
        int max = prices[0];
        int total = 0;
        for (int i = 1; i < prices.length; i++) {
            int current = prices[i];
            if (current > max){
                max = current;
                if (i == prices.length-1){
                    total += max-min;
                }
            }else {
                total+= max - min;
                min = current;
                max = current;
            }
        }

        return total;
    }

392

很简单的一道题  虽然效率很低  但是难得用dp做了出来。。。

class Solution {
    public boolean isSubsequence(String s, String t) {
    int[][] dp = new int[s.length()+1][t.length()+1];
        Arrays.fill(dp[0],0);
        for (int[] ints : dp) {
            ints[0] = 0;
        }
        for (int i = 1; i < s.length()+1; i++) {
            for (int j = 1; j < t.length()+1; j++) {
                if (s.charAt(i-1) == t.charAt(j-1) ){
                    dp[i][j] = dp[i-1][j-1]+1;
                }else {
                    dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        return dp[s.length()][t.length()] >= s.length();
    }
}

455  做点儿简单的感觉自己又行了一样。。。

    public static int findContentChildren(int[] g, int[] s) {
        Arrays.sort(g);
        Arrays.sort(s);
        int k1 = 0,k2 = 0;
        int per = 0;
        while (k1 != g.length && k2 != s.length){
            if (g[k1]<=s[k2]){
                per++;
                k1++;
            }
            k2++;
        }
        return per;
    }

605 种花问题

int len = flowerbed.length;
        for (int i = 0; i < len; i++) {
            int k = flowerbed[i];
            if ( k == 0){
                //左右均越界   左右均为0   左越界 右为0  右越界左为0
                if ((i == 0 && ( i == len-1 || flowerbed[i+1] == 0 ) )
                    || (i == len-1 && ( i == 0 || flowerbed[i-1] == 0 ) )
                    || (i != 0 &&  i != len-1 && flowerbed[i-1] == 0 && flowerbed[i+1] == 0) ){
                    flowerbed[i] = 1;
                    n--;
                    if (n == 0){
                        return true;
                    }
                }

            }
        }

        return n <= 0;

860  找0问题

public static boolean lemonadeChange(int[] bills) {
        int k1 = 0,k2 = 0;
        for (int bill : bills) {
            if (bill == 5){
                k1++;
            }else if (bill == 10){
                k2++;
                if (k1 < 1){
                    return false;
                }
                k1--;
            }else {
                if (k1 < 1){
                    return false;
                }
                if (k2 >=1 ){
                    k2--;
                    k1--;
                }else if (k1 >= 3){
                    k1-=3;
                }else {
                    return false;
                }

            }
        }


        return true;

    }

874  这题写起来真是个力气活

public static int robotSim(int[] commands, int[][] obstacles) {
        Map<Integer, HashSet<Integer>> xMap = new HashMap<>();
        Map<Integer, HashSet<Integer>> yMap = new HashMap<>();
        for (int[] obstacle : obstacles) {
            if (obstacle.length == 0)continue;
            HashSet<Integer> xTree = xMap.getOrDefault(obstacle[0], new HashSet<>());
            xTree.add(obstacle[1]);
            xMap.put(obstacle[0],xTree);

            HashSet<Integer> yTree = yMap.getOrDefault(obstacle[1], new HashSet<>());
            yTree.add(obstacle[0]);
            yMap.put(obstacle[1],yTree);
        }

        int forward = 1;// 1上 2 右  3 下 4左
        int[] op = new int[]{0,1,1,-1,-1};
        int x = 0,y = 0,xMaxVal = 0,yMaxVal = 0,max = 0;
        for (int i = 0; i < commands.length; i++) {
            int c = commands[i];
            if (c <0){
                if (c == -2){
                    if (forward == 1){
                        forward = 4;
                    }else {
                        forward --;
                    }
                }else {
                    if (forward == 4){
                        forward = 1;
                    }else {
                        forward++;
                    }
                }
            }else {
                boolean flag =  forward == 1|| forward == 3;

                int code = op[forward];
                HashSet<Integer> blocks = flag? xMap.get(x):yMap.get(y);
                if (blocks == null){
                    if (flag){
                        y+=c*code;
                    }else {
                        x+=c*code;
                    }

                }else {
                    for (int j = 1; j <= c; j++) {
                        //int s = code*j;
                        if (blocks.contains(flag?y+code:x+code)){
                            break;
                        }
                        if (flag){
                            y+=code;
                        }else {
                            x+=code;
                        }
                    }
                }
                if (Math.abs(x) >Math.abs(xMaxVal) && Math.abs(y) > Math.abs(yMaxVal)){
                    xMaxVal = x;yMaxVal = y;
                    double t = Math.pow(x,2)+Math.pow(y,2);
                    max = (int)t;
                }else if (Math.abs(x) >Math.abs(xMaxVal) || Math.abs(y) > Math.abs(yMaxVal)){
                    double t = Math.pow(x,2)+Math.pow(y,2);
                    if (t>max){
                        max = (int)t;
                        xMaxVal = x;yMaxVal = y;
                    }
                }
            }
        }

        return max;
    }

1005 

    public static int largestSumAfterKNegations(int[] A, int K) {
        int total = 0;
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        for (int i = 0; i < A.length; i++) {
            pq.offer(A[i]);
        }
        while (K>0){
            Integer peek = pq.peek();
            if (peek == 0){
                while (!pq.isEmpty()){
                    total+=pq.poll();
                }
                return total;
            }else {
                pq.poll();
                pq.offer(-peek);
                K--;
            }
        }
        while (!pq.isEmpty()){
            total+=pq.poll();
        }
        return total;
    }
原文地址:https://www.cnblogs.com/hetutu-5238/p/14366851.html