We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
dataclassNode(varprev:Node?,valvalue:Int,varnext:Node?)funpoisonousPlants(p:Array<Int>):Int{// Write your code herevartoBeDeleted=mutableListOf<Node>()varpreviousNode:Node?=nullfor(valueinp.reversed()){valcurrentNode=Node(previousNode,value,null)previousNode?.next=currentNodepreviousNode=currentNodecurrentNode.prev?.let{prev->if(prev.value>currentNode.value)toBeDeleted.add(prev)}}vardays=0while(toBeDeleted.isNotEmpty()){valtoBeDeletedTemp=mutableListOf<Node>()toBeDeleted.forEach{nodeToBeDeleted->nodeToBeDeleted.prev?.next=nodeToBeDeleted.nextnodeToBeDeleted.next?.prev=nodeToBeDeleted.previf(nodeToBeDeleted.next!=null&&nodeToBeDeleted.prev!=null&&nodeToBeDeleted.next?.value!!<nodeToBeDeleted.prev!!.value)toBeDeletedTemp.add(nodeToBeDeleted.prev!!)}toBeDeleted=toBeDeletedTempdays++}returndays}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
Kotlin