Skip to content

Commit adb06c1

Browse files
Merge pull request #172
add new problems 8.04
2 parents 7821ec2 + c2b8615 commit adb06c1

File tree

7 files changed

+293
-27
lines changed

7 files changed

+293
-27
lines changed

app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,31 +2612,6 @@ fun findRelativeRanks(score: IntArray): Array<String> {
26122612
return ans
26132613
}
26142614

2615-
/**
2616-
* 347. Top K Frequent Elements
2617-
*/
2618-
2619-
fun topKFrequent(nums: IntArray, k: Int): IntArray? {
2620-
val map: MutableMap<Int, Int> = HashMap()
2621-
for (n in nums) {
2622-
map[n] = map.getOrDefault(n, 0) + 1
2623-
}
2624-
2625-
val heap = PriorityQueue { a: Map.Entry<Int, Int>, b: Map.Entry<Int, Int> ->
2626-
b.value.compareTo(a.value)
2627-
}
2628-
2629-
for (entry in map.entries) {
2630-
heap.offer(entry)
2631-
}
2632-
2633-
val res = IntArray(k)
2634-
for (i in 0 until k) {
2635-
res[i] = heap.poll().key
2636-
}
2637-
2638-
return res
2639-
}
26402615

26412616
/**
26422617
* 437. Path Sum III

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.strings.isNumber
4+
import com.github.contest.hashTable.minimumOperations
55
import java.util.TreeMap
66

77

@@ -11,7 +11,7 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
isNumber("6+1").also { println(it) }
14+
minimumOperations(intArrayOf(1, 2, 3, 4, 2, 3, 3, 5, 7)).also { println(it) }
1515
}
1616

1717
fun testing() {

contest/src/main/java/com/github/contest/design/DesignProdVariant.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,43 @@ class ProductOfNumbersProdVariant() {
3030
}
3131
return products.last() / products[products.size - k - 1]
3232
}
33+
}
34+
35+
/**
36+
* 981. Time Based Key-Value Store
37+
* Prod Variant
38+
*/
39+
40+
class TimeMapProdVariant {
41+
42+
private val store = mutableMapOf<String, MutableList<Pair<Int, String>>>()
43+
44+
fun set(key: String, value: String, timestamp: Int) {
45+
store.getOrPut(key) { mutableListOf(Pair(timestamp, value)) }.also {
46+
it.add(Pair(timestamp, value))
47+
}
48+
}
49+
50+
fun get(key: String, timestamp: Int): String = when {
51+
store[key] == null -> ""
52+
else -> getFromList(store.getOrDefault(key, listOf()), timestamp)
53+
}
54+
55+
private fun getFromList(list: List<Pair<Int, String>>, timestamp: Int): String =
56+
list.binarySearch {
57+
when {
58+
it.first == timestamp -> 0
59+
it.first < timestamp -> -1
60+
else -> 1
61+
}
62+
}.let { index ->
63+
when {
64+
index >= 0 -> list[index].second
65+
else -> {
66+
val insertionPoint = -index - 1
67+
if (insertionPoint == 0) "" else list[insertionPoint - 1].second
68+
}
69+
}
70+
}
71+
3372
}

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,64 @@ fun repeatedCharacter(s: String): Char {
271271
}
272272

