thảo luận Leetcode + Codeforces, Competitive programming contest. Đường tới Guardian + Candidate Master.

  • Người tạo chủ đề Người tạo chủ đề freedom.9
  • Ngày bắt đầu Ngày bắt đầu
Bài cuối thấy có tí sqrt decomposition thì bỏ thôi :giggle: . Thấy gpt gen phát một, nay chắc cheat nhiều. Đề khó hơn hqua mà 3 bài trong 45p còn rank 1k mấy 2k
 
Cái ý tưởng greedy sai mẹ ngay từ đầu, quên cân nhắc mất cái case là 3 case overlapped thì ko greedy được, hơi ngu =(( rank 2k7 cmnr
 
Sửa lần cuối:
Ngồi fix bug cái Q3 bằng cái segment tree chứ nghĩ qua hướng DP là ngon trym cmnr :ah:
 
1755405216727.png

Lên thì đc 10 điểm, tụt phát mất gần 30 điểm
 
Cái bài 4 mình nghĩ là giải bằng Onsqrt(n) kiểu lấy boundary là sqrt(maxn) xử lí số nhỏ trước, số lớn thì có thể jump rồi update.
 
cái Q3 em thấy khắm đoạn chập vào nhau sinh ra case mới ( k DP đc ) hoá ra là nó cũng bao ngoài case đó rồi :(
 
Bài 4 hôm Leetcode Biweekly em có giải như thế này
Java:
class Solution {
  public static final int R = 0;
  public static final int C = 1;
  public static final int W = 2;
  public static final int K = 3;

  public int minCost(int[][] grid, int t) {
    int height = grid.length;
    int width = grid[0].length;

    int[][] teleport = new int[height * width][3];
    int idx = 0;
    for (int r = 0; r < height; r += 1) {
      for (int c = 0; c < width; c += 1) {
        teleport[idx][R] = r;
        teleport[idx][C] = c;
        teleport[idx][W] = grid[r][c];
        idx += 1;
      }
    }

    Arrays.sort(teleport, (a, b) -> a[W] - b[W]);
    
    PriorityQueue<int[]> minWeight = new PriorityQueue<>((a, b) -> a[W] - b[W]);

    int[][][] isVisited = new int[height][width][t + 1];
    for (int r = 0; r < height; r += 1) {
      for (int c = 0; c < width; c += 1) {
        Arrays.fill(isVisited[r][c], 1_000_000_000);
      }
    }
    isVisited[0][0][t] = 0;
    minWeight.add(new int[]{0, 0, 0, t});
    
    int[] teleIdx = new int[t + 1];

    while (!minWeight.isEmpty()) {
      int[] curCell = minWeight.poll();
      int r = curCell[R];
      int c = curCell[C];
      int w = curCell[W];
      int k = curCell[K];
      if (r == height - 1 && c == width - 1) {
        return w;
      }

      if (w > isVisited[r][c][k]) continue;

      if (r + 1 < height && w + grid[r + 1][c] < isVisited[r + 1][c][k]) {
        isVisited[r + 1][c][k] = w + grid[r + 1][c];
        minWeight.add(new int[]{r + 1, c, w + grid[r + 1][c], k});
      }

      if (c + 1 < width && w + grid[r][c + 1] < isVisited[r][c + 1][k]) {
        isVisited[r][c + 1][k] = w + grid[r][c + 1];
        minWeight.add(new int[]{r, c + 1, w + grid[r][c + 1], k});
      }

      if (k > 0) {
        while (teleIdx[k] < teleport.length && teleport[teleIdx[k]][W] <= grid[r][c]) {
          int[] next = teleport[teleIdx[k]];
          if (isVisited[next[R]][next[C]][k - 1] > w) {
            isVisited[next[R]][next[C]][k - 1] = w;
            minWeight.add(new int[]{next[R], next[C], w, k - 1});
          }
          teleIdx[k] += 1;
        }
      }
    }

    int min = Integer.MAX_VALUE;
    for (int i = 0; i <= t; i += 1) {
      min = Math.min(min, isVisited[height - 1][width - 1][i]);
    }
    return min;

  }
}
Thì độ phức tạp của nó là O(nmk.log(nm)) hay là O(nmk.log(nmk)) ạ? Em nghĩ là PriorityQueue có thể sẽ phải lưu tối đa n*m*k state chứ nhỉ?
 
Bài 4 hôm Leetcode Biweekly em có giải như thế này
Java:
class Solution {
  public static final int R = 0;
  public static final int C = 1;
  public static final int W = 2;
  public static final int K = 3;

  public int minCost(int[][] grid, int t) {
    int height = grid.length;
    int width = grid[0].length;

    int[][] teleport = new int[height * width][3];
    int idx = 0;
    for (int r = 0; r < height; r += 1) {
      for (int c = 0; c < width; c += 1) {
        teleport[idx][R] = r;
        teleport[idx][C] = c;
        teleport[idx][W] = grid[r][c];
        idx += 1;
      }
    }

    Arrays.sort(teleport, (a, b) -> a[W] - b[W]);
    
    PriorityQueue<int[]> minWeight = new PriorityQueue<>((a, b) -> a[W] - b[W]);

    int[][][] isVisited = new int[height][width][t + 1];
    for (int r = 0; r < height; r += 1) {
      for (int c = 0; c < width; c += 1) {
        Arrays.fill(isVisited[r][c], 1_000_000_000);
      }
    }
    isVisited[0][0][t] = 0;
    minWeight.add(new int[]{0, 0, 0, t});
    
    int[] teleIdx = new int[t + 1];

    while (!minWeight.isEmpty()) {
      int[] curCell = minWeight.poll();
      int r = curCell[R];
      int c = curCell[C];
      int w = curCell[W];
      int k = curCell[K];
      if (r == height - 1 && c == width - 1) {
        return w;
      }

      if (w > isVisited[r][c][k]) continue;

      if (r + 1 < height && w + grid[r + 1][c] < isVisited[r + 1][c][k]) {
        isVisited[r + 1][c][k] = w + grid[r + 1][c];
        minWeight.add(new int[]{r + 1, c, w + grid[r + 1][c], k});
      }

      if (c + 1 < width && w + grid[r][c + 1] < isVisited[r][c + 1][k]) {
        isVisited[r][c + 1][k] = w + grid[r][c + 1];
        minWeight.add(new int[]{r, c + 1, w + grid[r][c + 1], k});
      }

      if (k > 0) {
        while (teleIdx[k] < teleport.length && teleport[teleIdx[k]][W] <= grid[r][c]) {
          int[] next = teleport[teleIdx[k]];
          if (isVisited[next[R]][next[C]][k - 1] > w) {
            isVisited[next[R]][next[C]][k - 1] = w;
            minWeight.add(new int[]{next[R], next[C], w, k - 1});
          }
          teleIdx[k] += 1;
        }
      }
    }

    int min = Integer.MAX_VALUE;
    for (int i = 0; i <= t; i += 1) {
      min = Math.min(min, isVisited[height - 1][width - 1][i]);
    }
    return min;

  }
}
Thì độ phức tạp của nó là O(nmk.log(nm)) hay là O(nmk.log(nmk)) ạ? Em nghĩ là PriorityQueue có thể sẽ phải lưu tối đa n*m*k state chứ nhỉ?
mnk(logmnk) + mnk nhé fen
Dijkstra thì độ phức tạp tính vào maximum state *log(maximum state) nữa vì trong queue có thể sẽ có maximum state
via theNEXTvoz for iPhone
 
FSoft hỏi cả thuật toán à bác :vv lạ vậy
tôi cũng thấy lạ, bình thường pv ở mấy công ty Việt chả bao giờ dính thuật.
Btw đề cũng dễ nhớ cho ae quẩy: top down, bottom up chắc chục dòng.
Input: String x and y length < 2000. Find longest common string which is sub sequence of x and is substring of y. Output: Int
 
tôi cũng thấy lạ, bình thường pv ở mấy công ty Việt chả bao giờ dính thuật.
Btw đề cũng dễ nhớ cho ae quẩy: top down, bottom up chắc chục dòng.
Input: String x and y length < 2000. Find longest common string which is sub sequence of x and is substring of y. Output: Int
bài này thì brute-force thui mà bác. bên y chỉ lấy substring thì mình cứ phang hết thui:matrix:2000 *2000
 

Thống kê chủ đề

Ngày tạo
freedom.9,
Người trả lời cuối
deple20k,
Trả lời
1.686
Lượt xem
107.104
Quay lại
Lên đầu trang