Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e81783a

Browse files
committed
Add solution #3028
1 parent f57d616 commit e81783a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@
21372137
3024|[Type of Triangle](./solutions/3024-type-of-triangle.js)|Easy|
21382138
3025|[Find the Number of Ways to Place People I](./solutions/3025-find-the-number-of-ways-to-place-people-i.js)|Medium|
21392139
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|
21402141
3042|[Count Prefix and Suffix Pairs I](./solutions/3042-count-prefix-and-suffix-pairs-i.js)|Easy|
21412142
3066|[Minimum Operations to Exceed Threshold Value II](./solutions/3066-minimum-operations-to-exceed-threshold-value-ii.js)|Medium|
21422143
3068|[Find the Maximum Sum of Node Values](./solutions/3068-find-the-maximum-sum-of-node-values.js)|Hard|

solutions/3028-ant-on-the-boundary.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
};

0 commit comments

Comments
 (0)