From 1a1224589ede78d12273df59a4a1060b7c222a5d Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 13:58:13 +0900 Subject: [PATCH 1/8] [Union Find] The Earliest Moment When Everyone Become Friends --- .../inudev5.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 LeetCode/Union_Find/The Earliest Moment When Everyone Become Friends/inudev5.kt 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 From e50566b354150c83fe3ab6957b0187972024e97e Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 14:07:15 +0900 Subject: [PATCH 2/8] [Union Find]Sentence Similarity II --- .../Sentent Similarity 2/inudev5.kt | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 LeetCode/Union_Find/Sentent Similarity 2/inudev5.kt 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 From d92c463b9722ecb87bc37e33c9f30d8712dc0f3d Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 14:11:43 +0900 Subject: [PATCH 3/8] [Union Find] Lexicographically Smallest Equivalent String --- .../inudev5.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 LeetCode/Union_Find/Lexicographically Smallest Equivalent String/inudev5.kt 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..deb58185f --- /dev/null +++ b/LeetCode/Union_Find/Lexicographically Smallest Equivalent String/inudev5.kt @@ -0,0 +1,37 @@ +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] + } + } + 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 From 992d05d17b3cfbc315f1e057dafdbaecbdb639f5 Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 14:58:29 +0900 Subject: [PATCH 4/8] [Binary_Search] Kth Smallest Number in Multiplication Table --- .../inudev5.kt | 20 +++++++++++++++++++ .../inudev5.kt | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 LeetCode/Binary_Search/Kth Smallest Number in Multiplication Table/inudev5.kt diff --git a/LeetCode/Binary_Search/Kth Smallest Number in Multiplication Table/inudev5.kt b/LeetCode/Binary_Search/Kth Smallest Number in Multiplication Table/inudev5.kt new file mode 100644 index 000000000..940bfb391 --- /dev/null +++ b/LeetCode/Binary_Search/Kth Smallest Number in Multiplication Table/inudev5.kt @@ -0,0 +1,20 @@ +class Solution { + fun findKthNumber(m: Int, n: Int, k: Int): Int { + //k번째 작은수 + // mat[i][j] = (i+1)*(j+1) + var l = 1 + var r = m*n + fun count(x:Int,k:Int):Boolean{ //if x has k or more values less than it + var count=0 + for(i in 1..m)count+= minOf(x/i,n) //col에서 가능한 count + return count>=k + } + while(l Date: Wed, 1 Feb 2023 15:37:37 +0900 Subject: [PATCH 5/8] [Binary_Search]Find K-th Smallest Pair Distance --- .../inudev5.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt 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..f6f6ae796 --- /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 Date: Wed, 1 Feb 2023 16:17:16 +0900 Subject: [PATCH 6/8] .. --- .../inudev5.kt | 2 +- .../inudev5.kt | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5.kt 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 index f6f6ae796..c0028ffc4 100644 --- a/LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt +++ b/LeetCode/Binary_Search/Find K-th Smallest Pair Distance/inudev5.kt @@ -25,4 +25,4 @@ class Solution { } return l } -} \ No newline at end of file +} diff --git a/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5.kt b/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5.kt new file mode 100644 index 000000000..390dedaa0 --- /dev/null +++ b/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5.kt @@ -0,0 +1,30 @@ +class Solution { + fun maxValue(events: Array, 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 From 55f0086566dc429942958e467cf781886101fb33 Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 16:23:55 +0900 Subject: [PATCH 7/8] .. --- .../inudev5-iterative.kt | 24 +++++++++++++++++++ .../inudev5.kt | 1 + 2 files changed, 25 insertions(+) create mode 100644 LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5-iterative.kt diff --git a/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5-iterative.kt b/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5-iterative.kt new file mode 100644 index 000000000..b62a7338f --- /dev/null +++ b/LeetCode/Binary_Search/Maximum Number of Events That Can Be Attended II/inudev5-iterative.kt @@ -0,0 +1,24 @@ +class Solution { + fun maxValue(events: Array, 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 From 69dd83de1cb7bb853f0c855794e9c71add13575a Mon Sep 17 00:00:00 2001 From: Inu Jung Date: Wed, 1 Feb 2023 16:43:50 +0900 Subject: [PATCH 8/8] .. --- .../inudev5.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 LeetCode/Binary_Search/Shortest Distance to Target Color/inudev5.kt 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