锘??xml version="1.0" encoding="utf-8" standalone="yes"?>久久这里只精品99re66,久久久国产精华液,国产激情久久久久影院小草http://www.shnenglu.com/Uriel/category/11823.htmlResearch Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learningzh-cnTue, 02 Jan 2024 08:46:24 GMTTue, 02 Jan 2024 08:46:24 GMT60[LeetCode]455. Assign Cookies (Easy) Python-2024.01.01http://www.shnenglu.com/Uriel/articles/230245.htmlUrielUrielMon, 01 Jan 2024 13:13:00 GMThttp://www.shnenglu.com/Uriel/articles/230245.htmlhttp://www.shnenglu.com/Uriel/comments/230245.htmlhttp://www.shnenglu.com/Uriel/articles/230245.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230245.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230245.html
 1 #455
 2 #Runtime: 230 ms (Beats 12.76%)
 3 #Memory: 15 MB (Beats 6.38%)
 4 
 5 class Solution(object):
 6     def findContentChildren(self, g, s):
 7         """
 8         :type g: List[int]
 9         :type s: List[int]
10         :rtype: int
11         """
12         g.sort()
13         s.sort()
14         p1, p2 = 0, 0
15         while p1 < len(g) and p2 < len(s):
16             if g[p1] <= s[p2]:
17                 p1 += 1
18                 p2 += 1
19             else:
20                 p2 += 1
21         return p1


Uriel 2024-01-01 21:13 鍙戣〃璇勮
]]>
[LeetCode]1578. Minimum Time to Make Rope Colorful (Medium) Python-2023.12.27http://www.shnenglu.com/Uriel/articles/230242.htmlUrielUrielWed, 27 Dec 2023 09:38:00 GMThttp://www.shnenglu.com/Uriel/articles/230242.htmlhttp://www.shnenglu.com/Uriel/comments/230242.htmlhttp://www.shnenglu.com/Uriel/articles/230242.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230242.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230242.html

#1578
#
Runtime: 812 ms (Beats 62.22%)
#
Memory: 22.1 MB (Beats 88.89%)

class Solution(object):
    def minCost(self, colors, neededTime):
        """
        :type colors: str
        :type neededTime: List[int]
        :rtype: int
        
"""
        ans = 0
        for i in xrange(1, len(neededTime)):
            if colors[i] == colors[i - 1]:
                ans += min(neededTime[i], neededTime[i - 1])
                neededTime[i] = max(neededTime[i - 1], neededTime[i])
        return ans


Uriel 2023-12-27 17:38 鍙戣〃璇勮
]]>
[LeetCode]1903. Largest Odd Number in String (Easy) Python-2023.12.07http://www.shnenglu.com/Uriel/articles/230224.htmlUrielUrielFri, 08 Dec 2023 16:16:00 GMThttp://www.shnenglu.com/Uriel/articles/230224.htmlhttp://www.shnenglu.com/Uriel/comments/230224.htmlhttp://www.shnenglu.com/Uriel/articles/230224.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230224.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230224.html

 1 #1903
 2 #Runtime: 27 ms (Beats 98.59%)
 3 #Memory: 15.3 MB (Beats 81.69%)
 4 
 5 class Solution(object):
 6     def largestOddNumber(self, num):
 7         """
 8         :type num: str
 9         :rtype: str
10         """
11         p = -1
12         for i in xrange(len(num) - 1, -1, -1):
13             if (ord(num[i]) - ord('0')) % 2:
14                 p = i
15                 break
16         return num[0:p+1]
17                     


Uriel 2023-12-09 00:16 鍙戣〃璇勮
]]>
[LeetCode]1727. Largest Submatrix With Rearrangements (Medium) Python3-2023.12.01http://www.shnenglu.com/Uriel/articles/230209.htmlUrielUrielFri, 01 Dec 2023 13:36:00 GMThttp://www.shnenglu.com/Uriel/articles/230209.htmlhttp://www.shnenglu.com/Uriel/comments/230209.htmlhttp://www.shnenglu.com/Uriel/articles/230209.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230209.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230209.html
 1 #1727
 2 #Runtime: 1041 ms (Beats 11.18%)
 3 #Memory: 44.8 MB (Beats 45.71%)
 4 
 5 class Solution(object):
 6     def largestSubmatrix(self, matrix):
 7         """
 8         :type matrix: List[List[int]]
 9         :rtype: int
10         """
11         r, c = len(matrix), len(matrix[0])
12         ans = 0
13         for i in xrange(1, r):
14             for j in xrange(c):
15                 matrix[i][j] += matrix[i - 1][j] if matrix[i][j] else 0
16         for i in xrange(r):
17             matrix[i].sort(reverse=True)
18             for j in xrange(c):
19                 ans = max(ans, (j + 1) * matrix[i][j])
20         return ans


Uriel 2023-12-01 21:36 鍙戣〃璇勮
]]>
[LeetCode]1561. Maximum Number of Coins You Can Get (Medium) Python-2023.11.24http://www.shnenglu.com/Uriel/articles/230196.htmlUrielUrielFri, 24 Nov 2023 14:41:00 GMThttp://www.shnenglu.com/Uriel/articles/230196.htmlhttp://www.shnenglu.com/Uriel/comments/230196.htmlhttp://www.shnenglu.com/Uriel/articles/230196.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230196.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230196.html

 1 #1561
 2 #Runtime: 516 ms (Beats 18.92%)
 3 #Memory: 23.6 MB (Beats 7.21%)
 4 
 5 class Solution(object):
 6     def maxCoins(self, piles):
 7         """
 8         :type piles: List[int]
 9         :rtype: int
10         """
11         piles.sort()
12         q = deque(piles)
13         ans = 0
14         while q:
15             q.popleft()
16             q.pop()
17             ans += q[-1]
18             q.pop()
19         return ans


