-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# id: 문제 id를 숫자로 작성 # categories : 해당 문제의 유형을 ,로 구분하여 작성 # tags : 해당 문제의 태그를 ,로 구분하여 작성 # time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성 # try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성 # help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성 # url : 해당 문제의 url을 작성 id: categories: [] tags: [] time: try: help: false url:
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import kotlin.math.* | ||
|
||
class Heap(){ | ||
val arr=mutableListOf<Int>() | ||
|
||
fun push(e:Int){ | ||
arr.add(e) | ||
var cIndex=arr.size | ||
if(arr.size>1){ | ||
while(cIndex!=1){ | ||
val parent=cIndex/2 | ||
if(arr[parent-1]<arr[cIndex-1]){ | ||
val temp=arr[parent-1] | ||
arr[parent-1]=e | ||
arr[cIndex-1]=temp | ||
cIndex=parent | ||
}else break | ||
} | ||
} | ||
} | ||
|
||
fun pop():Int?{ | ||
if(arr.isEmpty()) return null | ||
val res=arr[0] | ||
val leaf=arr.removeLast() | ||
if(arr.isEmpty()) return res | ||
arr[0]=leaf | ||
var idx=1 | ||
while(arr.isNotEmpty() && idx<arr.size){ | ||
val c1=2.0.pow(idx).toInt() | ||
val c2=c1+1 | ||
if(c1-1<arr.size && arr[idx-1]<arr[c1-1] && arr[c1-1]>arr[c2-1]){ | ||
val temp=arr[c1-1] | ||
arr[c1-1]=arr[idx-1] | ||
arr[idx-1]=temp | ||
idx=c1 | ||
}else if(c2-1<arr.size && arr[idx-1]<arr[c2-1] && arr[c2-1]>arr[c1-1]){ | ||
val temp=arr[c2-1] | ||
arr[c2-1]=arr[idx-1] | ||
arr[idx-1]=temp | ||
idx=c2 | ||
}else break | ||
} | ||
return res | ||
} | ||
} | ||
|
||
fun main(){ | ||
|
||
val h=Heap() | ||
h.push(2) | ||
h.push(1) | ||
h.push(3) | ||
h.push(1) | ||
println(h.arr) | ||
println(h.pop()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import java.util.* | ||
|
||
class ArrayQueue(val size:Int){ | ||
val arr=IntArray(size) | ||
var front=0 | ||
var rear=0 | ||
var qSize=0 | ||
fun enqueue(e:Int){ | ||
if(qSize==size){ | ||
println("queue is full") | ||
return | ||
} | ||
arr[rear]=e | ||
qSize++ | ||
rear=(rear+1)%size | ||
} | ||
|
||
fun dequeue():Int?{ | ||
if(qSize==0) return null | ||
val e=arr[front] | ||
qSize-- | ||
front=(front+1)%size | ||
return e | ||
|
||
} | ||
} | ||
|
||
class LinkedListQueue(){ | ||
val arr=LinkedList<Int>() | ||
|
||
fun enqueue(e:Int){ | ||
arr.add(e) | ||
} | ||
|
||
fun dequeue():Int?{ | ||
if(arr.isEmpty())return null | ||
return arr.removeLast() | ||
} | ||
} | ||
|
||
fun main() { | ||
val q=LinkedListQueue() | ||
q.enqueue(0) | ||
q.enqueue(1) | ||
q.enqueue(2) | ||
println(q.dequeue()) | ||
println(q.dequeue()) | ||
println(q.dequeue()) | ||
println(q.dequeue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import java.util.* | ||
|
||
class ArrayStack(size:Int){ | ||
val arr=IntArray(size) | ||
var top=-1 | ||
|
||
fun push(e:Int){ | ||
if(top==arr.size-1){ | ||
println("stack is full") | ||
return | ||
} | ||
|
||
top++ | ||
arr[top]=e | ||
} | ||
|
||
fun pop():Int?{ | ||
if(top==-1){ | ||
println("stack is empty") | ||
return null | ||
} | ||
val e=arr[top] | ||
top-- | ||
return e | ||
} | ||
} | ||
|
||
class DynamicArrayStack(){ | ||
|
||
var size=2 | ||
var arr=IntArray(size) | ||
var top=-1 | ||
|
||
fun push(e:Int){ | ||
if(top==arr.size-1){ | ||
swapBigger() | ||
} | ||
|
||
top++ | ||
arr[top]=e | ||
} | ||
|
||
fun swapBigger(){ | ||
val newArr=IntArray(size*2) | ||
size*=2 | ||
for(i in arr.indices){ | ||
newArr[i]=arr[i] | ||
} | ||
|
||
arr=newArr | ||
} | ||
|
||
|
||
|
||
fun pop():Int?{ | ||
if(top==-1){ | ||
println("stack is empty") | ||
return null | ||
} | ||
val e=arr[top] | ||
top-- | ||
return e | ||
} | ||
|
||
} | ||
|
||
class LinkedListStack(){ | ||
|
||
val arr=LinkedList<Int>() | ||
|
||
fun push(e:Int){ | ||
arr.add(e) | ||
} | ||
|
||
fun pop():Int?{ | ||
if(arr.isEmpty()) return null | ||
return arr.removeLast() | ||
} | ||
|
||
} | ||
|
||
fun main() { | ||
val stack=DynamicArrayStack() | ||
|
||
stack.push(1) | ||
stack.push(2) | ||
stack.push(3) | ||
println(stack.pop()) | ||
println(stack.pop()) | ||
println(stack.pop()) | ||
println(stack.pop()) | ||
|
||
} |