|
4 | 4 |
|
5 | 5 | ## Description
|
6 | 6 |
|
7 |
| -<p>Given a <strong>0-indexed</strong> array of strings <code>words</code> where <code>words[i]</code> is either a positive integer represented as a string or the string <code>"prev"</code>.</p> |
| 7 | +<p>Given an integer array <code>nums</code> where <code>nums[i]</code> is either a positive integer or <code>-1</code>.</p> |
8 | 8 |
|
9 |
| -<p>Start iterating from the beginning of the array; for every <code>"prev"</code> string seen in <code>words</code>, find the <strong>last visited integer</strong> in <code>words</code> which is defined as follows:</p> |
| 9 | +<p>We need to find for each <code>-1</code> the respective positive integer, which we call the last visited integer.</p> |
| 10 | + |
| 11 | +<p>To achieve this goal, let's define two empty arrays: <code>seen</code> and <code>ans</code>.</p> |
| 12 | + |
| 13 | +<p>Start iterating from the beginning of the array <code>nums</code>.</p> |
10 | 14 |
|
11 | 15 | <ul>
|
12 |
| - <li>Let <code>k</code> be the number of consecutive <code>"prev"</code> strings seen so far (containing the current string). Let <code>nums</code> be the <strong>0-indexed </strong>array of <strong>integers</strong> seen so far and <code>nums_reverse</code> be the reverse of <code>nums</code>, then the integer at <code>(k - 1)<sup>th</sup></code> index of <code>nums_reverse</code> will be the <strong>last visited integer</strong> for this <code>"prev"</code>.</li> |
13 |
| - <li>If <code>k</code> is <strong>greater</strong> than the total visited integers, then the last visited integer will be <code>-1</code>.</li> |
| 16 | + <li>If a positive integer is encountered, prepend it to the <strong>front</strong> of <code>seen</code>.</li> |
| 17 | + <li>If <code>-1</code> is encountered, let <code>k</code> be the number of <strong>consecutive</strong> <code>-1</code>s seen so far (including the current <code>-1</code>), |
| 18 | + <ul> |
| 19 | + <li>If <code>k</code> is less than or equal to the length of <code>seen</code>, append the <code>k</code>-th element of <code>seen</code> to <code>ans</code>.</li> |
| 20 | + <li>If <code>k</code> is strictly greater than the length of <code>seen</code>, append <code>-1</code> to <code>ans</code>.</li> |
| 21 | + </ul> |
| 22 | + </li> |
14 | 23 | </ul>
|
15 | 24 |
|
16 |
| -<p>Return <em>an integer array containing the last visited integers.</em></p> |
| 25 | +<p>Return <em>the array </em><code>ans</code>.</p> |
17 | 26 |
|
18 | 27 | <p> </p>
|
19 | 28 | <p><strong class="example">Example 1:</strong></p>
|
20 | 29 |
|
21 |
| -<pre> |
22 |
| -<strong>Input:</strong> words = ["1","2","prev","prev","prev"] |
23 |
| -<strong>Output:</strong> [2,1,-1] |
24 |
| -<strong>Explanation:</strong> |
25 |
| -For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element. |
26 |
| -For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer. |
27 |
| -For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two. |
28 |
| -</pre> |
| 30 | +<div class="example-block" style=" |
| 31 | + border-color: var(--border-tertiary); |
| 32 | + border-left-width: 2px; |
| 33 | + color: var(--text-secondary); |
| 34 | + font-size: .875rem; |
| 35 | + line-height: 1.25rem; |
| 36 | + margin-bottom: 1rem; |
| 37 | + margin-top: 1rem; |
| 38 | + overflow: visible; |
| 39 | + padding-left: 1rem; |
| 40 | +"> |
| 41 | +<p><strong>Input:</strong> <code>nums = [1,2,-1,-1,-1]</code></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <code>[2,1,-1]</code></p> |
| 44 | + |
| 45 | +<p><strong>Explanation:</strong> Start with <code>seen = []</code> and <code>ans = []</code>.</p> |
| 46 | + |
| 47 | +<ol> |
| 48 | + <li>Process <code>nums[0]</code>: The first element in nums is <code>1</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [1]</code>.</li> |
| 49 | + <li>Process <code>nums[1]</code>: The next element is <code>2</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [2, 1]</code>.</li> |
| 50 | + <li>Process <code>nums[2]</code>: The next element is <code>-1</code>. This is the first occurrence of <code>-1</code>, so <code>k == 1</code>. We look for the first element in seen. We append <code>2</code> to <code>ans</code>. Now, <code>ans == [2]</code>.</li> |
| 51 | + <li>Process <code>nums[3]</code>: Another <code>-1</code>. This is the second consecutive <code>-1</code>, so <code>k == 2</code>. The second element in <code>seen</code> is <code>1</code>, so we append <code>1</code> to <code>ans</code>. Now, <code>ans == [2, 1]</code>.</li> |
| 52 | + <li>Process <code>nums[4]</code>: Another <code>-1</code>, the third in a row, making <code>k = 3</code>. However, <code>seen</code> only has two elements (<code>[2, 1]</code>). Since <code>k</code> is greater than the number of elements in <code>seen</code>, we append <code>-1</code> to <code>ans</code>. Finally, <code>ans == [2, 1, -1]</code>.</li> |
| 53 | +</ol> |
| 54 | +</div> |
29 | 55 |
|
30 | 56 | <p><strong class="example">Example 2:</strong></p>
|
31 | 57 |
|
32 |
| -<pre> |
33 |
| -<strong>Input:</strong> words = ["1","prev","2","prev","prev"] |
34 |
| -<strong>Output:</strong> [1,2,1] |
35 |
| -<strong>Explanation:</strong> |
36 |
| -For "prev" at index = 1, last visited integer will be 1. |
37 |
| -For "prev" at index = 3, last visited integer will be 2. |
38 |
| -For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer. |
39 |
| -</pre> |
| 58 | +<div class="example-block" style=" |
| 59 | + border-color: var(--border-tertiary); |
| 60 | + border-left-width: 2px; |
| 61 | + color: var(--text-secondary); |
| 62 | + font-size: .875rem; |
| 63 | + line-height: 1.25rem; |
| 64 | + margin-bottom: 1rem; |
| 65 | + margin-top: 1rem; |
| 66 | + overflow: visible; |
| 67 | + padding-left: 1rem; |
| 68 | +"> |
| 69 | +<p><strong>Input:</strong> <code>nums = [1,-1,2,-1,-1]</code></p> |
| 70 | + |
| 71 | +<p><strong>Output:</strong> <code>[1,2,1]</code></p> |
| 72 | + |
| 73 | +<p><strong>Explanation:</strong> Start with <code>seen = []</code> and <code>ans = []</code>.</p> |
| 74 | + |
| 75 | +<ol> |
| 76 | + <li>Process <code>nums[0]</code>: The first element in nums is <code>1</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [1]</code>.</li> |
| 77 | + <li>Process <code>nums[1]</code>: The next element is <code>-1</code>. This is the first occurrence of <code>-1</code>, so <code>k == 1</code>. We look for the first element in <code>seen</code>, which is <code>1</code>. Append <code>1</code> to <code>ans</code>. Now, <code>ans == [1]</code>.</li> |
| 78 | + <li>Process <code>nums[2]</code>: The next element is <code>2</code>. Prepend this to the front of <code>seen</code>. Now, <code>seen == [2, 1]</code>.</li> |
| 79 | + <li>Process <code>nums[3]</code>: The next element is <code>-1</code>. This <code>-1</code> is not consecutive to the first <code>-1</code> since <code>2</code> was in between. Thus, <code>k</code> resets to <code>1</code>. The first element in <code>seen</code> is <code>2</code>, so append <code>2</code> to <code>ans</code>. Now, <code>ans == [1, 2]</code>.</li> |
| 80 | + <li>Process <code>nums[4]</code>: Another <code>-1</code>. This is consecutive to the previous <code>-1</code>, so <code>k == 2</code>. The second element in <code>seen</code> is <code>1</code>, append <code>1</code> to <code>ans</code>. Finally, <code>ans == [1, 2, 1]</code>.</li> |
| 81 | +</ol> |
| 82 | +</div> |
40 | 83 |
|
41 | 84 | <p> </p>
|
42 | 85 | <p><strong>Constraints:</strong></p>
|
43 | 86 |
|
44 | 87 | <ul>
|
45 |
| - <li><code>1 <= words.length <= 100</code></li> |
46 |
| - <li><code>words[i] == "prev"</code> or <code>1 <= int(words[i]) <= 100</code></li> |
| 88 | + <li><code>1 <= nums.length <= 100</code></li> |
| 89 | + <li><code>nums[i] == -1</code> or <code>1 <= nums[i] <= 100</code></li> |
47 | 90 | </ul>
|
48 | 91 |
|
49 | 92 | ## Solutions
|
|
0 commit comments