Uriel 2023-11-24 22:41 鍙戣〃璇勮
]]>
[LeetCode]1877. Minimize Maximum Pair Sum in Array (Medium) Python-2023.11.17http://www.shnenglu.com/Uriel/articles/230185.htmlUrielUrielFri, 17 Nov 2023 13:10:00 GMThttp://www.shnenglu.com/Uriel/articles/230185.htmlhttp://www.shnenglu.com/Uriel/comments/230185.htmlhttp://www.shnenglu.com/Uriel/articles/230185.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230185.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230185.html

 1 #1877
 2 #Runtime: 939 ms (Beats 68.83%)
 3 #Memory: 25.7 MB (Beats 14.29%)
 4 
 5 class Solution(object):
 6     def minPairSum(self, nums):
 7         """
 8         :type nums: List[int]
 9         :rtype: int
10         """
11         nums.sort()
12         ans = 0
13         n = len(nums)
14         for i in xrange(n//2):
15             ans = max(ans, nums[i] + nums[n - i - 1])
16         return ans


Uriel 2023-11-17 21:10 鍙戣〃璇勮
]]>
[LeetCode]1846. Maximum Element After Decreasing and Rearranging (Medium) Python-2023.11.15http://www.shnenglu.com/Uriel/articles/230180.htmlUrielUrielWed, 15 Nov 2023 14:01:00 GMThttp://www.shnenglu.com/Uriel/articles/230180.htmlhttp://www.shnenglu.com/Uriel/comments/230180.htmlhttp://www.shnenglu.com/Uriel/articles/230180.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230180.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230180.html

 1 #1846
 2 #Runtime: 359 ms (Beats 100%)
 3 #Memory: 21.7 MB (Beats 5.56%)
 4 
 5 class Solution(object):
 6     def maximumElementAfterDecrementingAndRearranging(self, arr):
 7         """
 8         :type arr: List[int]
 9         :rtype: int
10         """
11         arr.sort()
12         st = 1
13         for x in arr[1:]:
14             if x > st:
15                 st += 1
16         return st


Uriel 2023-11-15 22:01 鍙戣〃璇勮
]]>
[LeetCode]1921. Eliminate Maximum Number of Monsters (Medium) Python-2023.11.07http://www.shnenglu.com/Uriel/articles/230169.htmlUrielUrielTue, 07 Nov 2023 09:23:00 GMThttp://www.shnenglu.com/Uriel/articles/230169.htmlhttp://www.shnenglu.com/Uriel/comments/230169.htmlhttp://www.shnenglu.com/Uriel/articles/230169.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230169.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230169.html

#1921
#
Runtime: 690 ms (Beats 57.14%)
#
Memory: 35.4 MB (Beats 19.5%)

class Solution(object):
    def eliminateMaximum(self, dist, speed):
        """
        :type dist: List[int]
        :type speed: List[int]
        :rtype: int
        
"""
        mon = []
        for d, s in zip(dist, speed):
            mon.append(1.0 * d / s)
        mon.sort()
        ans = 0
        for i in xrange(len(mon)):
            if mon[i] > i:
                ans += 1
            else:
                break
        return ans


Uriel 2023-11-07 17:23 鍙戣〃璇勮
]]>
[LeetCode]2038. Remove Colored Pieces if Both Neighbors are the Same Color (Medium) Python-2023.10.02http://www.shnenglu.com/Uriel/articles/230125.htmlUrielUrielMon, 02 Oct 2023 09:49:00 GMThttp://www.shnenglu.com/Uriel/articles/230125.htmlhttp://www.shnenglu.com/Uriel/comments/230125.htmlhttp://www.shnenglu.com/Uriel/articles/230125.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230125.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230125.html姣忔鎶婁笁涓浉閭葷殑鏁頒腑闂翠竴涓幓鎺夛紝騫朵笉浼氬獎鍝嶅鎵嬪彲閫夋嫨鐨勬秷闄や綅緗紝浜庢槸鐩存帴緇熻涓変釜A鍜孊錛堣嫢鏈夊洓涓浉閭伙紝緇熻涓ゆ錛?br />

 1 #2038
 2 #Runtime: 166 ms (Beats 83.6%)
 3 #Memory: 14.8 MB (Beats 89.52%)
 4 
 5 class Solution(object):
 6     def winnerOfGame(self, colors):
 7         """
 8         :type colors: str
 9         :rtype: bool
10         """
11         a = 0
12         for i in xrange(1, len(colors) - 1):
13             if colors[i - 1] == colors[i] == colors[i + 1]:
14                 if colors[i] == 'A':
15                     a += 1
16                 else:
17                     a -= 1
18         return a > 0


Uriel 2023-10-02 17:49 鍙戣〃璇勮
]]>
[LeetCode]1647. Minimum Deletions to Make Character Frequencies Unique (Medium) Python-2023.09.12http://www.shnenglu.com/Uriel/articles/230079.htmlUrielUrielTue, 12 Sep 2023 09:17:00 GMThttp://www.shnenglu.com/Uriel/articles/230079.htmlhttp://www.shnenglu.com/Uriel/comments/230079.htmlhttp://www.shnenglu.com/Uriel/articles/230079.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230079.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230079.html

 1 #1647
 2 #Runtime: 274 ms (Beats 29.54%)
 3 #Memory: 14.2 MB (Beats 87.50%)
 4 
 5 class Solution(object):
 6     def minDeletions(self, s):
 7         """
 8         :type s: str
 9         :rtype: int
10         """
11         ans = 0
12         fq_set = set()
13         cnt = collections.Counter(s)
14         for ch, fq in cnt.items():
15             while fq  and fq in fq_set:
16                 fq -= 1
17                 ans += 1
18             fq_set.add(fq)
19         return ans


Uriel 2023-09-12 17:17 鍙戣〃璇勮
]]>
[LeetCode]2366. Minimum Replacements to Sort the Array (Hard) Python-2023.08.30http://www.shnenglu.com/Uriel/articles/230031.htmlUrielUrielWed, 30 Aug 2023 20:53:00 GMThttp://www.shnenglu.com/Uriel/articles/230031.htmlhttp://www.shnenglu.com/Uriel/comments/230031.htmlhttp://www.shnenglu.com/Uriel/articles/230031.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230031.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230031.html

 1 #2366
 2 #Runtime: 432 ms (Beats 100%) Sorry, there are not enough accepted submissions to show data
 3 #Memory: 25.8 MB (Beats 100%) Sorry, there are not enough accepted submissions to show data
 4 
 5 class Solution(object):
 6     def minimumReplacement(self, nums):
 7         """
 8         :type nums: List[int]
 9         :rtype: int
10         """
11         pre = nums[-1]
12         ans = 0
13         for i in range(len(nums) - 1, -1, -1):
14             d = nums[i] // pre
15             if nums[i] % pre != 0:
16                 d += 1
17             ans += d - 1
18             pre = nums[i] // d
19         return ans


