Skip to content

[iam-edwin] WEEK 01 solutions #1279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contains-duplicate/iam-edwin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
fun containsDuplicate(nums: IntArray): Boolean {
return nums.size != nums.distinct().size
}
}
13 changes: 13 additions & 0 deletions house-robber/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int rob(int[] nums) {
int[] result = new int[nums.length + 2];

for (int index = nums.length - 1; index >= 0; index--) {
int robFirst = nums[index] + result[index + 2];
int passFirst = result[index + 1];
result[index] = Integer.max(robFirst, passFirst);
}

return result[0];
}
}
26 changes: 26 additions & 0 deletions longest-consecutive-sequence/iam-edwin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = Arrays.stream(nums)
.boxed()
.collect(Collectors.toSet());

return set.stream()
.map(num -> {
if (set.contains(num - 1)) {
return 0;
}

int consecutiveLength = 1;

while (set.contains(num + 1)) {
consecutiveLength += 1;
num += 1;
}

return consecutiveLength;
})
.mapToInt(num -> num)
.max()
.orElse(0);
}
}
10 changes: 10 additions & 0 deletions top-k-frequent-elements/iam-edwin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
fun topKFrequent(nums: IntArray, k: Int): IntArray {
val countMap = nums.groupBy { it }
.mapValues { it.value.size }
val sortedList = countMap.entries
.sortedByDescending { it.value }
.map { it.key }
return sortedList.subList(0, k).toIntArray()
}
}
22 changes: 22 additions & 0 deletions two-sum/iam-edwin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val indexMap = nums.mapIndexed { index, num -> num to index }
.groupBy({ it.first }, {it.second})

indexMap.forEach { (firstNum, firstIndicies) ->
val secondNum = target - firstNum
if (firstNum == secondNum) {
if (firstIndicies.size > 1) {
return intArrayOf(firstIndicies[0], firstIndicies[1])
}
} else {
val secondIndicies = indexMap[secondNum]
if (secondIndicies != null) {
return intArrayOf(firstIndicies[0], secondIndicies[0])
}
}
}

return intArrayOf()
}
}