kk
This commit is contained in:
95
src/main/java/com/dota/arr/Solution.java
Normal file
95
src/main/java/com/dota/arr/Solution.java
Normal 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";
|
||||
}
|
||||
}
|
59
src/main/java/com/dota/design/FoodRatings.java
Normal file
59
src/main/java/com/dota/design/FoodRatings.java
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user