Skip to content

Commit 7682238

Browse files
add kotlin solution for 886 Possible-Bipartition.kt
1 parent 5b056a2 commit 7682238

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
class PossibleBipartitionKotlin886 {
2+
fun possibleBipartition(N: Int, dislikes: Array<IntArray>): Boolean {
3+
val dislikesMap: MutableMap<Int, MutableList<Int>> = HashMap()
4+
for (dislike in dislikes) {
5+
dislikesMap.computeIfAbsent(dislike[0]) { mutableListOf() }.add(dislike[1])
6+
dislikesMap.computeIfAbsent(dislike[1]) { mutableListOf() }.add(dislike[0])
7+
}
8+
val hashArray = IntArray(N + 1)
9+
for (index in hashArray.indices) {
10+
hashArray[index] = index
11+
}
12+
for (index in 1 until hashArray.size) {
13+
if (!dislikesMap[index].isNullOrEmpty()) {
14+
val currentHash = hash(hashArray, index)
15+
val dislikeList = dislikesMap.getValue(index)
16+
val first = hash(hashArray, dislikeList[0])
17+
if (currentHash == first) {
18+
return false
19+
}
20+
for (d in 1 until dislikeList.size) {
21+
val dislikeHash = hash(hashArray, dislikeList[d])
22+
if (currentHash == dislikeHash) {
23+
return false
24+
}
25+
hashArray[dislikeHash] = first
26+
}
27+
}
28+
}
29+
return true
30+
}
31+
32+
private fun hash(hashArray: IntArray, target: Int): Int = when (target) {
33+
hashArray[target] -> target
34+
else -> hash(hashArray, hashArray[target])
35+
}
36+
/*
37+
fun possibleBipartition(N: Int, dislikes: Array<IntArray>): Boolean {
38+
val dislikesMap: MutableMap<Int, MutableList<Int>> = HashMap()
39+
for (dislike in dislikes) {
40+
dislikesMap.computeIfAbsent(dislike[0]) { mutableListOf() }.add(dislike[1])
41+
dislikesMap.computeIfAbsent(dislike[1]) { mutableListOf() }.add(dislike[0])
42+
}
43+
val nArray = IntArray(N + 1)
44+
for (index in 1 until nArray.size) {
45+
if (nArray[index] == 0) {
46+
val queue: Queue<Int> = LinkedList()
47+
nArray[index] = 1
48+
queue.offer(index)
49+
while (queue.isNotEmpty()) {
50+
val current = queue.poll()
51+
dislikesMap[current]?.forEach {
52+
when {
53+
nArray[it] == nArray[current] -> return false
54+
nArray[it] == 0 -> {
55+
nArray[it] = -nArray[current]
56+
queue.offer(it)
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
return true
64+
}
65+
*/
66+
/*
67+
fun possibleBipartition(N: Int, dislikes: Array<IntArray>): Boolean {
68+
val dpDislikes = Array(N + 1) { IntArray(N + 1) }
69+
for (dislike in dislikes) {
70+
dpDislikes[dislike[0]][dislike[1]] = 1
71+
dpDislikes[dislike[1]][dislike[0]] = 1
72+
}
73+
val nArray = IntArray(N + 1)
74+
for (index in 1 until nArray.size) {
75+
if (nArray[index] == 0 && canNotGroup(index, 1, nArray, dpDislikes)) {
76+
return false
77+
}
78+
}
79+
return true
80+
}
81+
82+
private fun canNotGroup(
83+
n: Int,
84+
group: Int,
85+
nArray: IntArray,
86+
dpDislikes: Array<IntArray>
87+
): Boolean = !canGroup(n, group, nArray, dpDislikes)
88+
89+
private fun canGroup(
90+
n: Int,
91+
group: Int,
92+
nArray: IntArray,
93+
dpDislikes: Array<IntArray>
94+
): Boolean {
95+
nArray[n] = group
96+
for (index in 1 until dpDislikes.size) {
97+
if (dpDislikes[n][index] == 1) {
98+
when {
99+
nArray[index] == group -> return false
100+
nArray[index] == 0 && canNotGroup(index, -group, nArray, dpDislikes) -> return false
101+
}
102+
}
103+
}
104+
return true
105+
}
106+
*/
107+
}

0 commit comments

Comments
 (0)