Skip to content

Commit 542f201

Browse files
Merge pull request #213 from xiaoluome/dev
第四周算法作业#36
2 parents 498064b + 23da125 commit 542f201

File tree

8 files changed

+580
-0
lines changed

8 files changed

+580
-0
lines changed

Week_04/id_36/LeetCode_169_36.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#
2+
# @lc app=leetcode.cn id=169 lang=python3
3+
#
4+
# [169] 求众数
5+
#
6+
# https://leetcode-cn.com/problems/majority-element/description/
7+
#
8+
# algorithms
9+
# Easy (59.73%)
10+
# Likes: 259
11+
# Dislikes: 0
12+
# Total Accepted: 49.1K
13+
# Total Submissions: 82.1K
14+
# Testcase Example: '[3,2,3]'
15+
#
16+
# 给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
17+
#
18+
# 你可以假设数组是非空的,并且给定的数组总是存在众数。
19+
#
20+
# 示例 1:
21+
#
22+
# 输入: [3,2,3]
23+
# 输出: 3
24+
#
25+
# 示例 2:
26+
#
27+
# 输入: [2,2,1,1,1,2,2]
28+
# 输出: 2
29+
#
30+
#
31+
32+
# # 暴力法
33+
# class Solution:
34+
# def majorityElement(self, nums: List[int]) -> int:
35+
# majority_num = len(nums)//2
36+
37+
# for num in nums:
38+
# count = 0
39+
# for n in nums:
40+
# if n == num:
41+
# count += 1
42+
43+
# if count > majority_num:
44+
# return num
45+
46+
# return -1
47+
48+
# # 哈希表-1
49+
# class Solution:
50+
# def majorityElement(self, nums: List[int]) -> int:
51+
# if not nums or len(nums) < 1:
52+
# return 0
53+
54+
# counts = {}
55+
56+
# for num in nums:
57+
# if not counts.get(num):
58+
# counts[num] = 1
59+
# else:
60+
# counts[num] += 1
61+
62+
# max_value = 0
63+
# majority = 0
64+
65+
# for key, value in counts.items():
66+
# if value > max_value:
67+
# max_value = value
68+
# majority = key
69+
70+
# return majority
71+
72+
# # 哈希表-2
73+
# class Solution:
74+
# def majorityElement(self, nums: List[int]) -> int:
75+
# import collections
76+
# counts = collections.Counter(nums)
77+
# return max(counts.keys(), key = counts.get)
78+
79+
# # 排序
80+
# class Solution:
81+
# def majorityElement(self, nums: List[int]) -> int:
82+
# nums_len = len(nums)
83+
# if not nums or nums_len < 1:
84+
# return 0
85+
86+
# nums.sort()
87+
# return nums[nums_len // 2]
88+
89+
# # 分治
90+
# class Solution:
91+
# def majorityElement(self, nums: List[int]) -> int:
92+
# def majority_element_rec(low, high):
93+
# if low == high:
94+
# return nums[low]
95+
96+
# middle = low + (high - low) // 2
97+
# left = majority_element_rec(low, middle)
98+
# right = majority_element_rec(middle + 1, high)
99+
100+
# if left == right:
101+
# return left
102+
103+
# # left_count = sum(1 for i in range(low, high + 1) if nums[i] == left)
104+
# # right_count = sum(1 for i in range(low, high + 1) if nums[i] == right)
105+
# left_count = 0
106+
# right_count = 0
107+
# for i in range(low, high + 1):
108+
# if nums[i] == left:
109+
# left_count += 1
110+
# elif nums[i] == right:
111+
# right_count += 1
112+
113+
# return left if left_count > right_count else right
114+
115+
# return majority_element_rec(0, len(nums) - 1)
116+
117+
# Boyer-Moore 投票算法
118+
class Solution:
119+
def majorityElement(self, nums: List[int]) -> int:
120+
count = 0
121+
candidate = None
122+
123+
for num in nums:
124+
if count == 0:
125+
candidate = num
126+
count += (1 if num == candidate else -1)
127+
128+
return candidate
129+

Week_04/id_36/LeetCode_455_36.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#
2+
# @lc app=leetcode.cn id=455 lang=python3
3+
#
4+
# [455] 分发饼干
5+
#
6+
# https://leetcode-cn.com/problems/assign-cookies/description/
7+
#
8+
# algorithms
9+
# Easy (49.91%)
10+
# Likes: 91
11+
# Dislikes: 0
12+
# Total Accepted: 10.7K
13+
# Total Submissions: 21.4K
14+
# Testcase Example: '[1,2,3]\n[1,1]'
15+
#
16+
# 假设你是一位很棒的家长,想要给你的孩子们一些小饼干。但是,每个孩子最多只能给一块饼干。对每个孩子 i ,都有一个胃口值 gi
17+
# ,这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干 j ,都有一个尺寸 sj 。如果 sj >= gi ,我们可以将这个饼干 j 分配给孩子 i
18+
# ,这个孩子会得到满足。你的目标是尽可能满足越多数量的孩子,并输出这个最大数值。
19+
#
20+
# 注意:
21+
#
22+
# 你可以假设胃口值为正。
23+
# 一个小朋友最多只能拥有一块饼干。
24+
#
25+
# 示例 1:
26+
#
27+
#
28+
# 输入: [1,2,3], [1,1]
29+
#
30+
# 输出: 1
31+
#
32+
# 解释:
33+
# 你有三个孩子和两块小饼干,3个孩子的胃口值分别是:1,2,3。
34+
# 虽然你有两块小饼干,由于他们的尺寸都是1,你只能让胃口值是1的孩子满足。
35+
# 所以你应该输出1。
36+
#
37+
#
38+
# 示例 2:
39+
#
40+
#
41+
# 输入: [1,2], [1,2,3]
42+
#
43+
# 输出: 2
44+
#
45+
# 解释:
46+
# 你有两个孩子和三块小饼干,2个孩子的胃口值分别是1,2。
47+
# 你拥有的饼干数量和尺寸都足以让所有孩子满足。
48+
# 所以你应该输出2.
49+
#
50+
#
51+
#
52+
# class Solution:
53+
# def findContentChildren(self, g: List[int], s: List[int]) -> int:
54+
# g.sort()
55+
# s.sort()
56+
57+
# gi = 0
58+
# si = 0
59+
# res = 0
60+
61+
# while gi < len(g) and si < len(s):
62+
# if s[si] >= g[gi]:
63+
# si += 1
64+
# gi += 1
65+
# res += 1
66+
# else:
67+
# si += 1
68+
69+
# return res
70+
71+
class Solution:
72+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
73+
import heapq
74+
75+
heapq.heapify(g)
76+
heapq.heapify(s)
77+
78+
res = 0
79+
80+
while g and s:
81+
if s[0] >= g[0]:
82+
heapq.heappop(g)
83+
heapq.heappop(s)
84+
res += 1
85+
else:
86+
heapq.heappop(s)
87+
88+
return res
89+

