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.
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
đây nhé, mấy bài rating k8 này muỗi :ah:
JavaScript:
function robotSim(commands: number[], obstacles: number[][]): number {
    let res = 0, x = 0, y = 0, i = 0
    const dirs = [[0,1], [1, 0], [0, -1], [-1, 0]];
    const set = new Set<string>();
    for (const [a,b] of obstacles) set.add(`${a},${b}`)
    for (const c of commands) {
        if (c === -1) i = (i + 1) % 4;
        else if (c === -2) i = (i + 3) % 4;
        else {
            const [dx, dy] = dirs[i];
            for (let j = 0; j < c; j++) {
                if (set.has(`${x + dx},${y + dy}`)) break;
                x = x + dx, y = y + dy
            }
        }
        res = Math.max(res, x * x + y * y)
    }
    return res;
};
 
Sửa lần cuối:
QwJ0V0V.png
Bài này mà liếc á, tí phạt bài khác nhé
đệ gà quá chấp nhận hình phạt của bill huynh
yBBewst.png


Mã:
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])
        x , y = 0 ,0
        res = 0
        direction = 0
        for command in commands:
            if command == -2: # turn left
                direction = (direction + 3) % 4
            elif command == -1: # turn right
                direction = (direction + 1) % 4
            else:
                step = 0
                xx , yy = directions[direction]
                while step < command and (x + xx , y + yy) not in obstacles:
                    x += xx
                    y += yy
                    step += 1
                res = max(res , x * x + y * y)
            
        
        return res
 
đệ gà quá chấp nhận hình phạt của bill huynh
yBBewst.png


Mã:
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])
        x , y = 0 ,0
        res = 0
        direction = 0
        for command in commands:
            if command == -2: # turn left
                direction = (direction + 3) % 4
            elif command == -1: # turn right
                direction = (direction + 1) % 4
            else:
                step = 0
                xx , yy = directions[direction]
                while step < command and (x + xx , y + yy) not in obstacles:
                    x += xx
                    y += yy
                    step += 1
                res = max(res , x * x + y * y)
         
     
        return res
nhìn code python muốn kỳ thị quá. coi neetcode nó gõ thuật toán nhanh vl nhìn cũng ham
mình cũng muốn nhanh nhưng khai báo biến java xong ngta return cmnr;
sao mình lại phải type 1 tỷ thứ như v
4gmOAMB.png
xài ide còn có tab chứ text editor có cái nịt
 
Java:
class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        boolean n = true;
        boolean s = false;
        boolean w = false;
        boolean e = false;
        int[] location = new int[2];
        int ans = 0;
        Set<Integer> obstacleSet = new HashSet<>();
        for (int[] obstacle : obstacles) {
            obstacleSet.add(hashCoordinates(obstacle[0], obstacle[1]));
        }
        for (int command : commands) {
            if (command == - 1) {
                if (n) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (s) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (w) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                } else if (e) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                }
            } else if (command == -2) {
                if (n) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (s) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (w) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                } else if (e) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                }
            } else {
                for (int i = 0; i < command; i++) {
                    int nextX = location[0];
                    int nextY = location[1];
                    if (n) {
                        nextY += 1;   
                    } else if (s) {
                        nextY -= 1;
                    } else if (w) {
                        nextX -= 1;
                    } else if (e) {
                        nextX += 1;
                    }
                    if (obstacleSet.contains(hashCoordinates(nextX, nextY))) {
                        break;
                    }
                    location[0] = nextX;
                    location[1] = nextY;
                }
            }
            ans = Math.max(ans, location[0] * location[0] + location[1] * location[1]);
        }
        return ans;
    }

    private int hashCoordinates(int x, int y) {
        return x + 60001 * y;
    }
}
đoạn hashCoordinates liếc sol tí :D
 
Java:
class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        boolean n = true;
        boolean s = false;
        boolean w = false;
        boolean e = false;
        int[] location = new int[2];
        int ans = 0;
        Set<Integer> obstacleSet = new HashSet<>();
        for (int[] obstacle : obstacles) {
            obstacleSet.add(hashCoordinates(obstacle[0], obstacle[1]));
        }
        for (int command : commands) {
            if (command == - 1) {
                if (n) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (s) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (w) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                } else if (e) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                }
            } else if (command == -2) {
                if (n) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (s) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (w) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                } else if (e) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                }
            } else {
                for (int i = 0; i < command; i++) {
                    int nextX = location[0];
                    int nextY = location[1];
                    if (n) {
                        nextY += 1;   
                    } else if (s) {
                        nextY -= 1;
                    } else if (w) {
                        nextX -= 1;
                    } else if (e) {
                        nextX += 1;
                    }
                    if (obstacleSet.contains(hashCoordinates(nextX, nextY))) {
                        break;
                    }
                    location[0] = nextX;
                    location[1] = nextY;
                }
            }
            ans = Math.max(ans, location[0] * location[0] + location[1] * location[1]);
        }
        return ans;
    }

    private int hashCoordinates(int x, int y) {
        return x + 60001 * y;
    }
}
đoạn hashCoordinates liếc sol tí :D
Code xong có mệt ko mai fen :sweat:

via theNEXTvoz for iPhone
 
