def second_max(nums):
first=second=float('-inf')
for n in nums:
if n>first: second=first; first=n
elif n>second: second=n
return second
public int secondMax(int[] nums){
int first=Integer.MIN_VALUE,second=Integer.MIN_VALUE;
for(int n:nums){
if(n>first){second=first;first=n;}
else if(n>second) second=n;
} return second;
}
float('-inf') vs Integer.MIN_VALUE. elif vs else if.
def second_max(nums):
first=second=float('-inf')
for n in nums:
if n>first: second=first; first=n
elif n>second: second=n
return second
public int secondMax(int[] nums){
int first=Integer.MIN_VALUE,second=Integer.MIN_VALUE;
for(int n:nums){
if(n>first){second=first;first=n;}
else if(n>second) second=n;
} return second;
}
1. Track top two values