Skip to content

Commit d29438b

Browse files
committed
new
1 parent 1763d35 commit d29438b

14 files changed

+566
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# You are given an array prices where prices[i] is the price of a given stock on the ith day.
2+
3+
# You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
4+
5+
# Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
6+
7+
8+
9+
# Example 1:
10+
11+
# Input: prices = [7,1,5,3,6,4]
12+
# 1 10 9 8 2 0 18
13+
# Output: 5
14+
# Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
15+
# Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
16+
class Solution:
17+
def maxProfit(self, prices: List[int]) -> int:
18+
current = prices[0]
19+
profit = 0
20+
for i in prices[1:]:
21+
if current > i:
22+
current = i
23+
profit = max(profit, i - current)
24+
return profit
25+
26+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
2+
3+
# On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
4+
5+
# Find and return the maximum profit you can achieve.
6+
7+
8+
9+
# Example 1:
10+
11+
# Input: prices = [7,1,5,3,6,4]
12+
# Output: 7
13+
# Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
14+
# Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
15+
# Total profit is 4 + 3 = 7.
16+
class Solution:
17+
def maxProfit(self, prices):
18+
totalProfit = 0
19+
profit = 0
20+
current = prices[0]
21+
last = current
22+
for i in prices[1:]:
23+
if current > i or last > i:
24+
current = i
25+
totalProfit += profit
26+
profit = 0
27+
28+
profit = max(profit, i - current)
29+
last = i
30+
totalProfit += profit
31+
return totalProfit
32+
33+
sol = Solution()
34+
res = sol.maxProfit([1,2,3,4,5,6])
35+
print(res)
36+

134.GasStation.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution(object):
2+
def canCompleteCircuit(self, gas, cost):
3+
'''
4+
gas = [1,2,3,4,5], cost = [3,4,5,1,2]
5+
if you start from a, and stuck at b, then you can't start from any station between a and b.
6+
也就是说排除法,走过的路如果不通,那么中间的任何一条路都不通
7+
'''
8+
# start from 0, find first stuck station
9+
start = 0
10+
fly = 0
11+
while True:
12+
# find next stuck point
13+
i = start
14+
current = gas[i]
15+
flag = 0
16+
init = True
17+
while init or i != start:
18+
init = False
19+
current = current - cost[i]
20+
if current < 0:
21+
# stuck , break
22+
if start + 1 >= len(gas):
23+
return -1
24+
start = (i+1) % len(gas)
25+
flag = 1
26+
break
27+
i = (i+1) % len(gas)
28+
if i + 1 >= len(gas):
29+
fly = 1
30+
current = current + gas[i]
31+
32+
if flag == 0:
33+
# find answer
34+
return start
35+
if flag == 1 and fly == 1:
36+
return -1
37+
38+
s = Solution()
39+
res = s.canCompleteCircuit(gas = [4,5,2,6,5,3], cost = [3,2,7,3,2,9])
40+
print(res)

15.3sum.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution(object):
2+
def threeSum(self, nums):
3+
'''
4+
nums: List[int]
5+
rtype: List[List[int]]
6+
Input: nums = [-1,0,1,2,-1,-4]
7+
Output: [[-1,-1,2],[-1,0,1]]
8+
在一个数组中,找到三个数,相加等于0。穷尽所有组合。
9+
如果使用暴力遍历的方式,那么需要遍历数组三次。复杂度太高。
10+
1. 首先对数组进行排序
11+
2. 然后从两端开始进行筛选,但是如何对两端进行移动呢,无法解决这一问题
12+
3. 同时移动,还是只移动一端,如何判断移动哪一端?
13+
14+
解答:
15+
1. 固定一端,然后对其他两端进行twosum
16+
17+
该算法用途:
18+
用于配对问题上,例如那几个物体结合起来能达到特定效果,这和硬币DP问题有什么区别?
19+
硬币问题是硬币个数无限以及结合的数量并非固定
20+
'''
21+
res = []
22+
nums2 = sorted(nums)
23+
24+
for i in range(len(nums2) - 2):
25+
if i > 0 and nums2[i] == nums2[i-1]:
26+
continue
27+
if nums2[i] > 0:
28+
continue
29+
j = i + 1
30+
k = len(nums2) - 1
31+
total = 0 - nums2[i]
32+
while j < k:
33+
tmp = nums2[j] + nums2[k]
34+
if tmp < total:
35+
j += 1
36+
elif tmp > total:
37+
k -= 1
38+
else:
39+
res.append([nums2[i], nums2[j], nums2[k]])
40+
j += 1
41+
while nums2[j] == nums2[j-1] and j < k:
42+
j += 1
43+
return res
44+
45+
46+
47+
s = Solution()
48+
res = s.threeSum([-1,0,1,2,-1,-4])
49+
print(res)

189. Rotate Array.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
2+
3+
4+
5+
# Example 1:
6+
7+
# Input: nums = [1,2,3,4,5,6,7], k = 3
8+
# Output: [5,6,7,1,2,3,4]
9+
# Explanation:
10+
# rotate 1 steps to the right: [7,1,2,3,4,5,6]
11+
# rotate 2 steps to the right: [6,7,1,2,3,4,5]
12+
# rotate 3 steps to the right: [5,6,7,1,2,3,4]
13+
14+
class Solution(object):
15+
def rotate(self, nums, k):
16+
"""
17+
:type nums: List[int]
18+
:type k: int
19+
:rtype: None Do not return anything, modify nums in-place instead.
20+
"""
21+
total = len(nums)
22+
k = k % total
23+
if k == 0:
24+
return nums
25+
nums[0: k], nums[k:-1] = nums[k:-1], nums[0: k]
26+
return nums
27+
28+
sol = Solution()
29+
res = sol.rotate(nums = [1,2,3,4,5,6,7], k = 3)
30+
print(res)