Uriel 2023-08-31 04:53 鍙戣〃璇勮
]]>
[LeetCode]767. Reorganize String (Medium) Python-2023.08.23http://www.shnenglu.com/Uriel/articles/230023.htmlUrielUrielWed, 23 Aug 2023 08:31:00 GMThttp://www.shnenglu.com/Uriel/articles/230023.htmlhttp://www.shnenglu.com/Uriel/comments/230023.htmlhttp://www.shnenglu.com/Uriel/articles/230023.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230023.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230023.html璐績鎬濇兂錛岀敤priority queue灝嗗瓧絎︽寜鐓у嚭鐜版鏁頒粠澶氬埌灝戞帓搴忥紝鐒跺悗涓嬈″~鍏ョ粨鏋滃瓧絎︿覆錛屽鏋滃崟縐嶅瓧絎︽鏁拌秴榪囧瓧絎︿覆鎬婚暱搴︿竴鍗婏紝鍒欎笉鑳藉疄鐜般傚厛濉厖鎵鏈夊鏁頒綅錛岀劧鍚庡~鍋舵暟浣?br />

 1 #767
 2 #Runtime: 11 ms (Beats 98.89%)
 3 #Memory: 13.5 MB (Beats 11.11%)
 4 
 5 class Solution(object):
 6     def reorganizeString(self, s):
 7         """
 8         :type s: str
 9         :rtype: str
10         """
11         n = len(s)
12         freq = defaultdict(int)
13         for i in range(n):
14             freq[s[i]] += 1
15         hp = [(-v, k) for k, v in freq.items()]  
16         heapify(hp)
17         i = 0
18         ans = [""] * n
19         while hp:
20             v, k = heappop(hp)
21             if 2 * (-v) - 1 > n:
22                 return ""
23             for _ in range(-v):
24                 ans[i] = k
25                 if i + 2 < n:
26                     i += 2
27                 else:
28                     i = 1                
29         return "".join(ans)


Uriel 2023-08-23 16:31 鍙戣〃璇勮
]]>
[LeetCode]2616. Minimize the Maximum Difference of Pairs (Medium) Python-2023.08.09http://www.shnenglu.com/Uriel/articles/230009.htmlUrielUrielWed, 09 Aug 2023 13:53:00 GMThttp://www.shnenglu.com/Uriel/articles/230009.htmlhttp://www.shnenglu.com/Uriel/comments/230009.htmlhttp://www.shnenglu.com/Uriel/articles/230009.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/230009.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/230009.html

 1 #2616
 2 #Runtime: 831 ms (Beats 100%)
 3 #Memory: 25.4 MB (Beats 100%)
 4 
 5 class Solution(object):
 6     def minimizeMax(self, nums, p):
 7         """
 8         :type nums: List[int]
 9         :type p: int
10         :rtype: int
11         """
12         n = len(nums)
13         nums.sort()
14         l, r = 0, nums[-1] - nums[0]
15 
16         def cal(dif):
17             nt = 0
18             i = 1
19             while i < len(nums):
20                 if nums[i] - nums[i - 1] <= dif:
21                     i += 1
22                     nt += 1
23                 i += 1
24             return nt
25 
26         while l < r:
27             mid = (l + r) // 2
28             if cal(mid) >= p:
29                 r = mid
30             else:
31                 l = mid + 1
32         return l


Uriel 2023-08-09 21:53 鍙戣〃璇勮
]]>
[LeetCode]435. Non-overlapping Intervals (Medium) Python-2023.07.19http://www.shnenglu.com/Uriel/articles/229980.htmlUrielUrielWed, 19 Jul 2023 09:22:00 GMThttp://www.shnenglu.com/Uriel/articles/229980.htmlhttp://www.shnenglu.com/Uriel/comments/229980.htmlhttp://www.shnenglu.com/Uriel/articles/229980.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229980.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229980.html

 1 #435
 2 #Runtime: 1421 ms (Beats 25.19%)
 3 #Memory: 59.8 MB (Beats 44.83%)
 4 
 5 class Solution(object):
 6     def eraseOverlapIntervals(self, intervals):
 7         """
 8         :type intervals: List[List[int]]
 9         :rtype: int
10         """
11         intervals.sort()
12         ans = 0
13         pre = intervals[0][1]
14         for st, ed in intervals[1:]:
15             if st >= pre:
16                 pre = ed
17             else:
18                 ans += 1
19                 pre = min(ed, pre)
20         return ans


Uriel 2023-07-19 17:22 鍙戣〃璇勮
]]>
[LeetCode]2551. Put Marbles in Bags (Hard) Python-2023.07.08http://www.shnenglu.com/Uriel/articles/229966.htmlUrielUrielSat, 08 Jul 2023 20:02:00 GMThttp://www.shnenglu.com/Uriel/articles/229966.htmlhttp://www.shnenglu.com/Uriel/comments/229966.htmlhttp://www.shnenglu.com/Uriel/articles/229966.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229966.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229966.html鎬濊礬鍙傝?-> https://leetcode.com/problems/put-marbles-in-bags/solutions/3734284/sort-video-solution/


 1 #2551
 2 #Runtime: 758 ms (Beats 16.67%)
 3 #Memory: 25.1 MB (Beats 16.67%)
 4 
 5 class Solution(object):
 6     def putMarbles(self, weights, k):
 7         """
 8         :type weights: List[int]
 9         :type k: int
10         :rtype: int
11         """
12         n = len(weights)
13         p = [0] * (n - 1)
14         for i in range(1, n):
15             p[i - 1] = weights[i] + weights[i - 1]
16         p.sort()
17         minx, maxx = 0, 0
18         for i in range(k - 1):
19             minx += p[i]
20             maxx += p[n - i - 2]
21         return maxx - minx


