Skip to content

Commit 5ce9443

Browse files
author
Mohammad
committed
after ox round
1 parent 812b3ec commit 5ce9443

22 files changed

+804
-4
lines changed

src/test/kotlin/com/sample/umar/SampleTry.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ private fun createEmptyTempFile(fileName: String?, contentType: String?) {
4343
} catch (e: IOException) {
4444
println("Security exception occurred: ${e.message}")
4545
}
46+
4647
}
4748

4849
private fun getFileNamePrefix(fileName: String?): String {

src/test/kotlin/com/sample/umar/leetcodemediums/RotateArray.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fun main() {
77
// Given an array, rotate it to the right by k steps.
88
// Reverse method steps, since it is in-place and o(1) space and o(n) time
99
fun rotateArray(inputList: MutableList<Int>, step: Int): List<Int> {
10-
//normalise the k (reduces the number of rotations that produce same result)
10+
//normalise the k (reduces the number of rotations which produce same result)
1111
val k = step % inputList.size
1212

1313
fun reverse(startIndex: Int, endIndex: Int) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.sample.umar.leetcodemediums.okx
2+
3+
/*
4+
You are given 2 strings of the same length, consisting of 0-1. We call them string A and string B. Now we want to make them become the same string. We only have one operation. That is to select one character in A and swap with another character in B(You can choose any position in the string).
5+
Then what is the minimum number of operations, we can make two strings become the same string.
6+
*/
7+
8+
//Also some questions
9+
/**
10+
* 1. What is memory leaks and how to detect memory leaks in production
11+
* 2. What is fps and how ui is rendered and what process happened during ui rendering
12+
* 3. What is the difference between apk and aab
13+
*
14+
*/
15+
16+
fun main() {
17+
println(minOperationsToMakeEqual("0101", "0011")) // Output: 1
18+
println(minOperationsToMakeEqual("01010", "10100")) // Output: 2
19+
println(minOperationsToMakeEqual("011", "000")) // Output: -1
20+
}
21+
22+
fun minOperationsToMakeEqual(A: String, B: String): Int {
23+
if (A.length != B.length) return -1
24+
25+
var count01 = 0
26+
var count10 = 0
27+
28+
for (i in A.indices) {
29+
if (A[i] == '0' && B[i] == '1') count01++
30+
if (A[i] == '1' && B[i] == '0') count10++
31+
}
32+
33+
return if (count01 == count10) count01 else -1
34+
}

src/test/kotlin/com/sample/umar/leetcodemediums/practice/BackSpaceComparePrac.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fun backSpaceCompare(str1: String, str2: String): Boolean {
7777
• You skip all characters that should be removed
7878
• You stop on the next valid letter: break
7979
2. Move j left same way.
80-
3. Compare the valid characters at i and j.
80+
3. Compare the valid characters at i and j if mismatch return false.
8181
4. Else If mismatch in size → return false.
8282
5. Else decrement both the pointers
8383
6. If both reach past the beginning → return true.

src/test/kotlin/com/sample/umar/leetcodemediums/practice/BinaryInsertPositionPrac.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package com.sample.umar.leetcodemediums.practice
44
* Given a sorted list of integers and a target value, return the index where the target should be inserted.
55
* Requirements:
66
* • Use binary search
7-
* • O(log n) time
7+
* • O(log n) time (Anything we divide)
88
* • O(1) space
99
* • If target exists → return its index
1010
* • If not → return the index where it should be inserted
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.sample.umar.leetcodemediums.practice
2+
3+
/**
4+
* maxProfit(listOf(7,1,5,3,6,4)) // returns 5
5+
*
6+
* Algo:
7+
* Two pointer approach: create buy and sell planned for next day to arrive max profit on the given list of prises
8+
* If the value of sell is greater the value of buy then compute (sell - buy) to arrive max profit
9+
* Else assign the buy as sell and continue until arrive we arrive profit by moving sell pointer ahead
10+
*
11+
* O(n)
12+
*/
13+
14+
15+
fun main() {
16+
println(maxProfit(listOf(7, 1, 5, 3, 6, 4)))
17+
}
18+
19+
fun maxProfit(inputList: List<Int>): Int {
20+
// two pointer approach
21+
var buy = 0
22+
var sell = 1
23+
var maxProfit = 0
24+
25+
while (sell < inputList.size) {
26+
if (inputList[sell] > inputList[buy]) {
27+
// Profit made
28+
val profit = inputList[sell] - inputList[buy]
29+
maxProfit = maxOf(maxProfit, profit)
30+
} else {
31+
// find better price to buy
32+
buy = sell
33+
}
34+
sell++
35+
}
36+
37+
return maxProfit
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.sample.umar.leetcodemediums.practice
2+
3+
/**
4+
* 1. Create a map of <String, MutableList<String>>
5+
*
6+
* 2. Loop through each word in the input list:
7+
* - Sort the characters of the word → this becomes the key
8+
* - If this sorted key is NOT in the map:
9+
* create a new empty list for that key
10+
* - Add the original word to the list stored under this key
11+
*
12+
* 3. After processing all words:
13+
* return all the values of the map (each value is a group of anagrams)
14+
*
15+
* ⏱ Time:
16+
*
17+
* O(n · k log k)
18+
* (n strings, each sorted with length k)
19+
*
20+
* 💾 Space:
21+
*
22+
* O(n · k)
23+
* (groups stored in map)
24+
*
25+
* Time Complexity:
26+
*
27+
* “For each of n strings I sort its characters, which costs O(k log k).
28+
* Therefore the total complexity is O(n · k log k).”
29+
*
30+
* Space Complexity:
31+
*
32+
* “I store groups of anagrams inside a map.
33+
* In the worst case that requires O(nk) space.”
34+
*/
35+
36+
fun main() {
37+
println(groupAnagrams(listOf<String>("eat", "tea", "tan", "ate", "nat", "bat")))
38+
}
39+
40+
fun groupAnagrams(inputString: List<String>): List<List<String>> {
41+
// Create a map to store the key as input words in a sorted order and for every match create and append it to a list of word and return
42+
val freqMap = mutableMapOf<String, MutableList<String>>()
43+
inputString.forEach { str ->
44+
val charListStr = mutableListOf<Char>()
45+
for (i in 0 until str.length) {
46+
charListStr.add(str[i])
47+
}
48+
var sortedString = ""
49+
charListStr.sorted().forEach {
50+
sortedString += it.toString()
51+
}
52+
if (!freqMap.containsKey(sortedString)) {
53+
freqMap[sortedString] = mutableListOf<String>(str)
54+
} else {
55+
val valueList = freqMap[sortedString]
56+
valueList?.add(str)
57+
}
58+
}
59+
return freqMap.values.toList()
60+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.sample.umar.leetcodemediums.practice
2+
3+
import kotlin.math.max
4+
5+
fun main() {
6+
println(maxSubArraySumKadane(listOf(-1, 0, 2, 3, 4, -2, 4, 7, -9)))
7+
}
8+
9+
10+
/**
11+
* Iterate the given input list and find the max sum of subarray
12+
* Declare two vars called currentsum and maxsum and access the first element
13+
* Decide to start a new subarray sum or extend the summation through finding its max
14+
*/
15+
16+
fun maxSubArraySumKadane(inputList: List<Int>): Int {
17+
var currentMax = inputList[0]
18+
var maxSum = inputList[0]
19+
20+
for (i in 1 until inputList.size) {
21+
currentMax = max(inputList[i], inputList[i] + currentMax)
22+
maxSum = max(maxSum, currentMax)
23+
}
24+
return maxSum
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.sample.umar.leetcodemediums.practice
2+
3+
/**
4+
* Given: When two intervals overlap, you should merge them into ONE interval.
5+
* [[1,3], [2,6], [8,10], [15,18]] -> [[1,6], [8,10], [15,18]]
6+
*
7+
* Complexity: Time o(n log n) and space O(n)
8+
*
9+
* Algo:
10+
* 1. Sort intervals by start.
11+
* 2. Scan left to right and merge whenever current.start <= last.end.
12+
* 3. If no overlap, append as new interval.
13+
* 4. Return merged list.
14+
*/
15+
16+
17+
fun main() {
18+
val inputList: MutableList<MutableList<Int>> =
19+
mutableListOf(mutableListOf<Int>(1, 3), mutableListOf<Int>(8, 10), mutableListOf<Int>(2, 6))
20+
println(mergeInterval(inputList))
21+
}
22+
23+
fun mergeInterval(inputList: MutableList<MutableList<Int>>): List<List<Int>> {
24+
// Step 1: Sort intervals by start time
25+
val sortedList = inputList.sortedBy { it[0] }
26+
27+
val resultList = mutableListOf<MutableList<Int>>()
28+
29+
for (interval in sortedList) {
30+
// Let interval = [start, end]
31+
val currentStart = interval[0]
32+
val currentEnd = interval[1]
33+
// If result is empty OR current interval starts after last merged interval ends
34+
if (resultList.isEmpty() || currentStart > resultList.last()[1]) {
35+
// --------- NO OVERLAP ---------
36+
// Safe to add new interval
37+
resultList.add(interval)
38+
39+
} else {
40+
// --------- OVERLAP ---------
41+
// Merge current interval with the last one in the result
42+
val lastMerged = resultList.last()
43+
lastMerged[1] = maxOf(lastMerged[1], currentEnd)
44+
}
45+
}
46+
47+
return resultList
48+
}
49+
50+
/**
51+
* For each interval:
52+
* If result is empty → add it
53+
* Else:
54+
* If no overlap → add it
55+
* If overlap → merge by extending end
56+
*/

src/test/kotlin/com/sample/umar/leetcodemediums/practice/MoveZerosPrac.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package com.sample.umar.leetcodemediums.practice
1414
// Use in-place method with a slow and fast pointer. o(1) space
1515
1. slow pointer to index/put the nonzero value
1616
2. fast to scan the input list
17+
3. Finally, move the zeros to the last by using slow value and check against the inputList.size
1718
*/
1819

1920

@@ -46,6 +47,7 @@ fun moveZeroInPlace(inputList: MutableList<Int>): List<Int> {
4647
}
4748
}
4849

50+
// New move the zeroes to the right
4951
while (slow < inputList.size) {
5052
inputList[slow] = 0
5153
slow++

0 commit comments

Comments
 (0)