Week_04/id_36/LeetCode_46_36.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# @lc app=leetcode.cn id=46 lang=python3
3+
#
4+
# [46] 全排列
5+
#
6+
# https://leetcode-cn.com/problems/permutations/description/
7+
#
8+
# algorithms
9+
# Medium (69.78%)
10+
# Likes: 294
11+
# Dislikes: 0
12+
# Total Accepted: 29.2K
13+
# Total Submissions: 41.9K
14+
# Testcase Example: '[1,2,3]'
15+
#
16+
# 给定一个没有重复数字的序列,返回其所有可能的全排列。
17+
#
18+
# 示例:
19+
#
20+
# 输入: [1,2,3]
21+
# 输出:
22+
# [
23+
# ⁠ [1,2,3],
24+
# ⁠ [1,3,2],
25+
# ⁠ [2,1,3],
26+
# ⁠ [2,3,1],
27+
# ⁠ [3,1,2],
28+
# ⁠ [3,2,1]
29+
# ]
30+
#
31+
#
32+
class Solution:
33+
def permute(self, nums: List[int]) -> List[List[int]]:
34+
def backtrack(first = 0):
35+
if first == n:
36+
output.append(nums[:])
37+
38+
for i in range(first, n):
39+
nums[first], nums[i] = nums[i], nums[first]
40+
backtrack(first + 1)
41+
nums[first], nums[i] = nums[i], nums[first]
42+
43+
n = len(nums)
44+
output = []
45+
backtrack()
46+
47+
return output
48+

Week_04/id_36/LeetCode_53_36.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# @lc app=leetcode.cn id=53 lang=python3
3+
#
4+
# [53] 最大子序和
5+
#
6+
# https://leetcode-cn.com/problems/maximum-subarray/description/
7+
#
8+
# algorithms
9+
# Easy (45.55%)
10+
# Likes: 1009
11+
# Dislikes: 0
12+
# Total Accepted: 65.3K
13+
# Total Submissions: 142.9K
14+
# Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]'
15+
#
16+
# 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
17+
#
18+
# 示例:
19+
#
20+
# 输入: [-2,1,-3,4,-1,2,1,-5,4],
21+
# 输出: 6
22+
# 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
23+
#
24+
#
25+
# 进阶:
26+
#
27+
# 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。
28+
#
29+
# # 暴力法
30+
# class Solution:
31+
# def maxSubArray(self, nums: List[int]) -> int:
32+
# n = len(nums)
33+
# if n <= 0:
34+
# return 0
35+
36+
# tmp = nums[0]
37+
# max_value = tmp
38+
39+
# for i in range(1, n):
40+
# tmp = tmp + nums[i] if tmp > 0 else nums[i]
41+
# max_value = max(max_value, tmp)
42+
43+
# return max_value
44+
45+
# class Solution:
46+
# def maxSubArray(self, nums: List[int]) -> int:
47+
# n = len(nums)
48+
# if n <= 0:
49+
# return 0
50+
# elif n == 1:
51+
# return nums[0]
52+
53+
# max_value = nums[0]
54+
# tmp = 0
55+
56+
# for num in nums:
57+
# tmp = tmp + num if tmp > 0 else num
58+
# max_value = max(max_value, tmp)
59+
60+
# return max_value
61+
62+
# 分治法
63+
class Solution:
64+
def maxSubArray(self, nums: List[int]) -> int:
65+
n = len(nums)
66+
if n <= 0:
67+
return 0
68+
elif n == 1:
69+
return nums[0]
70+
71+
middle = n // 2
72+
max_left = self.maxSubArray(nums[0 : middle])
73+
max_right = self.maxSubArray(nums[middle : n])
74+
75+
tmp = 0
76+
max_le = nums[middle - 1]
77+
for i in range(middle - 1, -1, -1):
78+
tmp += nums[i]
79+
max_le = max(max_le, tmp)
80+
81+
max_ri = nums[middle]
82+
tmp = 0
83+
for i in range(middle, n):
84+
tmp += nums[i]
85+
max_ri = max(max_ri, tmp)
86+
87+
return max(max_left, max_right, max_le + max_ri)
88+

0 commit comments

Comments
 (0)