自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 Leetcode 2244. Minimum Rounds to Complete All Tasks [Python]

思路比较直观,先把每个数字都多少个统计出来,组成value,和key的pair(这里的key要不要其实都可以)。对于每个数字的个数value,看用3来减,可以执行多少次,整除一下,加入到执行次数里;接下来,如果这个个数value没法被整除,那么就会余1或者2,余2好办,再用-2处理一次,也就是贡献一次执行次数,余1呢,其实可以从前面被3整除的次数里(假设为n次,3。n-1)-2,然后这不就剩下一个2(1+1)了么,再-2操作一次。n)拿出最后一次来变成(n-1次-3操作,一次-2操作,3。

2022-09-27 16:30:11 354

原创 【无标题】

还是前缀和吧,从左向右,从右向左都求出来。

2022-09-26 17:25:57 170

原创 Leetcode 2386. Find the K-Sum of an Array [Python]

首先,最大的部分就是非负值的部分加和。然后要在这个最大值部分-去哪些正值,或者加上哪些负的值,从而找到第k大的加和的部分,这样就可以转化使用哪些数字。那按照数字绝对值从大到小排列,选择使用某个绝对值或不使用,找到第k大的使用这些值产生的加和(记为minuspart),来用最大值减去这个minuspart就可以得到第k大的值。

2022-09-26 17:03:15 266

原创 Leetcode 2398. Maximum Number of Robots Within Budget [Python]

使用双指针和单调栈,双指针的用处是维护现在入选的sequence的开头和结尾,单调栈则是要维护目前入选的sequence中最大值的位置,我们把这个位置放在单调栈的首位。每次在不超过预算的前提下,挪动前面的双指针,更新最大值位置,获得新的长度。如果超过,则开始挪动后一个双指针,同时确定丢弃的(后一个双指针所指的数字是不是被选中sequence中的最大数字)位置的数字是不是最大值,不是则最大值不变,是,则只用把后一个指针所指的数字值从tempsum中减去。

2022-09-25 16:28:08 733

原创 Leetcode 2385. Amount of Time for Binary Tree to Be Infected [Python]

把tree转化成graph:因为node的value都是不一样的,可以转化成graph后宽搜就好。

2022-09-23 09:59:52 663

原创 Leetcode 2406. Divide Intervals Into Minimum Number of Groups [Python]

先把intervals排序后,开始从小到大遍历。如果res中没有现成的subinterval,则加入当前被遍历interval,成为新的一个subinterval。如果有,我们把当前被遍历的interval的最小值拿出来,与res中全部subinterval中结尾值最小的那个比较,如果没有交集则就把当前interval加入到结尾值最小的subinterval中,并更新这个subinterval的结尾值为当前被遍历interval的最大值(heappop后不再push back)。

2022-09-23 09:33:43 263

原创 Leetcode 2405. Optimal Partition of String [Python]

设置temp string,遇到新的char在temp中没出现过,就加入到temp,直到遇到重复的char,将temp加入到res中,将temp归零到空的string。

2022-09-21 11:31:06 622

原创 Leetcode 1436. Destination City [Python]

在入度的dict中,如果有个key没有出现在出度的dict中,则返回。

2022-09-20 00:12:02 223

原创 Leetcode 1464. Maximum Product of Two Elements in an Array [Python]

算出新的array来,为了避免排序,用heappush的方式把新的值在计算的时候就放入new array中,最后弹出前两个来,就是最大和第二大的新的数字。

2022-09-20 00:06:15 181

原创 Leetcode 1523. Count Odd Numbers in an Interval Range [Python]

【代码】1523. Count Odd Numbers in an Interval Range [Python]

2022-09-19 23:58:38 284

原创 Leetcode 1864. Minimum Number of Swaps to Make the Binary String Alternating [Python]

Greedy

2022-09-04 23:18:38 206

原创 Leetcode 1834. Single-Threaded CPU [Python]

heap PQ

2022-09-03 01:41:50 648

原创 Leetcode 2070. Most Beautiful Item for Each Query [Python]

bisect

2022-09-01 15:25:15 499

原创 Leetcode 1962. Remove Stones to Minimize the Total [Python]

第一个办法会TLE在第46个TC。

2022-09-01 01:17:35 549

原创 Leetcode 2256. Minimum Average Difference [Python]

前缀和

2022-08-30 18:17:21 303

原创 Leetcode 2288. Apply Discount to Prices [Python]

string int float format(num, '.2f')

2022-08-30 17:16:49 235

原创 Leetcode 2289. Steps to Make Array Non-decreasing [Python]

monostack

2022-08-29 19:49:09 391

原创 Leetcode 2099. Find Subsequence of Length K With the Largest Sum [Python]

heap

2022-08-27 23:05:56 227

原创 Leetcode 2340. Minimum Adjacent Swaps to Make a Valid Array [Python]

Greedy 2340. Minimum Adjacent Swaps to Make a Valid Array

2022-08-27 22:03:03 245

原创 Leetcode 2336. Smallest Number in Infinite Set [Python]

系统设计 2336. Smallest Number in Infinite Set

2022-08-27 21:06:12 392

原创 Leetcode 2327. Number of People Aware of a Secret [Python]

2327. Number of People Aware of a Secret

2022-08-26 18:11:25 313

原创 Leetcode 2294. Partition Array Such That Maximum Difference Is K [Python]

先sort下,然后开始遍历nums中的元素,设置一个空的array,装入元素,只要被遍历元素小于空array的第一个元素 + k值,就可以继续把当前被遍历元素放到array中,否则将array记录到res中,array清空后装入当前被遍历元素,开始新的一组分组。...

2022-06-14 09:56:05 129

原创 Leetcode 2169. Count Operations to Obtain Zero [Python]

递归一下

2022-06-13 15:14:57 142

原创 Leetcode 2193. Minimum Number of Moves to Make Palindrome [Python]

Greedy

2022-06-13 15:05:52 1163

原创 Leetcode 2222. Number of Ways to Select Buildings [Python]

2222. Number of Ways to Select Buildings

2022-06-05 21:18:20 1192 1

原创 Leetcode 2221. Find Triangular Sum of an Array [Python]

2221. Find Triangular Sum of an Array

2022-06-02 22:27:30 1018

原创 Leetcode 2272. Substring With Largest Variance [Python]

改版的Kadane算法,原有的kadane算法是dp[i] = max(dp[i-1], 0) + arr[i]。这里a,b的两个字分别统计数量,也就是类似1和-1,当counta >countb, 且b已经在当前的substring里了,就可以计算res = max(res,counta - countb)。而如果counta 已经 < countb,且依然还有 letter b还没遍历到,则此时的counta - countb已经无意义,counta 和countb改回0.这里之所以要看是否

2022-06-02 18:25:12 479

原创 Leetcode 2262. Total Appeal of A String [Python]

一眼看上去真不会,看了Hint,写出来了。Hint的思路很详细,找一个index i,那包含某一个字母且截止于index i的substring的总数就是这个字母离index i最近出现的位置(假设为j,j <= i)j + 1。比如abc,在index i = 2处截止,含有c的substring数量,c,bc,abc,含有b的,b,ab, 含有a的a。所以,到此为止,截止于index i = 0处的全部substring,只有一个a;截止于index i = 1处的string的全部substr

2022-05-31 13:05:37 330

原创 Leetcode 2268. Minimum Number of Keypresses [Python]

把字母出现的次数统计出来,从多到少排序,出现次数多的字母尽可能排在第一轮就可以按到,也就是press1次,以此类推到press 2次,press 3次。class Solution: def minimumKeypresses(self, s: str) -> int: Counter = collections.Counter(s) newCounter = [] for c,num in Counter.items():

2022-05-30 15:59:05 490

原创 Leetcode 2040. Kth Smallest Product of Two Sorted Arrays [Python]

binary search的难题。首先把两个Array的大于等于0的数字和小于0的数字分离出来。方便确定两者相乘后会产生多少小于0的数据。下一步检查K是个+数还是个-数。如果大于全部-数的结果数(假设为L),则K=K-L,我们只需要在相乘的结果里的+数找就好。如果小于L,则K = L - K + 1.我们在。并且将原本的其中一个array分割出来+数array和-数array交换一下,方便计算。定义一个函数,以K 大于L 为例子,nums1的+array和nums2的+array元素相乘,一个从前往后

2022-04-08 17:21:30 601

原创 Leetcode 2134. Minimum Swaps to Group All 1‘s Together II [Python]

sliding window。window大小设置为总的1的个数,假定为Z,原因是咱们的目的是把1都聚在一起,那么如果在Z大长度内,含有最多的1,需要swap的1就少。一种办法,就是每次把nums的0到Z-2位置内的元素复制,append到nums尾部(circulate的array)。然后滑动窗口后,计算新窗口内的1.当然,这里就存在重复计算。每次滑动后,把滑进来的元素+到远窗口的sum中,同时减去离开窗口的元素值即可。同时,circulate的部分,用%操作就可以了。class Solution:

2022-04-06 10:56:56 755

原创 Leetcode 2214. Minimum Health to Beat Game [Python]

只用考虑两种情况。armor比最大的伤害还大,那就扛住max(demage)就好了。准备sum(demage) + 1 - max(demage)就够了。剩下的,无论哪种情况。准备好sum(demage) + 1 - armor的血,就好了。无论是刚好扛住某次伤害,还是介于两次伤害的大小之间。或则每次伤害都比armor大,都是一样的准备值。class Solution: def minimumHealth(self, damage: List[int], armor: int) -> int

2022-04-04 00:15:57 1352 1

原创 Leetcode 1696. Jump Game VI [Python]

好题目 DP暴力的做法不用想都TLE,从后往前遍历,dp的含义是从这里往后跳,可以得到的最大的回报。只用记录下某的位置之后的i+1 到end之间哪个位置的回报最大,则由当前位置往这个位置跳就好。也就是t[i] = nums[i] + temp。class Solution: def maxResult(self, nums: List[int], k: int) -> int: n = len(nums) t = [0 for _ in range(n)]

2022-04-03 23:46:27 465

原创 Leetcode 1752. Check if Array Is Sorted and Rotated [Python]

友好的题目class Solution: def check(self, nums: List[int]) -> bool: copy = [] breakpoint = -1 for i,num in enumerate(nums): if copy and num< copy[-1]: breakpoint = i copy.append(num)

2022-04-01 23:32:55 530

原创 Leetcode 1856. Maximum Subarray Min-Product [Python]

一道奇怪的题目,用单调栈。遍历array中的元素,然后对比栈顶的数字,如果比栈顶的大,入栈。如果比栈顶的小,栈顶元素出栈。此时,被从栈顶元素开始往回看,因为是单调上升的单调栈,其之后的元素一定小于栈顶这个元素。于是,每次弹出的元素,都是原本栈顶元素对应的位置往回遍历到当前栈顶元素中的最小值。于是,我们可以更新此时的susbarry的计算值。我们还需要注意的是,每次遍历到的值入栈,我们都入栈这个值,以及这个值往回看,作为最小值可以到达的最左侧的位置。也就是这个值为最小值的subarray的最左侧起始位

2022-04-01 23:10:53 959

原创 Leetcode 1925. Count Square Sum Triples [Python]

基本思路是暴力破解。但是注意sqrt给出的是float,转成int后检查下是否还是原来的数值,随后检查给出的sqrt是不是小于等于n的。class Solution: def countTriples(self, n: int) -> int: res = 0 for a in range(1,n): for b in range(1,a): root1 = sqrt(a**2 + b**2)

2022-03-22 23:12:33 1102

原创 Leetcode 2047. Number of Valid Words in a Sentence [Python]

展开了,把不符合条件的全部返回False。细心点就好了。class Solution: def countValidWords(self, sentence: str) -> int: lst = sentence.split(' ') hyphens = False res = 0 #print(lst) for part in lst: if part == '':

2022-03-22 22:31:49 610

原创 Leetcode 2102. Sequentially Ordinal Rank Tracker [Python]

system design

2022-03-22 18:38:28 1163

原创 Leetcode 2009. Minimum Number of Operations to Make Array Continuous [Python]

把重复的部分去掉,sort,挨个数字遍历,计算出来以这个数字为开始,所需要的结尾数字,也就是n + N - 1,并查看原来的array中有没有这个数字,有,则只用改总数N - 这个数所在位置到被遍历到的位置的数 + 1.而如果没有,则修改N - 这个数所在位置到被遍历到的位置的数,因为结尾数字也需要修改。注意查找end数字是否存在,保留数字的set而不是在sorted的list中寻找,速度更快。class Solution: def minOperations(self, nums: List[

2022-03-22 13:35:59 923

原创 Leetcode 2018. Check if Word Can Be Placed In Crossword [Python]

字符串处理

2022-03-21 18:58:09 141

空空如也

空空如也

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

TA关注的人

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