Skip to content

Commit 5b34d87

Browse files
Merge pull request #216
add new problems 18.09
2 parents cd24a08 + 02853d3 commit 5b34d87

File tree

3 files changed

+119
-3
lines changed

3 files changed

+119
-3
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.github.contest
22

33

44
import com.github.contest.math.numberOfPowerfulInt
5-
import com.github.contest.math.replaceNonCoPrimes
65
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
76
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
87
import com.github.contest.strings.fullJustify
@@ -15,9 +14,19 @@ import java.util.TreeMap
1514

1615
fun main() {
1716

18-
val data = listOf(287, 41, 49, 287, 899, 23, 23, 20677, 5, 825).toIntArray()
17+
Example("Param")
18+
}
19+
20+
class Example(val param: String) { // Primary constructor parameter
21+
init {
22+
println("init block: $param") // Can access param!
23+
}
1924

20-
replaceNonCoPrimes(data).also { println(it) }
25+
val property = "Property: $param".also { println(it) } // Property initializer
26+
27+
constructor(secondary: Int) : this("Secondary:$secondary") { // Secondary constructor
28+
println("Secondary constructor")
29+
}
2130
}
2231

2332

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.contest.binaryTree;
2+
3+
import java.util.LinkedList;
4+
5+
6+
/**
7+
* 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
8+
*/
9+
10+
public class FindCorrespondingNode {
11+
12+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
13+
LinkedList<TreeNode> queue = new LinkedList<>();
14+
queue.offer(cloned);
15+
16+
while (!queue.isEmpty()) {
17+
int size = queue.size();
18+
19+
for (int i = 0; i < size; i++) {
20+
TreeNode node = queue.poll();
21+
22+
if (node.getVal() == target.getVal()) {
23+
return node;
24+
}
25+
26+
if (node.getLeft() != null) {
27+
queue.offer(node.getLeft());
28+
}
29+
30+
if (node.getRight() != null) {
31+
queue.offer(node.getRight());
32+
}
33+
}
34+
}
35+
36+
return null;
37+
}
38+
39+
}

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.contest.design
22

33
import java.util.LinkedList
4+
import java.util.PriorityQueue
45

56
/**
67
* 1352. Product of the Last K Numbers
@@ -403,4 +404,71 @@ class FindSumPairs(private val nums1: IntArray, private val nums2: IntArray) {
403404
return c
404405
}
405406

407+
}
408+
409+
/**
410+
* 2353. Design a Food Rating System
411+
*/
412+
413+
class FoodRatings(foods: Array<String>, cuisines: Array<String>, ratings: IntArray) {
414+
415+
private val foodData = mutableMapOf<String, FoodData>()
416+
private val cuisinesFoods = mutableMapOf<String, PriorityQueue<CuisineFoodInfo>>()
417+
private val versionFood = mutableMapOf<String, Int>()
418+
419+
init {
420+
for (i in 0 until foods.size) {
421+
val dataFood = FoodData(cuisines[i], ratings[i])
422+
val info = CuisineFoodInfo(foods[i], ratings[i], 1)
423+
424+
versionFood[foods[i]] = 1
425+
foodData[foods[i]] = dataFood
426+
cuisinesFoods.getOrPut(cuisines[i]) {
427+
PriorityQueue(comparatorFood).apply {
428+
add(info)
429+
}
430+
}.add(info)
431+
}
432+
}
433+
434+
435+
fun changeRating(food: String, rating: Int) {
436+
val dataFood = foodData[food]
437+
val oldVersion = versionFood[food]!!
438+
versionFood[food] = versionFood.getOrDefault(food, 0) + 1
439+
val cuisineFoodInfo = CuisineFoodInfo(food, rating, oldVersion + 1)
440+
cuisinesFoods[dataFood?.cuisine]?.add(cuisineFoodInfo)
441+
}
442+
443+
444+
fun highestRated(cuisine: String): String {
445+
val queue = cuisinesFoods[cuisine]!!
446+
447+
while (queue.isNotEmpty()) {
448+
val top = queue.peek()
449+
450+
if (top.version != versionFood[top.food]) {
451+
queue.poll()
452+
continue
453+
}
454+
455+
if (top.version == versionFood[top.food]) {
456+
return top.food
457+
}
458+
}
459+
460+
return ""
461+
462+
}
463+
464+
private data class FoodData(val cuisine: String, val rating: Int)
465+
private data class CuisineFoodInfo(val food: String, val rating: Int, val version: Int)
466+
467+
companion object {
468+
private val comparatorFood = Comparator<CuisineFoodInfo> { a, b ->
469+
if (a.rating != b.rating) b.rating - a.rating
470+
else a.food.compareTo(b.food)
471+
}
472+
}
473+
406474
}

0 commit comments

Comments
 (0)