Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports Kotlin 1.6.10, Java 17 and gradle 7.3.3 #84

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .idea/misc.xml

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
- Please do not add a signature inside the code. The commit history is sufficient to determine who has added the code to the repo.
- Make sure the algorithm which is getting added comes under a certain domain of Algorithms. Please don't create a package with a name such as Misc, Others, etc.
- While making a PR, make sure you are committing the Kotlin files only and not any project specific files. If you feel that your IDE is generating some extra files, then either don't add them to git, or add the extensions to ```.gitignore```.
- Please don't raise a PR for solutions to problems from online judges such as Hackerrank, Leetcode, etc.
- Please don't raise a PR for solutions to problems from online judges such as HackerRank, LeetCode, etc.

## Steps to raise a PR
- Fork the [Kotlin Repo](https://github.com/TheAlgorithms/Kotlin)
- Open the forked repo on your local machine through IntelliJ by importing the project as a Gradle project
- Make the changes on your local machine
- Push the changes to the forked repository
- Raise a PR against the master branch
- Raise a PR against the master branch
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
}

group 'main.kotlin.com'
Expand All @@ -14,7 +14,7 @@ repositories {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
testImplementation group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStorePath=wrapper/dists
27 changes: 13 additions & 14 deletions src/main/kotlin/dynamicProgramming/AssemblyLineScheduling.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@ import kotlin.math.min
* @Params n- number of stations, a- service time at each station, t- line switch time from each station, e- entry time, x- exit time
* @Return minimum time to cross n stations
* */
fun AssemblyLineScheduling(n:Int,a:Array<IntArray>,t:Array<IntArray>,e:IntArray,x:IntArray):Int {
fun assemblyLineScheduling(n:Int, a:Array<IntArray>, t:Array<IntArray>, e:IntArray, x:IntArray):Int {

var L1:IntArray = IntArray(n);
var L2:IntArray = IntArray(n) ;
var i=0;
val line1 = IntArray(n)
val line2 = IntArray(n)

L1[0] = e[0] + a[0][0];
line1[0] = e[0] + a[0][0]

L2[0] = e[1] + a[1][0];
line2[0] = e[1] + a[1][0]

for(i in 1..n-1)
for(i in 1 until n)
{
L1[i] = min(L1[i - 1] + a[0][i],
L2[i - 1] + t[1][i] + a[0][i]);
L2[i] = min(L2[i - 1] + a[1][i],
L1[i - 1] + t[0][i] + a[1][i]);
line1[i] = min(line1[i - 1] + a[0][i],
line2[i - 1] + t[1][i] + a[0][i])
line2[i] = min(line2[i - 1] + a[1][i],
line1[i - 1] + t[0][i] + a[1][i])
}

return min(L1[n-1] + x[0],
L2[n-1] + x[1]);
return min(line1[n-1] + x[0],
line2[n-1] + x[1])


}
}
10 changes: 5 additions & 5 deletions src/main/kotlin/dynamicProgramming/LCS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import kotlin.math.max
*/

fun lcs(s1: String, s2: String): Int {
val L = Array(s1.length + 1) { IntArray(s2.length + 1) }
val l = Array(s1.length + 1) { IntArray(s2.length + 1) }

for (i in 0..s1.length) {
for (j in 0..s2.length) {
if (i == 0 || j == 0) L[i][j] = 0
else if (s1[i - 1] == s2[j - 1]) L[i][j] = L[i - 1][j - 1] + 1
else L[i][j] = max(L[i - 1][j], L[i][j - 1])
if (i == 0 || j == 0) l[i][j] = 0
else if (s1[i - 1] == s2[j - 1]) l[i][j] = l[i - 1][j - 1] + 1
else l[i][j] = max(l[i - 1][j], l[i][j - 1])
}
}
return L[s1.length][s2.length]
return l[s1.length][s2.length]
}
14 changes: 7 additions & 7 deletions src/main/kotlin/dynamicProgramming/MatrixChainMultiplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ package dynamicProgramming
* @Params p- array which represents the chain of matrices such that the ith matrix Ai is of dimension p[i-1] x p[i]
* @Return minimum number of multiplications needed to multiply the chain
* */
fun MatrixChainOrder(p: IntArray): Int {
fun matrixChainOrder(p: IntArray): Int {

val m = Array(p.size) { IntArray(p.size) }
var i: Int
var j: Int
var k: Int
var L: Int
var l: Int
var q: Int

i = 1
Expand All @@ -20,11 +20,11 @@ fun MatrixChainOrder(p: IntArray): Int {
i++
}

L = 2
while (L < p.size) {
l = 2
while (l < p.size) {
i = 1
while (i < p.size - L + 1) {
j = i + L - 1
while (i < p.size - l + 1) {
j = i + l - 1
if (j == p.size) {
i++
continue
Expand All @@ -40,7 +40,7 @@ fun MatrixChainOrder(p: IntArray): Int {
}
i++
}
L++
l++
}
return m[1][p.size - 1]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlin.math.max
* @return Maximum value that can be obtained
*/

fun zerooneknapsack(W:Int, weight: IntArray, values:IntArray, n:Int):Int{
fun zeroOneKnapsack(W:Int, weight: IntArray, values:IntArray, n:Int):Int{
if (W<0) return 0
val k = Array(n+1) {IntArray(W+1) {0} }
for (i in 0..n)
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/dynamic_programming/PalindromePartitioning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ package dynamic_programming


/**
* @param String is the string to be checked
* @param Int is the starting index of the string in consideration
* @param Int is the ending index of the string in consideration
* @param string is the string to be checked
* @param i is the starting index of the string in consideration
* @param j is the ending index of the string in consideration
* @return whether string is a palindrome or not
**/
fun isPalindrome(string: String, i: Int, j: Int): Boolean {
Expand All @@ -29,9 +29,9 @@ fun isPalindrome(string: String, i: Int, j: Int): Boolean {


/**
* @param String is the string to be checked
* @param Int is the starting index of the string in consideration
* @param Int is the ending index of the string in consideration
* @param string is the string to be checked
* @param i is the starting index of the string in consideration
* @param j is the ending index of the string in consideration
* @return minimum number of partitions required
**/
fun palindromePartition(string: String, i: Int, j: Int): Int {
Expand Down Expand Up @@ -63,9 +63,9 @@ lateinit var dp: Array<Array<Int>>


/**
* @param String the string on which algorithm is to be operated
* @param string the string on which algorithm is to be operated
*/
fun initialize(string: String): Int {
dp = Array(string.length) { Array(string.length) { -1 } }
return palindromePartition(string, 0, string.length - 1)
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/math/Area.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package math

import java.lang.IllegalArgumentException
import kotlin.math.pow

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/math/Factorial.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mathematics
package math

import java.security.InvalidParameterException

Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/other/Palindrome.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package other

import java.text.Normalizer
import java.util.Locale
import java.util.regex.Pattern

/**
Expand All @@ -20,7 +21,7 @@ fun isPalindrome(text: String): Boolean {
for(i in normalizedText.indices)
if(normalizedText[i] != normalizedText[normalizedText.length - (i + 1)])
return false
return true;
return true
}


Expand All @@ -30,7 +31,7 @@ fun String.normalize(): String {
.compile("\\p{InCombiningDiacriticalMarks}+")
.matcher(nfdNormalizedString)
.replaceAll("")
.toLowerCase()
.lowercase(Locale.getDefault())
.replace(" ", "")

}
10 changes: 5 additions & 5 deletions src/main/kotlin/search/BinarySearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package search
/**
* Binary search is an algorithm which finds the position of a target value within an array (Sorted)
*
* Worst-case performance O(log(n))
* Best-case performance O(1)
* Average performance O(log(n))
* Worst-case performance O(log(n))
* Best-case performance O(1)
* Average performance O(log(n))
* Worst-case space complexity O(1)
*/

Expand All @@ -32,7 +32,7 @@ fun <T : Comparable<T>> binarySearchHelper(array: Array<T>, key: T, start: Int,

return when {
array[mid].compareTo(key) == 0 -> mid
array[mid].compareTo(key) > 0 -> binarySearchHelper(array, key, start, mid - 1)
array[mid] > key -> binarySearchHelper(array, key, start, mid - 1)
else -> binarySearchHelper(array, key, mid + 1, end)
}
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/search/InterpolationSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package search
/**
* Interpolation search is an algorithm which finds the position of a target value within an array (Sorted)
*
* Worst-case performance O(n)
* Best-case performance O(1)
* Average performance O(log log n)
* Worst-case performance O(n)
* Best-case performance O(1)
* Average performance O(log(log(n)))
* Worst-case space complexity O(1)
*/

Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/search/LinearSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package search
/**
* Linear search is an algorithm which finds the position of a target value within an array (Usually unsorted)
*
* Worst-case performance O(n)
* Best-case performance O(1)
* Average performance O(n)
* Worst-case performance O(n)
* Best-case performance O(1)
* Average performance O(n)
* Worst-case space complexity O(1)
*/
**/

/**
* @param array is an array where the element should be found
Expand All @@ -31,4 +31,4 @@ fun <T : Comparable<T>> linearSearchImpl(array: Array<T>, key: T): Int {
}

return -1
}
}
10 changes: 4 additions & 6 deletions src/main/kotlin/search/TernarySearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package search
/**
* Complexity Analysis:
*
* Time Complexity: O(log3 n)
* Space Complexity: O(1)
*
*/

* Time Complexity: O(log3 n)
* Space Complexity: O(1)
**/
fun ternarySearch(l: Double, r: Double, func: (Double) -> Double, eps: Double = 1e-3): Double {
var left = l
var right = r
Expand All @@ -21,4 +19,4 @@ fun ternarySearch(l: Double, r: Double, func: (Double) -> Double, eps: Double =
}
}
return left
}
}
7 changes: 3 additions & 4 deletions src/main/kotlin/sort/BrickSort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ package sort
* @param array The array to be sorted
* Sorts the array in increasing order
*
* Worst-case performance O(n^2)
* Best-case performance O(n)
* Average performance O(n^2)
* Worst-case performance O(n^2)
* Best-case performance O(n)
* Average performance O(n^2)
* Worst-case space complexity O(1)
**/

fun <T : Comparable<T>> oddEvenSort(array: Array<T>) {
var isSorted = false
while (!isSorted) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/sort/BubbleSort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package sort
* @param array The array to be sorted
* Sorts the array in increasing order
*
* Worst-case performance O(n^2)
* Best-case performance O(n)
* Average performance O(n^2)
* Worst-case performance O(n^2)
* Best-case performance O(n)
* Average performance O(n^2)
* Worst-case space complexity O(1)
**/
fun <T : Comparable<T>> bubbleSort(array: Array<T>) {
Expand Down Expand Up @@ -39,4 +39,4 @@ fun <T : Comparable<T>> swapElements(array: Array<T>, idx1: Int, idx2: Int) {
array[idx1] = array[idx2].also {
array[idx2] = array[idx1]
}
}
}
Loading