Uriel 2023-07-09 04:02 鍙戣〃璇勮
]]>
[LeetCode]2439. Minimize Maximum of Array (Medium) Python-2023.04.05http://www.shnenglu.com/Uriel/articles/229804.htmlUrielUrielWed, 05 Apr 2023 09:28:00 GMThttp://www.shnenglu.com/Uriel/articles/229804.htmlhttp://www.shnenglu.com/Uriel/comments/229804.htmlhttp://www.shnenglu.com/Uriel/articles/229804.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229804.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229804.html榪欎釜鎿嶄綔涓嶆敼鍙樻暟鍒楃殑騫沖潎鍊鹼紝璁╂渶澶у兼渶灝忓氨鏄鏁板垪灝藉彲鑳藉鉤鍧囷紝浜庢槸姹傛暟鍒楁墍鏈夌殑鍓嶇紑鐨勫鉤鍧囧奸噷闈㈡渶澶х殑鍗沖彲

 1 #2439
 2 #Runtime: 637 ms (Beats 60%)
 3 #Memory: 23.3 MB (Beats 80%)
 4 
 5 class Solution(object):
 6     def minimizeArrayValue(self, nums):
 7         """
 8         :type nums: List[int]
 9         :rtype: int
10         """
11         ans, cnt = 0, 0
12         for i in range(len(nums)):
13             cnt += nums[i]
14             ans = max(ans, ceil(1.0 * cnt / (i + 1)))
15         return int(ans)


Uriel 2023-04-05 17:28 鍙戣〃璇勮
]]>
[LeetCode]1402. Reducing Dishes (Hard) Python-2023.03.29http://www.shnenglu.com/Uriel/articles/229789.htmlUrielUrielWed, 29 Mar 2023 10:04:00 GMThttp://www.shnenglu.com/Uriel/articles/229789.htmlhttp://www.shnenglu.com/Uriel/comments/229789.htmlhttp://www.shnenglu.com/Uriel/articles/229789.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229789.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229789.html瀹為檯鍋氱殑鏃跺欏皢鑿滃搧鎸夋弧鎰忓害浠庡ぇ鍒板皬鎺掑簭錛屼笉鏂瘡鍔犳弧鎰忓害鍊鹼紝濡傛灉鍒版煇涓鏃跺埢绱姞鍊間負璐燂紝鍚庨潰鐨勮彍閮戒笉鐢ㄨ冭檻浜?br />

 1 #1402
 2 #Runtime: 16 ms (Beats 100%)
 3 #Memory: 13.5 MB (Beats 70.27%)
 4 
 5 class Solution(object):
 6     def maxSatisfaction(self, satisfaction):
 7         """
 8         :type satisfaction: List[int]
 9         :rtype: int
10         """
11         satisfaction.sort(reverse=True)
12         ans, tp = 0, 0
13         for i in range(len(satisfaction)):
14             tp += satisfaction[i]
15             if tp < 0:
16                 break
17             ans += tp
18         return ans






Uriel 2023-03-29 18:04 鍙戣〃璇勮
]]>
[LeetCode]605. Can Place Flowers (Easy) Python-2023.03.20http://www.shnenglu.com/Uriel/articles/229763.htmlUrielUrielMon, 20 Mar 2023 07:38:00 GMThttp://www.shnenglu.com/Uriel/articles/229763.htmlhttp://www.shnenglu.com/Uriel/comments/229763.htmlhttp://www.shnenglu.com/Uriel/articles/229763.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229763.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229763.html

 1 #605
 2 #Runtime: 119 ms (Beats 83.80%)
 3 #Memory: 14.3 MB (Beats 19.44%)
 4 
 5 class Solution(object):
 6     def canPlaceFlowers(self, flowerbed, n):
 7         """
 8         :type flowerbed: List[int]
 9         :type n: int
10         :rtype: bool
11         """
12         x = 0
13         for i in range(len(flowerbed)):
14             if flowerbed[i] == 0:
15                 if i >= 1:
16                     if flowerbed[i - 1] == 1:
17                         continue
18                 if i < len(flowerbed) - 1:
19                     if flowerbed[i + 1] == 1:
20                         continue
21                 flowerbed[i] = 1
22                 x += 1
23         return x >= n