273273
return 'a'
274+
}
275+
276+
/**
277+
* 3396. Minimum Number of Operations to Make Elements in Array Distinct
278+
*/
279+
280+
fun minimumOperations(nums: IntArray): Int {
281+
val map = mutableMapOf<Int, Int>()
282+
var operations = 0
283+
284+
for (num in nums) map[num] = map.getOrDefault(num, 0) + 1
285+
286+
if (map.size == nums.size) return 0
287+
288+
for (i in nums.indices step 3) {
289+
if (i + 2 < nums.size) {
290+
val one = nums[i]
291+
val two = nums[i + 1]
292+
val three = nums[i + 2]
293+
map.reduceOrRemove(one)
294+
map.reduceOrRemove(two)
295+
map.reduceOrRemove(three)
296+
var isUnique = true
297+
for (value in map.values) {
298+
if (value > 1) {
299+
isUnique = false
300+
break
301+
}
302+
}
303+
if (isUnique) return operations + 1
304+
operations++
305+
}
306+
}
307+
308+
return if (map.isNotEmpty()) operations + 1 else operations
309+
310+
}
311+
312+
private fun MutableMap<Int, Int>.reduceOrRemove(key: Int) {
313+
this[key] = this.getOrDefault(key, 0) - 1
314+
if (this.getOrDefault(key, 0) == 0) this.remove(key)
315+
}
316+
317+
/**
318+
* 242. Valid Anagram
319+
*/
320+
321+
fun isAnagram(s: String, t: String): Boolean {
322+
if (s.length != t.length) return false
323+
val first = IntArray(26)
324+
val second = IntArray(26)
325+
326+
for (char in s) first[char - 'a']++
327+
for (char in t) second[char - 'a']++
328+
329+
for (char in t) {
330+
if (first[char - 'a'] == 0 || first[char - 'a'] != second[char - 'a']) return false
331+
}
332+
333+
return true
274334
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.contest.priorityqueue
2+
3+
import java.util.PriorityQueue
4+
5+
/**
6+
* 347. Top K Frequent Elements
7+
*/
8+
9+
fun topKFrequent(nums: IntArray, k: Int): IntArray? {
10+
val map: MutableMap<Int, Int> = HashMap()
11+
for (n in nums) {
12+
map[n] = map.getOrDefault(n, 0) + 1
13+
}
14+
15+
val heap = PriorityQueue { a: Map.Entry<Int, Int>, b: Map.Entry<Int, Int> ->
16+
b.value.compareTo(a.value)
17+
}
18+
19+
for (entry in map.entries) {
20+
heap.offer(entry)
21+
}
22+
23+
val res = IntArray(k)
24+
for (i in 0 until k) {
25+
res[i] = heap.poll().key
26+
}
27+
28+
return res
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.contest.priorityqueue
2+
3+
/**
4+
* 347. Top K Frequent Elements
5+
*/
6+
7+
fun topKFrequentProdVariant(nums: IntArray, k: Int): IntArray {
8+
val freq = mutableMapOf<Int, Int>()
9+
val repeated = Array<MutableList<Int>>(nums.size + 1) {mutableListOf()}
10+
11+
for (num in nums) freq[num] = freq.getOrDefault(num, 0) + 1
12+
13+
for ((num, count) in freq) {
14+
repeated[count].add(num)
15+
}
16+
17+
return repeated.flatMap {it}.takeLast(k).toIntArray()
18+
}

contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,149 @@ fun percentageLetter(s: String, letter: Char): Int {
8989
}
9090

9191

92+
/**
93+
* 1309. Decrypt String from Alphabet to Integer Mapping
94+
*/
95+
96+
fun freqAlphabets(s: String): String {
97+
val result = StringBuilder()
98+
var i = s.length - 1
99+
100+
while (i >= 0) {
101+
if (s[i] == '#') {
102+
val twoDigit = s.substring(i - 2, i).toInt()
103+
result.append(getLetterFromIndex(twoDigit - 1))
104+
i -= 3
105+
} else {
106+
result.append(getLetterFromIndex(s[i].digitToInt() - 1))
107+
i -= 1
108+
}
109+
}
110+
111+
return result.reverse().toString()
112+
}
113+
114+
fun getLetterFromIndex(index: Int): Char {
115+
require(index in 0..25) { "Index must be between 0 and 25 (inclusive)" }
116+
return 'a' + index
117+
}
118+
119+
120+
/**
121+
*
122+
*/
123+
124+
fun isNumber(s: String): Boolean = when {
125+
isOnlyNumbers(s) -> true
126+
hasLetter(s) -> false
127+
isValidNumberSign(s) && !hasExponential(s) -> true
128+
isNotValidNumberSign(s) -> false
129+
else -> hasNumberExponential(s)
130+
}
131+
132+
private fun hasNumberExponential(str: String): Boolean {
133+
var indexExp = 0
134+
var isNotDigits = true
135+
for (i in str.indices) {
136+
if (isExponential(str[i])) {
137+
indexExp = i
138+
break
139+
}
140+
}
141+
142+
for (i in 0 until indexExp) {
143+
if (isDigit(str[i])) isNotDigits = false
144+
}
145+
146+
if (isNotDigits) return false
147+
isNotDigits = true
148+
for (i in indexExp until str.length) {
149+
if (str[i] == '.') return false
150+
if (isDigit(str[i])) isNotDigits = false
151+
}
152+
153+
return !isNotDigits
154+
}
155+
156+
private fun isValidNumberSign(str: String): Boolean {
157+
var isPlusOrMinus = false
158+
var isOneDot = false
159+
for (char in str) {
160+
if (isSign(char)) {
161+
if (isPlusOrMinus) return false
162+
else isPlusOrMinus = true
163+
}
164+
if (isDot(char) && !isOneDot) {
165+
isOneDot = true
166+
continue
167+
}
168+
if (isDot(char) && isOneDot) return false
169+
}
170+
return true
171+
}
172+
173+
private fun hasExponential(str: String): Boolean {
174+
for (char in str) if (isExponential(char)) return true
175+
return false
176+
}
177+
178+
private fun isNotValidNumberSign(str: String): Boolean {
179+
var isPlusOrMinus = false
180+
var isOneDot = false
181+
for (char in str) {
182+
if (isSign(char)) {
183+
if (isPlusOrMinus) return true
184+
else isPlusOrMinus = true
185+
}
186+
if (isDot(char) && !isOneDot) {
187+
isOneDot = true
188+
continue
189+
}
190+
if (isOneDot) return true
191+
}
192+
return false
193+
}
194+
195+
private fun hasLetter(str: String): Boolean {
196+
for (char in str) {
197+
if (isLetter(char)) return true
198+
}
199+
return false
200+
}
201+
202+
private fun isOnlyNumbers(str: String): Boolean {
203+
for (char in str) {
204+
if (isLetter(char) || isExponential(char) || isSign(char) || isDot(char)) return false
205+
}
206+
return true
207+
}
208+
209+
fun isDigit(char: Char): Boolean = when {
210+
char in '0'..'9' -> true
211+
else -> false
212+
}
213+
214+
fun isLetter(char: Char): Boolean = when {
215+
(char != 'e' && char != 'E') && (char in 'a'..'z' || char in 'A'..'Z') -> true
216+
else -> false
217+
}
218+
219+
fun isExponential(char: Char): Boolean = when {
220+
char == 'e' || char == 'E' -> true
221+
else -> false
222+
}
223+
224+
fun isDot(char: Char) = when {
225+
char == '.' -> true
226+
else -> false
227+
}
228+
229+
fun isSign(char: Char): Boolean {
230+
return when {
231+
char == '+' || char == '-' -> true
232+
else -> false
233+
}
234+
}
235+
236+
92237

0 commit comments

Comments
 (0)