diff --git a/LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt b/LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt new file mode 100644 index 000000000..c0028ffc4 --- /dev/null +++ b/LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt @@ -0,0 +1,28 @@ +class Solution { + fun smallestDistancePair(nums: IntArray, k: Int): Int { + //pick two regardlesss of order + val n = nums.size + + fun count(dist:Int, k:Int):Boolean{ //using two pointer two count number of valid distances + var (l,r,count) = listOf(0,0,0) + while(l=k + } + nums.sort() + + var l = 0 + var r = nums[n-1]-nums[0] + + while(l=k + } + while(l, k: Int): Int { + + events.sortBy{it[0]}//시작일,종료일로 정렬 + val n = events.size + // val lastDay=events[n-1][1] + val dp = Array(n+1){IntArray(k+1)} + fun upperBound(events:Array, start:Int, target:Int):Int{ + var l=start + var r= events.size + while(l, k: Int): Int { + //(시작 ,종료,보상) 종료일+1부터 다음 이벤트 + //k개를 골랐을 때 maxSum + // var r = events.sumOf{it[2]} + events.sortBy{it[0]}//시작일로 정렬(이분탐색 대상이 시작일이기 때문) + val n = events.size + // val lastDay=events[n-1][1] + val dp = Array(n+1){IntArray(k+1){-1}} + fun upperBound(events:Array, start:Int, target:Int):Int{ + var l=start + var r= events.size + while(l=n || k==0)return 0 + if(dp[idx][k]!=-1)return dp[idx][k] + val next = upperBound(events,idx+1,events[idx][1]) + dp[idx][k] = maxOf(solve(idx+1,k), events[idx][2]+solve(next,k-1)) + return dp[idx][k] + } + + return solve(0,k) + } +} \ No newline at end of file diff --git a/LeetCode/Binary_Search/Shortest Distance to Target Color/inudev5.kt b/LeetCode/Binary_Search/Shortest Distance to Target Color/inudev5.kt new file mode 100644 index 000000000..3f3ac455b --- /dev/null +++ b/LeetCode/Binary_Search/Shortest Distance to Target Color/inudev5.kt @@ -0,0 +1,39 @@ +class Solution { + //solution using binary search + fun shortestDistanceColor(colors: IntArray, queries: Array): List { + val map = hashMapOf>() + for(i in colors.indices){ + map.putIfAbsent(colors[i], mutableListOf()) + map[colors[i]]!!.add(i) + } + val res = mutableListOf() + fun lowerBound(arr:MutableList, target:Int):Int{ + var l=0 + var r=arr.size + while(l=target -> r=mid + else -> l=mid+1 + } + } + return l + } + for((idx,color) in queries){ + if(color !in map){res.add(-1);continue} + val idxList = map[color]!! + val pos = lowerBound(idxList,idx) //idx 이상인 최소 인덱스 + when{ + pos==idxList.size -> res.add(idx-idxList[idxList.lastIndex]) + pos==0 -> res.add(idxList[0]-idx) + else-> { + val left = idx-idxList[pos-1] + val right = idxList[pos]-idx + res.add(minOf(left,right)) + } + } + } + + return res + } +} \ No newline at end of file diff --git a/LeetCode/Union_Find/Lexicographically Smallest Equivalent String/inudev5.kt b/LeetCode/Union_Find/Lexicographically Smallest Equivalent String/inudev5.kt new file mode 100644 index 000000000..9c4098a13 --- /dev/null +++ b/LeetCode/Union_Find/Lexicographically Smallest Equivalent String/inudev5.kt @@ -0,0 +1,41 @@ +class Solution { + class UnionFind(size:Int){ + val root:IntArray by lazy { IntArray(size){it} } + + + fun union(x:Int, y:Int):Boolean{ + val rootX = find(x) + val rootY = find(y) + if(rootX!=rootY){ + when{ + rootX>rootY -> root[rootX] = rootY + rootX root[rootY] = rootX + } + return true + } + return false + } + fun find(x:Int):Int{ + if(x==root[x])return x + root[x] = find(root[x]) + return root[x] + val set = hashSetOf(intArrayOf(1,2,3,4)) + + } + + } + + fun smallestEquivalentString(s1: String, s2: String, baseStr: String): String { + val n = s1.length + val unionfind = UnionFind(26) + for(i in 0..n-1){ + unionfind.union(s1[i]-'a', s2[i]-'a') + } + var res = "" + for(i in baseStr.indices){ + val num = unionfind.find(baseStr[i]-'a') + res += (num+'a'.toInt()).toChar() + } + return res + } +} \ No newline at end of file diff --git a/LeetCode/Union_Find/Sentent Similarity 2/inudev5.kt b/LeetCode/Union_Find/Sentent Similarity 2/inudev5.kt new file mode 100644 index 000000000..8b2ca832e --- /dev/null +++ b/LeetCode/Union_Find/Sentent Similarity 2/inudev5.kt @@ -0,0 +1,51 @@ +class Solution { + class UnionFind(sentence:List>){ + + val root:HashMap by lazy { + val map =hashMapOf() + for((a,b) in sentence){map.putIfAbsent(a,a);map.putIfAbsent(b,b)} + map + } + val rank:HashMap by lazy { + val map =hashMapOf() + root.keys.forEach{key-> map.put(key,1)} + map + } + + //node가 String인 경우 + + + fun union(x:String, y:String):Boolean{ + val rootX = find(x) + val rootY = find(y) + if(rootX!=rootY){ + when{ + rank[rootX]!!>rank[rootY]!! -> root[rootY] = rootX + rank[rootX]!! root[rootX] = rootY + else->{ + root[rootX] = rootY + rank.put(rootY, rank.getOrDefault(rootY,1)+1) + } + } + return true + } + return false + } + fun find(word:String):String{ + if(word==root[word])return word + root[word] = find(root.getValue(word)) + return root.getValue(word) + } + } + fun areSentencesSimilarTwo(sentence1: Array, sentence2: Array, similarPairs: List>): Boolean { + if(sentence1.size!=sentence2.size)return false + val unionfind = UnionFind(similarPairs) + for((a,b) in similarPairs)unionfind.union(a,b) + for(i in sentence1.indices){ + if(sentence1[i]==sentence2[i])continue + if(unionfind.root[sentence1[i]]==null ||unionfind.root[sentence2[i]]==null) return false + if(unionfind.find(sentence1[i])!=unionfind.find(sentence2[i]))return false + } + return true + } +} \ No newline at end of file diff --git a/LeetCode/Union_Find/The Earliest Moment When Everyone Become Friends/inudev5.kt b/LeetCode/Union_Find/The Earliest Moment When Everyone Become Friends/inudev5.kt new file mode 100644 index 000000000..67bbb6962 --- /dev/null +++ b/LeetCode/Union_Find/The Earliest Moment When Everyone Become Friends/inudev5.kt @@ -0,0 +1,39 @@ +class Solution { + class UnionFind(size:Int){ + val root:IntArray by lazy { IntArray(size){it} } + val rank:IntArray by lazy { IntArray(size){1} } + + fun union(x:Int, y:Int):Boolean{ + val rootX = find(x) + val rootY = find(y) + if(rootX!=rootY){ + when{ + rank[rootX]>rank[rootY] -> root[rootY] = rootX + rank[rootX] root[rootX]= rootY + else->{ + root[rootY]= rootX + rank[rootX]+=1 + } + } + return true + } + return false + } + fun find(x:Int):Int{ + if(x==root[x])return x + root[x] = find(root[x]) + return root[x] + } + } + fun earliestAcq(logs: Array, n: Int): Int { + val uf=UnionFind(n) + var prestamp = 0 + var count=n + logs.sortBy({it[0]}) + for((timestamp,a,b) in logs){ + if(uf.union(a,b))count-=1 + if(count==1)return timestamp + } + return -1 + } +} \ No newline at end of file