Uriel 2023-03-20 15:38 鍙戣〃璇勮
]]>
[LeetCode]1675.聽Minimize Deviation in Array (Hard) Python-2023.02.25http://www.shnenglu.com/Uriel/articles/229701.htmlUrielUrielSat, 25 Feb 2023 12:31:00 GMThttp://www.shnenglu.com/Uriel/articles/229701.htmlhttp://www.shnenglu.com/Uriel/comments/229701.htmlhttp://www.shnenglu.com/Uriel/articles/229701.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229701.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229701.html緇欏嚭涓鍒楁暟錛屽浜庡鏁幫紝鍙互*2錛屽浜庡伓鏁幫紝鍙互/2錛岄棶鎵ц浠繪剰澶氭鎿嶄綔涔嬪悗錛岃繖鍒楁暟鐨勬渶澶ф渶灝忓間箣宸渶灝忔槸澶氬皯
鍙傝冧簡錛歨ttps://leetcode.com/problems/minimize-deviation-in-array/solutions/3223578/priority-queue-video-java-c-python/
鍒╃敤python鐨刪eapq


 1 #1675
 2 #Runtime: 5340 ms (Beats 100%)
 3 #Memory: 19.7 MB (Beats 100%)
 4 (Sorry, there are not enough accepted submissions to show data)
 5 
 6 class Solution(object):
 7     def minimumDeviation(self, nums):
 8         """
 9         :type nums: List[int]
10         :rtype: int
11         """
12         q = []
13         q_min = 2000000000
14         for i in nums:
15             if i % 2:
16                 i *= 2
17             heapq.heappush(q, -i)
18             q_min = min(q_min, i)
19 
20         ans = -q[0] - q_min
21         while q[0] % 2 == 0:
22             t = -heapq.heappop(q)
23             heapq.heappush(q, -(t // 2))
24             ans = min(ans, t - q_min)
25             q_min = min(q_min, t // 2)
26         return min(ans, -q[0] - q_min)


Uriel 2023-02-25 20:31 鍙戣〃璇勮
]]>
[LeetCode]502. IPO (Hard) Python-2023.02.23http://www.shnenglu.com/Uriel/articles/229697.htmlUrielUrielThu, 23 Feb 2023 16:07:00 GMThttp://www.shnenglu.com/Uriel/articles/229697.htmlhttp://www.shnenglu.com/Uriel/comments/229697.htmlhttp://www.shnenglu.com/Uriel/articles/229697.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229697.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229697.html鍒╃敤heap錛坥r浼樺厛闃熷垪錛夌殑璐績
鍏堟寜鐓apacity浠庡皬鍒板ぇ瀵筽rojects鎺掑簭錛岀劧鍚庝粠絎竴涓猵roject寮濮嬶紝濡傛灉capacity灝忎簬褰撳墠鐨剋錛屽垯鍔犲叆heap錛岀洿鍒癱apacity鐨勮姹傚ぇ浜庡綋鍓峸錛岀劧鍚庡幓瀹屾垚heap欏剁鐨刾roject


 1 #502
 2 #Runtime: 1335 ms (Beats 51.90%)
 3 #Memory: 37.9 MB (Beats 69.62%)
 4 
 5 class Solution(object):
 6     def findMaximizedCapital(self, k, w, profits, capital):
 7         """
 8         :type k: int
 9         :type w: int
10         :type profits: List[int]
11         :type capital: List[int]
12         :rtype: int
13         """
14         n = len(profits)
15         proj = list(zip(capital, profits))
16         proj.sort()
17         j = 0
18         q = []
19         for i in range(k):
20             while j < n and proj[j][0] <= w:
21                 heappush(q, -proj[j][1])
22                 j += 1
23             if not q:
24                 break
25             w += -heappop(q)
26         return w


Uriel 2023-02-24 00:07 鍙戣〃璇勮
]]>
[LeetCode]926. Flip String to Monotone Increasing (Medium) Python-2023.01.17http://www.shnenglu.com/Uriel/articles/229631.htmlUrielUrielTue, 17 Jan 2023 12:52:00 GMThttp://www.shnenglu.com/Uriel/articles/229631.htmlhttp://www.shnenglu.com/Uriel/comments/229631.htmlhttp://www.shnenglu.com/Uriel/articles/229631.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229631.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229631.html鍙傝冧簡Discussion鐨勫閥濡欐濊礬->https://leetcode.com/problems/flip-string-to-monotone-increasing/solutions/3061225/python-3-7-lines-w-explanation-and-example-t-m-100-96/

 1 #926
 2 #Runtime: 92 ms (Beats 95.45%)
 3 #Memory: 14.6 MB (Beats 95.45%)
 4 
 5 class Solution(object):
 6     def minFlipsMonoIncr(self, s):
 7         """
 8         :type s: str
 9         :rtype: int
10         """
11         t, ans = 0, 0
12         for i in s:
13             if i == '1':
14                 t += 1
15             elif t:
16                 ans += 1
17                 t -= 1
18         return ans
19 


Uriel 2023-01-17 20:52 鍙戣〃璇勮
]]>
[LeetCode]134. Gas Station (Medium) C++/Python-2014.01.09/2023.01.07http://www.shnenglu.com/Uriel/articles/229615.htmlUrielUrielSat, 07 Jan 2023 10:01:00 GMThttp://www.shnenglu.com/Uriel/articles/229615.htmlhttp://www.shnenglu.com/Uriel/comments/229615.htmlhttp://www.shnenglu.com/Uriel/articles/229615.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229615.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229615.html
浠庣涓涓姞娌圭珯寮濮嬶紝鍋囪浠庤绔欏嚭鍙戯紝鐒跺悗渚濇閬嶅巻錛屽鏋滃彂鐜板埌杈炬煇涓姞娌圭珯鏃跺簱瀛樻補閲忓姞涓婅绔欏姞鍏ョ殑娌歸噺鏃犳硶鍒拌揪涓嬩竴绔欙紝鍒欓噸緗紝浠庝笅涓绔欏嚭鍙戯紝鍦ㄩ亶鍘嗘椂绱姞鎵鏈夌珯鐨刧as[i]-cost[i]錛屽鏋滄誨拰灝忎簬0錛屽垯杈撳嚭-1

C++鐗?br />
 1 //134
 2 //Runtime: 52 ms (Beats 100%)
 3 
 4 class Solution {
 5 public:
 6     int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
 7         int sum = 0, cnt = 0, st = 0;
 8         for(int i = 0; i < gas.size(); ++i) {
 9             sum += gas[i] - cost[i];
10             cnt += gas[i] - cost[i];
11             if(sum < 0) {
12                 sum = 0;
13                 st = (i + 1) % gas.size();
14             }
15         }
16         if(cnt < 0) return -1;
17         return st;
18     }
19 };

Python鐗?br />
 1 #134
 2 #Runtime: 551 ms (Beats 85.35%)
 3 #Memory: 20.2 MB (Beats 26.28%)
 4 
 5 class Solution(object):
 6     def canCompleteCircuit(self, gas, cost):
 7         """
 8         :type gas: List[int]
 9         :type cost: List[int]
10         :rtype: int
11         """
12         cur_gas = 0
13         tol_gas = 0
14         ans = 0
15         for i in range(len(gas)):
16             tol_gas += gas[i] - cost[i]
17             cur_gas = cur_gas + gas[i] - cost[i]
18             if cur_gas < 0:
19                 cur_gas = 0
20                 ans = (i + 1) % len(gas)
21         if tol_gas < 0:
22             return -1
23         return ans


Uriel 2023-01-07 18:01 鍙戣〃璇勮
]]>
[LeetCode]1833. Maximum Ice Cream Bars (Medium) Python-2023.01.06http://www.shnenglu.com/Uriel/articles/229613.htmlUrielUrielFri, 06 Jan 2023 12:31:00 GMThttp://www.shnenglu.com/Uriel/articles/229613.htmlhttp://www.shnenglu.com/Uriel/comments/229613.htmlhttp://www.shnenglu.com/Uriel/articles/229613.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229613.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229613.html
 1 #1833
 2 
 3 class Solution(object):
 4     def maxIceCream(self, costs, coins):
 5         """
 6         :type costs: List[int]
 7         :type coins: int
 8         :rtype: int
 9         """