209.MinimumSizeSubarraySum.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution(object):
2+
def minSubArrayLen(self, target, nums):
3+
'''
4+
固定数组,找一个长度最小的subarray,元素之和大于或等于某target
5+
[2,3,1,2,4,3] 7 -> [4,3] -> len 2
6+
注意是subarray而不是subset
7+
[1,4,4] 4 -> [4] -> len 1
8+
9+
滑动窗口,不觉得像蜗牛吗,固定序列的顺序,找子序列的问题
10+
如果窗口合适,那记录下来,然后继续往前走
11+
'''
12+
i = 0
13+
j = 0
14+
minLen = float("inf")
15+
total = nums[0]
16+
while j < len(nums):
17+
if total >= target:
18+
minLen = min(minLen, j- i + 1)
19+
total -= nums[i]
20+
i += 1
21+
if i > j:
22+
j += 1
23+
if j > len(nums) - 1:
24+
break
25+
total += nums[j]
26+
else:
27+
j += 1
28+
if j > len(nums) - 1:
29+
break
30+
total += nums[j]
31+
if minLen == float("inf"):
32+
minLen = 0
33+
return minLen
34+
35+
s = Solution()
36+
res = s.minSubArrayLen(7, [2,3,1,2,4,3])
37+
print(res)

274.H-Index.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def hIndex(self, citations):
3+
"""
4+
:type citations: List[int]
5+
:rtype: int
6+
"""
7+
# sort the cite
8+
citations.sort()
9+
m = len(citations)
10+
hindex = 0
11+
for i in range(m-1, -1, -1):
12+
if citations[i] >= m - i:
13+
hindex = m -i
14+
return hindex
15+
16+
s = Solution()
17+
res = s.hIndex([3,0,6,1,5])
18+
print(res)
19+
20+
21+
# [4]
22+
# 0 1 2 6 7 8
23+
24+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from collections import deque, Counter
2+
3+
class Solution:
4+
def findSubstring(self, s: str, words: list[str]) -> list[int]:
5+
words_counter = Counter(words)
6+
words_len = len(words)
7+
word_len = len(words[0])
8+
final_list = []
9+
def single_slice(start):
10+
counter = Counter()
11+
word_queue = deque([], words_len)
12+
start_list = []
13+
for i in range(start, len(s), word_len):
14+
word = s[i:i+word_len]
15+
if word not in words:
16+
counter.clear()
17+
word_queue.clear()
18+
continue
19+
counter[word] += 1
20+
word_queue.append((i, word))
21+
if counter == words_counter:
22+
start_list.append(word_queue[0][0])
23+
if len(word_queue) == words_len:
24+
counter[word_queue[0][1]] -= 1
25+
return start_list
26+
27+
for i in range(word_len):
28+
final_list.extend(single_slice(i))
29+
return final_list
30+
31+
32+
s = Solution()
33+
res = s.findSubstring("barfoothefoobarman", ["foo","bar"])
34+
print(res)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def findMinArrowShots(self, points):
3+
"""
4+
:type points: List[List[int]]
5+
:rtype: int
6+
"""
7+
points.sort(key=lambda x: x[0])
8+
# sorted
9+
current = 0
10+
largest = points[0][1]
11+
count = 0
12+
13+
while current < len(points):
14+
current += 1
15+
if largest >= points[current][0]:
16+
if largest < points[current][1]:
17+
largest = points[current][1]
18+
else:
19+
count += 1
20+
return count
21+
22+
s = Solution()
23+
res = s.findMinArrowShots([[10,16],[2,8],[1,6],[7,12]])
24+
print(res)

55.JumpGame.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Solution(object):
2+
def canJump(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: bool
6+
给一个序列,每个元素代表从该元素能够跳的最长距离,从第一个元素开始,判断是否能跳到最后一个元素
7+
从最后的元素开始往前数,能到达该元素的最远元素A选择为下一个节点。为什么一定要选择最远的元素。
8+
假设选择的并非最远的元素B,那么,元素A必然能够到达元素B,然后是元素C在A之前跳过A直接到达B。
9+
那么从元素C必然也能够达到元素A。
10+
"""
11+
current = len(nums) - 1
12+
if current == 0:
13+
return 0
14+
count = 0
15+
while True:
16+
i = current
17+
step = 0
18+
# flag = 0
19+
while i >= 0:
20+
i -= 1
21+
if i < 0:
22+
break
23+
step += 1
24+
if nums[i] < step:
25+
continue
26+
current = i
27+
# flag = 1
28+
count += 1
29+
30+
if current == 0:
31+
return count
32+
def canJump2(self, nums):
33+
"""
34+
这里采用最近原则,因为考虑到全部元素都为1的情况,这种是最坏情况,时间复杂度为n方
35+
因此只选择第一个能够到达的元素进行遍历
36+
"""
37+
current = len(nums) - 1
38+
if current == 0:
39+
return True
40+
while True:
41+
i = current
42+
step = 0
43+
flag = 0
44+
while i >= 0:
45+
i -= 1
46+
if i < 0:
47+
break
48+
step += 1
49+
if nums[i] < step:
50+
continue
51+
current = i
52+
flag = 1
53+
break
54+
if flag == 0:
55+
return False
56+
if current == 0:
57+
return True
58+
59+
60+
s = Solution()
61+
res = s.canJump2([0])
62+
print(res)

0 commit comments

Comments
 (0)