Skip to content

Commit 22584b1

Browse files
Merge pull request #208
add new problems for merge 19.06
2 parents c9bf6ff + 6bd6675 commit 22584b1

26 files changed

+2655
-141
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
# solution algorithm problems
1+
# Contest Kotlin
2+
23
![Kotlin](https://img.shields.io/badge/kotlin-%237F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)
34
![Android Studio](https://img.shields.io/badge/Android%20Studio-3DDC84.svg?style=for-the-badge&logo=android-studio&logoColor=white)
45

5-
## Task
6+
# Description
7+
8+
This repository contains problems of leetcode. We might use this repo how get solution for define
9+
problem from leetcode. Also We might clone this repo and run or debug problems in android studio.
10+
11+
# How to use
12+
13+
1. Each problem will be in contest module
14+
2. Each problem have a few solutions. Main solution with using down level code. Alternative Solution
15+
with other approaches. Prod Variant - this code might be in prod in application or service. This
16+
code using std lib kotlin
17+
3. For search each problem have kotlin doc with name and number problem. Also each problem have tag
18+
for commit for search in github.
19+
4. Each Topic have package which contains problem
620

7-
- [leetcode task](app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt)
8-
- [codeWars task](app/src/main/java/com/leetcode_kotlin/AlgorithmCodeWars.kt)

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

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -578,36 +578,6 @@ fun findDuplicates(nums: IntArray): List<Int> {
578578
return ans
579579
}
580580

581-
/**
582-
* 73. Set Matrix Zeroes
583-
*/
584-
585-
586-
fun setZeroes(matrix: Array<IntArray>) {
587-
val m = matrix.size
588-
val n = matrix[0].size
589-
590-
val forRows = BooleanArray(m)
591-
val forCols = BooleanArray(n)
592-
593-
for (i in 0 until m) {
594-
for (j in 0 until n) {
595-
if (matrix[i][j] == 0) {
596-
forRows[i] = true
597-
forCols[j] = true
598-
}
599-
}
600-
}
601-
602-
603-
for (i in 0 until m) {
604-
for (j in 0 until n) {
605-
if (forRows[i] || forCols[j]) {
606-
matrix[i][j] = 0
607-
}
608-
}
609-
}
610-
}
611581

612582
/**
613583
* 48. Rotate Image

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -580,43 +580,6 @@ fun heightProdVariant(root: TreeNode?): Int = when {
580580
else -> 1 + maxOf(heightProdVariant(root?.left), heightProdVariant(root?.right))
581581
}
582582

583-
/**
584-
* 73. Set Matrix Zeroes
585-
* Prod Variant
586-
*/
587-
588-
fun setZeroesProdVariant(matrix: Array<IntArray>) {
589-
val m = matrix.size
590-
val n = matrix[0].size
591-
var firstRowZero = false
592-
var firstColZero = false
593-
594-
595-
matrix.forEachIndexed { i, row ->
596-
row.forEachIndexed { j, value ->
597-
if (value == 0) {
598-
if (i == 0) firstRowZero = true
599-
if (j == 0) firstColZero = true
600-
matrix[i][0] = 0
601-
matrix[0][j] = 0
602-
}
603-
}
604-
}
605-
606-
607-
for (i in 1 until m) {
608-
for (j in 1 until n) {
609-
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
610-
matrix[i][j] = 0
611-
}
612-
}
613-
}
614-
615-
616-
if (firstRowZero) matrix[0].fill(0)
617-
if (firstColZero) for (i in 0 until m) matrix[i][0] = 0
618-
}
619-
620583
/**
621584
* 287. Find the Duplicate Number
622585
* Prod Variant

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -926,51 +926,6 @@ fun canBreak(start: Int, s: String, trie: TrieWordBreak): Boolean {
926926
return false
927927
}
928928

929-
/**
930-
* 73. Set Matrix Zeroes
931-
* Alternative Solution
932-
*/
933-
934-
fun setZeroesAltSolution(matrix: Array<IntArray>) {
935-
val m = matrix.size
936-
val n = matrix[0].size
937-
var firstRowZero = false
938-
var firstColZero = false
939-
940-
941-
for (i in 0 until m) {
942-
for (j in 0 until n) {
943-
if (matrix[i][j] == 0) {
944-
if (i == 0) firstRowZero = true
945-
if (j == 0) firstColZero = true
946-
matrix[i][0] = 0
947-
matrix[0][j] = 0
948-
}
949-
}
950-
}
951-
952-
953-
for (i in 1 until m) {
954-
for (j in 1 until n) {
955-
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
956-
matrix[i][j] = 0
957-
}
958-
}
959-
}
960-
961-
962-
if (firstRowZero) {
963-
for (j in 0 until n) {
964-
matrix[0][j] = 0
965-
}
966-
}
967-
if (firstColZero) {
968-
for (i in 0 until m) {
969-
matrix[i][0] = 0
970-
}
971-
}
972-
}
973-
974929
/**
975930
* 230. Kth Smallest Element in a BST
976931
* Alt Solution
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.github.contest
22

3-
fun MutableMap<Int, Int>.removeIfEmptyBucket(key: Int) {
4-
this[key] = this.getOrDefault(key, 0) - 1
3+
4+
fun <K, V> MutableMap<K, V>.removeIfEmptyBucket(key: K) {
55
if (this[key] == 0) this.remove(key)
66
}
77

8-
9-
fun Any.printData(label: String) {
10-
println("$label $this")
8+
fun <K> MutableMap<K, Int>.reduceCount(key: K) {
9+
this[key] = this.getOrDefault(key, 0) - 1
10+
removeIfEmptyBucket(key)
1111
}
1212

1313

14-
inline fun abs(number: Int): Int = when {
14+
fun abs(number: Int): Int = when {
1515
number < 0 -> number * -1
1616
else -> number
1717
}

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

Lines changed: 55 additions & 1 deletion
Large diffs are not rendered by default.

contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,70 @@ fun pivotArrayAlternativeSolution(nums: IntArray, pivot: Int): IntArray {
164164

165165
return result
166166
}
167+
168+
169+
/**
170+
* 73. Set Matrix Zeroes
171+
* Alternative Solution
172+
*/
173+
174+
fun setZeroesAltSolution(matrix: Array<IntArray>) {
175+
val m = matrix.size
176+
val n = matrix[0].size
177+
var firstRowZero = false
178+
var firstColZero = false
179+
180+
181+
for (i in 0 until m) {
182+
for (j in 0 until n) {
183+
if (matrix[i][j] == 0) {
184+
if (i == 0) firstRowZero = true
185+
if (j == 0) firstColZero = true
186+
matrix[i][0] = 0
187+
matrix[0][j] = 0
188+
}
189+
}
190+
}
191+
192+
193+
for (i in 1 until m) {
194+
for (j in 1 until n) {
195+
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
196+
matrix[i][j] = 0
197+
}
198+
}
199+
}
200+
201+
202+
if (firstRowZero) {
203+
for (j in 0 until n) {
204+
matrix[0][j] = 0
205+
}
206+
}
207+
if (firstColZero) {
208+
for (i in 0 until m) {
209+
matrix[i][0] = 0
210+
}
211+
}
212+
}
213+
214+
/**
215+
* 2016. Maximum Difference Between Increasing Elements
216+
* Alternative Solution
217+
*/
218+
219+
fun maximumDifferenceAlternativeSolution(nums: IntArray): Int {
220+
var diff = -1
221+
var minVal = Int.MAX_VALUE
222+
223+
for (i in nums.indices) {
224+
when {
225+
nums[i] < minVal -> minVal = minOf(minVal, nums[i])
226+
else -> if (nums[i] > minVal) diff = maxOf(diff, nums[i] - minVal) else continue
227+
}
228+
}
229+
230+
return diff
231+
}
232+
233+

0 commit comments

Comments
 (0)