File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 2137
2137
3024|[ Type of Triangle] ( ./solutions/3024-type-of-triangle.js ) |Easy|
2138
2138
3025|[ Find the Number of Ways to Place People I] ( ./solutions/3025-find-the-number-of-ways-to-place-people-i.js ) |Medium|
2139
2139
3027|[ Find the Number of Ways to Place People II] ( ./solutions/3027-find-the-number-of-ways-to-place-people-ii.js ) |Hard|
2140
+ 3028|[ Ant on the Boundary] ( ./solutions/3028-ant-on-the-boundary.js ) |Easy|
2140
2141
3042|[ Count Prefix and Suffix Pairs I] ( ./solutions/3042-count-prefix-and-suffix-pairs-i.js ) |Easy|
2141
2142
3066|[ Minimum Operations to Exceed Threshold Value II] ( ./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js ) |Medium|
2142
2143
3068|[ Find the Maximum Sum of Node Values] ( ./solutions/3068-find-the-maximum-sum-of-node-values.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 3028. Ant on the Boundary
3
+ * https://leetcode.com/problems/ant-on-the-boundary/
4
+ * Difficulty: Easy
5
+ *
6
+ * An ant is on a boundary. It sometimes goes left and sometimes right.
7
+ *
8
+ * You are given an array of non-zero integers nums. The ant starts reading nums from the
9
+ * first element of it to its end. At each step, it moves according to the value of the
10
+ * current element:
11
+ * - If nums[i] < 0, it moves left by -nums[i] units.
12
+ * - If nums[i] > 0, it moves right by nums[i] units.
13
+ *
14
+ * Return the number of times the ant returns to the boundary.
15
+ *
16
+ * Notes:
17
+ * - There is an infinite space on both sides of the boundary.
18
+ * - We check whether the ant is on the boundary only after it has moved |nums[i]| units.
19
+ * In other words, if the ant crosses the boundary during its movement, it does not count.
20
+ */
21
+
22
+ /**
23
+ * @param {number[] } nums
24
+ * @return {number }
25
+ */
26
+ var returnToBoundaryCount = function ( nums ) {
27
+ let position = 0 ;
28
+ let result = 0 ;
29
+
30
+ for ( const step of nums ) {
31
+ position += step ;
32
+ if ( position === 0 ) {
33
+ result ++ ;
34
+ }
35
+ }
36
+
37
+ return result ;
38
+ } ;
You can’t perform that action at this time.
0 commit comments