自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(27)
  • 收藏
  • 关注

原创 leetcode 129, 236 (dfs in a tree)

Solution for leetcode 129https://leetcode.com/problems/sum-root-to-leaf-numbers/class Solution { public int sumNumbers(TreeNode root) { return dfs(root, 0); } public int dfs(TreeNode node, int num){ if(node == null){ .

2021-07-18 13:46:08 70

原创 leetcode 105, 106, 1008 (Recover a tree)

Solution for leetcode 105https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/class Solution { HashMap<Integer,Integer> map = new HashMap(); public TreeNode buildTree(int[] preorder, int[] inorder) {

2021-07-18 13:39:28 67

原创 leetcode 735 (stack)

Solution to leetcode 735https://leetcode.com/problems/asteroid-collision/class Solution { public int[] asteroidCollision(int[] asteroids) { Stack<Integer> stack = new Stack(); for(int i = 0; i < asteroids.length; i++){ .

2021-07-15 00:31:36 88

原创 leetcode 215, 378 (kth element, priority queue)

class Solution { public int kthSmallest(int[][] matrix, int k) { PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0] - b[0]); pq.offer(new int[]{matrix[0][0],0,0}); int row = matrix.length; int co.

2021-07-14 09:52:06 52

原创 leetcode 1129 (bfs)

Thoughts on leetcode 11291.2.Solution for leetcode 1129https://leetcode.com/problems/shortest-path-with-alternating-colors/class Solution { public int[] shortestAlternatingPaths(int n, int[][] red_edges, int[][] blue_edges) { Set&lt.

2021-07-13 00:17:05 64

原创 leetcode 69, 278, 153, 378, 34, 162 (binary search)

template1class BinarySearch { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (nums[mi

2021-07-08 13:38:21 96

原创 leetcode 1584 (minimal spanning tree)

IntroductionGiven a connected and undirected graph, aspanning treeof that graph is a subgraph that is a tree and connects all the vertices together. Aminimum spanning tree (MST)or minimum weight spanning tree for a weighted, connected, undirected gra...

2021-07-07 17:25:44 121

原创 leetcode 236 (DFS in a tree)

Thoughts on leetcode 236Traverse the tree in a depth first manner. The moment you encounter either of the nodesporq, return some boolean flag. The flag helps to determine if we found the required nodes in any of the paths. The least common ancestor w...

2021-07-02 11:54:55 50

原创 leetcode 743, 787, 1514 (Dijkstra Algorithm)

Solution for leetcode 743class Solution { public int networkDelayTime(int[][] times, int n, int k) { Map<Integer, ArrayList<int[]>> map = new HashMap(); for(int i = 0; i < times.length; i++){ int from = tim

2021-06-30 19:17:31 135

原创 leetcode 739, 84, 42, 901 (Monotonic Stack)

If a problem is suitable to use the monotonic stack, it must haveat least three characters:It is a “range queries in an array” problem. The minima/maxima element or the monotonic order of elements in a range is useful to getthe answerto every range q...

2021-06-30 11:37:34 89

原创 leetcode 307 (Binary Indexed Tree)

Binary Indexed Tree introductionbinary indexed treeis a data structure that can efficiently update elements and calculateprefix sumsin a table of numbers.

2021-06-27 09:07:12 52

原创 leetcode1696 (dp + priority queue)

public int maxResult(int[] nums, int k) { PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> b[1] - a[1]); pq.add(new int[]{0,nums[0]}); int n = nums.length; int[] dp = new int[n]; Arrays.

2021-06-10 22:55:23 96

原创 LeetCode797, 802, 130, 133 (DFS)

DFS templatevoid dfs()//参数用来表示状态 { if(到达终点状态){ ...//根据题意添加 return; } if(越界或者是不合法状态) return; if(特殊状态)//剪枝 return ; for(扩展方式){ if(扩展方式所达到状态合法){ 修改操作;//根据题意来添加

2021-06-09 13:40:52 66

原创 leetcode97, 688 (dp)

class Solution { public boolean isInterleave(String s1, String s2, String s3) { int l1 = s1.length(); int l2 = s2.length(); if(l1 + l2 != s3.length()){ return false; } boolean[][] dp = new boolean[l1.

2021-06-04 08:37:51 87 1

原创 leetcode 1368 (0-1 dp)

Thoughts on leetcode 1368class Solution { public int minCost(int[][] grid) { int m = grid.length; int n = grid[0].length; Deque<int[]>queue=new ArrayDeque(); int[][]directions={{0,0},{0,1},{0,-1},{1,0},{-1,0}};

2021-05-27 23:07:03 63

原创 leetcode 994, 934, 752, 310, 1654 (BFS)

Thoughts on leetcode 934Use dfs to find the first island and insert every cell of itinto a queue. In this way,we can use bfs later to find the shortest distance from a certaincell of the first island to the other island. We should remember both the df...

2021-05-25 23:26:37 152

原创 leetcode 1349 Maximum Students Taking Exam (dp state compression)

When doing Bitmasking DP, we are always handling problems like "what is the i-th bit in the state" or "what is the number of valid bits in a state". Here are some tricksWe can use(x >> i) & 1to get i-th bit in statex We can use(x & y) ...

2021-05-23 17:18:02 99

原创 leetcode 1014 best sightseeing pair

Thoughts on 1014The final output can be divided into two parts values[i] + i and values[j] - jwe need to update the maximum value for values[i] + i in each iteration and calculate the output for that certain indexSolution for 1014https://leetcode-c

2021-05-23 11:12:49 60

原创 Leetcode 124, 543, 968 (Tree Path Sum)

Solution for leetcode 543https://leetcode.com/problems/diameter-of-binary-tree/Note that the output and what we return in recursion are differentclass Solution { private int result = 0; public int diameterOfBinaryTree(TreeNode root) { .

2021-05-17 20:58:41 76

原创 Leetcode 120,576 (dp triangle)

Solution for leetcode 120https://leetcode.com/problems/triangle/Draw a picture for better visualization and it can help me know the relationship between different indices.Used dimension reduction for this problemclass Solution { public int m

2021-05-14 21:01:40 65

原创 Leetcode 1143, 1035, 1092 (longest common subsequence)

ThinkingLet A=“a0,a1,…,am”,B=“b0,b1,…,bn”,and Z=“z0,z1,…,zk” is their longest common sequence. Then, we have:if am = bn,then zk = am = bn,and “z0,z1,…,z(k-1)” is the longest common sequence of “a0,a1,…,a(m-1)” and “b0,b1,…,b(n-1)” if am != bn, then i.

2021-05-14 18:59:24 71

原创 leetcode 300, 673 (longest increasing subsequence)

Solution for leetcode 300dp[i] represents the longest subsequence ending at position [i] so that nums[i] is includedThe transition matrix is : for allj < i and nums[j] < nums[i], dp[i] can be updated as the maximum value between dp[i] (origina...

2021-05-14 13:24:13 63

原创 leetcode 312, 1039 (dp over interval)

IntroductionGoal is to find an optimal (min or max) solution to a problem withA Problem of size n and ordered input of items 1,2…,n Define substructure as ordered input of items i..j (Problem of size j-i+1) Recurrence gives optimal solution of ...

2021-05-13 08:25:13 76

原创 leetcode 122, 714, 309 (dp state machine)

When approaching the problem, we can draw a state machine to visualize the states we have. The maximumprofit comes from s0 or s2.s0[0] = 0; // At the start, you don't have any stock if you just rests1[0] = -prices[0]; // After buy, you should have ...

2021-05-11 10:24:57 121

原创 leetcode1319, leetcode 721, leetcode 684, leetcode 1584, lintcode 591, leetcode 990 (union find)

Three stepsInitialization: making every node its own parent private int[] father = null; public Initialization(int n) { father = new int[n + 1]; for (int i = 1; i <= n; ++i) father[i] = i; } Find: fi.

2021-05-10 08:04:45 140

原创 leetcode 322, 377, 139 (unbounded knapsack problem)

Unbounded knapsack problemDifferent from 0 -1Knapsack problem,theunbounded knapsack problem allowsusing an unlimited number of instances of an item.Let dp[i][v] represent the largest valuewe can get by choosing fromthe previous i items to put int...

2021-05-09 20:30:48 222 1

原创 leetcode1049, 474 (0-1 knapsack problem)

Thevalue of the last stone is the difference between the two stone groups. Therefore, we can modify it to a 0-1 knapsack problem.0-1 knapsack problem definitionGiven weights and values of n items, put these items in a knapsack of capacity W to get the.

2021-05-08 08:11:33 185

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除