Skip to content

Commit 2f005b4

Browse files
committed
+4
1 parent fcb0856 commit 2f005b4

File tree

4 files changed

+334
-0
lines changed

4 files changed

+334
-0
lines changed

1103.distribute-candies-to-people.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#
2+
# @lc app=leetcode id=1103 lang=python3
3+
#
4+
# [1103] Distribute Candies to People
5+
#
6+
# https://leetcode.com/problems/distribute-candies-to-people/description/
7+
#
8+
# algorithms
9+
# Easy (60.40%)
10+
# Likes: 321
11+
# Dislikes: 79
12+
# Total Accepted: 33.1K
13+
# Total Submissions: 53.1K
14+
# Testcase Example: '7\n4'
15+
#
16+
# We distribute some number of candies, to a row of n = num_people people in
17+
# the following way:
18+
#
19+
# We then give 1 candy to the first person, 2 candies to the second person, and
20+
# so on until we give n candies to the last person.
21+
#
22+
# Then, we go back to the start of the row, giving n + 1 candies to the first
23+
# person, n + 2 candies to the second person, and so on until we give 2 * n
24+
# candies to the last person.
25+
#
26+
# This process repeats (with us giving one more candy each time, and moving to
27+
# the start of the row after we reach the end) until we run out of candies.
28+
# The last person will receive all of our remaining candies (not necessarily
29+
# one more than the previous gift).
30+
#
31+
# Return an array (of length num_people and sum candies) that represents the
32+
# final distribution of candies.
33+
#
34+
#
35+
# Example 1:
36+
#
37+
#
38+
# Input: candies = 7, num_people = 4
39+
# Output: [1,2,3,1]
40+
# Explanation:
41+
# On the first turn, ans[0] += 1, and the array is [1,0,0,0].
42+
# On the second turn, ans[1] += 2, and the array is [1,2,0,0].
43+
# On the third turn, ans[2] += 3, and the array is [1,2,3,0].
44+
# On the fourth turn, ans[3] += 1 (because there is only one candy left), and
45+
# the final array is [1,2,3,1].
46+
#
47+
#
48+
# Example 2:
49+
#
50+
#
51+
# Input: candies = 10, num_people = 3
52+
# Output: [5,2,3]
53+
# Explanation:
54+
# On the first turn, ans[0] += 1, and the array is [1,0,0].
55+
# On the second turn, ans[1] += 2, and the array is [1,2,0].
56+
# On the third turn, ans[2] += 3, and the array is [1,2,3].
57+
# On the fourth turn, ans[0] += 4, and the final array is [5,2,3].
58+
#
59+
#
60+
#
61+
# Constraints:
62+
#
63+
#
64+
# 1 <= candies <= 10^9
65+
# 1 <= num_people <= 1000
66+
#
67+
#
68+
#
69+
70+
# @lc code=start
71+
class Solution:
72+
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
73+
# get how many full rounds
74+
rounds, n = 0, num_people
75+
nxtRound, addition = n*(n+1)//2, n*n
76+
while candies >= nxtRound:
77+
rounds += 1
78+
candies -= nxtRound
79+
nxtRound += addition
80+
81+
# calculate the numbers after full rounds
82+
res = [0]*num_people
83+
n_multiples = num_people*rounds*(rounds-1)//2
84+
for i in range(num_people):
85+
res[i] += n_multiples + (i+1)*rounds
86+
87+
# distribute the rest
88+
nxt, j = rounds*n+1, 0
89+
while candies > 0:
90+
if candies <= nxt:
91+
res[j] += candies
92+
break
93+
else:
94+
res[j] += nxt
95+
candies -= nxt
96+
nxt += 1
97+
j += 1
98+
return res
99+
# @lc code=end
100+

1544.make-the-string-great.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#
2+
# @lc app=leetcode id=1544 lang=python3
3+
#
4+
# [1544] Make The String Great
5+
#
6+
# https://leetcode.com/problems/make-the-string-great/description/
7+
#
8+
# algorithms
9+
# Easy (54.23%)
10+
# Likes: 112
11+
# Dislikes: 16
12+
# Total Accepted: 13.1K
13+
# Total Submissions: 24.2K
14+
# Testcase Example: '"leEeetcode"'
15+
#
16+
# Given a string s of lower and upper case English letters.
17+
#
18+
# A good string is a string which doesn't have two adjacent characters s[i] and
19+
# s[i + 1] where:
20+
#
21+
#
22+
# 0 <= i <= s.length - 2
23+
# s[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case
24+
# or vice-versa.
25+
#
26+
#
27+
# To make the string good, you can choose two adjacent characters that make the
28+
# string bad and remove them. You can keep doing this until the string becomes
29+
# good.
30+
#
31+
# Return the string after making it good. The answer is guaranteed to be unique
32+
# under the given constraints.
33+
#
34+
# Notice that an empty string is also good.
35+
#
36+
#
37+
# Example 1:
38+
#
39+
#
40+
# Input: s = "leEeetcode"
41+
# Output: "leetcode"
42+
# Explanation: In the first step, either you choose i = 1 or i = 2, both will
43+
# result "leEeetcode" to be reduced to "leetcode".
44+
#
45+
#
46+
# Example 2:
47+
#
48+
#
49+
# Input: s = "abBAcC"
50+
# Output: ""
51+
# Explanation: We have many possible scenarios, and all lead to the same
52+
# answer. For example:
53+
# "abBAcC" --> "aAcC" --> "cC" --> ""
54+
# "abBAcC" --> "abBA" --> "aA" --> ""
55+
#
56+
#
57+
# Example 3:
58+
#
59+
#
60+
# Input: s = "s"
61+
# Output: "s"
62+
#
63+
#
64+
#
65+
# Constraints:
66+
#
67+
#
68+
# 1 <= s.length <= 100
69+
# s contains only lower and upper case English letters.
70+
#
71+
#
72+
73+
# @lc code=start
74+
class Solution:
75+
def makeGood(self, s: str) -> str:
76+
stack, diff = [], abs(ord('a')-ord('A'))
77+
for l in s:
78+
if stack and abs(ord(stack[-1])-ord(l)) == diff:
79+
stack.pop()
80+
else:
81+
stack.append(l)
82+
return ''.join(stack)
83+
84+
# @lc code=end
85+

