Skip to content

Commit 377e713

Browse files
committed
Add some problems
1 parent a3e4f30 commit 377e713

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

17-distribute-candies-to-people.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -------------------------------------------------------
2+
# Distribute Candies to People - https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/551/week-3-august-15th-august-21st/3427/
3+
# -------------------------------------------------------
4+
# Author: Arshad Mehmood
5+
# Github: https://github.com/arshad115
6+
# Blog: https://arshadmehmood.com
7+
# LinkedIn: https://www.linkedin.com/in/arshadmehmood115
8+
# Date : 2020-08-17
9+
# Project: leetcode-august-2020
10+
# -------------------------------------------------------
11+
from typing import List
12+
13+
14+
class Solution:
15+
def distributeCandies(self, candies: int, num_people: int) -> List[int]:
16+
people = [0]*num_people
17+
i = 0
18+
while candies>0:
19+
people[i % num_people] += min(i + 1,candies)
20+
i += 1
21+
candies -= i
22+
return people
23+
24+
25+
26+
solution = Solution()
27+
result = solution.distributeCandies(10, 3)
28+
print(result)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -------------------------------------------------------
2+
# Numbers With Same Consecutive Differences - https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/551/week-3-august-15th-august-21st/3428/
3+
# -------------------------------------------------------
4+
# Author: Arshad Mehmood
5+
# Github: https://github.com/arshad115
6+
# Blog: https://arshadmehmood.com
7+
# LinkedIn: https://www.linkedin.com/in/arshadmehmood115
8+
# Date : 2020-08-18
9+
# Project: leetcode-august-2020
10+
# -------------------------------------------------------
11+
from typing import List
12+
13+
class Solution:
14+
def numsSameConsecDiff(self, N: int, K: int) -> List[int]:
15+
numbers = list(range(10))
16+
if N == 1:
17+
return numbers
18+
19+
for i in range(1,N):
20+
numset = set()
21+
for n in numbers:
22+
num = n % 10
23+
minusK = num - K
24+
plusK = num + K
25+
26+
if n>0 and plusK < 10:
27+
numset.add(n*10 + plusK)
28+
if n > 0 and minusK >= 0:
29+
numset.add(n * 10 + minusK)
30+
31+
numbers = numset
32+
return list(numbers)
33+
34+
solution = Solution()
35+
result = solution.numsSameConsecDiff(2, 1)
36+
print(result)

8-path-sum-iii.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -------------------------------------------------------
2+
# Path Sum III - https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3415/
3+
# -------------------------------------------------------
4+
# Author: Arshad Mehmood
5+
# Github: https://github.com/arshad115
6+
# Blog: https://arshadmehmood.com
7+
# LinkedIn: https://www.linkedin.com/in/arshadmehmood115
8+
# Date : 2020-08-8
9+
# Project: leetcode-august-2020
10+
# -------------------------------------------------------
11+
# Definition for a binary tree node.
12+
import collections
13+
14+
15+
class TreeNode:
16+
def __init__(self, val=0, left=None, right=None):
17+
self.val = val
18+
self.left = left
19+
self.right = right
20+
21+
class Solution(object):
22+
def pathSum(self, root, sum):
23+
def dfs(sumHash, prefixSum, node):
24+
if not node:
25+
return 0
26+
27+
# Sum of current path
28+
prefixSum += node.val
29+
30+
# number of paths that ends at current node
31+
path = sumHash[prefixSum - sum]
32+
33+
# add currentSum to prefixSum Hash
34+
sumHash[prefixSum] += 1
35+
36+
# traverse left and right of tree
37+
path += dfs(sumHash, prefixSum, node.left) + dfs(sumHash, prefixSum, node.right)
38+
39+
# remove currentSum from prefixSum Hash
40+
sumHash[prefixSum] -= 1
41+
42+
return path
43+
44+
# depth first search, initialize sumHash with prefix sum of 0, occurring once
45+
return dfs(collections.defaultdict(int, {0: 1}), 0, root)
46+
47+
solution = Solution()
48+
result = solution.pathSum([10,5,-3,3,2,None,11,3,-2,None,1],8)
49+
print(result)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ My leetcode profile can be viewed [here](https://leetcode.com/arshad115/)
1212
| 4 | [Power of Four](./codes/4-valid-palindrome.py) |
1313
| 5 | [Add and Search Word - Data structure design](./codes/5-add-and-search-word-data-structure-design.py) |
1414
| 6 | [Find All Duplicates in an Array](./codes/6-find-all-duplicates-in-an-array.py) |
15+
| 8 | [Path Sum III](./codes/8-path-sum-iii.py) |
16+
| 17 | [Distribute Candies to People](./codes/17-distribute-candies-to-people.py) |
17+
| 18 | [Numbers With Same Consecutive Differences](./codes/18-numbers-with-same-consecutive-differences.py) |

0 commit comments

Comments
 (0)