Skip to content

Commit e4b650f

Browse files
committed
back to business
1 parent 46783d8 commit e4b650f

File tree

2 files changed

+35
-98
lines changed

2 files changed

+35
-98
lines changed

Daily-challenge/Dec/28/README.md

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,41 @@
1-
# Sliding Window Maximum
2-
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.
1+
# Reach a Number
2+
You are standing at position 0 on an infinite number line. There is a goal at position target.
33

4-
Return the max sliding window.
4+
On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.
55

6-
6+
Return the minimum number of steps required to reach the destination.
77

88
Example 1:
9-
10-
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
11-
Output: [3,3,5,5,6,7]
12-
Explanation:
13-
Window position Max
14-
--------------- -----
15-
[1 3 -1] -3 5 3 6 7 3
16-
1 [3 -1 -3] 5 3 6 7 3
17-
1 3 [-1 -3 5] 3 6 7 5
18-
1 3 -1 [-3 5 3] 6 7 5
19-
1 3 -1 -3 [5 3 6] 7 6
20-
1 3 -1 -3 5 [3 6 7] 7
9+
Input: target = 3
10+
Output: 2
11+
Explanation:
12+
On the first move we step from 0 to 1.
13+
On the second step we step from 1 to 3.
2114
Example 2:
22-
23-
Input: nums = [1], k = 1
24-
Output: [1]
25-
Example 3:
26-
27-
Input: nums = [1,-1], k = 1
28-
Output: [1,-1]
29-
Example 4:
30-
31-
Input: nums = [9,11], k = 2
32-
Output: [11]
33-
Example 5:
34-
35-
Input: nums = [4,-2], k = 2
36-
Output: [4]
37-
38-
39-
Constraints:
40-
41-
1 <= nums.length <= 105
42-
-104 <= nums[i] <= 104
43-
1 <= k <= nums.length<br>
15+
Input: target = 2
16+
Output: 3
17+
Explanation:
18+
On the first move we step from 0 to 1.
19+
On the second move we step from 1 to -1.
20+
On the third move we step from -1 to 2.
21+
Note:
22+
target will be a non-zero integer in the range [-10^9, 10^9].<br>
4423

4524
## Idea
4625
Just do while loop and convert to decimal
4726

4827
## Code
4928
```python
5029
class Solution:
51-
def maxSlidingWindow(self, nums: List[int], w: int) -> List[int]:
52-
max_left = [0 for n in range(len(nums))]
53-
max_right = [0 for n in range(len(nums))]
54-
max_left[0] = nums[0]
55-
max_right[-1] = nums[-1]
56-
57-
for i in range(1, len(nums)):
58-
if i % w == 0:
59-
60-
max_left[i] = nums[i]
61-
else:
62-
max_left[i] = max(nums[i], max_left[i-1])
30+
def reachNumber(self, target: int) -> int:
31+
bound = ceil(sqrt(2*abs(target)+0.25) - 0.5)
32+
if target % 2 == 0:
33+
if bound % 4 == 1: bound += 2
34+
if bound % 4 == 2: bound += 1
35+
else:
36+
if bound % 4 == 3: bound += 2
37+
if bound % 4 == 0: bound += 1
6338

64-
65-
j = len(nums) - i - 1
66-
67-
if j % w == 0:
68-
max_right[j] = nums[j]
69-
else:
70-
max_right[j] = max(nums[j], max_right[j+1])
71-
# print(max_right, max_left)
72-
lst = []
73-
x = 0
74-
# j = 0
75-
while x + w <= len(nums):
76-
# j += 1
77-
# print(j)
78-
lst += [max(max_right[x], max_left[x+w-1])]
79-
x += 1
80-
81-
return lst
39+
return bound
8240

8341
```

Daily-challenge/Dec/28/sol.py

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
class Solution:
2-
def maxSlidingWindow(self, nums: List[int], w: int) -> List[int]:
3-
max_left = [0 for n in range(len(nums))]
4-
max_right = [0 for n in range(len(nums))]
5-
max_left[0] = nums[0]
6-
max_right[-1] = nums[-1]
7-
8-
for i in range(1, len(nums)):
9-
if i % w == 0:
2+
def reachNumber(self, target: int) -> int:
3+
bound = ceil(sqrt(2*abs(target)+0.25) - 0.5)
4+
if target % 2 == 0:
5+
if bound % 4 == 1: bound += 2
6+
if bound % 4 == 2: bound += 1
7+
else:
8+
if bound % 4 == 3: bound += 2
9+
if bound % 4 == 0: bound += 1
1010

11-
max_left[i] = nums[i]
12-
else:
13-
max_left[i] = max(nums[i], max_left[i-1])
14-
15-
16-
j = len(nums) - i - 1
17-
18-
if j % w == 0:
19-
max_right[j] = nums[j]
20-
else:
21-
max_right[j] = max(nums[j], max_right[j+1])
22-
# print(max_right, max_left)
23-
lst = []
24-
x = 0
25-
# j = 0
26-
while x + w <= len(nums):
27-
# j += 1
28-
# print(j)
29-
lst += [max(max_right[x], max_left[x+w-1])]
30-
x += 1
31-
32-
return lst
11+
return bound

0 commit comments

Comments
 (0)