(算法题)Day-20230307
股票问题01
class Solution {
    public int maxProfit(int[] prices) {
        int minPrice=prices[0];
        int maxProfit=0;
        for(int price:prices){
            if(price<minPrice) minPrice=price;
            maxProfit = Math.max(maxProfit,price-minPrice);
        }
        return maxProfit;
    }
}
股票问题02
122. 买卖股票的最佳时机 II - 力扣(Leetcode)
class Solution {
    public int maxProfit(int[] prices) {
        int maxProfit=0;
        for(int i=1;i<prices.length;i++){
            int tmp=prices[i]-prices[i-1];
            if(tmp>0) maxProfit+=tmp;
        }
        return maxProfit;
    }
}
股票问题03
123. 买卖股票的最佳时机 III - 力扣(Leetcode)
先从前往后得到 第 \(i\) 天之前的 最大利润,然后在此基础上,从后往前得到 到第 \(i\) 天的最大利润,相加取最大利润
package hhh.dp;
/**
 * @author hdbing
 * @create 2023-03-07 19:16
 */
public class Stock03 {
	public static void main(String[] args) {
		int[] prices = {3, 3, 5, 0, 0, 3, 1, 4};
		System.out.println(maxProfit(prices));
	}
	static int maxProfit(int[] prices) {
		int n = prices.length;
		int[] dp = new int[n];    //dp[i] 表示前 prices[i] 第一次交易所获得的最大利润
		int min = prices[0];
		for (int i = 1; i < n; i++) {
			dp[i] = Math.max(prices[i] - min, dp[i - 1]);
			min = Math.min(min, prices[i]);
		}
		int max = prices[n - 1];
		// 初始化最大利润,其实是只卖一次股票的情况
		int ans = dp[n - 1];
		for (int i = n - 2; i >= 2; i--) {
			ans = Math.max(Math.max(max - prices[i], 0) + dp[i - 1], ans);
			max = Math.max(max, prices[i]);
		}
		return ans;
	}
}
单词搜索
package hhh.backtracking;
/**
 * @author hdbing
 * @create 2023-03-07 19:50
 */
public class SeachWord {
   private static final int[][] DIRECTIONS = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
   private int rows;
   private int cols;
   private int len;
   private boolean[][] visited;
   private char[] charArray;
   private char[][] board;
   public boolean exist(char[][] board, String word) {
      rows = board.length;
      if (rows == 0) {
         return false;
      }
      cols = board[0].length;
      visited = new boolean[rows][cols];
      this.len = word.length();
      this.charArray = word.toCharArray();
      this.board = board;
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++) {
            if (dfs(i, j, 0)) {
               return true;
            }
         }
      }
      return false;
   }
   private boolean dfs(int x, int y, int begin) {
      if (begin == len - 1) {
         return board[x][y] == charArray[begin];
      }
      if (board[x][y] == charArray[begin]) {
         visited[x][y] = true;
         for (int[] direction : DIRECTIONS) {
            int newX = x + direction[0];
            int newY = y + direction[1];
            if (inArea(newX, newY) && !visited[newX][newY]) {
               if (dfs(newX, newY, begin + 1)) {
                  return true;
               }
            }
         }
         visited[x][y] = false;
      }
      return false;
   }
   private boolean inArea(int x, int y) {
      return x >= 0 && x < rows && y >= 0 && y < cols;
   }
}
重排链表
package hhh.linklist;
/**
 * @author hdbing
 * @create 2023-03-07 20:16
 */
public class ReSortLinkList {
   public static void main(String[] args) {
      int[] input = {1, 2, 3, 4, 5, 6, 7, 8};
      ListNode head = Utils.createLinkList(input);
      reorderList(head);
      Utils.printLinkList(head);
   }
   public static void reorderList(ListNode head) {
      if (head == null || head.next == null || head.next.next == null) {
         return;
      }
      int len = 0;
      ListNode h = head;
      //求出节点数
      while (h != null) {
         len++;
         h = h.next;
      }
      reorderListHelper(head, len);
   }
   private static ListNode reorderListHelper(ListNode head, int len) {
      if (len == 1) {
         ListNode outTail = head.next;
         head.next = null;
         return outTail;
      }
      if (len == 2) {
         ListNode outTail = head.next.next;
         head.next.next = null;
         return outTail;
      }
      // 得到对应的尾节点,并且将头结点和尾节点之间的链表通过递归处理
      ListNode tail = reorderListHelper(head.next, len - 2);
      ListNode subHead = head.next;  // 中间链表的头结点
      head.next = tail;
      ListNode outTail = tail.next;      // 上一层 head 对应的 tail
      tail.next = subHead;
      return outTail;
   }
}

                
            
        
浙公网安备 33010602011771号