Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b9a44ec

Browse files
committed
add 2 files sep challenge
1 parent fce7e6c commit b9a44ec

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

‎src/main/kotlin/06-sep.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution06Sep {
2+
data class Coor(val x: Int, val y: Int)
3+
fun largestOverlap(A: Array<IntArray>, B: Array<IntArray>): Int {
4+
val aOnes= mutableListOf<Coor>()
5+
val bOnes= mutableListOf<Coor>()
6+
val vectors= mutableListOf<Coor>()
7+
val r = A.size
8+
val c = A[0].size
9+
for (i in A.indices){
10+
for (j in A[0].indices){
11+
if(A[i][j] == 1) aOnes.add(Coor(i,j))
12+
if(B[i][j] == 1) bOnes.add(Coor(i,j))
13+
}
14+
}
15+
16+
for (i in aOnes){
17+
for (j in bOnes){
18+
vectors.add(Coor(j.x-i.x, j.y-i.y))
19+
}
20+
}
21+
22+
if (vectors.isEmpty()) return 0
23+
24+
return vectors.groupBy { it }.maxBy { it.value.size}!!.value.size
25+
}
26+
}

‎src/main/kotlin/07-sep.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution07Sep {
2+
fun wordPattern(pattern: String, str: String): Boolean {
3+
val tokens = str.split(" ")
4+
if (tokens.size != pattern.length) return false
5+
6+
val m = mutableMapOf<Char, String>()
7+
for (i in pattern.indices) {
8+
val c = pattern[i]
9+
if (m.containsKey(c)) {
10+
if (m[c] != tokens[i]) return false
11+
} else m[c] = tokens[i]
12+
}
13+
return tokens.groupBy { it }.size == m.size
14+
}
15+
}

‎src/main/kotlin/08-sep.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import utility.TreeNode
2+
3+
class Solution08Sep {
4+
5+
fun sumHelper(r: TreeNode?, v: Int) : Int{
6+
if(r==null) return 0
7+
val newV = 2*v+r.`val`
8+
if(r.left==null && r.right==null) return newV
9+
return sumHelper(r.left, newV)+sumHelper(r.right, newV)
10+
}
11+
fun sumRootToLeaf(root: TreeNode?): Int {
12+
return sumHelper(root, 0)
13+
}
14+
}

‎src/main/kotlin/template.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class SolutionAug {
1+
class SolutionSep {
22

33
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /