Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 78 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,86 @@
# Contest Kotlin
# LeetCode Kotlin Project

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

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

This repository contains problems of leetcode. We might use this repo how get solution for define
problem from leetcode. Also We might clone this repo and run or debug problems in android studio.
## Key Features

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

1. Each problem will be in contest module
2. Each problem have a few solutions. Main solution with using down level code. Alternative Solution
with other approaches. Prod Variant - this code might be in prod in application or service. This
code using std lib kotlin
3. For search each problem have kotlin doc with name and number problem. Also each problem have tag
for commit for search in github.
4. Each Topic have package which contains problem
## Why Use This Project?

There are several great reasons to use this project:

* **Accelerate Your Learning:** If you're learning algorithms and data structures, this project
provides a rich library of examples that you can study and learn from.
* **Prepare for Interviews:** The problems in this collection are representative of what you might
encounter in a technical interview. You can use these solutions to practice and prepare.
* **Discover Kotlin Best Practices:** The code in this project demonstrates how to write clean,
efficient, and expressive Kotlin. It's a great way to see how the language is used in a practical
context.

## Getting Started: How to Use the Solutions

The solutions in this project are organized into functions. To use a solution, you can simply call
the function with the required input.

Here is an example of how you could call the `getCommon` function from the `HashTableLeetcode.kt`
file within a `main` function:

```kotlin

fun main() {
// Example usage of the getCommon function
val nums1 = intArrayOf(1, 2, 3)
val nums2 = intArrayOf(2, 4)
val common = getCommon(nums1, nums2)

if (common != -1) {
println("The minimum common value is: $common")
} else {
println("No common value was found.")
}
}
```

## Table of Contents by Topic

Here is a list of the topics covered in this project, with a reference to the corresponding package:

| Topic | Package |
|----------------------|--------------------------------------|
| **Array** | `com.github.contest.array` |
| **Backtracking** | `com.github.contest.backtracking` |
| **Binary Search** | `com.github.contest.binarySearch` |
| **Binary Tree** | `com.github.contest.binaryTree` |
| **Bit Manipulation** | `com.github.contest.bitManipulation` |
| **Design** | `com.github.contest.design` |
| **Dynamic Prog.** | `com.github.contest.dp` |
| **Graph** | `com.github.contest.graph` |
| **Hash Table** | `com.github.contest.hashTable` |
| **Heap** | `com.github.contest.heap` |
| **Linked List** | `com.github.contest.linkedList` |
| **Math** | `com.github.contest.math` |
| **Priority Queue** | `com.github.contest.priorityqueue` |
| **Queue** | `com.github.contest.queue` |
| **Recursion** | `com.github.contest.recursion` |
| **Sliding Window** | `com.github.contest.slidingWindow` |
| **Sorting** | `com.github.contest.sorting` |
| **Stack** | `com.github.contest.stack` |
| **Strings** | `com.github.contest.strings` |
| **Two Pointer** | `com.github.contest.twoPointer` |

``

10 changes: 3 additions & 7 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.contest


import com.github.contest.binaryTree.TreeNode
import com.github.contest.binaryTree.findTilt
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.math.replaceNonCoPrimes
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.strings.fullJustify
Expand All @@ -16,12 +15,9 @@ import java.util.TreeMap

fun main() {

val root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)

findTilt(root).also { println(it) }
val data = listOf(287, 41, 49, 287, 899, 23, 23, 20677, 5, 825).toIntArray()

replaceNonCoPrimes(data).also { println(it) }
}


Expand Down
45 changes: 45 additions & 0 deletions contest/src/main/java/com/github/contest/math/MathLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,48 @@ fun triangleType(nums: IntArray): String {
}
}

/**
* 2197. Replace Non-Coprime Numbers in Array
*/

fun replaceNonCoPrimes(nums: IntArray): List<Int> {
if (nums.hasSingle()) return listOf(nums[0])

val res = mutableListOf<Int>()

for (num in nums) {
var currentNum = num
while (res.isNotEmpty() && isNonCoprime(res.last(), currentNum)) {
val last = res.removeLast()
currentNum = lcm(last, currentNum)
}
res.add(currentNum)
}

return res
}

fun isNonCoprime(first: Int, second: Int) = gcd(first, second) > 1

fun IntArray.hasSingle() = when {
this.size == 1 -> true
else -> false
}

fun gcd(first: Int, second: Int): Int {
if (first == 1 || second == 1) return 1
if (first == second) return first

val maxValue = maxOf(first, second)
val minValue = minOf(first, second)

if (maxValue % minValue == 0) return minValue

return gcd(minValue, maxValue % minValue)
}

fun lcm(first: Int, second: Int): Int {
if (first == second) return first
val product = (first.toLong() / gcd(first, second)) * second
return abs(product).toInt()
}