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

  • Người tạo chủ đề Người tạo chủ đề Vipluckystar
  • Ngày bắt đầu Ngày bắt đầu
Mã:
class Solution {
    public int[] arrayRankTransform(int[] arr) {
        int[] tmp = Arrays.stream(arr)
                             .distinct()
                             .sorted()   
                             .toArray();
        Map<Integer, Integer> map = new HashMap<>();
        int rank = 1;

        for (int n: tmp)
            map.put(n, rank++);
        

        for (int i = 0; i < arr.length; i++)
            arr[i] = map.get(arr[i]);

        return arr;

            
    }
}
 
JavaScript:
function sequentialDigits(low: number, high: number): number[] {
    const res: number[] = [];

    for (let num = 1; num < 9; num++) {
        let next = num;
        let n = next;

        while (n <= high && next < 10) {
            if (n >= low) {
                res.push(n);
            }
            next++;
            n = n * 10 + next;
        }
    }

    res.sort((a, b) => a - b);
    return res;
};
 
Python:
class Solution:
    def sequentialDigits(self, low: int, high: int) -> List[int]:
        digits = "123456789"
        out = []
        a, b = len(str(low)), len(str(high))

        for i in range(a, b + 1):
            j = 0
            while i + j < 10:
                n = int(digits[j:j + i])
                if low <= n <= high:
                    out.append(n)
                j += 1
        
        return out
 
DP co ban
JavaScript:
function subsequencePairCount(nums: number[]): number {
    const mod = 1e9 + 7;
    const m = Math.max(...nums);
    let dp = Array.from({ length: m + 1 }, () => new Int32Array(m + 1));
    dp[0][0] = 1;
    const gcd = (a: number, b: number) => {
        while (b !== 0) {
            const r = a % b;
            a = b;
            b = r;
        }
        return a;
    }
    for (const num of nums) {
        const nextDp = Array.from({ length: m + 1 }, () => new Int32Array(m + 1));
        for (let i = 0; i <= m; i++) {
            for (let j = 0; j <= m; j++) {
                const cur = dp[i][j];
                if (cur === 0) continue;
                // khong chon num
                nextDp[i][j] = (nextDp[i][j] + cur) % mod;

                // cho vao sequence 1
                const g1 = gcd(i, num);
                nextDp[g1][j] = (nextDp[g1][j] + cur) % mod;

                // cho vao sequence 2
                const g2 = gcd(j, num);
                nextDp[i][g2] = (nextDp[i][g2] + cur) % mod;

            }
        }
        dp = nextDp;
    }
    let res = 0;
    for (let i = 1; i <= m; i++) {
        res = (res + dp[i][i]) % mod;
    }
    return res;
};
 
de bai bao gi lam vay
JavaScript:
function gcdSum(nums: number[]): number {
    const n = nums.length;
    let mx = 0;
    const arr = new Array(n).fill(0);
    const gcd = (a: number, b: number) => {
        while (b !== 0) {
            const r = a % b;
            a = b;
            b = r;
        }
        return a;
    }
    for (let i = 0; i < n; i++) {
        mx = Math.max(nums[i], mx);
        arr[i] = gcd(nums[i], mx)
    }
    arr.sort((a,b) => a - b);
    let i = 0, j = n - 1, res = 0;
    while (i < j) {
        res+= gcd(arr[i], arr[j]);
        i++, j--;
    }
    return res;
};
 
Dịch tất cả các phần tử sang phải k phần tử
JavaScript:
function shiftGrid(grid: number[][], k: number): number[][] {
    const m = grid.length, n = grid[0].length;
    const total = m * n;
    k%= total;
    const res = Array.from({length: m}, () => new Array(n).fill(0));

    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            const idx = i * n + j;
            const newIdx = (idx + k) % total;
            const xx = Math.floor(newIdx / n);
            const yy = newIdx % n;
            res[xx][yy] = grid[i][j]
        }
    }
    return res;
};
 
JavaScript:
function maxActiveSectionsAfterTrade(s: string): number {
    const n = s.length;

    let ones = 0;
    let l = 0;
    let r = 0;
    let max = 0;

    for (let i = 0; i <= n; i++) {
        const c = i < n ? s[i] : '1';

        if (c === '0') r++
        else {
            if (i < n) {
                ones++;
            }
            if (r > 0) {
                if (l > 0) {
                    max = Math.max(
                        max,
                        l + r
                    );
                }

                l = r;
                r = 0;
            }
        }
    }

    return ones + max;
}
 
Trong đây có bác nào hiện tại đang làm việc với Quarkus không nhỉ, cho em hỏi thường thì cty mấy bác sẽ chọn code theo style reactive hay blocking vậy :smile:
 

Thống kê chủ đề

Ngày tạo
Vipluckystar,
Người trả lời cuối
Holo code dạo,
Trả lời
7.740
Lượt xem
455.952
Quay lại
Lên đầu trang