10         costs.sort()
11         ans = 0
12         for i in costs:
13             coins -= i
14             if coins < 0:
15                 break
16             ans += 1
17         return ans


Uriel 2023-01-06 20:31 鍙戣〃璇勮
]]>
[LeetCode]452. Minimum Number of Arrows to Burst Balloons (Medium) Python-2023.01.05http://www.shnenglu.com/Uriel/articles/229610.htmlUrielUrielThu, 05 Jan 2023 07:49:00 GMThttp://www.shnenglu.com/Uriel/articles/229610.htmlhttp://www.shnenglu.com/Uriel/comments/229610.htmlhttp://www.shnenglu.com/Uriel/articles/229610.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229610.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229610.html
鍏堝榪欎釜浜岀淮point list鎸夌浜岀淮鐨勫間粠灝忓埌澶ф帓搴忥紝鐒跺悗浠庣涓鏍圭嚎孌靛紑濮嬶紝灝勫嚮鏈鍙崇鐨勭偣錛屼箣鍚庣殑綰挎鑻ュ彲浠over榪欎釜鐐癸紝鍒欎竴騫惰娑堝幓

 1 #452
 2 #Runtime: 2320 ms (Beats 46.75%)
 3 #Memory: 62.9 MB (Beats 34.32%)
 4 
 5 class Solution(object):
 6     def findMinArrowShots(self, points):
 7         """
 8         :type points: List[List[int]]
 9         :rtype: int
10         """
11         pt_sorted = sorted(points, key = (lambda x:[x[1], x[0]]))
12         ans = 0
13         i = 0
14         while i < len(pt_sorted):
15             p = pt_sorted[i][1]
16             while i < len(pt_sorted) and pt_sorted[i][0] <= p <= pt_sorted[i][1]:
17                 i += 1
18             ans += 1
19         return ans


Uriel 2023-01-05 15:49 鍙戣〃璇勮
]]>
[LeetCode]944. Delete Columns to Make Sorted (Medium) Python-2023.01.04http://www.shnenglu.com/Uriel/articles/229608.htmlUrielUrielWed, 04 Jan 2023 10:19:00 GMThttp://www.shnenglu.com/Uriel/articles/229608.htmlhttp://www.shnenglu.com/Uriel/comments/229608.htmlhttp://www.shnenglu.com/Uriel/articles/229608.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229608.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229608.html
鍏堢粺璁℃瘡涓縐嶉毦搴﹀肩殑鍑虹幇嬈℃暟錛屽垎浠ヤ笅鍑犵鎯呭喌
1. 鍙嚭鐜頒竴嬈★紝閭d箞榪欎釜task鏃犳硶琚崟鐙畬鎴愶紝鐩存帴return -1
2. 鍑虹幇mod 3=0嬈★紝閭d箞姣忔瀹屾垚3涓綾籺ask
3. 鍑虹幇mod 3=1嬈★紝閭d箞姣忔瀹屾垚3涓綾籺ask錛屾渶鍚庡墿4涓綾籺ask姣忔瀹屾垚2涓?br />4. 鍑虹幇mod 3=2嬈★紝閭d箞姣忔瀹屾垚3涓綾籺ask錛屾渶鍚庡墿2涓綾籺ask涓嬈″畬鎴?br />
 1 #944
 2 #Runtime: 2137 ms (Beats 43.75%)
 3 #Memory: 26.8 MB (Beats 39.58%)
 4 
 5 class Solution(object):
 6     def minimumRounds(self, tasks):
 7         """
 8         :type tasks: List[int]
 9         :rtype: int
10         """
11         cnt = Counter(tasks)
12         ans = 0
13         for i in cnt:
14             if cnt[i] == 1:
15                 return -1
16             if cnt[i] % 3 == 0:
17                 ans += cnt[i] / 3
18             elif cnt[i] % 3 == 1:
19                 ans += 2 + (cnt[i] - 4) / 3
20             elif cnt[i] % 3 == 2:
21                 ans += 1 + (cnt[i] - 2) / 3
22         return ans


Uriel 2023-01-04 18:19 鍙戣〃璇勮
]]>
[LeetCode]1962. Remove Stones to Minimize the Total (Medium) Python-2022.12.28http://www.shnenglu.com/Uriel/articles/229593.htmlUrielUrielWed, 28 Dec 2022 06:28:00 GMThttp://www.shnenglu.com/Uriel/articles/229593.htmlhttp://www.shnenglu.com/Uriel/comments/229593.htmlhttp://www.shnenglu.com/Uriel/articles/229593.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229593.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229593.html緇欏嚭涓鍒楁暟錛屼竴鍏卞彲浠ュ疄鏂絢涓搷浣滐紝姣忔鍙互灝嗗叾涓竴涓暟鍑忓幓floor(piles[i] / 2)錛岄棶k嬈℃搷浣滀箣鍚庡墿涓嬬殑鏁板瓧鍜屾渶灝忔槸澶氬皯
鎬濊礬錛氱敤浼樺厛闃熷垪淇濆瓨緇忚繃x嬈℃搷浣滀箣鍚庣殑鏁板瓧錛岀敤python鐨刪eapq鍐欒搗鏉ュ氨寰堟柟渚匡紝姣忔鎿嶄綔鐩存帴鐢╤eapreplace鎶奾eap欏剁殑鏁板噺鍗婃浛浠e師鍊煎嵆鍙紙姣斿厛pop錛屾敼鍊間箣鍚庡啀push瑕佸揩錛宐eat 100%錛?br />鍥犱負heapq鏄渶灝忓爢錛屾墍浠ュ緩鍫嗘椂鍏堝皢鎵鏈夋暟鍙栬礋

 1 #2279
 2 #Runtime: 3519 ms (Beats 100%)
 3 #Memory: 26 MB (Beats 37.21%)
 4 
 5 class Solution(object):
 6     def minStoneSum(self, piles, k):
 7         """
 8         :type piles: List[int]
 9         :type k: int
10         :rtype: int
11         """
12         hq = [-x for x in piles]
13         heapify(hq)
14         for _ in range(k):
15             heapreplace(hq, hq[0] // 2)
16         return -sum(hq)


Uriel 2022-12-28 14:28 鍙戣〃璇勮
]]>
[LeetCode]2279. Maximum Bags With Full Capacity of Rocks (Medium) Python-2022.12.27http://www.shnenglu.com/Uriel/articles/229592.htmlUrielUrielTue, 27 Dec 2022 05:42:00 GMThttp://www.shnenglu.com/Uriel/articles/229592.htmlhttp://www.shnenglu.com/Uriel/comments/229592.htmlhttp://www.shnenglu.com/Uriel/articles/229592.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229592.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229592.html
璁$畻姣忎釜鍖呭墿浣欏閲忥紝騫朵笖浠庡皬鍒板ぇ鎺掑簭錛屼竴涓釜瑁呯洿鍒板寘鍏ㄩ儴瑁呮弧鎴栬卆dditionalRocks鐢ㄥ畬鍗沖彲

 1 #2279
 2 #Runtime: 836 ms (Beats 86.21%)
 3 #Memory: 20.2 MB (Beats 93.10%)
 4 
 5 class Solution(object):
 6     def maximumBags(self, capacity, rocks, additionalRocks):
 7         """
 8         :type capacity: List[int]
 9         :type rocks: List[int]
10         :type additionalRocks: int
11         :rtype: int
12         """
13         for i in range(len(capacity)):
14             capacity[i] = max(capacity[i] - rocks[i], 0)
15         capacity.sort()
16         for i in range(len(capacity)):
17             additionalRocks -= capacity[i]
18             if additionalRocks < 0:
19                 i -= 1
20                 break
21         return i + 1


