3028. Ant on the Boundary Easy
1/**
2 * [3028] Ant on the Boundary
3 *
4 * An ant is on a boundary. It sometimes goes left and sometimes right.
5 * 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:
6 *
7 * If nums[i] < 0, it moves left by<!-- notionvc: 55fee232-4fc9-445f-952a-f1b979415864 --> -nums[i] units.
8 * If nums[i] > 0, it moves right by nums[i] units.
9 *
10 * Return the number of times the ant returns to the boundary.
11 * Notes:
12 *
13 * There is an infinite space on both sides of the boundary.
14 * 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.<!-- notionvc: 5ff95338-8634-4d02-a085-1e83c0be6fcd -->
15 *
16 *
17 * <strong class="example">Example 1:
18 *
19 * Input: nums = [2,3,-5]
20 * Output: 1
21 * Explanation: After the first step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
22 * After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
23 * After the third step, the ant is on the boundary.
24 * So the answer is 1.
25 *
26 * <strong class="example">Example 2:
27 *
28 * Input: nums = [3,2,-3,-4]
29 * Output: 0
30 * Explanation: After the first step, the ant is 3 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
31 * After the second step, the ant is 5 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
32 * After the third step, the ant is 2 steps to the right of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
33 * After the fourth step, the ant is 2 steps to the left of the boundary<!-- notionvc: 61ace51c-559f-4bc6-800f-0a0db2540433 -->.
34 * The ant never returned to the boundary, so the answer is 0.
35 *
36 *
37 * Constraints:
38 *
39 * 1 <= nums.length <= 100
40 * -10 <= nums[i] <= 10
41 * nums[i] != 0
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/ant-on-the-boundary/
47// discuss: https://leetcode.com/problems/ant-on-the-boundary/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52 pub fn return_to_boundary_count(nums: Vec<i32>) -> i32 {
53 0
54 }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn test_3028() {
65 }
66}
67
Back
© 2025 bowen.ge All Rights Reserved.