LeetCode 414. Third Maximum Number Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

I'm really feeling bad right now. it took me 4 hours to solve partially solve this problem even though my first attempt was the same as nick's attempt but I didn't do the "continue" check because i thought this will not working so my brain kinda discarded it. I have solved some medium problems in a day previously but this easy one took me like 4 hours.

I feel like I'm not good enough even though I've been working in the software industry for a few years now. @Nick White how long did it take you solve this problem?

ninehoursful
Автор

Wow your solution is so easy to understand. Thanks

ploratran
Автор

When Nick says the problem is pretty I'm F*cked!

marcoalanis
Автор

That's a neat solution.
In C++ you can use optional<int> to get the behaviour of Integer in Java.

xxxyyyzzz
Автор

Great Idea Nick. It's easy to understand.

saravanansarangan
Автор

thanks man! after trying a lot I decided to take help and found your video. thanks again.

SandeepSharma-srrj
Автор

Record the sound in mono. Sound is not even on both sides.

a.yashwanth
Автор

The easiest way to understand this problem is by using heap:
class Heap:
def __init__(self):
self.f = 0
self.s = 0
self.t = 0
def add(self, val):
if val > self.f:
self.t = self.s
self.s = self.f
self.f = val
elif val < self.f and val > self.s:
self.t = self.s
self.s = val
elif val < self.s and val > self.t:
self.t = val


def find_third_max(input):
heap = Heap()
for val in input:
heap.add(val)
return heap.t

jugsma
Автор

Code fails when input is [1, 2, 0]. 2 will be returned instead of zero

santoshrathod
Автор

Hi Nike:- can you check below code? is it correct implementation from complexity and performance point of view.
package io.Algorithm;

import java.util.Arrays;
import java.util.Stack;

public class ThirdMax {
public static int thidMax(int[] nums) {
if(nums==null || nums.length==0) {
return 0;
}
Stack<Integer> stack = new Stack<Integer>();
Arrays.sort(nums);
for(int k=0;k<nums.length-1;k++) {
if(nums[k]!=nums[k+1]) {
stack.push(nums[k]);
}
}


if(nums.length>=3) {
return stack.get(stack.size()-3);
} else {
return stack.get(stack.size()-1);
}

}

public static void main(String[] args) {
int[] nums= {1, 2, 2, 2, 2, 2, 2, 2, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 9, 9, 9};
int res= ThirdMax.thidMax(nums);
System.out.println("Result:- "+res);

}

}
please give your view.

sameer
visit shbcf.ru