thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
Câu kia trư ko thích, đền lại câu khác hay hơn nạ
CeBgXls.png

Java:
class Solution {
    public boolean canMeasureWater(int x, int y, int target) {
        if(x+y<target)
            return false;
        int gcd = gcd(x,y);
        if (target%gcd ==0)
            return true;
        else
            return false;
    }

    public int gcd(int x, int y){
        if(y==0)
            return x;
        return gcd(y,x%y);
    }
}
 
Java:
class Solution {
    public boolean canMeasureWater(int x, int y, int target) {
        if(x+y<target)
            return false;
        int gcd = gcd(x,y);
        if (target%gcd ==0)
            return true;
        else
            return false;
    }

    public int gcd(int x, int y){
        if(y==0)
            return x;
        return gcd(y,x%y);
    }
}
Gút chóp tiểu đơn, nếu một người ko biết trước về cách này thì làm thế nào :big_smile:
 
Gút chóp tiểu đơn, nếu một người ko biết trước về cách này thì làm thế nào :big_smile:
ko biết đi ô phăng thì chơi kiểu này vậy
Java:
class Solution {
    public boolean canMeasureWater(int x, int y, int target) {
        if(x>y)
            return canMeasureWater(y,x,target);
        if(x+y<target)
            return false;
        for(int i = 0;i<x;i++){
            if((y*i)%x == target%x)
                return true;
        }
        return false;
    }
}
 
ko biết đi ô phăng thì chơi kiểu này vậy
Java:
class Solution {
    public boolean canMeasureWater(int x, int y, int target) {
        if(x>y)
            return canMeasureWater(y,x,target);
        if(x+y<target)
            return false;
        for(int i = 0;i<x;i++){
            if((y*i)%x == target%x)
                return true;
        }
        return false;
    }
}
thiên long nhân mạnh quá
c8ufM1R.png
khó thế cũng giải dc
giải thích ý tưởng thuật toán đi tiểu đơn
 
Java:
class Solution {
    int maxX = -1;
    int maxY = -1;
    public boolean canMeasureWater(int x, int y, int target) {
        maxX = x;
        maxY = y;
        boolean[][] isVisited = new boolean[x + 1][y + 1];
        isVisited[0][0] = true;

        return backtrack(0, 0, target, isVisited);
    }

    public boolean backtrack(int x, int y, int target, boolean[][] isVisited) {
        System.out.println(x + " " + y);
        if (x == target || y == target || x + y == target) {
            return true;
        }

        boolean canMesure = false;

        if (!isVisited[x][maxY]) {
            isVisited[x][maxY] = true;
            canMesure |= backtrack(x, maxY, target, isVisited);
        }

        if (!isVisited[maxX][y]) {
            isVisited[maxX][y] = true;
            canMesure |= backtrack(maxX, y, target, isVisited);
        }

        if (!isVisited[0][y]) {
            isVisited[0][y] = true;
            canMesure |= backtrack(0, y, target, isVisited);
        }

        if (!isVisited[x][0]) {
            isVisited[x][0] = true;
            canMesure |= backtrack(x, 0, target, isVisited);
        }

        int yAfterFill = Math.min(maxY, x + y);
        if (!isVisited[x + y - yAfterFill][yAfterFill]) {
            isVisited[x + y - yAfterFill][yAfterFill] = true;
            canMesure |= backtrack(x + y - yAfterFill , yAfterFill, target, isVisited);
        }

        int xAfterFill = Math.min(maxX, x + y);
        if (!isVisited[xAfterFill][x + y - xAfterFill]) {
            isVisited[xAfterFill][x + y - xAfterFill] = true;
            canMesure |= backtrack(xAfterFill, x + y - xAfterFill, target, isVisited);
        }

        return canMesure;
    }
}
Hơi mất công tí :v
zFNuZTA.png
Đọc code đã cảm thấy sự kiên trì rồi
 
Python:
class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
        obstacles = set([(x, y) for x, y in obstacles])
        current = 0
        x, y = 0, 0
        ans = 0
        for command in commands:
            if command == -1:
                current = (current + 1)%4
            elif command == -2:
                current = 3 if current == 0 else current - 1
            else:
                steps = 0
                while steps != command:
                    nx = x + (steps + 1)*directions[current][0]
                    ny = y + (steps + 1)*directions[current][1]
                    if (nx, ny) in obstacles:
                        break
                    
                    steps += 1
                
                x += steps*directions[current][0]
                y += steps*directions[current][1]
                ans = max(ans, x**2 + y**2)
        
        return ans
 
Edit: Dùng set<pair> để cải thiện hiệu năng thay vì map<int, list>
Java:
class Solution {
    int deg = 90;
    Set<Pair<Integer,Integer>> set = new HashSet();

    public int robotSim(int[] commands, int[][] obstacles) {
        genMap(obstacles);

        int x = 0, y = 0, nextX, nextY;
        int max = 0;
        boolean moveAble = true;

        for(int i : commands) {
            if (i < 0) {
                nextDeg(i);
                continue;
            }

            nextX = x;
            nextY = y;

            while(i-- > 0) {
                switch(deg) {
                    case 90:
                        moveAble = tryMove(nextX, ++nextY);
                        break;
                    case 180:
                        moveAble = tryMove(++nextX, nextY);
                        break;
                    case 270:
                        moveAble = tryMove(nextX, --nextY);
                        break;
                    case 0:
                        moveAble = tryMove(--nextX, nextY);
                        break;
                }

                if (!moveAble)
                    break;
    
                x = nextX;
                y = nextY;
                max = Math.max(max, x * x + y * y);
            }
        }

        return max;
    }

    boolean tryMove(int x, int y) {
        return !set.contains(new Pair(x,y));
    }

    void nextDeg(int i) {
        if (i == -1)
            deg += 90;
        else
            deg -= 90;
        
        if (deg == 360)
            deg = 0;
        else if (deg < 0)
            deg = 270;
    }

    void genMap(int[][] obstacles) {
        for(int[] i : obstacles){
            set.add(new Pair(i[0],i[1]));
        }
    } 
}
 
Sửa lần cuối:
Java:
class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] directions = {{0,1},{1,0},{0,-1},{-1,0}};
        int current =0;
        int[] pos = {0,0};
        int res =0;
        Set<Pair<Integer,Integer>> set = new HashSet();
        for(int[] obstacle:obstacles){
            set.add(new Pair(obstacle[0],obstacle[1]));
        }
        for(int command:commands){
            if(command ==-1){
              current = (current+1+directions.length)%directions.length;
            }
            else if(command==-2){
               current = (current-1+directions.length)%directions.length;
            }
            else{
                for(int i =0 ; i <command;i++){
                    int newX = pos[0]+directions[current][0];
                    int newY = pos[1]+directions[current][1];
                    if(set.contains(new Pair(newX,newY))) break;
                    pos[0]=newX;
                    pos[1]= newY;
                    res=Math.max(res, pos[0]*pos[0]+pos[1]*pos[1]);
                }
            }
          
        }
        return res;
    }
}
 
anh em cho hỏi sao đoạn này là turn left với turn right vậy đọc sol mà lú quá
Mã:
 if (commands[i] == -2) { // Turns left
                direction = (direction + 3) % 4;
            } else if (commands[i] == -1) { // Turns right
                direction = (direction + 1) % 4;
 
anh em cho hỏi sao đoạn này là turn left với turn right vậy đọc sol mà lú quá
Mã:
 if (commands[i] == -2) { // Turns left
                direction = (direction + 3) % 4;
            } else if (commands[i] == -1) { // Turns right
                direction = (direction + 1) % 4;
mảng direction theo chiều kim đồng hồ thôi, + 3 là = -1 xoay ngược chiều 90 độ ấy nhưng mà java -1 %4 = -1 nên phải xài (-1+4)%4 = 3%4
 
anh em cho hỏi sao đoạn này là turn left với turn right vậy đọc sol mà lú quá
Mã:
 if (commands[i] == -2) { // Turns left
                direction = (direction + 3) % 4;
            } else if (commands[i] == -1) { // Turns right
                direction = (direction + 1) % 4;
nó viết vậy để xử lý out of bounds thôi fency
vd đang ở i = 3 turn right đi sang i = 0, đang ở i = 0 turn left quay lại i = 3
cái này có vẻ giống cái bài circular array ấy :ah:
 
anh em cho hỏi sao đoạn này là turn left với turn right vậy đọc sol mà lú quá
Mã:
 if (commands[i] == -2) { // Turns left
                direction = (direction + 3) % 4;
            } else if (commands[i] == -1) { // Turns right
                direction = (direction + 1) % 4;
mà sao bài này lại đọc sol hử
osCpCsi.png
đề bài nó mô tả step by step r còn ko làm theo
 
Giống mình quá, đang lười
BdgiW7R.png
Python:
import numpy as np
class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        direction = np.array([0, 1])
        left = np.array([[0, -1],[1, 0]])
        right = np.array([[0, 1],[-1, 0]])
        location = np.array([0, 0])
        res = 0

        obsSet = set(tuple(o) for o in obstacles)

        for command in commands:
            if command == -2:
                direction = np.dot(left, direction)
            elif command == -1:
                direction = np.dot(right, direction)
            else:
                for i in range(command):
                    nextLocation = direction + location
                    if tuple(nextLocation) in obsSet:
                        break
                    else:
                        location = nextLocation
                        res = max(res, location[0] * location[0] + location[1] * location[1])
        
        return res

Gạch tôi làm gì
osCpCsi.png
bài của fen đâu?
osCpCsi.png
@anoldvozer1710.v2
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.213.099
Quay lại
Lên đầu trang