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

Commit 4c1b04b

Browse files
authored
Create Maximize the Topmost Element After K Moves.java
1 parent 72ce4fc commit 4c1b04b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maximumTop(int[] nums, int k) {
3+
if (nums.length == 1) {
4+
return k % 2 == 1 ? -1 : nums[0];
5+
}
6+
int maxTop = -1;
7+
if (k < nums.length) {
8+
maxTop = nums[k];
9+
} else if (k > nums.length) {
10+
k = nums.length + 1;
11+
}
12+
for (int i = 0; i < k - 1; i++) {
13+
maxTop = Math.max(maxTop, nums[i]);
14+
}
15+
return maxTop;
16+
}
17+
}

0 commit comments

Comments
 (0)