class Solution {
public int[] circularGameLosers(int n, int k) {
int[] count = new int[n];
int i=0;
int j=0;
while(true){
count[i]++;
if(count[i] >1) {
return getZero(count);
}
j++;
i=(j*k+i)%n;
}
}
public int[] getZero(int[] count ){
List<Integer> list = new ArrayList<>();
for(int i=0;i<count.length;i++){
if(count[i] == 0){
list.add(i+1);
}
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
class Solution {
public boolean doesValidArrayExist(int[] derived) {
int[] res = new int[derived.length];
res[0]=1;
for(int i=1;i<derived.length;i++){
if(derived[i-1]==1){
res[i] = 1 - res[i-1];
}else{
res[i] = res[i-1];
}
}
if(derived[derived.length-1]==1){
return res[derived.length-1] == 1 - res[0];
}else{
return res[derived.length-1] == res[0];
}
}
}
class Solution {
public int maxMoves(int[][] grid) {
int[][] dp = new int[grid.length][grid[0].length];
int max = 0;
for(int i = grid[0].length-2;i>=0;i--){
for(int j=0;j<grid.length;j++){
if(j == 0 ){
if(grid[j+1][i+1] > grid[j][i] && dp[j+1][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j+1][i+1] +1;
}
if(grid[j][i+1] > grid[j][i] && dp[j][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j][i+1] +1;
}
} else if(j == grid.length-1){
if(grid[j-1][i+1] > grid[j][i] && dp[j-1][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j-1][i+1] +1;
}
if(grid[j][i+1] > grid[j][i] && dp[j][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j][i+1] +1;
}
}else {
if(grid[j-1][i+1] > grid[j][i] && dp[j-1][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j-1][i+1] +1;
}
if(grid[j][i+1] > grid[j][i] && dp[j][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j][i+1] +1;
} if(grid[j+1][i+1] > grid[j][i] && dp[j+1][i+1] +1 > dp[j][i]) {
dp[j][i] = dp[j+1][i+1] +1;
}
}
if(i==0 && dp[j][i] >max){
max = dp[j][i];
}
}
}
return max;
}
public int getMax(int a,int b,int c) {
return Math.max(Math.max(a,b),c);
}
}
class Solution {
private List<Integer>[] g;
private boolean vis[];
private int v, e;
public int countCompleteComponents(int n, int[][] edges) {
g = new ArrayList[n];
Arrays.setAll(g, e -> new ArrayList<>());
for (var e : edges) {
int x = e[0], y = e[1];
g[x].add(y);
g[y].add(x); // 建图
}
int ans = 0;
vis = new boolean[n];
for (int i = 0; i < n; i++) {
if (!vis[i]) {
v = 0;
e = 0;
dfs(i);
if (e == v * (v - 1))
ans++;
}
}
return ans;
}
private void dfs(int x) {
vis[x] = true;
v++;
e += g[x].size();
for (var y : g[x])
if (!vis[y])
dfs(y);
}
}