From d9e507f6aea1ce82db3de0b39e470649ae84f5b1 Mon Sep 17 00:00:00 2001 From: CmplxN Date: Tue, 13 Oct 2020 23:41:03 +0900 Subject: [PATCH 1/4] Create README.md --- CmplxN/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 CmplxN/README.md diff --git a/CmplxN/README.md b/CmplxN/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/CmplxN/README.md @@ -0,0 +1 @@ + From b8dfe42dd84a149efaa1b0b84c2ce00ed1edf22c Mon Sep 17 00:00:00 2001 From: CmplxN Date: Wed, 14 Oct 2020 00:16:28 +0900 Subject: [PATCH 2/4] Create CombinationSum.kt --- CmplxN/CombinationSum.kt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CmplxN/CombinationSum.kt diff --git a/CmplxN/CombinationSum.kt b/CmplxN/CombinationSum.kt new file mode 100644 index 0000000..cf01534 --- /dev/null +++ b/CmplxN/CombinationSum.kt @@ -0,0 +1,21 @@ +class Solution { + fun combinationSum(candidates: IntArray, target: Int): List> { + val sortedCandidates = candidates.sorted() + val rtn = mutableListOf>() + val temp = mutableListOf() + fun dfs(sum: Int, idx: Int) { + if (sum == target) { + rtn.add(temp.map { it }) + return + } + for (i in idx..sortedCandidates.lastIndex) { + if (sum + sortedCandidates[i] > target) return + temp.add(sortedCandidates[i]) + dfs(sum + temp.last(), i) + temp.removeAt(temp.lastIndex) + } + } + dfs(0, 0) + return rtn + } +} From 7e0564c0c177116f5974e236e2d12a677651b3f0 Mon Sep 17 00:00:00 2001 From: CmplxN Date: Thu, 15 Oct 2020 19:54:55 +0900 Subject: [PATCH 3/4] Update CombinationSum.kt --- CmplxN/CombinationSum.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CmplxN/CombinationSum.kt b/CmplxN/CombinationSum.kt index cf01534..3c09a68 100644 --- a/CmplxN/CombinationSum.kt +++ b/CmplxN/CombinationSum.kt @@ -9,9 +9,10 @@ class Solution { return } for (i in idx..sortedCandidates.lastIndex) { - if (sum + sortedCandidates[i] > target) return - temp.add(sortedCandidates[i]) - dfs(sum + temp.last(), i) + val pick = sortedCandidates[i] + if (sum + pick > target) return + temp.add(pick) + dfs(sum + pick, i) temp.removeAt(temp.lastIndex) } } From c793f1dc7fe91a9ca1e5671bc08cb23861b8731e Mon Sep 17 00:00:00 2001 From: CmplxN Date: Sun, 18 Oct 2020 00:59:18 +0900 Subject: [PATCH 4/4] ChocolatesByNumbers.kt --- CmplxN/ChocolatesByNumbers.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 CmplxN/ChocolatesByNumbers.kt diff --git a/CmplxN/ChocolatesByNumbers.kt b/CmplxN/ChocolatesByNumbers.kt new file mode 100644 index 0000000..32f5bb6 --- /dev/null +++ b/CmplxN/ChocolatesByNumbers.kt @@ -0,0 +1,14 @@ +// you can also use imports, for example: +// import kotlin.math.* + +// you can write to stdout for debugging purposes, e.g. +// println("this is a debug message") + +fun solution(N: Int, M: Int): Int = N / gcd(N, M) + +tailrec fun gcd(a: Int, b: Int): Int { + return when (val r = a % b) { + 0 -> b + else -> gcd(b, r) + } +}