![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Leetcode Medium
文章平均质量分 71
_TCgogogo_
这个作者很懒,什么都没留下…
展开
-
LeetCode 1477 Find Two Non-overlapping Sub-arrays Each With Target Sum (滑动窗)
题目分析:像这种求两段不重复的问题很容易想到分别从左往右,从右往左计算答案再枚举中点。题目大意:求两段不重复的和为target的子串的最小长度和。原创 2023-12-28 15:47:18 · 546 阅读 · 0 评论 -
LeetCode 1765 Map of Highest Peak (多源点BFS 推荐)
题目分析:多源点bfs,将水的位置都作为源点,每次向四个方向扩散,高度加1,开始一直在想这么做有没有可能出现违反高度差不超过1的限制。其实是不可能的,因为bfs是用队列维护的,特点是先进先出,换句话说,假设当前队首的高度为h,队尾的高度为h+1,不可能出现队尾高度仍为h但队尾高度为h+2的情况,因为处理h+2的前提是所有高度为h的都已处理完毕。题目大意:n*m的矩阵,水的高度为0,地的高度大于0,且要求相邻两个高度差不能超过1,求一种排列方式使得其中的最大值最大。65ms,时间击败91.58%原创 2022-09-07 23:44:49 · 323 阅读 · 1 评论 -
LeetCode 1423 Maximum Points You Can Obtain from Cards (前缀和)
There are several cardsarranged in a row, and each card has an associated number of points. The points are given in the integer arraycardPoints.In one step, you can take one card from the beginning or from the end of the row. You have to take exactlyk...原创 2022-05-09 22:36:24 · 269 阅读 · 0 评论 -
LeetCode 1300 Sum of Mutated Array Closest to Target (前缀和 二分)
Given an integer arrayarrand a target valuetarget, return the integervaluesuch that when we change all the integers larger thanvaluein the given array to be equal tovalue, the sum of the array gets as close as possible (in absolute difference) tot...原创 2022-05-08 17:57:20 · 289 阅读 · 0 评论 -
LeetCode 1721 Swapping Nodes in a Linked List (链表 快慢指针)
You are given theheadof a linked list, and an integerk.Returnthe head of the linked list afterswappingthe values of thekthnode from the beginning and thekthnode from the end (the list is1-indexed).Example 1:Input: head = [1,2,3,4,5], k ...原创 2022-05-07 21:24:15 · 377 阅读 · 0 评论 -
LeetCode 994 Rotting Oranges (bfs 推荐)
You are given anm x ngridwhere each cell can have one of three values:0representing an empty cell, 1representing a fresh orange, or 2representing a rotten orange.Every minute, any fresh orange that is4-directionally adjacentto a rotten orange ...原创 2022-05-07 16:19:35 · 267 阅读 · 0 评论 -
LeetCode 452 Minimum Number of Arrows to Burst Balloons (贪心,排序)
There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer arraypointswherepoints[i] = [xstart, xend]denotes a balloon whosehorizontal diameterstretches betweenxstartandxend....原创 2022-05-07 15:53:14 · 199 阅读 · 0 评论 -
LeetCode 1035 Uncrossed Lines(dp 推荐)
You are given two integer arraysnums1andnums2. We write the integers ofnums1andnums2(in the order they are given) on two separate horizontal lines.We may draw connecting lines: a straight line connecting two numbersnums1[i]andnums2[j]such that...原创 2022-05-07 10:13:48 · 191 阅读 · 0 评论 -
LeetCode 2131 Longest Palindrome by Concatenating Two Letter Words (hash)
You are given an array of stringswords. Each element ofwordsconsists oftwolowercase English letters.Create thelongest possible palindromeby selecting some elements fromwordsand concatenating them inany order. Each element can be selectedat mos...原创 2022-05-05 19:58:47 · 402 阅读 · 0 评论 -
LeetCode 838 Push Dominoes (二点法,模拟)
There arendominoes in a line, and we place each domino vertically upright. In the beginning, we simultaneously push some of the dominoes either to the left or to the right.After each second, each domino that is falling to the left pushes the adjacent d..原创 2022-04-30 17:37:43 · 326 阅读 · 0 评论 -
LeetCode 1590 Make Sum Divisible by P (前缀和 hash)
Given an array of positive integersnums, remove thesmallestsubarray (possiblyempty) such that thesumof the remaining elements is divisible byp. It isnotallowed to remove the whole array.Returnthe length of the smallest subarray that you need to...原创 2022-04-03 23:48:56 · 615 阅读 · 0 评论 -
LeetCode 1233 Remove Sub-Folders from the Filesystem (Trie)
Given a list of foldersfolder, returnthe folders after removing allsub-foldersin those folders. You may return the answer inany order.If afolder[i]is located within anotherfolder[j], it is called asub-folderof it.The format of a path is one o...原创 2022-04-03 22:52:28 · 596 阅读 · 0 评论 -
LeetCode 990 Satisfiability of Equality Equations (并查集 推荐)
You are given an array of stringsequationsthat represent relationships between variables where each stringequations[i]is of length4and takes one of two different forms:"xi==yi"or"xi!=yi".Here,xiandyiare lowercase letters (not necessarily diffe...原创 2022-03-20 15:42:57 · 448 阅读 · 0 评论 -
LeetCode 1975 Maximum Matrix Sum (贪心)
You are given ann x nintegermatrix. You can do the following operationanynumber of times:Choose any twoadjacentelements ofmatrixandmultiplyeach of them by-1.Two elements are consideredadjacentif and only if they share aborder.Your goal ...原创 2022-03-20 14:24:24 · 632 阅读 · 0 评论 -
LeetCode 1524 Number of Sub-arrays With Odd Sum (前缀和)
Given an array of integersarr, returnthe number of subarrays with anoddsum.Since the answer can be very large, return it modulo10^9+7.Example 1:Input: arr = [1,3,5]Output: 4Explanation: All subarrays are [[1],[1,3],[1,3,5],[3],[3,5],[5]]All ...原创 2022-03-19 15:59:57 · 481 阅读 · 0 评论 -
LeetCode 688 Knight Probability in Chessboard (概率dp)
On ann x nchessboard, a knight starts at the cell(row, column)and attempts to make exactlykmoves. The rows and columns are0-indexed, so the top-left cell is(0, 0), and the bottom-right cell is(n - 1, n - 1).A chess knight has eight possible move...原创 2022-03-19 11:09:12 · 415 阅读 · 0 评论 -
LeetCode 1054 Distant Barcodes (排序 贪心)
In a warehouse, there is a row of barcodes, where the ith barcode is barcodes[i].Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.Example 1:Input: barcodes = [1,1,1,2原创 2022-03-18 16:55:07 · 361 阅读 · 0 评论 -
LeetCode 1685 Sum of Absolute Differences in a Sorted Array (前缀和)
You are given an integer arraynumssorted innon-decreasingorder.Build and returnan integer arrayresultwith the same length asnumssuch thatresult[i]is equal to thesummation of absolute differencesbetweennums[i]and all the other elements in t...原创 2022-03-16 16:43:23 · 965 阅读 · 0 评论 -
LeetCode 456 132 Pattern (单调栈 推荐)
Given an arrayofnintegersnums, a132 patternis a subsequence of three integersnums[i],nums[j]andnums[k]such thati < j < kandnums[i] < nums[k] < nums[j].Returntrueif there is a132 patterninnums, otherwise, returnfalse.Exam...原创 2022-03-11 15:47:00 · 898 阅读 · 0 评论 -
LeetCode 881 Boats to Save People (两点法)
You are given an arraypeoplewherepeople[i]is the weight of theithperson, and aninfinite number of boatswhere each boat can carry a maximum weight oflimit. Each boat carries at most two people at the same time, provided the sum of the weight of tho...原创 2022-03-09 10:16:05 · 124 阅读 · 0 评论 -
LeetCode 59 Spiral Matrix II (模拟)
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.Example 1:Input: n = 3Output: [[1,2,3],[8,9,4],[7,6,5]]Example 2:Input: n = 1Output: [[1]]Constraints:1 <= n <= 20题目链接:https:/原创 2022-03-02 17:03:43 · 156 阅读 · 0 评论 -
LeetCode 652 Find Duplicate Subtrees (dfs 推荐)
Given therootof a binary tree, return allduplicate subtrees.For each kind of duplicate subtrees, you only need to return the root node of anyoneof them.Two trees areduplicateif they have thesame structurewith thesame node values.Example 1:...原创 2022-02-28 20:15:17 · 176 阅读 · 0 评论 -
LeetCode 430 Flatten a Multilevel Doubly Linked List (dfs 链表)
You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additionalchild pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These chi.原创 2022-02-28 20:03:45 · 343 阅读 · 0 评论 -
LeetCode 1749 Maximum Absolute Sum of Any Subarray (dp)
You are given an integer arraynums. Theabsolute sumof a subarray[numsl, numsl+1, ..., numsr-1, numsr]isabs(numsl+ numsl+1+ ... + numsr-1+ numsr).Returnthemaximumabsolute sum of any(possibly empty)subarray ofnums.Note thatabs(x)is defin...原创 2022-02-28 18:42:50 · 179 阅读 · 0 评论 -
LeetCode 1261 Find Elements in a Contaminated Binary Tree (递归)
Given a binary tree with the following rules:root.val == 0 IftreeNode.val == xandtreeNode.left != null, thentreeNode.left.val == 2 * x + 1 IftreeNode.val == xandtreeNode.right != null, thentreeNode.right.val == 2 * x + 2Now the binary tree is ...原创 2022-02-26 14:18:48 · 335 阅读 · 0 评论 -
LeetCode 1609 Even Odd Tree (bfs)
A binary tree is namedEven-Oddif it meets the following conditions:The root of the binary tree is at level index0, its children are at level index1, their children are at level index2, etc. For everyeven-indexedlevel, all nodes at the level have...原创 2022-02-25 12:14:00 · 147 阅读 · 0 评论 -
LeetCode 1094 Car Pooling (模拟)
There is a car withcapacityempty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).You are given the integercapacityand an arraytripswheretrips[i] = [numPassengersi, fromi, toi]indicates that theithtrip hasnumPas...原创 2022-02-24 21:23:23 · 213 阅读 · 0 评论 -
LeetCode 2171 Removing Minimum Number of Magic Beans (前缀和 推荐)
You are given an array ofpositiveintegersbeans, where each integer represents the number of magic beans found in a particular magic bag.Removeany number of beans (possibly none) from each bag such that the number of beans in each remainingnon-empty...原创 2022-02-24 20:38:24 · 261 阅读 · 0 评论 -
LeetCode 1567 Maximum Length of Subarray With Positive Product (递推? 推荐)
Given an array of integersnums, find the maximum length of a subarray where the product of all its elements is positive.A subarray of an array is a consecutive sequence of zero or more values taken out of that array.Returnthe maximum length of a suba..原创 2022-02-23 12:33:29 · 221 阅读 · 0 评论 -
LeetCode 665 Non-decreasing Array (贪心)
Given an arraynumswithnintegers, your task is to check if it could become non-decreasing by modifyingat most one element.We define an array is non-decreasing ifnums[i] <= nums[i + 1]holds for everyi(0-based) such that (0 <= i <= n - 2)....原创 2022-02-22 21:25:48 · 184 阅读 · 0 评论 -
LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60 (hash)
You are given a list of songs where theithsong has a duration oftime[i]seconds.Returnthe number of pairs of songs for which their total duration in seconds is divisible by60. Formally, we want the number of indicesi,jsuch thati < jwith(tim...原创 2022-02-22 20:48:47 · 251 阅读 · 0 评论 -
LeetCode 1024 Video Stitching (dp,jump game)
You are given a series of video clips from a sporting event that lastedtimeseconds. These video clips can be overlapping with each other and have varying lengths.Each video clip is described by an arrayclipswhereclips[i] = [starti, endi]indicates t...原创 2022-02-22 12:49:50 · 236 阅读 · 0 评论 -
LeetCode 1116 Print Zero Even Odd (并发 多线程 CountDownLatch Lock Condition)
You have a functionprintNumberthat can be called with an integer parameter and prints it to the console.For example, callingprintNumber(7)prints7to the console.You are given an instance of the classZeroEvenOddthat has three functions:zero,even...原创 2022-02-20 08:30:53 · 211 阅读 · 0 评论 -
LeetCode 2054 Two Best Non-Overlapping Events (排序 二分)
You are given a0-indexed2D integer array ofeventswhereevents[i] = [startTimei, endTimei, valuei]. Theithevent starts atstartTimeiand ends atendTimei, and if you attend this event, you will receive a value ofvaluei. You can chooseat mosttwonon...原创 2022-02-19 14:59:33 · 401 阅读 · 0 评论 -
LeetCode 698 Partition to K Equal Sum Subsets (dfs 剪枝)
Given an integer array nums and an integer k, return true if it is possible to divide this array into k non-empty subsets whose sums are all equal.Example 1:Input: nums = [4,3,2,3,5,2,1], k = 4Output: trueExplanation: It is possible to divide it int原创 2022-02-18 21:01:14 · 273 阅读 · 0 评论 -
LeetCode 1248 Count Number of Nice Subarrays (哈希 推荐)
Given an array of integersnumsand an integerk. A continuous subarray is calledniceif there arekodd numbers on it.Returnthe number ofnicesub-arrays.Example 1:Input: nums = [1,1,2,1,1], k = 3Output: 2Explanation: The only sub-arrays with 3...原创 2022-02-17 15:32:34 · 472 阅读 · 0 评论 -
LeetCode 1605 Find Valid Matrix Given Row and Column Sums (思维 构造 推荐)
You are given two arraysrowSumandcolSumof non-negative integers whererowSum[i]is the sum of the elements in theithrow andcolSum[j]is the sum of the elements of thejthcolumn of a 2D matrix. In other words, you do not know the elements of the mat...原创 2022-02-17 15:23:21 · 310 阅读 · 0 评论 -
LeetCode 1574 Shortest Subarray to be Removed to Make Array Sorted (两点法 + 二分)
Given an integer arrayarr, remove a subarray (can be empty) fromarrsuch that the remaining elements inarrarenon-decreasing.Returnthe length of the shortest subarray to remove.Asubarrayis a contiguous subsequence of the array.Example 1:I...原创 2022-02-14 17:43:22 · 263 阅读 · 0 评论 -
LeetCode 1029 Two City Scheduling (dp)
A company is planning to interview2npeople. Given the arraycostswherecosts[i] = [aCosti, bCosti],the cost of flying theithperson to cityaisaCosti, and the cost of flying theithperson to citybisbCosti.Returnthe minimum cost to fly every p...原创 2022-02-10 13:58:13 · 386 阅读 · 0 评论 -
LeetCode 1814 Count Nice Pairs in an Array (map 推荐)
You are given an arraynumsthat consists of non-negative integers. Let us definerev(x)as the reverse of the non-negative integerx. For example,rev(123) = 321, andrev(120) = 21. A pair of indices(i, j)isniceif it satisfies all of the following con...原创 2022-02-09 12:59:03 · 469 阅读 · 0 评论