File size: 5,214 Bytes
24e07f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | {
"id": 3311,
"name": "ant_on_the_boundary",
"difficulty": "Easy",
"link": "https://leetcode.com/problems/ant-on-the-boundary/",
"date": "2024-01-28 00:00:00",
"task_description": "An ant is on a boundary. It sometimes goes **left** and sometimes **right**. 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: If `nums[i] < 0`, it moves **left** by `-nums[i]` units. If `nums[i] > 0`, it moves **right** by `nums[i]` units. Return _the number of times the ant **returns** to the boundary._ **Notes:** There is an infinite space on both sides of the boundary. 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. **Example 1:** ``` **Input:** nums = [2,3,-5] **Output:** 1 **Explanation:** After the first step, the ant is 2 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is on the boundary. So the answer is 1. ``` **Example 2:** ``` **Input:** nums = [3,2,-3,-4] **Output:** 0 **Explanation:** After the first step, the ant is 3 steps to the right of the boundary. After the second step, the ant is 5 steps to the right of the boundary. After the third step, the ant is 2 steps to the right of the boundary. After the fourth step, the ant is 2 steps to the left of the boundary. The ant never returned to the boundary, so the answer is 0. ``` **Constraints:** `1 <= nums.length <= 100` `-10 <= nums[i] <= 10` `nums[i] != 0`",
"public_test_cases": [
{
"label": "Example 1",
"input": "nums = [2,3,-5]",
"output": "1 "
},
{
"label": "Example 2",
"input": "nums = [3,2,-3,-4]",
"output": "0 "
}
],
"private_test_cases": [
{
"input": [
-10,
9,
-10,
10,
1,
-8,
5,
9,
-7,
-9,
2,
-8,
-1,
-5,
-7,
7,
-5,
-2,
-3,
9,
-9,
-9,
9,
-3,
-7,
-6,
-9,
-7,
0,
4,
-1,
-10,
0,
8,
8,
-7,
-1,
3,
8,
-3,
-1,
-6
],
"output": 1
},
{
"input": [
-6,
0,
7,
-4,
-2,
5,
3,
4,
3,
-7,
4,
-6,
7,
8,
-1,
5,
1,
10,
-7,
-6,
1,
-5,
3,
8,
-6,
5,
-8,
10,
-4,
3,
-10,
-3,
-1,
10,
9,
10,
-9,
10,
-3,
-3,
-6,
0,
7,
4,
2,
0,
7,
2,
-6
],
"output": 1
},
{
"input": [
-2,
5,
1,
-2,
7,
-6,
6,
2,
-2,
-10,
10,
8,
-3,
6
],
"output": 0
},
{
"input": [
-2,
-7,
-7,
-10,
-10
],
"output": 0
},
{
"input": [
-10,
-9,
0
],
"output": 0
}
],
"haskell_template": "returnToBoundaryCount :: [Int] -> Int\nreturnToBoundaryCount nums ",
"ocaml_template": "let returnToBoundaryCount (nums: int list) : int = ",
"scala_template": "def returnToBoundaryCount(nums: List[Int]): Int = { \n \n}",
"java_template": "class Solution {\n public int returnToBoundaryCount(int[] nums) {\n \n }\n}",
"python_template": "class Solution(object):\n def returnToBoundaryCount(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n "
} |