Skip to content

Commit d6067bb

Browse files
committed
initial commit
1 parent 7f43515 commit d6067bb

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Python/candy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
#
4+
# There are N children standing in a line. Each child is assigned a rating value.
5+
#
6+
# You are giving candies to these children subjected to the following requirements:
7+
#
8+
# Each child must have at least one candy.
9+
# Children with a higher rating get more candies than their neighbors.
10+
# What is the minimum candies you must give?
11+
#
12+
import operator
13+
14+
class Solution:
15+
# @param ratings, a list of integer
16+
# @return an integer
17+
def candy(self, ratings):
18+
candies = [1 for i in range(len(ratings))]
19+
for i in range(1, len(ratings)):
20+
if ratings[i] > ratings[i - 1]:
21+
candies[i] = candies[i - 1] + 1
22+
23+
for i in reversed(range(1, len(ratings))):
24+
if ratings[i - 1] > ratings[i] and candies[i - 1] <= candies[i]:
25+
candies[i - 1] = candies[i] + 1
26+
27+
return reduce(operator.add, candies)
28+
29+
if __name__ == "__main__":
30+
result = Solution().candy([1, 2, 3, 2, 3, 5, 2, 5])
31+
print result

Python/climbStairs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
#
4+
# You are climbing a stair case. It takes n steps to reach to the top.
5+
#
6+
# Each time you can either climb 1 or 2 steps.
7+
# In how many distinct ways can you climb to the top?
8+
#
9+
class Solution:
10+
# @param n, an integer
11+
# @return an integer
12+
def climbStairs(self, n):
13+
prev, current = 0, 1
14+
for i in range(n):
15+
prev, current = current, prev + current,
16+
return current
17+
18+
if __name__ == "__main__":
19+
result = Solution().climbStairs(2)
20+
print result

0 commit comments

Comments
 (0)