File tree Expand file tree Collapse file tree 5 files changed +76
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun containsDuplicate (nums : IntArray ): Boolean {
3+ return nums.size != nums.distinct().size
4+ }
5+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int rob (int [] nums ) {
3+ int [] result = new int [nums .length + 2 ];
4+
5+ for (int index = nums .length - 1 ; index >= 0 ; index --) {
6+ int robFirst = nums [index ] + result [index + 2 ];
7+ int passFirst = result [index + 1 ];
8+ result [index ] = Integer .max (robFirst , passFirst );
9+ }
10+
11+ return result [0 ];
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int longestConsecutive (int [] nums ) {
3+ Set <Integer > set = Arrays .stream (nums )
4+ .boxed ()
5+ .collect (Collectors .toSet ());
6+
7+ return set .stream ()
8+ .map (num -> {
9+ if (set .contains (num - 1 )) {
10+ return 0 ;
11+ }
12+
13+ int consecutiveLength = 1 ;
14+
15+ while (set .contains (num + 1 )) {
16+ consecutiveLength += 1 ;
17+ num += 1 ;
18+ }
19+
20+ return consecutiveLength ;
21+ })
22+ .mapToInt (num -> num )
23+ .max ()
24+ .orElse (0 );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun topKFrequent (nums : IntArray , k : Int ): IntArray {
3+ val countMap = nums.groupBy { it }
4+ .mapValues { it.value.size }
5+ val sortedList = countMap.entries
6+ .sortedByDescending { it.value }
7+ .map { it.key }
8+ return sortedList.subList(0 , k).toIntArray()
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun twoSum (nums : IntArray , target : Int ): IntArray {
3+ val indexMap = nums.mapIndexed { index, num -> num to index }
4+ .groupBy({ it.first }, {it.second})
5+
6+ indexMap.forEach { (firstNum, firstIndicies) ->
7+ val secondNum = target - firstNum
8+ if (firstNum == secondNum) {
9+ if (firstIndicies.size > 1 ) {
10+ return intArrayOf(firstIndicies[0 ], firstIndicies[1 ])
11+ }
12+ } else {
13+ val secondIndicies = indexMap[secondNum]
14+ if (secondIndicies != null ) {
15+ return intArrayOf(firstIndicies[0 ], secondIndicies[0 ])
16+ }
17+ }
18+ }
19+
20+ return intArrayOf()
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments