自定义博客皮肤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 129 https://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 136

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

Solution for leetcode 105 https://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 163

原创 leetcode 735 (stack)

Solution to leetcode 735 https://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 160

原创 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 135

原创 leetcode 1129 (bfs)

Thoughts on leetcode 1129 1. 2. Solution for leetcode 1129 https://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 133

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

template1 class 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 169

原创 leetcode 1584 (minimal spanning tree)

Introduction Given 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 257

原创 leetcode 236 (DFS in a tree)

Thoughts on leetcode 236 Traverse 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 116

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

Solution for leetcode 743 class 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 236

原创 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 230

原创 leetcode 307 (Binary Indexed Tree)

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

2021-06-27 09:07:12 124

原创 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 199

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

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

2021-06-09 13:40:52 128

原创 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 190 1

原创 leetcode 1368 (0-1 dp)

Thoughts on leetcode 1368 class 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 129

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

Thoughts on leetcode 934 Use 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 264

原创 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 tricks We can use(x >> i) & 1to get i-th bit in statex We can use(x & y) ...

2021-05-23 17:18:02 191

原创 leetcode 1014 best sightseeing pair

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

2021-05-23 11:12:49 124

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

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

2021-05-17 20:58:41 156

原创 Leetcode 120,576 (dp triangle)

Solution for leetcode 120 https://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 problem class Solution { public int m

2021-05-14 21:01:40 126

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

Thinking Let 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 138

原创 leetcode 300, 673 (longest increasing subsequence)

Solution for leetcode 300 dp[i] represents the longest subsequence ending at position [i] so that nums[i] is included The 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 112

原创 leetcode 312, 1039 (dp over interval)

Introduction Goal is to find an optimal (min or max) solution to a problem with A 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 128

原创 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 rest s1[0] = -prices[0]; // After buy, you should have ...

2021-05-11 10:24:57 233

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

Three steps Initialization: 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 261

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

Unbounded knapsack problem Different 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 380 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 definition Given weights and values of n items, put these items in a knapsack of capacity W to get the.

2021-05-08 08:11:33 352

空空如也

空空如也

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

TA关注的人

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