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.
Medium trá hình, chủ yếu là racist

Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        k, ans = 0, 0
        for i, ch in enumerate(s):
            if ch == '0':
                ans += i - k
                k += 1

        return ans
 
Java:
class Solution {
    public long minimumSteps(String s) {
        long res=0;
        int n = s.length();
        int l =0;
        int r = n-1;
        while(l<r){
            while(l<r && s.charAt(l)=='0') l++;
            while(r>l && s.charAt(r)=='1') r--;
            res+=r-l;
            l++;r--;
        }
        return res;
    }
}
Java:
class Solution {
    public long minimumSteps(String s) {
        long res=0;
        int n = s.length();
        int b =0;
        for(char c: s.toCharArray()){
            if(c=='1') b++;
        }
        b=n-b;

        for(int i =0 ;i < n;i++){
            if(s.charAt(i)=='1'){
                if(i<b){
                    res+=b-i;
                    b++;
                }
                else break;
            }
         
        }
        return res;
    }
}
mấy câu dễ phải font bạt liền :confident: dễ không fontbat khó lấy code đâu đăng
 
Java:
class Solution {
    public long minimumSteps(String s) {
        int n = s.length();
        long swap = 0, group = 0;
        char[] arr = s.toCharArray();
        for (int i = 0; i < n; i++) {
            if (arr[i] == '1') group++;
            if (arr[i] == '0') swap += group;
        }
        return swap;
    }
}
 
Sửa lần cuối:
Medium trá hình, chủ yếu là racist

Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        k, ans = 0, 0
        for i, ch in enumerate(s):
            if ch == '0':
                ans += i - k
                k += 1

        return ans
Lâu lắm mới thấy chủ thớt ngoi lên
TZLgGvD.png
 
LC 2938 GoLang
C-like:
func minimumSteps(s string) int64 {
    rs, countOne := 0, 0
    for _, ch := range s {
        if ch == '1' {
            countOne++
        } else {
            rs += countOne
        }
    }
    return int64(rs)
}
===
Mã:
  [SPOILER="DD/MM Go"][CODE=CLike]
  [\/CODE][\/SPOILER]
 
Sửa lần cuối:
Java:
class Solution {
    public long minimumSteps(String s) {
        int last0 = 0;
        long sum = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '0') {
                sum += i - last0;
                last0++;
            }
        }
        return sum;
    }
}
 
Nãy có bạn hỏi nên e share luôn cái đề em từng làm ở anh hùng lao động.

Write a function that takes an array of strings representing connections between friends, along with the names of two individuals. The function should return a number indicating the degrees of separation between those two people.

Each string in the array will be formatted as name1:name2 (e.g., alice:bob). The strings will only contain lowercase letters (a-z).

The two names provided will always be non-empty strings like "alice" or "bob".

The function should return the number of degrees of separation between the two people. If there is no connection between them, either directly or through mutual friends, return -1.

Example 1:​

Connections: ["fred:joe", "joe:mary", "mary:fred", "mary:bill"]
Person 1: "fred"
Person 2: "bill"

Output: 2

Explanation:

  • Fred is directly connected to Mary, and Mary is directly connected to Bill.
  • Therefore, the degrees of separation between Fred and Bill is 2.
  • Fred -> Mary -> Bill

Example 2:​

Connections: ["fred:joe", "joe:mary", "kate:sean", "sean:sally"]
Person 1: "fred"
Person 2: "sally"

Output: -1

Explanation:

  • There is no chain of connections that link Fred and Sally.
 
Nãy có bạn hỏi nên e share luôn cái đề em từng làm ở anh hùng lao động.

Write a function that takes an array of strings representing connections between friends, along with the names of two individuals. The function should return a number indicating the degrees of separation between those two people.

Each string in the array will be formatted as name1:name2 (e.g., alice:bob). The strings will only contain lowercase letters (a-z).

The two names provided will always be non-empty strings like "alice" or "bob".

The function should return the number of degrees of separation between the two people. If there is no connection between them, either directly or through mutual friends, return -1.

Example 1:​

Connections: ["fred:joe", "joe:mary", "mary:fred", "mary:bill"]
Person 1: "fred"
Person 2: "bill"

Output: 2

Explanation:

  • Fred is directly connected to Mary, and Mary is directly connected to Bill.
  • Therefore, the degrees of separation between Fred and Bill is 2.
  • Fred -> Mary -> Bill

Example 2:​

Connections: ["fred:joe", "joe:mary", "kate:sean", "sean:sally"]
Person 1: "fred"
Person 2: "sally"

Output: -1

Explanation:

  • There is no chain of connections that link Fred and Sally.
bfs :shame:
 
Nãy có bạn hỏi nên e share luôn cái đề em từng làm ở anh hùng lao động.

Write a function that takes an array of strings representing connections between friends, along with the names of two individuals. The function should return a number indicating the degrees of separation between those two people.

Each string in the array will be formatted as name1:name2 (e.g., alice:bob). The strings will only contain lowercase letters (a-z).

The two names provided will always be non-empty strings like "alice" or "bob".

The function should return the number of degrees of separation between the two people. If there is no connection between them, either directly or through mutual friends, return -1.

Example 1:​

Connections: ["fred:joe", "joe:mary", "mary:fred", "mary:bill"]
Person 1: "fred"
Person 2: "bill"

Output: 2

