diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index f90fcfd..a39e140 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -2,7 +2,6 @@ package com.github.contest import com.github.contest.math.numberOfPowerfulInt -import com.github.contest.math.replaceNonCoPrimes import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern import com.github.contest.slidingWindow.customStructure.slidingWindowClassic import com.github.contest.strings.fullJustify @@ -15,9 +14,19 @@ import java.util.TreeMap fun main() { - val data = listOf(287, 41, 49, 287, 899, 23, 23, 20677, 5, 825).toIntArray() + Example("Param") +} + +class Example(val param: String) { // Primary constructor parameter + init { + println("init block: $param") // Can access param! + } - replaceNonCoPrimes(data).also { println(it) } + val property = "Property: $param".also { println(it) } // Property initializer + + constructor(secondary: Int) : this("Secondary:$secondary") { // Secondary constructor + println("Secondary constructor") + } } diff --git a/contest/src/main/java/com/github/contest/binaryTree/FindCorrespondingNode.java b/contest/src/main/java/com/github/contest/binaryTree/FindCorrespondingNode.java new file mode 100644 index 0000000..7a19074 --- /dev/null +++ b/contest/src/main/java/com/github/contest/binaryTree/FindCorrespondingNode.java @@ -0,0 +1,39 @@ +package com.github.contest.binaryTree; + +import java.util.LinkedList; + + +/** + * 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree + */ + +public class FindCorrespondingNode { + + public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) { + LinkedList queue = new LinkedList<>(); + queue.offer(cloned); + + while (!queue.isEmpty()) { + int size = queue.size(); + + for (int i = 0; i < size; i++) { + TreeNode node = queue.poll(); + + if (node.getVal() == target.getVal()) { + return node; + } + + if (node.getLeft() != null) { + queue.offer(node.getLeft()); + } + + if (node.getRight() != null) { + queue.offer(node.getRight()); + } + } + } + + return null; + } + +} diff --git a/contest/src/main/java/com/github/contest/design/DesignLeetcode.kt b/contest/src/main/java/com/github/contest/design/DesignLeetcode.kt index c75e6e7..71582c2 100644 --- a/contest/src/main/java/com/github/contest/design/DesignLeetcode.kt +++ b/contest/src/main/java/com/github/contest/design/DesignLeetcode.kt @@ -1,6 +1,7 @@ package com.github.contest.design import java.util.LinkedList +import java.util.PriorityQueue /** * 1352. Product of the Last K Numbers @@ -403,4 +404,71 @@ class FindSumPairs(private val nums1: IntArray, private val nums2: IntArray) { return c } +} + +/** + * 2353. Design a Food Rating System + */ + +class FoodRatings(foods: Array, cuisines: Array, ratings: IntArray) { + + private val foodData = mutableMapOf() + private val cuisinesFoods = mutableMapOf>() + private val versionFood = mutableMapOf() + + init { + for (i in 0 until foods.size) { + val dataFood = FoodData(cuisines[i], ratings[i]) + val info = CuisineFoodInfo(foods[i], ratings[i], 1) + + versionFood[foods[i]] = 1 + foodData[foods[i]] = dataFood + cuisinesFoods.getOrPut(cuisines[i]) { + PriorityQueue(comparatorFood).apply { + add(info) + } + }.add(info) + } + } + + + fun changeRating(food: String, rating: Int) { + val dataFood = foodData[food] + val oldVersion = versionFood[food]!! + versionFood[food] = versionFood.getOrDefault(food, 0) + 1 + val cuisineFoodInfo = CuisineFoodInfo(food, rating, oldVersion + 1) + cuisinesFoods[dataFood?.cuisine]?.add(cuisineFoodInfo) + } + + + fun highestRated(cuisine: String): String { + val queue = cuisinesFoods[cuisine]!! + + while (queue.isNotEmpty()) { + val top = queue.peek() + + if (top.version != versionFood[top.food]) { + queue.poll() + continue + } + + if (top.version == versionFood[top.food]) { + return top.food + } + } + + return "" + + } + + private data class FoodData(val cuisine: String, val rating: Int) + private data class CuisineFoodInfo(val food: String, val rating: Int, val version: Int) + + companion object { + private val comparatorFood = Comparator { a, b -> + if (a.rating != b.rating) b.rating - a.rating + else a.food.compareTo(b.food) + } + } + } \ No newline at end of file