Skip to content

Commit f1629cd

Browse files
author
Mohammad
committed
leet code mediums
1 parent c9d6604 commit f1629cd

14 files changed

Lines changed: 368 additions & 2 deletions

File tree

src/test/kotlin/com/igorwojda/integer/digitfrequency/Challenge.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ private fun processMap(data: String): MutableMap<String, Int> {
3131
return counterMap
3232
}
3333

34+
35+
private fun sortWithFreq(inputList: List<Int>): List<Int> {
36+
val countFreq: MutableMap<Int, Int> = mutableMapOf<Int, Int>()
37+
38+
for (num in inputList) {
39+
countFreq[num] = (countFreq[num] ?: 0) + 1
40+
}
41+
42+
return emptyList()
43+
}
44+
3445
private class Test {
3546
@Test
3647
fun `'789' and '897' have the same digit frequency`() {

src/test/kotlin/com/igorwojda/integer/pyramidgenerator/Challenge.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ import org.amshove.kluent.shouldBeEqualTo
44
import org.junit.jupiter.api.Test
55

66
fun generatePyramid(n: Int): List<String> {
7-
TODO("not implemented")
7+
// two loops, space usually (n - i) and # is 2*i-1
8+
val hash = "#"
9+
val spacer = " "
10+
val resultList: MutableList<String> = mutableListOf()
11+
(1..n).forEach {
12+
resultList.add(hash.repeat(2 * it - 1))
13+
}
14+
println(resultList)
15+
return resultList
816
}
917

1018
private class Test {

src/test/kotlin/com/igorwojda/integer/stepsgenerator/Challenge.kt

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,29 @@ import org.amshove.kluent.shouldBeEqualTo
44
import org.junit.jupiter.api.Test
55

66
fun generateSteps(n: Int): List<String> {
7-
TODO("not implemented")
7+
val result: MutableList<String> = mutableListOf<String>()
8+
//val str = "#"
9+
//val spacer = " "
10+
/*for (i in 1 .. n) {
11+
result.add((str.repeat(i)) + spacer.repeat(n - i))
12+
}
13+
println(result)*/
14+
// Without repeat, we need a total of three loops: 1. exact n times (rows), 2. for no. of "#", 3. for space (n - i)
15+
16+
for (i in 1 .. n) {
17+
var str = ""
18+
var storeSpacer = ""
19+
val spacer = " "
20+
for (j in 1 .. i) {
21+
str += "#"
22+
(j .. (n - i)).forEach {
23+
storeSpacer += spacer
24+
}
25+
}
26+
result.add(str + storeSpacer)
27+
}
28+
println(result)
29+
return result
830
}
931

1032
private class Test {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Need to learn:
2+
* HashMap internals and collections in general
3+
• What is hash collision and Why hash collisions are rare
4+
• How HashSet avoids duplicates
5+
• Big-O cheat sheet for Kotlin collections
6+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
fun main() {
4+
println(findDuplicates(listOf<Int>(1, 2, 2, 3, 4, 2, 3, 6, 4, 7, 8, 3)))
5+
}
6+
7+
// return the duplicate numbers
8+
fun findDuplicates(inputList: List<Int>): List<Int> {
9+
// declare a map and use the input value to process and set the numbers freq
10+
val freqMap: MutableMap<Int, Int> = mutableMapOf<Int, Int>()
11+
val resultList: MutableList<Int> = mutableListOf()
12+
for (i in inputList) {
13+
freqMap[i] = (freqMap[i] ?: 0) + 1
14+
}
15+
16+
freqMap.forEach { (value, count) ->
17+
if (count > 1) {
18+
resultList.add(value)
19+
}
20+
}
21+
return resultList
22+
}
23+
24+
/*
25+
also learn about how to add prefix values in existing list?
26+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
import java.lang.Integer.max
4+
5+
fun main() {
6+
println(longestSubString("abcghsccdef"))
7+
}
8+
9+
fun longestSubString(inputStr: String): Int {
10+
var maxLength = 0
11+
val setChars = mutableSetOf<Char>()
12+
var start = 0
13+
14+
for(end in inputStr.indices) {
15+
val currentChar = inputStr[end]
16+
// use while, not if, because the window may contain multiple copies of the same chars 'bbb'
17+
while (setChars.contains(currentChar)) {
18+
setChars.remove(inputStr[start])
19+
start ++
20+
}
21+
setChars.add(currentChar)
22+
maxLength = max(maxLength, end - start + 1)
23+
}
24+
25+
return maxLength
26+
}
27+
28+
/**
29+
* for (end in s.indices) {
30+
* while (duplicate) shrink from left
31+
* add char at end
32+
* update max (maxLength, end-start + 1)
33+
* }
34+
*
35+
* O(n) due to sign operation of add or remove and o(1) for space due to unique chars
36+
*/
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
fun main() {
4+
println(majorityNumber(listOf<Int>(1, 2, 3, 3, 3, 2, 3, 2, 3, 3, 3, 5, 6)))
5+
}
6+
7+
fun majorityNumber(inputList: List<Int>): Int {
8+
// store the freq of numbers in a map
9+
val freqMap = mutableMapOf<Int, Int>()
10+
11+
inputList.forEach {
12+
freqMap[it] = (freqMap[it] ?: 0) + 1
13+
}
14+
15+
for (i in freqMap.keys) {
16+
val counter = freqMap[i]
17+
if (counter != null && counter > inputList.size / 2) {
18+
return i
19+
}
20+
}
21+
return 0
22+
}
23+
24+
fun majorityElementMooreSolution(inputList: List<Int>): Int {
25+
var candidate = 0
26+
var count = 0
27+
for (num in inputList) {
28+
if (count == 0) {
29+
candidate = num
30+
}
31+
32+
if (num == candidate) {
33+
count++
34+
} else {
35+
count--
36+
}
37+
}
38+
39+
return candidate
40+
}
41+
42+
/*
43+
. When count hits 0, pick a new candidate
44+
• Increase count when numbers match
45+
• Decrease count when they differ
46+
• Final candidate is majority element
47+
No maps, no sorting, no extra loops.
48+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
import kotlin.math.max
4+
5+
fun main() {
6+
println(maxSubArraySum(listOf<Int>(-1, 0, 1, 2, 3, -2, 1, 4, 5)))
7+
}
8+
9+
fun maxSubArraySum(inputList: List<Int>): Int {
10+
var currentSum = inputList[0]
11+
var maxSum = inputList[0]
12+
13+
// Should I start a new subarray from this number?
14+
// Or should I extend the previous running sum?
15+
// update the maxSum to keep a track and return
16+
for (i in 1..inputList.size - 1) {
17+
currentSum = max(inputList[i], inputList[i] + currentSum)
18+
maxSum = max(maxSum, currentSum)
19+
}
20+
return maxSum
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
fun main() {
4+
println(findMissingNumber(listOf<Int>(0)))
5+
}
6+
7+
// Only one number will be missing and the input range is always n + 1 inclusive of 0
8+
fun findMissingNumber(inputList: List<Int>): Int {
9+
// Let's use a set to store the numbers and lookup against the input list that will be o(1) for lookup
10+
val elementSet: MutableSet<Int> = mutableSetOf()
11+
12+
inputList.forEach {
13+
elementSet.add(it)
14+
}
15+
16+
for (i in 0..inputList.size) {
17+
if (!elementSet.contains(i)) return i
18+
}
19+
20+
return 0
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.sample.umar.leetcodemediums
2+
3+
fun main() {
4+
println(moveZeros(listOf<Int>(1, 3, 4, 5, 6, 0, 1, 4, 0, 3, 2, 0, 9)))
5+
}
6+
7+
8+
fun moveZeros(inputList: List<Int>): List<Int> {
9+
// filter non zeros from the give list and move it to a result list
10+
val resultList = mutableListOf<Int>()
11+
val inputSize = inputList.size
12+
13+
for (i in inputList.indices) {
14+
if (inputList[i] != 0) {
15+
resultList.add(inputList[i])
16+
}
17+
}
18+
19+
println(resultList)
20+
21+
// filtered and added all non-zero elements now get the difference in size
22+
val zeroSize = inputSize - resultList.size
23+
24+
(0 until zeroSize).forEach { _ ->
25+
resultList.add(0)
26+
}
27+
28+
return resultList
29+
}

0 commit comments

Comments
 (0)