Skip to content

Commit e546228

Browse files
Merge pull request #223
new problems and change structure september
2 parents 814569f + 922e21d commit e546228

21 files changed

+1346
-176
lines changed

.idea/appInsightsSettings.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,86 @@
1-
# Contest Kotlin
1+
# LeetCode Kotlin Project
22

33
![Kotlin](https://img.shields.io/badge/kotlin-%237F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)
44
![Android Studio](https://img.shields.io/badge/Android%20Studio-3DDC84.svg?style=for-the-badge&logo=android-studio&logoColor=white)
55

6-
# Description
6+
This project is a curated collection of solutions to a wide variety of LeetCode problems, all
7+
implemented in modern, idiomatic Kotlin. It's designed to be a valuable resource for anyone
8+
preparing for technical interviews, learning Kotlin, or exploring different algorithmic approaches
9+
to common problems.
710

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.
11+
## Key Features
1012

11-
# How to use
13+
* **Comprehensive Problem Coverage:** The project includes solutions for a diverse set of problems,
14+
covering many important data structures and algorithms.
15+
* **Organized by Topic:** Solutions are neatly organized into packages based on the primary data
16+
structure or algorithm used, making it easy to find examples and study specific topics.
17+
* **Idiomatic Kotlin:** The code is written in a clean, readable, and idiomatic Kotlin style,
18+
demonstrating best practices and modern language features.
19+
* **Educational Resource:** By studying the solutions, you can learn how to approach different types
20+
of algorithmic problems and how to implement them effectively in Kotlin.
1221

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
22+
## Why Use This Project?
23+
24+
There are several great reasons to use this project:
25+
26+
* **Accelerate Your Learning:** If you're learning algorithms and data structures, this project
27+
provides a rich library of examples that you can study and learn from.
28+
* **Prepare for Interviews:** The problems in this collection are representative of what you might
29+
encounter in a technical interview. You can use these solutions to practice and prepare.
30+
* **Discover Kotlin Best Practices:** The code in this project demonstrates how to write clean,
31+
efficient, and expressive Kotlin. It's a great way to see how the language is used in a practical
32+
context.
33+
34+
## Getting Started: How to Use the Solutions
35+
36+
The solutions in this project are organized into functions. To use a solution, you can simply call
37+
the function with the required input.
38+
39+
Here is an example of how you could call the `getCommon` function from the `HashTableLeetcode.kt`
40+
file within a `main` function:
41+
42+
```kotlin
43+
44+
fun main() {
45+
// Example usage of the getCommon function
46+
val nums1 = intArrayOf(1, 2, 3)
47+
val nums2 = intArrayOf(2, 4)
48+
val common = getCommon(nums1, nums2)
49+
50+
if (common != -1) {
51+
println("The minimum common value is: $common")
52+
} else {
53+
println("No common value was found.")
54+
}
55+
}
56+
```
57+
58+
## Table of Contents by Topic
59+
60+
Here is a list of the topics covered in this project, with a reference to the corresponding package:
61+
62+
| Topic | Package |
63+
|----------------------|--------------------------------------|
64+
| **Array** | `com.github.contest.array` |
65+
| **Backtracking** | `com.github.contest.backtracking` |
66+
| **Binary Search** | `com.github.contest.binarySearch` |
67+
| **Binary Tree** | `com.github.contest.binaryTree` |
68+
| **Bit Manipulation** | `com.github.contest.bitManipulation` |
69+
| **Design** | `com.github.contest.design` |
70+
| **Dynamic Prog.** | `com.github.contest.dp` |
71+
| **Graph** | `com.github.contest.graph` |
72+
| **Hash Table** | `com.github.contest.hashTable` |
73+
| **Heap** | `com.github.contest.heap` |
74+
| **Linked List** | `com.github.contest.linkedList` |
75+
| **Math** | `com.github.contest.math` |
76+
| **Priority Queue** | `com.github.contest.priorityqueue` |
77+
| **Queue** | `com.github.contest.queue` |
78+
| **Recursion** | `com.github.contest.recursion` |
79+
| **Sliding Window** | `com.github.contest.slidingWindow` |
80+
| **Sorting** | `com.github.contest.sorting` |
81+
| **Stack** | `com.github.contest.stack` |
82+
| **Strings** | `com.github.contest.strings` |
83+
| **Two Pointer** | `com.github.contest.twoPointer` |
84+
85+
``
2086

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.github.contest
22

33

4-
import com.github.contest.bitManipulation.hammingWeight
4+
import com.github.contest.array.isTriangle
5+
import com.github.contest.binaryTree.toTreeNode
6+
import com.github.contest.binaryTree.tree2str
57
import com.github.contest.math.numberOfPowerfulInt
68
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
79
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
810
import com.github.contest.strings.fullJustify
9-
1011
import java.util.TreeMap
1112

1213

@@ -15,9 +16,33 @@ import java.util.TreeMap
1516
*/
1617

1718
fun main() {
19+
//largestPerimeter(intArrayOf(3, 2, 3, 10, 2, 1, 4, 4)).also { println(it) }
20+
21+
isTriangle(3, 4, 4).also { println(it) }
22+
}
1823

19-
val str: String? = "ghdirfghdi"
24+
fun treeLaunch() {
25+
val tree1 = listOf(1, 2, 3, 4).toTreeNode()
26+
val tree2 = listOf(1).toTreeNode()
27+
val tree3 = listOf(1, 2, 3, null, 4).toTreeNode()
28+
//tree1.printTree()
29+
30+
tree2str(tree3).also {
31+
println(it)
32+
}
33+
}
2034

35+
36+
class Example(val param: String) { // Primary constructor parameter
37+
init {
38+
println("init block: $param") // Can access param!
39+
}
40+
41+
val property = "Property: $param".also { println(it) } // Property initializer
42+
43+
constructor(secondary: Int) : this("Secondary:$secondary") { // Secondary constructor
44+
println("Secondary constructor")
45+
}
2146
}
2247

2348

@@ -151,7 +176,7 @@ fun workWithTreeMap() {
151176

152177

153178
fun IntArray.printArray() {
154-
var s = when (this.size) {
179+
val s = when (this.size) {
155180
0 -> "[]"
156181
1 -> "[${this[0]}]"
157182
2 -> "[${this[0]}, ${this[1]}]"

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,43 @@ fun maximumDifference(nums: IntArray): Int {
439439
return diff
440440
}
441441

442+
/**
443+
* 54. Spiral Matrix
444+
*/
445+
446+
fun spiralOrder(matrix: Array<IntArray>): List<Int> = when (matrix.size) {
447+
1 -> matrix[0].toList()
448+
2 -> matrix[0].toList() + matrix[1].reversed()
449+
450+
else -> {
451+
452+
val res = mutableListOf<Int>()
453+
var top = 0
454+
var bottom = matrix.size - 1
455+
var left = 0
456+
var right = matrix[0].size - 1
457+
458+
while (top <= bottom && left <= right) {
459+
for (i in left..right) res.add(matrix[top][i])
460+
top++
461+
462+
for (i in top..bottom) res.add(matrix[i][right])
463+
right--
464+
465+
if (top <= bottom) {
466+
for (i in right downTo left) res.add(matrix[bottom][i])
467+
bottom--
468+
}
469+
470+
if (left <= right) {
471+
for (i in bottom downTo top) res.add(matrix[i][left])
472+
left++
473+
}
474+
475+
}
476+
477+
res
478+
}
479+
}
480+
442481

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeAlternativeSolution.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,66 @@ class FindElementsAlternativeSolution(root: TreeNode?) {
2828
fun find(target: Int): Boolean {
2929
return values.contains(target)
3030
}
31+
}
32+
33+
/**
34+
* 572. Subtree of Another Tree
35+
* Alternative Solution
36+
* Serialization
37+
*/
38+
39+
fun isSubtreeSerialization(root: TreeNode?, subRoot: TreeNode?): Boolean {
40+
41+
val stringRoot = serialize(root)
42+
val stringSubRoot = serialize(subRoot)
43+
44+
return stringRoot.contains(stringSubRoot)
45+
}
46+
47+
48+
fun serialize(root: TreeNode?): String {
49+
if (root == null) return "null"
50+
51+
return "#${root.`val`} ${serialize(root.left)} ${serialize(root.right)}"
52+
}
53+
54+
/**
55+
* 652. Find Duplicate Subtrees
56+
* Alternative Solution
57+
* Using Serialize + Traverse
58+
*/
59+
60+
fun findDuplicateSubtreesOtherSolution(root: TreeNode?): List<TreeNode?> {
61+
val result = mutableListOf<TreeNode?>()
62+
val subtreeMap = mutableMapOf<String, Int>() // serialization -> count
63+
serialize(root, subtreeMap, result)
64+
return result
65+
}
66+
67+
private fun serialize(
68+
node: TreeNode?,
69+
subtreeMap: MutableMap<String, Int>,
70+
result: MutableList<TreeNode?>
71+
): String {
72+
if (node == null) return "#"
73+
74+
// Postorder serialization: left + right + root
75+
val serial = buildString {
76+
append(serialize(node.left, subtreeMap, result))
77+
append(",")
78+
append(serialize(node.right, subtreeMap, result))
79+
append(",")
80+
append(node.`val`)
81+
}
82+
83+
84+
val count = subtreeMap.getOrDefault(serial, 0)
85+
subtreeMap[serial] = count + 1
86+
87+
88+
if (count == 1) {
89+
result.add(node)
90+
}
91+
92+
return serial
3193
}

0 commit comments

Comments
 (0)