Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cf8f743

Browse files
authoredApr 19, 2025
[iam-edwin] WEEK 01 solutions (#1279)
* contains duplicate * two sum * top k frequent elements * longest consecutive sequence * house robber
1 parent 575c4a9 commit cf8f743

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed
 

‎contains-duplicate/iam-edwin.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
fun containsDuplicate(nums: IntArray): Boolean {
3+
return nums.size != nums.distinct().size
4+
}
5+
}

‎house-robber/iam-edwin.java

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

‎top-k-frequent-elements/iam-edwin.kt

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

‎two-sum/iam-edwin.kt

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

0 commit comments

Comments
 (0)
Please sign in to comment.