Uriel 2022-12-27 13:42 鍙戣〃璇勮
]]>
[LeetCode]2131. Longest Palindrome by Concatenating Two Letter Words (Medium) Python-2022.11.03http://www.shnenglu.com/Uriel/articles/229477.htmlUrielUrielThu, 03 Nov 2022 06:43:00 GMThttp://www.shnenglu.com/Uriel/articles/229477.htmlhttp://www.shnenglu.com/Uriel/comments/229477.htmlhttp://www.shnenglu.com/Uriel/articles/229477.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229477.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229477.html鐢╬ython瀛楀吀緇熻姣忎竴縐嶉暱搴?鐨勫瓧絎︿覆i鍑虹幇鐨勬鏁癲ict_word[i]

灝嗛暱搴︿負2鐨勫瓧絎︿覆鍒嗕負涓ょ被錛?br />絎竴綾伙細ab鍨嬶紝鍗充袱涓瓧姣嶄笉鍚岋紝閭d箞闇瑕佹壘瀵瑰簲鐨刡a瀛楃涓?br />
ans = ans + 4 * min(dict_word[‘ab’], dict_word[‘ba’])

絎竴綾伙細aa鍨嬶紝鍗充袱涓瓧姣嶄笉鍚岋紝濡傛灉dict_word[‘aa’]鍑虹幇濂囨暟嬈★紝閭d箞澶氬嚭鏉ョ殑閭d竴涓彧鑳芥斁涓棿錛堝鏋滄湁涓嶆涓涓猘a鍨嬪瓧絎︿覆錛屼笖鍑虹幇瀛楁暟涓嶆涓嬈″嚭鐜板鏁幫紝閭f涓棿浣嶇疆涔熷彧鑳芥斁涓涓?div>ans = ans + 4 * (dict_word[i] // 2) + double_chr * 2 錛坉ouble_chr璁板綍鏄惁鍑虹幇榪囧鏁版鐨刟a鍨嬪瓧絎︿覆錛?br />
 1 #2131
 2 #Runtime: 1376 ms
 3 #Memory Usage: 37.8 MB
 4 
 5 class Solution(object):
 6     def longestPalindrome(self, words):
 7         """
 8         :type words: List[str]
 9         :rtype: int
10         """
11         dict_word = {}
12         for i in words:
13             if i in dict_word:
14                 dict_word[i] += 1
15             else:
16                 dict_word[i] = 1
17         ans = 0
18         double_chr = 0
19         for i in dict_word:
20             j = i[::-1]
21             if i == j:
22                 if dict_word[i] % 2:
23                     double_chr = 1
24                 ans += 4 * (dict_word[i] // 2)
25             else:
26                 if j in dict_word:
27                     ans += 4 * min(dict_word[i], dict_word[j])
28                     dict_word[j] = 0
29         return ans + double_chr * 2


Uriel 2022-11-03 14:43 鍙戣〃璇勮
]]>
[LeetCode]11. Container With Most Water (Medium) Python-2022.10.31http://www.shnenglu.com/Uriel/articles/229470.htmlUrielUrielMon, 31 Oct 2022 10:30:00 GMThttp://www.shnenglu.com/Uriel/articles/229470.htmlhttp://www.shnenglu.com/Uriel/comments/229470.htmlhttp://www.shnenglu.com/Uriel/articles/229470.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229470.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229470.html璐績錛屽乏鍙充袱涓父鏍囧垎鍒悜涓棿縐誨姩錛屾瘡嬈$Щ鍔ㄦ《楂樺皬鐨勬父鏍囷紝鏇存柊鏈澶ф按閲?br />
 1 #11
 2 #Runtime: 1703 ms
 3 #Memory Usage: 23.9 MB
 4 
 5 class Solution(object):
 6     def maxArea(self, height):
 7         """
 8         :type height: List[int]
 9         :rtype: int
10         """
11         p1 = 1
12         p2 = len(height)
13         ans = min(height[0], height[-1]) * (p2 - p1)
14         while p1 < p2 - 1:
15             if height[p1 - 1] < height[p2 - 1]:
16                 p1 += 1
17             else:
18                 p2 -= 1
19             ans = max(ans, min(height[p1 - 1], height[p2 - 1]) * (p2 - p1))
20         return ans


Uriel 2022-10-31 18:30 鍙戣〃璇勮
]]>
[LeetCode]121-123, 188. Best Time to Buy and Sell Stock I~IV (Easy-Medium-Hard) Python-2022.10.21http://www.shnenglu.com/Uriel/articles/229450.htmlUrielUrielFri, 21 Oct 2022 23:24:00 GMThttp://www.shnenglu.com/Uriel/articles/229450.htmlhttp://www.shnenglu.com/Uriel/comments/229450.htmlhttp://www.shnenglu.com/Uriel/articles/229450.html#Feedback0http://www.shnenglu.com/Uriel/comments/commentRss/229450.htmlhttp://www.shnenglu.com/Uriel/services/trackbacks/229450.html
121. Best Time to Buy and Sell Stock (Easy)
緇欎竴鍒楄偂紲ㄤ環鏍鹼紝闂拱鍗栦笉瓚呰繃1嬈$殑鎯呭喌涓嬫渶澶氳禋澶氬皯
122. Best Time to Buy and Sell Stock III (Medium)
緇欎竴鍒楄偂紲ㄤ環鏍鹼紝闂棤闄愭涔板崠鐨勬儏鍐典笅鏈澶氳禋澶氬皯錛堜絾鎵嬮噷鍚屼竴鏃跺埢鍙兘鎸佹湁涓浠借偂紲級
123. Best Time to Buy and Sell Stock III (Hard)
緇欎竴鍒楄偂紲ㄤ環鏍鹼紝闂拱鍗栦笉瓚呰繃2嬈$殑鎯呭喌涓嬫渶澶氳禋澶氬皯
188. Best Time to Buy and Sell Stock IV (Hard)
緇欎竴鍒楄偂紲ㄤ環鏍鹼紝闂拱鍗栦笉瓚呰繃k嬈$殑鎯呭喌涓嬫渶澶氳禋澶氬皯
121, 123鍜?88綾諱技錛岄兘鏄疍P鎬濇兂錛宺emain[j]錛氬凡緇忎拱鍏嬈′箣鍚庢渶澶氭墜澶村墿涓嬬殑閽憋紝profit[j]錛氬凡緇忓崠鍑簀嬈′箣鍚庢渶澶氳禋浜嗗灝戯紝娉ㄦ剰鍗栧嚭鍓嶅繀欏昏鍏堜拱鍏ワ紝188娉ㄦ剰鐗瑰垽len(prices)=1鐨勬儏鍐?/div>
122璐績錛岄澶勭悊i鍜宨+1澶╀箣闂寸殑浠峰樊錛屾湁鐨勮禋灝遍兘鍔犱笂

 1 #121
 2 #Runtime: 869 ms
 3 #Memory Usage: 22.5 MB
 4 
 5 class Solution(object):
 6     def maxProfit(self, prices):
 7         """
 8         :type prices: List[int]
 9         :rtype: int
10         """
11         remain = 0
12         profit = 0
13         remain = prices[0]
14         for i in range(1, len(prices)):               
15             remain = min(remain, prices[i])
16             profit = max(profit, prices[i] - remain)
17         return profit


 1 #122
 2 #Runtime: 48 ms
 3 #Memory Usage: 14.8 MB
 4 
 5 class Solution(object):
 6     def maxProfit(self, prices):
 7         """
 8         :type prices: List[int]
 9         :rtype: int
10         """
11         profit = []
12         for i in range(len(prices) - 1):
13             profit.append(prices[i + 1] - prices[i])
14         ans = 0
15         for i in profit:
16             if i > 0:
17                 ans += i
18         return ans
19                 


 1 #123
 2 #Runtime: 1075 ms
 3 #Memory Usage: 25 MB
 4 
 5 class Solution(object):
 6     def maxProfit(self, prices):
 7         """
 8         :type prices: List[int]
 9         :rtype: int
10         """
11         remain = [0, -sys.maxint, -sys.maxint]
12         profit = [0, -sys.maxint, -sys.maxint]
13         
14         for i in range(0, len(prices)):
15             for j in range(1, 3):
16                 remain[j] = max(remain[j], profit[j - 1] - prices[i])
17                 profit[j] = max(profit[j], remain[j] + prices[i])
18         return max(profit)


 1 #188
 2 #Runtime: 250 ms
 3 #Memory Usage: 13.3 MB
 4 
 5 class Solution(object):
 6     def maxProfit(self, k, prices):
 7         """
 8         :type k: int
 9         :type prices: List[int]
10         :rtype: int
11         """
12         remain = [-sys.maxint] * (len(prices) + 1)
13         profit = [-sys.maxint] * (len(prices) + 1)
14         remain[0] = 0
15         profit[0] = 0
16         for i in range(0, len(prices)):
17             for j in range(1, min(len(prices) + 1, k + 1)):
18                 remain[j] = max(remain[j], profit[j - 1] - prices[i])
19                 profit[j] = max(profit[j], remain[j] + prices[i])
20         return max(profit)












Uriel 2022-10-22 07:24 鍙戣〃璇勮
]]> 精品久久久无码21p发布| 女同久久| 天天爽天天狠久久久综合麻豆| 久久久精品人妻无码专区不卡| 国产成人精品久久亚洲高清不卡| 91精品国产91热久久久久福利| 99久久精品国产麻豆| 国产精品一久久香蕉国产线看| 久久久久无码精品国产| 久久w5ww成w人免费| 国产精品久久毛片完整版| 97r久久精品国产99国产精| 久久伊人精品青青草原高清| 久久久综合九色合综国产| 久久久久99精品成人片| 久久国产欧美日韩精品免费| 亚洲日本va中文字幕久久| 精品免费久久久久久久| 国产日韩久久久精品影院首页| 久久91精品综合国产首页| 亚洲精品第一综合99久久| 久久久精品人妻一区二区三区蜜桃 | 72种姿势欧美久久久久大黄蕉| WWW婷婷AV久久久影片| 久久艹国产| 亚洲va国产va天堂va久久| 99久久成人国产精品免费| 欧美日韩中文字幕久久久不卡| 久久亚洲美女精品国产精品| 国产三级精品久久| 国内精品九九久久精品| 国产成人无码精品久久久久免费 | 色综合久久综合网观看| 中文字幕无码久久精品青草| 国内精品久久久久影院日本| 久久亚洲精品无码播放| 国产精品禁18久久久夂久| 中文成人无码精品久久久不卡 | 国内精品久久久久久久久| 无码日韩人妻精品久久蜜桃| 国产午夜精品理论片久久|