This commit is contained in:
kkunkka
2025-02-28 16:07:54 +08:00
parent 07747aebba
commit d252ccc790
2 changed files with 154 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
package com.dota.arr;
/**
* Your FoodRatings object will be instantiated and called as such:
* FoodRatings obj = new FoodRatings(foods, cuisines, ratings);
* obj.changeRating(food,newRating);
* String param_2 = obj.highestRated(cuisine);
*/
class Solution {
public static void main(String[] args) {
new Solution().tictactoe(new String[]{"O X", " XO", "X O"});
}
public String tictactoe(String[] board) {
int n = board.length;
char[][] boards = new char[n][n];
var flag = false;
for (int i = 0; i < n; i++) {
var tmp = boards[i];
int j = 0;
for (char c : board[i].toCharArray()) {
tmp[j++] = c;
if (c == ' ') {
flag = true;
}
}
}
for (int i = 0; i < n; i++) {
int j;
for (j = 1; j < n; j++) {
if (boards[i][j] == ' ' || boards[i][j] != boards[i][j - 1]) {
break;
}
}
if (j == n) {
if (boards[i][j - 1] == ' ') {
return "Pending";
}
return String.valueOf(boards[i][0]);
}
}
for (int j = 0; j < n; j++) {
int i;
for (i = 1; i < n; i++) {
if (boards[i][j] == ' ' || boards[i][j] != boards[i - 1][j]) {
break;
}
}
if (i == n) {
return String.valueOf(boards[0][j]);
}
}
int i = 1;
int j = 1;
var count = 1;
while (i < n && j < n) {
if (boards[i][j] == ' ' || boards[i][j] != boards[i - 1][j - 1]) {
break;
}
count++;
i++;
j++;
if (count == n) {
return String.valueOf(boards[0][0]);
}
}
i = 1;
j = n - 2;
count = 1;
while (i < n && j >= 0) {
if (boards[i][j] == ' ' || boards[i][j] != boards[i - 1][j + 1]) {
break;
}
count++;
i++;
j--;
if (count == n) {
return String.valueOf(boards[0][n - 1]);
}
}
if (flag) {
return "Pending";
}
return "Draw";
}
}

View File

@@ -0,0 +1,59 @@
package com.dota.design;
import java.util.*;
class Food {
public String name;
public String type;
public int order;
public Food(String name, String type, int order) {
this.name = name;
this.type = type;
this.order = order;
}
public String toString() {
return name + type + order;
}
}
class FoodRatings {
public static void main(String[] args) {
var f = new FoodRatings(new String[]{"A", "B"}, new String[]{"FF", "FF"}, new int[]{1, 2});
System.out.println(f.highestRated("FF"));
f.changeRating("A", 4);
System.out.println(f.highestRated("FF"));
}
// 名
Map<String, Food> foodMap = new HashMap<>();
// 类
Map<String, TreeSet<Food>> foodTreeMap = new HashMap<>();
public FoodRatings(String[] foods, String[] cuisines, int[] ratings) {
for (int i = 0; i < foods.length; i++) {
var f = new Food(foods[i], cuisines[i], ratings[i]);
foodMap.put(foods[i], f);
foodTreeMap.computeIfAbsent(cuisines[i], k -> new TreeSet<>((a, b) -> {
if (a.order == b.order) {
return a.name.compareTo(b.name);
}
return a.order - b.order;
}));
foodTreeMap.get(cuisines[i]).add(f);
}
}
public void changeRating(String food, int newRating) {
var f = foodMap.get(food);
foodTreeMap.get(f.type).remove(f);
f.order = newRating;
foodTreeMap.get(f.type).add(f);
}
public String highestRated(String cuisine) {
return foodTreeMap.get(cuisine).last().name;
}
}