Java:
class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        boolean n = true;
        boolean s = false;
        boolean w = false;
        boolean e = false;
        int[] location = new int[2];
        int ans = 0;
        Set<Integer> obstacleSet = new HashSet<>();
        for (int[] obstacle : obstacles) {
            obstacleSet.add(hashCoordinates(obstacle[0], obstacle[1]));
        }
        for (int command : commands) {
            if (command == - 1) {
                if (n) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (s) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (w) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                } else if (e) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                }
            } else if (command == -2) {
                if (n) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (s) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (w) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                } else if (e) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                }
            } else {
                for (int i = 0; i < command; i++) {
                    int nextX = location[0];
                    int nextY = location[1];
                    if (n) {
                        nextY += 1;  
                    } else if (s) {
                        nextY -= 1;
                    } else if (w) {
                        nextX -= 1;
                    } else if (e) {
                        nextX += 1;
                    }
                    if (obstacleSet.contains(hashCoordinates(nextX, nextY))) {
                        break;
                    }
                    location[0] = nextX;
                    location[1] = nextY;
                }
            }
            ans = Math.max(ans, location[0] * location[0] + location[1] * location[1]);
        }
        return ans;
    }

    private int hashCoordinates(int x, int y) {
        return x + 60001 * y;
    }
}
đoạn hashCoordinates liếc sol tí :D
Vãi lồng cơ bắp quá :too_sad:
 
Java:
class Solution {
    public int robotSim(int[] commands, int[][] obstacles) {
        boolean n = true;
        boolean s = false;
        boolean w = false;
        boolean e = false;
        int[] location = new int[2];
        int ans = 0;
        Set<Integer> obstacleSet = new HashSet<>();
        for (int[] obstacle : obstacles) {
            obstacleSet.add(hashCoordinates(obstacle[0], obstacle[1]));
        }
        for (int command : commands) {
            if (command == - 1) {
                if (n) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (s) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (w) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                } else if (e) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                }
            } else if (command == -2) {
                if (n) {
                    n = false;
                    s = false;
                    w = true;
                    e = false;
                } else if (s) {
                    n = false;
                    s = false;
                    w = false;
                    e = true;
                } else if (w) {
                    n = false;
                    s = true;
                    w = false;
                    e = false;
                } else if (e) {
                    n = true;
                    s = false;
                    w = false;
                    e = false;
                }
            } else {
                for (int i = 0; i < command; i++) {
                    int nextX = location[0];
                    int nextY = location[1];
                    if (n) {
                        nextY += 1; 
                    } else if (s) {
                        nextY -= 1;
                    } else if (w) {
                        nextX -= 1;
                    } else if (e) {
                        nextX += 1;
                    }
                    if (obstacleSet.contains(hashCoordinates(nextX, nextY))) {
                        break;
                    }
                    location[0] = nextX;
                    location[1] = nextY;
                }
            }
            ans = Math.max(ans, location[0] * location[0] + location[1] * location[1]);
        }
        return ans;
    }

    private int hashCoordinates(int x, int y) {
        return x + 60001 * y;
    }
}
đoạn hashCoordinates liếc sol tí :D
Giống y chang cái cách anh Phó Goat ngoài đời đi bóng vậy 🫣
Lỡ con robot nó chỉ quay 45 độ thay vì 90 độ thì fen tính thế nào
 
Java:
class Solution {
    class Point{
        int x;
        int y;
        public Point(int x, int y){
            this.x = x;
            this.y = y;
        }
        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            Point point = (Point) obj;
            return x == point.x && y == point.y;
        }
        
        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            int res =  Integer.hashCode(x);
            res = Integer.hashCode(res);
            res+=Integer.hashCode(y);
            return res;
        }

        public int distance(){
            return x*x + y*y;
        }
    }

    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] direction = new int[][]{{0,1},{1,0},{0,-1},{-1,0}};
        Set<Point> set = new HashSet<Point>();
        for(int[] o:obstacles)
            set.add(new Point(o[0],o[1]));
        int dIndex = 0;
        int max = 0;
        Point startPoint = new Point(0,0);
        for(int command:commands){
            if(command == -1)
                dIndex = (dIndex+1)%4;
            else if(command == -2)
                dIndex = (dIndex+3)%4;
            else{
                for(int i = 0;i<command;i++){
                    startPoint.x+=direction[dIndex][0];
                    startPoint.y+=direction[dIndex][1];
                    if(set.contains(new Point(startPoint.x,startPoint.y))){
                        startPoint.x-=direction[dIndex][0];
                        startPoint.y-=direction[dIndex][1];
                        break;
                    }
                }
                max = Math.max(max,startPoint.distance());
            }
        }
        return max;
    }
}
 
Lâu lắm mới được bài chạy 0ms
Java:
public int intOf(String str) {
    int sum = 0;
    for (char c : str.toCharArray()) {
        int value = (c - 'a') + 1; // 'a' -> 1, 'b' -> 2, ..., 'z' -> 26
        sum += value < 10 ? value : value / 10 + value % 10;
    }
    return sum;
}

public int getLucky(String s, int k) {
    int n = intOf(s);
    while (--k > 0) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        n = sum;
    }
    return n;
}
 
nhìn code python muốn kỳ thị quá. coi neetcode nó gõ thuật toán nhanh vl nhìn cũng ham
mình cũng muốn nhanh nhưng khai báo biến java xong ngta return cmnr;
sao mình lại phải type 1 tỷ thứ như v
4gmOAMB.png
xài ide còn có tab chứ text editor có cái nịt
dùng py code aglo phê mà fen
zFNuZTA.png
đệ code mỗi python cho aglo thôi chứ code mấy khác cũng kì thị vl
 
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.179
Quay lại
Lên đầu trang