1550.three-consecutive-odds.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# @lc app=leetcode id=1550 lang=python3
3+
#
4+
# [1550] Three Consecutive Odds
5+
#
6+
# https://leetcode.com/problems/three-consecutive-odds/description/
7+
#
8+
# algorithms
9+
# Easy (68.73%)
10+
# Likes: 45
11+
# Dislikes: 4
12+
# Total Accepted: 10.7K
13+
# Total Submissions: 15.6K
14+
# Testcase Example: '[2,6,4,1]'
15+
#
16+
# Given an integer array arr, return true if there are three consecutive odd
17+
# numbers in the array. Otherwise, return false.
18+
#
19+
# Example 1:
20+
#
21+
#
22+
# Input: arr = [2,6,4,1]
23+
# Output: false
24+
# Explanation: There are no three consecutive odds.
25+
#
26+
#
27+
# Example 2:
28+
#
29+
#
30+
# Input: arr = [1,2,34,3,4,5,7,23,12]
31+
# Output: true
32+
# Explanation: [5,7,23] are three consecutive odds.
33+
#
34+
#
35+
#
36+
# Constraints:
37+
#
38+
#
39+
# 1 <= arr.length <= 1000
40+
# 1 <= arr[i] <= 1000
41+
#
42+
#
43+
#
44+
45+
# @lc code=start
46+
class Solution:
47+
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
48+
if len(arr) < 3:
49+
return False
50+
51+
for i in range(2, len(arr)):
52+
if arr[i-2]%2 and arr[i-1]%2 and arr[i]%2:
53+
return True
54+
return False
55+
# @lc code=end
56+

575.distribute-candies.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#
2+
# @lc app=leetcode id=575 lang=python3
3+
#
4+
# [575] Distribute Candies
5+
#
6+
# https://leetcode.com/problems/distribute-candies/description/
7+
#
8+
# algorithms
9+
# Easy (61.28%)
10+
# Likes: 472
11+
# Dislikes: 880
12+
# Total Accepted: 102.5K
13+
# Total Submissions: 167K
14+
# Testcase Example: '[1,1,2,2,3,3]'
15+
#
16+
# You have n candies, the i^th candy is of type candies[i].
17+
#
18+
# You want to distribute the candies equally between a sister and a brother so
19+
# that each of them gets n / 2 candies (n is even). The sister loves to collect
20+
# different types of candies, so you want to give her the maximum number of
21+
# different types of candies.
22+
#
23+
# Return the maximum number of different types of candies you can give to the
24+
# sister.
25+
#
26+
#
27+
#
28+
#
29+
#
30+
# Example 1:
31+
#
32+
#
33+
# Input: candies = [1,1,2,2,3,3]
34+
# Output: 3
35+
# Explanation:
36+
# There are three different kinds of candies (1, 2 and 3), and two candies for
37+
# each kind.
38+
# Optimal distribution: The sister has candies [1,2,3] and the brother has
39+
# candies [1,2,3], too.
40+
# The sister has three different kinds of candies.
41+
#
42+
#
43+
# Example 2:
44+
#
45+
#
46+
# Input: candies = [1,1,2,3]
47+
# Output: 2
48+
# Explanation: For example, the sister has candies [2,3] and the brother has
49+
# candies [1,1].
50+
# The sister has two different kinds of candies, the brother has only one kind
51+
# of candies.
52+
#
53+
#
54+
# Example 3:
55+
#
56+
#
57+
# Input: candies = [1,1]
58+
# Output: 1
59+
#
60+
#
61+
# Example 4:
62+
#
63+
#
64+
# Input: candies = [1,11]
65+
# Output: 1
66+
#
67+
#
68+
# Example 5:
69+
#
70+
#
71+
# Input: candies = [2,2]
72+
# Output: 1
73+
#
74+
#
75+
#
76+
# Constraints:
77+
#
78+
#
79+
# n == candies.length
80+
# 2 <= n <= 10^4
81+
# n is even.
82+
# -10^5 <= candies[i] <= 10^5
83+
#
84+
#
85+
#
86+
87+
# @lc code=start
88+
class Solution:
89+
def distributeCandies(self, candies: List[int]) -> int:
90+
d, n = set(candies), len(candies)
91+
return len(d) if len(d) < n//2 else n//2
92+
# @lc code=end
93+

0 commit comments

Comments
 (0)