LC.P729[我的日程安排表I]
LC.P729[我的日程安排表I]
方法一:模拟12345678910111213141516171819202122232425class MyCalendar {    List<int[]> booked;    public MyCalendar() {        booked = new ArrayList<int[]>();    }    public boolean book(int start, int end) {        for (int[] arr : booked) {            int left = arr[0], right = arr[1];            if (left < end && start < right) {                return false;            }        }        booked.add(new int[]{st ...
LC.P2582[递枕头]
LC.P2582[递枕头]
方法一:模拟123456789101112class Solution {    public int passThePillow(int n, int time) {        int ans = 1, k = 1;        while (time-- > 0) {            ans += k;            if (ans == 1 || ans == n) {                k *= -1;            }        }        return ans;    }}
时间复杂度:$O(time)$
空间复杂度:$O(1)$
方法二:数学1234567class Solution {    public int passThePillow(int n, int time) {        int k = time / (n - 1);        int mod = time  ...
LC.P211[添加与搜索单词-数据结构设计]
LC.P211[添加与搜索单词-数据结构设计]
方法一:Trie+DFS1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465class WordDictionary {    Trie trie;    public WordDictionary() {        trie = new Trie();    }    public void addWord(String word) {        trie.add(word);    }    public boolean search(String word) {        return dfs(word, 0, trie);    }    private boolean dfs(String word, int index, Trie node) {   ...
LC.P1993[树上的操作]
LC.P1993[树上的操作]
方法一:DFS1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071class LockingTree {    int[] locked;    int[] parent;    List<Integer>[] children;    public LockingTree(int[] parent) {        int n = parent.length;        locked = new int[n];        this.parent = parent;        children = new List[n];        Arrays.fill(locked, -1);        Arrays.setAll(children, k -> new ArrayList<& ...
LC.P2246[相邻字符不同的最长路径]
LC.P2246[相邻字符不同的最长路径]
方法一:树形DP12345678910111213141516171819202122232425262728class Solution {        List<Integer>[] g;    String s;    int ans;    public int longestPath(int[] parent, String s) {        this.s = s;        int n = parent.length;        g = new ArrayList[n];        Arrays.setAll(g, k -> new ArrayList<>());        for (int i = 1; i < n; ++i) g[parent[i]].add(i);        dfs(0);        return ans + 1;    }    private int dfs(int x) {        int  ...
LC.P2591[将钱分给最多的儿童]
LC.P2591[将钱分给最多的儿童]
方法一:分类讨论12345678class Solution {    public int distMoney(int money, int children) {        if (money < children) return -1;        if (money > 8 * children) return children - 1;        if (money == 8 * children - 4) return children - 2;        return (money - children) / 7;    }}
时间复杂度:$O(1)$
空间复杂度:$O(1)$
LC.P124[二叉树中的最大路径和]
LC.P124[二叉树中的最大路径和]
方法一:DFS1234567891011121314151617181920212223242526272829303132333435/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {        ...
LC.P2603[收集树中金币]
LC.P2603[收集树中金币]
方法一:拓扑排序123456789101112131415161718192021222324252627282930313233343536373839404142434445464748class Solution {    public int collectTheCoins(int[] coins, int[][] edges) {        int n = coins.length;        List<Integer>[] g = new ArrayList[n];        Arrays.setAll(g, e -> new ArrayList<>());        int[] degree = new int[n];        for (int[] e : edges) {            int x = e[0], y = e[1];            g[x].add(y);            g[y].add(x);            ...
LCP.P6[拿硬币]
LCP.P6[拿硬币]
方法一:数学123456789class Solution {    public int minCount(int[] coins) {        int ans = 0;        for (int coin : coins) {            ans += (coin + 1) >> 1;        }        return ans;    }}
时间复杂度:$O(n)$
空间复杂度:$O(1)$
LC.P2560[打家劫舍IV]
LC.P2560[打家劫舍IV]
方法一:二分查找+动态规划12345678910111213141516171819202122232425262728class Solution {    public int minCapability(int[] nums, int k) {        int left = 0, right = 0;        for (int x : nums) {            right = Math.max(right, x);        }        while (left < right) {            int mid = left + right >>> 1;            if (check(nums, k, mid)) right = mid;            else left = mid + 1;        }        return right;    }    private bool ...
