Skip to content

Commit 78d8fda

Browse files
committed
Solution added.
1 parent 1428578 commit 78d8fda

File tree

3 files changed

+134
-0
lines changed
  • 30 Days September Challange/Week 3
  • 30 Days of October Challange/Week 1/2. Combination Sum

3 files changed

+134
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:
3+
4+
"G": go straight 1 unit;
5+
"L": turn 90 degrees to the left;
6+
"R": turn 90 degress to the right.
7+
The robot performs the instructions given in order, and repeats them forever.
8+
9+
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
10+
11+
12+
13+
Example 1:
14+
15+
Input: "GGLLGG"
16+
Output: true
17+
Explanation:
18+
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
19+
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.
20+
Example 2:
21+
22+
Input: "GG"
23+
Output: false
24+
Explanation:
25+
The robot moves north indefinitely.
26+
Example 3:
27+
28+
Input: "GL"
29+
Output: true
30+
Explanation:
31+
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...
32+
33+
34+
Note:
35+
36+
1 <= instructions.length <= 100
37+
instructions[i] is in {'G', 'L', 'R'}
38+
"""
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
On a 2-dimensional grid, there are 4 types of squares:
3+
4+
1 represents the starting square. There is exactly one starting square.
5+
2 represents the ending square. There is exactly one ending square.
6+
0 represents empty squares we can walk over.
7+
-1 represents obstacles that we cannot walk over.
8+
Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.
9+
10+
11+
12+
Example 1:
13+
14+
Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
15+
Output: 2
16+
Explanation: We have the following two paths:
17+
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
18+
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)
19+
Example 2:
20+
21+
Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
22+
Output: 4
23+
Explanation: We have the following four paths:
24+
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
25+
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
26+
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
27+
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)
28+
Example 3:
29+
30+
Input: [[0,1],[2,0]]
31+
Output: 0
32+
Explanation:
33+
There is no path that walks over every empty square exactly once.
34+
Note that the starting and ending square can be anywhere in the grid.
35+
36+
37+
Note:
38+
39+
1 <= grid.length * grid[0].length <= 20
40+
"""
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
3+
4+
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
5+
6+
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
7+
8+
9+
10+
Example 1:
11+
12+
Input: candidates = [2,3,6,7], target = 7
13+
Output: [[2,2,3],[7]]
14+
Explanation:
15+
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
16+
7 is a candidate, and 7 = 7.
17+
These are the only two combinations.
18+
Example 2:
19+
20+
Input: candidates = [2,3,5], target = 8
21+
Output: [[2,2,2,2],[2,3,3],[3,5]]
22+
Example 3:
23+
24+
Input: candidates = [2], target = 1
25+
Output: []
26+
Example 4:
27+
28+
Input: candidates = [1], target = 1
29+
Output: [[1]]
30+
Example 5:
31+
32+
Input: candidates = [1], target = 2
33+
Output: [[1,1]]
34+
35+
36+
Constraints:
37+
38+
1 <= candidates.length <= 30
39+
1 <= candidates[i] <= 200
40+
All elements of candidates are distinct.
41+
1 <= target <= 500
42+
"""
43+
class Solution:
44+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
45+
result = []
46+
def dfs(nums,target,index,arr):
47+
nonlocal result
48+
if target < 0:
49+
return
50+
if target == 0:
51+
result.append(arr)
52+
return
53+
for i in range(index,len(candidates)):
54+
dfs(nums,target-nums[i],i,arr + [nums[i]])
55+
dfs(candidates,target,0,[])
56+
return result

0 commit comments

Comments
 (0)