头像

情意




离线:17天前


最近来访(0)

活动打卡代码 AcWing 435. 传球游戏

情意
2个月前
import java.util.Scanner;
public class Main{
    public static void main(String [] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int [][] f = new int[31][31];
        f[0][0] = 1;
         for (int i = 1; i <= m; i ++ ){
            for (int j = 0; j < n; j ++ ){
             f[i][j] = f[i - 1][(j + n - 1) % n] + f[i - 1][(j + 1) % n];
            }
         }
         System.out.println(f[m][0]);
    }
}


活动打卡代码 LeetCode 1473. 粉刷房子 III

情意
2个月前

不会做 复制粘贴…

class Solution {
    public int minCost(int[] houses, int[][] cost, int m, int n, int target) {
        // dp[i][j][k] 涂完前i个房子,目前有j个街区,且第i个房子的颜色为k的所有方案 cost最小的。
        // 房子从0开始,街区从1开始,颜色从1开始
        final int INF = (int)Math.pow(10, 8);
        // System.out.println(INF);
        int[][][] dp = new int [m+1][target+1][n+1];
        for(int i = 0; i < m + 1; i++){
            for(int j = 0; j < target + 1; j++){
                for(int k = 0; k < n + 1; k++){
                    dp[i][j][k] = INF;
                }
            }
        }
        // 初始化第0个房子
        // 第0个房子已经涂色
        if(houses[0] > 0){
            dp[0][1][houses[0]] = 0;
        }else{
            // 第0个房子没有涂色,初始化cost
            for(int i = 1; i <= n; i++){
                dp[0][1][i] = cost[0][i - 1];
            }
        }
        // 状态转移时涂色完第i个房子。
        for(int i = 1; i < m; i++){
            // 最多target个街区
            for(int j = 1; j <= target; j++){
                // 分成第i个房子是否涂色
                if(houses[i] > 0){
                    int temp = houses[i];
                    for(int k = 1; k <= n; k++){
                        // 分成第i个房子和第i-1的房子
                        // 如果两个房子颜色相同,那么街区数就相同
                        // 如果两个房子颜色不同,那么第i个房子就独自成一个街区
                        if(temp == k){
                            dp[i][j][temp] = Math.min(dp[i][j][temp], dp[i - 1][j][k]);
                        }else{
                            dp[i][j][temp] = Math.min(dp[i][j][temp], dp[i - 1][j - 1][k]);
                        }
                    }
                }else{
                    for(int k = 1; k <= n; k++){
                        for(int s = 1; s <= n; s++){
                            if(k == s){
                                dp[i][j][k] = Math.min(dp[i][j][k], dp[i - 1][j][s] + cost[i][k - 1]);
                            }else{
                                dp[i][j][k] = Math.min(dp[i][j][k], dp[i - 1][j - 1][s] + cost[i][k - 1]);
                            }
                        }
                    }

                }
            }
        }
        int res = INF;
        for(int i = 1; i <= n; i++){
            res = Math.min(res, dp[m-1][target][i]);
            // System.out.println(res);
        }
        if(res == INF) return -1;
        return res;


    }
}



活动打卡代码 AcWing 78. 左旋转字符串

情意
2个月前
class Solution {
    public String leftRotateString(String str,int n) {
        String left = str.substring(0,n);
        str = str.substring(n);
        return str + left;
    }
}


活动打卡代码 AcWing 7. 整数反转

情意
2个月前

class Solution {
    public int reverse(int x) {
        long res = 0;
        while (x!=0){
            res +=x%10;
            x/=10;
            res*=10;
        }
        res/=10;
       return (int)res==res? (int)res:0;
    }
}


活动打卡代码 LeetCode 554. 砖墙

情意
2个月前

import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
    public int leastBricks(List<List<Integer>> wall) {
        Map<Integer,Integer> map = new HashMap<>();
        for (List<Integer> list : wall) {
            int n = list.size();
            int sum = 0;
            for (int i = 0; i < n - 1; i++) {
                sum +=list.get(i);
                map.put(sum,map.getOrDefault(sum,0) + 1);
            }
        }
        int max = 0;
        for (Map.Entry<Integer, Integer> integerIntegerEntry : map.entrySet()) {
            max = Math.max(max,integerIntegerEntry.getValue());
        }
        return wall.size() - max;
    }
}


活动打卡代码 AcWing 53. 最小的k个数

情意
2个月前
// import java.util.*;

class Solution {
    public List<Integer> getLeastNumbers_Solution(int [] input, int k) {
        List<Integer> res = new LinkedList<>();
        Arrays.sort(input);
        for(int i = 0 ;i<k;i++){
            res.add(input[i]);
        }
        return res;
    }
}



情意
2个月前
/*
// Definition for Employee.
class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
};
*/


class Solution {
    public int getImportance(List<Employee> employees, int id) {
        for (Employee e : employees) {
            if (e.id==id){
                if (e.subordinates.size()==0){
                    return e.importance;
                }
                for (Integer s : e.subordinates) {
                    e.importance+=getImportance(employees,s);
                }
                return e.importance;
            }
        }
        return 0;
    }
}




情意
2个月前
class Solution {
    public int[] findNumsAppearOnce(int[] nums) {
        int xor = 0;
        for (int num : nums) {
            xor^=num;
        }
        int mask = 1;
        while((xor&mask)==0){
            mask<<=1;
        }
        int a = 0;
        int b = 0;
        for (int num : nums) {
            if ((num&mask)==0){
                a^=num;
            }else {
                b^=num;
            }
        }
        return new int[]{a,b};
    }
}



情意
2个月前
class Solution {
    public int singleNumber(int[] nums) {
        int a = 0, b = 0;
        for(int n : nums){
            a = (a^n) & ~b;
            b = (b^n) & ~a;
        }
        return a;
    }
}


活动打卡代码 LeetCode 403. 青蛙过河

情意
2个月前
class Solution {
    public boolean canCross(int[] stones) {
        int n  =stones.length;
        if(stones[1] !=1){
            return false;
        }
        int [][] dp = new int[n][n+1];
        dp[0][0] = 1;
        for(int i =1 ;i<n;i++){
            for(int j = 0;j<i;j++){
                int k = stones[i] - stones[j];
                if(k<=j+1){
                    dp[i][k] = (dp[j][k - 1]==1 || dp[j][k]==1 || dp[j][k + 1]==1) ? 1 : 0;
                    if(i==n-1 && dp[i][k]==1){
                        return true;
                    }
                }

            }
        }
        return false;
    }
}