Skip to content

Commit d157c1c

Browse files
committed
daily
1 parent a9a10c6 commit d157c1c

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

my-submissions/m1718 v1.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution:
2+
def helper(self, output: List[int], curr_indx: int, pot_vals: List[int]) -> List[int] | bool :
3+
# The check for if there are pot_vals remaining is to check
4+
# whether multiple "1"s were used
5+
if curr_indx >= len(output) :
6+
return output if not pot_vals else False
7+
8+
if output[curr_indx] :
9+
return self.helper(output, curr_indx + 1, pot_vals)
10+
11+
len_potvals = len(pot_vals)
12+
for i in range(len_potvals - 1, -1, -1) :
13+
# This part is quite inefficient due to it popping and inserting causing many O(n)
14+
# operations but is somewhat needed in order to keep pot_vals sorted when
15+
# parsing the potential answers
16+
if curr_indx + pot_vals[i] < len(output) and not output[curr_indx + pot_vals[i]] :
17+
curr = pot_vals.pop(i)
18+
output[curr_indx] = curr
19+
output[curr_indx + curr] = curr
20+
pot_result = self.helper(output, curr_indx + 1, pot_vals)
21+
22+
# if returns a list, then answer found
23+
# if returns bool FALSE, then invalid case
24+
if pot_result :
25+
return output
26+
27+
pot_vals.insert(i, curr)
28+
output[curr_indx] = 0
29+
output[curr_indx + curr] = 0
30+
31+
# Insert the "1" case since it appears once
32+
output[curr_indx] = 1
33+
pot_result = self.helper(output, curr_indx + 1, pot_vals)
34+
if pot_result :
35+
return pot_result
36+
output[curr_indx] = 0
37+
38+
return False
39+
40+
41+
def constructDistancedSequence(self, n: int) -> List[int]:
42+
output = [0] * (n + n - 1)
43+
pot_vals = list(range(2, n + 1))
44+
45+
return self.helper(output, 0, pot_vals)

my-submissions/m1718 v2.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution:
2+
def helper(self,
3+
output: List[int],
4+
curr_indx: int,
5+
pot_vals: List[int],
6+
one_used: bool = False) -> List[int] | bool :
7+
if curr_indx >= len(output) :
8+
return output
9+
10+
if output[curr_indx] :
11+
return self.helper(output, curr_indx + 1, pot_vals, one_used)
12+
13+
len_potvals = len(pot_vals)
14+
for i in range(len_potvals - 1, -1, -1) :
15+
# This part is quite inefficient due to it popping and inserting causing many O(n)
16+
# operations but is somewhat needed in order to keep pot_vals sorted when
17+
# parsing the potential answers
18+
if curr_indx + pot_vals[i] < len(output) and not output[curr_indx + pot_vals[i]] :
19+
curr = pot_vals.pop(i)
20+
output[curr_indx] = curr
21+
output[curr_indx + curr] = curr
22+
pot_result = self.helper(output, curr_indx + 1, pot_vals, one_used)
23+
24+
# if returns a list, then answer found
25+
# if returns bool FALSE, then invalid case
26+
if pot_result :
27+
return output
28+
29+
pot_vals.insert(i, curr)
30+
output[curr_indx] = 0
31+
output[curr_indx + curr] = 0
32+
33+
# Insert the "1" case since it appears once
34+
if not one_used :
35+
output[curr_indx] = 1
36+
pot_result = self.helper(output, curr_indx + 1, pot_vals, True)
37+
if pot_result :
38+
return pot_result
39+
output[curr_indx] = 0
40+
41+
return False
42+
43+
44+
def constructDistancedSequence(self, n: int) -> List[int]:
45+
output = [0] * (n + n - 1)
46+
pot_vals = list(range(2, n + 1))
47+
48+
return self.helper(output, 0, pot_vals)

my-submissions/m1718.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## V1
2+
3+
## V2
4+
5+
Added a parameter check to see if we've made use of our "1" insert since the prompt
6+
tells us that it will appear once in the output array. This is in contrast to V1
7+
where instead we would allow the recursion to fully propogate the OUTPUT array
8+
and check if any values were unused

0 commit comments

Comments
 (0)