Explanation:

  • Fred is directly connected to Mary, and Mary is directly connected to Bill.
  • Therefore, the degrees of separation between Fred and Bill is 2.
  • Fred -> Mary -> Bill

Example 2:​

Connections: ["fred:joe", "joe:mary", "kate:sean", "sean:sally"]
Person 1: "fred"
Person 2: "sally"

Output: -1

Explanation:

  • There is no chain of connections that link Fred and Sally.
Đề này lâu rồi giờ xài đề tìm lỗi sai trong solution của 1 bài giống như Number of Islands mà tìm hoài k ra lỗi sai thứ 2 ở đâu.
 
Đề này lâu rồi giờ xài đề tìm lỗi sai trong solution của 1 bài giống như Number of Islands mà tìm hoài k ra lỗi sai thứ 2 ở đâu.
Đề trên e vừa làm vài tháng trước thôi. Mà dạng đề tìm lỗi sai như thím nói cũng khó, phải hiểu logic người khác nữa
 
Java:
class Solution {
    public long minimumSteps(String s) {
        int n = s.length();
        int right = 0;
        long ans = 0;
        for(int i = n - 1; i > 0; i--) {
            if(s.charAt(i) == '0') {
                right = i;
                break;
            }
        }
        int left = right - 1;
        while(left >= 0) {
            if(s.charAt(left) == '1') {
                ans += (right-left);
                right--;
            }
            left--;
        }
        return ans;
    }
}
come back =))
 
hôm nay bài dễ quá làm thêm câu kinh điển sudoku solver
Java:
class Solution {
    boolean[][] rows = new boolean[9][9];
    boolean[][] cols = new boolean[9][9];
    boolean[][] squares = new boolean[9][9];
 
    public void solveSudoku(char[][] board) {
        for(int i =0 ; i<9;i++){
            for(int j = 0 ; j<9;j++){
                if(board[i][j]=='.') continue;
                int num = board[i][j]-'1';
                rows[i][num]=true;
                cols[j][num]=true;
                squares[(i/3)*3+j/3][num]=true;
            }
        }
        backtrack(board,0);
    }
    public boolean backtrack(char[][] board, int index){
        if(index ==81 ) return true;
        int i=  index/9;
        int j = index%9;
        if(board[i][j]!='.') return backtrack(board, index+1);
        for(char charNum = '1'; charNum<='9';charNum++){
            int num = charNum -'1';
            if(isValid(i,j, num)){
                rows[i][num]=true;
                cols[j][num]=true;
                squares[(i/3)*3+j/3][num]=true;
                board[i][j] = charNum;
                if(backtrack(board,index+1)) return true;
                rows[i][num]=false;
                cols[j][num]=false;
                squares[(i/3)*3+j/3][num]=false;
                board[i][j]='.';
            }
        }
        return false;
    }
    public boolean isValid(int i, int j, int num) {
        if(rows[i][num]==true ||
            cols[j][num]==true ||
            squares[(i/3)*3+j/3][num]==true) return false;
        return true;
    }
}
 
Sửa lần cuối:
medium trá hình :angry:
Java:
class Solution {
    public long minimumSteps(String s) {
        int n = s.length();
        long res = 0;
        int whitePosition = 0;
        for(int i = 0;i<n;i++)
            if(s.charAt(i)=='0'){
                res+=i-whitePosition;
                whitePosition++;
            }
                
        return res;
    }
}
 
Bài daily này giống 1 câu trong OA của N*ver
Java:
class Solution {
    public long minimumSteps(String s) {
        long res = 0l;
        int gap = 0;

        for (char ch : s.toCharArray()) {
            if (ch == '0') {
                res += gap;
            } else {
                gap++;
            }
        }

        return res;
    }
}
 
Python:
class Solution:
    def longestDiverseString(self, a: int, b: int, c: int) -> str:
        heap = []
        if a != 0:
            heapq.heappush(heap, (-a, 'a'))
        
        if b != 0:
            heapq.heappush(heap, (-b, 'b'))
        if c != 0:
            heapq.heappush(heap, (-c, 'c'))
        ans = []
        while heap:
            holder, charHolder = None, None
            if len(ans) > 1 and heap[0][1] == ans[-1] == ans[-2]:
                holder, charHolder = heapq.heappop(heap)
            if not heap:
                break
            
            freq, char = heapq.heappop(heap)
            ans.append(char)
            freq += 1
            if freq < 0:
                heapq.heappush(heap, (freq, char))
            if holder != None:
                heapq.heappush(heap, (holder, charHolder))
        return ''.join(ans)
 
Sửa lần cuối:
C++:
class Solution {
public:
    string longestDiverseString(int a, int b, int c) {
        priority_queue<pair<int, char>> pq;
        pq.push(make_pair(a, 'a'));
        pq.push(make_pair(b, 'b'));
        pq.push(make_pair(c, 'c'));
        stack<pair<int, char>> bk;

        string res = "";

        for (;!pq.empty();){
            auto top = pq.top();
            pq.pop();
            if (top.first == 0) continue;
            int r_size = res.size();
            if (r_size >= 2 && (res[r_size-1] == top.second && res[r_size-2] == top.second)) {
                bk.push(top);
                continue;
            }

            res = res + top.second;
            top.first--;
            pq.push(top);
            for (;!bk.empty();) {
                auto tmp = bk.top();
                pq.push(tmp);
                bk.pop();
            }
        }
        return res;
    }
};
 
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.944
Quay lại
Lên đầu trang