|
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. |
3 | 3 |
|
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. |
5 | 5 |
|
6 |
| - |
| 6 | +Return the minimum number of steps required to reach the destination. |
7 | 7 |
|
8 | 8 | 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. |
21 | 14 | 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> |
44 | 23 |
|
45 | 24 | ## Idea
|
46 | 25 | Just do while loop and convert to decimal
|
47 | 26 |
|
48 | 27 | ## Code
|
49 | 28 | ```python
|
50 | 29 | 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 |
63 | 38 |
|
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 |
82 | 40 |
|
83 | 41 | ```
|
0 commit comments