|
| 1 | +--- |
| 2 | +id: ant-on-the-boundary |
| 3 | +title: Ant on the Boundary (LeetCode) |
| 4 | +sidebar_label: 3028-AntOnTheBoundary |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Simulation |
| 8 | + - Boundary Conditions |
| 9 | +description: Determine how many times an ant returns to the boundary after reading an array of non-zero integers and moving according to their values. |
| 10 | +sidebar_position: 3028 |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 16 | +| :---------------- | :------------ | :--------------- | |
| 17 | +| [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary/) | [Ant on the Boundary Solution on LeetCode](https://leetcode.com/problems/ant-on-the-boundary/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +An ant is on a boundary. It sometimes goes left and sometimes right. |
| 22 | + |
| 23 | +You are given an array of non-zero integers `nums`. The ant starts reading `nums` from the first element of it to its end. At each step, it moves according to the value of the current element: |
| 24 | + |
| 25 | +- If `nums[i] < 0`, it moves left by `-nums[i]` units. |
| 26 | +- If `nums[i] > 0`, it moves right by `nums[i]` units. |
| 27 | + |
| 28 | +Return the number of times the ant returns to the boundary. |
| 29 | + |
| 30 | +### Notes: |
| 31 | +- There is infinite space on both sides of the boundary. |
| 32 | +- We check whether the ant is on the boundary only after it has moved `|nums[i]|` units. In other words, if the ant crosses the boundary during its movement, it does not count. |
| 33 | + |
| 34 | +### Example 1 |
| 35 | +- **Input:** `nums = [2,3,-5]` |
| 36 | +- **Output:** `1` |
| 37 | +- **Explanation:** |
| 38 | + - After the first step, the ant is 2 steps to the right of the boundary. |
| 39 | + - After the second step, the ant is 5 steps to the right of the boundary. |
| 40 | + - After the third step, the ant is on the boundary. |
| 41 | + - So the answer is 1. |
| 42 | + |
| 43 | +### Example 2 |
| 44 | +- **Input:** `nums = [3,2,-3,-4]` |
| 45 | +- **Output:** `0` |
| 46 | +- **Explanation:** |
| 47 | + - After the first step, the ant is 3 steps to the right of the boundary. |
| 48 | + - After the second step, the ant is 5 steps to the right of the boundary. |
| 49 | + - After the third step, the ant is 2 steps to the right of the boundary. |
| 50 | + - After the fourth step, the ant is 2 steps to the left of the boundary. |
| 51 | + - The ant never returned to the boundary, so the answer is 0. |
| 52 | + |
| 53 | +### Constraints |
| 54 | +- `1 <= nums.length <= 100` |
| 55 | +- `-10 <= nums[i] <= 10` |
| 56 | +- `nums[i] != 0` |
| 57 | + |
| 58 | +## Approach |
| 59 | + |
| 60 | +To determine how many times the ant returns to the boundary, we can use a simple simulation approach. Here's the approach: |
| 61 | + |
| 62 | +1. Initialize a variable `position` to keep track of the ant's current position relative to the boundary. |
| 63 | +2. Initialize a counter `boundary_count` to keep track of the number of times the ant returns to the boundary. |
| 64 | +3. Iterate through each element in `nums`: |
| 65 | + - Update the `position` based on the value of the current element. |
| 66 | + - Check if the `position` is zero (i.e., the ant is back at the boundary). |
| 67 | + - If the ant is back at the boundary, increment the `boundary_count`. |
| 68 | +4. Return the `boundary_count`. |
| 69 | + |
| 70 | +### Solution Code |
| 71 | + |
| 72 | +#### Python |
| 73 | + |
| 74 | +```python |
| 75 | +class Solution: |
| 76 | + def returnToBoundaryCount(self, nums: List[int]) -> int: |
| 77 | + position = 0 |
| 78 | + boundary_count = 0 |
| 79 | + |
| 80 | + for num in nums: |
| 81 | + position += num |
| 82 | + if position == 0: |
| 83 | + boundary_count += 1 |
| 84 | + |
| 85 | + return boundary_count |
| 86 | +``` |
| 87 | + |
| 88 | +#### C++ |
| 89 | +```c++ |
| 90 | +#include <vector> |
| 91 | +using namespace std; |
| 92 | + |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + int returnToBoundaryCount(vector<int>& nums) { |
| 96 | + int position = 0; |
| 97 | + int boundary_count = 0; |
| 98 | + |
| 99 | + for (int num : nums) { |
| 100 | + position += num; |
| 101 | + if (position == 0) { |
| 102 | + boundary_count += 1; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return boundary_count; |
| 107 | + } |
| 108 | +}; |
| 109 | + |
| 110 | +``` |
| 111 | +
|
| 112 | +#### Java |
| 113 | +```java |
| 114 | +class Solution { |
| 115 | + public int returnToBoundaryCount(int[] nums) { |
| 116 | + int position = 0; |
| 117 | + int boundary_count = 0; |
| 118 | + |
| 119 | + for (int num : nums) { |
| 120 | + position += num; |
| 121 | + if (position == 0) { |
| 122 | + boundary_count += 1; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return boundary_count; |
| 127 | + } |
| 128 | +} |
| 129 | +
|
| 130 | +``` |
| 131 | + |
| 132 | +### Conclusion |
| 133 | +The problem of determining how many times an ant returns to the boundary can be effectively solved |
| 134 | +using a straightforward simulation approach. By keeping track of the ant's position as it moves |
| 135 | +according to the values in the array, and counting each time the position returns to zero, we can |
| 136 | +determine the desired result. The provided solutions in Python, C++, and Java demonstrate this |
| 137 | +approach, ensuring the solution is efficient and easy to understand across different programming |
| 138 | +languages. |
0 commit comments