Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d50ada2

Browse files
Merge pull request #6 from sumanas27/latest-algos
LatestUniqueOrder, MaximumSubArray, TopKElements, Zalando tech screening
2 parents 412bcfe + 7e70cb1 commit d50ada2

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
class LatestUniqueOrder {
4+
5+
fun latestUniqueOrders(orders: List<Order>): List<Order> {
6+
val latestMap = mutableMapOf<String, Order>()
7+
8+
for(order in orders){
9+
val current = latestMap[order.id]
10+
if(current == null || order.timestamp > current.timestamp){
11+
latestMap[order.id] = order
12+
}
13+
}
14+
return latestMap.values.toList()
15+
}
16+
}
17+
18+
data class Order(val id: String, val timestamp: Long)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
import kotlin.math.max
4+
5+
/**
6+
* Problem Statement:
7+
* Given an integer array nums, find the contiguous subarray (containing at least one number)
8+
* that has the largest sum, and return the sum.
9+
*
10+
* Input
11+
* val nums = intArrayOf(-2, 1, -3, 4, -1, 2, 1, -5, 4)
12+
*
13+
* Output
14+
* 6
15+
*
16+
* Explanation
17+
* The subarray [4, -1, 2, 1] has the largest sum = 6.
18+
* */
19+
20+
class MaximumSubArray {
21+
22+
fun maximumSubArray(input: IntArray): Int{
23+
24+
var currentSum = input[0]
25+
var maxSum = input[0]
26+
27+
for (element in input){
28+
currentSum = currentSum + element
29+
maxSum = max(maxSum, currentSum)
30+
currentSum = max(currentSum, 0)
31+
}
32+
return maxSum
33+
}
34+
}
35+
36+
fun main(){
37+
38+
val maximumSubArray = MaximumSubArray()
39+
println(maximumSubArray.maximumSubArray(intArrayOf(2,3,-8,7,-1, 2,3)))
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* val nums = listOf("apple", "banana", "apple", "orange", "banana", "apple")
5+
* val k = 2
6+
* */
7+
8+
9+
class TopKElements {
10+
11+
fun topKFrequent(nums: List<String>, k: Int): List<String>{
12+
13+
return nums.groupingBy { it }
14+
.eachCount() //{"apple" -> 3, "banana" -> 2, "orange" -> 1}
15+
.entries // [Map.Entry("apple" → 3), Map.Entry("banana" → 2), ...]
16+
.sortedByDescending { it.value } // ["apple" (3), "banana" (2), "orange" (1)]
17+
.take(2)
18+
.map { it.key } // ["apple", "banana"]
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main.kotlin.zalando
2+
3+
/**
4+
* Problem Statement:
5+
* You are given an array of integers, where each element represents a product value.
6+
* Return a new array result[] such that:
7+
*
8+
* result[i] is equal to the product of all elements in the original array except arr[i].
9+
*
10+
* [1, 2, 3, 4] => [24, 12, 8, 6]
11+
* */
12+
13+
class FetchProductExcludingCurrent {
14+
15+
fun fetchProduct(input: List<Int>): List<Int> {
16+
17+
var currentProduct = 1
18+
val result = mutableListOf<Int>()
19+
for (i in 0..input.size - 1) {
20+
for (j in 0..input.size - 1) {
21+
if (i == j) continue
22+
currentProduct = currentProduct * input[j]
23+
}
24+
result.add(currentProduct)
25+
currentProduct = 1
26+
}
27+
28+
println(result)
29+
return result
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /