From cddc0359175cec50468d0e7c2e936fa6be67c91f Mon Sep 17 00:00:00 2001 From: Danil M Date: Mon, 15 Sep 2025 03:22:35 -0400 Subject: [PATCH 1/4] change jdk location --- .idea/misc.xml | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 6c5519f..40e1197 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,9 +1,10 @@ - - - - - - + + + + + + \ No newline at end of file From 6ed095a43819282b8cdbe3b4ced147d4ce687bc7 Mon Sep 17 00:00:00 2001 From: Danil M Date: Mon, 15 Sep 2025 03:40:06 -0400 Subject: [PATCH 2/4] add readable docs --- README.md | 90 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index eb0d9e9..69f1c0a 100644 --- a/README.md +++ b/README.md @@ -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` | + +`` From 9dbfa04028ebd955dc56fdbd9e0f9e4f3ec6f8e8 Mon Sep 17 00:00:00 2001 From: Danil M Date: Tue, 16 Sep 2025 04:08:57 -0400 Subject: [PATCH 3/4] add 2197 problem --- .../main/java/com/github/contest/Execute.kt | 10 ++--- .../com/github/contest/math/MathLeetcode.kt | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index d8021b7..7c0d1a5 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -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 @@ -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) } } diff --git a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt index 669d5e5..7a9df52 100644 --- a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt +++ b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt @@ -231,3 +231,48 @@ fun triangleType(nums: IntArray): String { } } +/** + * 2197. Replace Non-Coprime Numbers in Array + */ + +fun replaceNonCoprimes(nums: IntArray): List { + if (nums.hasSingle()) return listOf(nums[0]) + + val res = mutableListOf() + + 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() +} From 95aa74c11ffdda8d17c98150a4ba06b701b3e31f Mon Sep 17 00:00:00 2001 From: Danil M Date: Tue, 16 Sep 2025 04:10:08 -0400 Subject: [PATCH 4/4] change name --- contest/src/main/java/com/github/contest/Execute.kt | 4 ++-- contest/src/main/java/com/github/contest/math/MathLeetcode.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 7c0d1a5..f90fcfd 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -2,7 +2,7 @@ package com.github.contest import com.github.contest.math.numberOfPowerfulInt -import com.github.contest.math.replaceNonCoprimes +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 @@ -17,7 +17,7 @@ fun main() { val data = listOf(287, 41, 49, 287, 899, 23, 23, 20677, 5, 825).toIntArray() - replaceNonCoprimes(data).also { println(it) } + replaceNonCoPrimes(data).also { println(it) } } diff --git a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt index 7a9df52..3a08eb2 100644 --- a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt +++ b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt @@ -235,7 +235,7 @@ fun triangleType(nums: IntArray): String { * 2197. Replace Non-Coprime Numbers in Array */ -fun replaceNonCoprimes(nums: IntArray): List { +fun replaceNonCoPrimes(nums: IntArray): List { if (nums.hasSingle()) return listOf(nums[0]) val res = mutableListOf()