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
C++:
class Solution {
public:
    vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations) {
        vector<vector<int>> g(n);
        for (auto&& e : invocations) {
            int x = e[0];
            int y = e[1];
            g[x].push_back(y);
        }

        vector<int8_t> vis(n);
        auto dfs = [&](this auto&& dfs, int x) -> void {
            vis[x] = true;
            for (auto y : g[x]) {
                if (!vis[y]) {
                    dfs(y);
                }
            }
        };
        dfs(k);

        vector<int> ans;
        for (auto&& i : invocations) {
            if (!vis[i[0]] && vis[i[1]]) {
                ans.resize(n);
                ranges::iota(ans, 0);
                return ans;
            }
        }
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                ans.push_back(i);
            }
        }
        return ans;
    }
};
 
Python:
class Solution:
    def remainingMethods(self, n: int, k: int, invocations: List[List[int]]) -> List[int]:
        # if cannot remove all then nothing removed
        sus = {k}
        default_set = set(range(n))
        graph = defaultdict(set[int]) # show the direct invoke of a method
        for invoke in invocations:
            graph[invoke[0]].add(invoke[1])

        # find sus methods
        def find(graph: dict[int, set[int]], k: int, sus: set[int]) -> None:
            for i in graph.get(k, []):
                if i not in sus:
                    sus.add(i)
                    find(graph, i, sus)

        find(graph, k, sus)
        if len(sus) == n:
            return []
        non_sus = set()
        for i in range(n):
            if i not in sus:
                non_sus.update(graph[i])
        inter = non_sus.intersection(sus)

        if inter:
            return list(default_set)

        return list(default_set.difference(sus))
 
JavaScript:
function smallestNumber(n: number, t: number): number {
  while (true) {
    if (digitp(n) % t === 0) {
      return n;
    }

    n++;
  }
}

function digitp(num: number): number {
  let p = 1;

  while (num > 0) {
    p *= num % 10;
    num = Math.floor(num / 10);
  }

  return p;
}
 
Python:
class Solution:
    def smallestNumber(self, n: int, t: int) -> int:
        while True:
            digits = [int(c) for c in str(n)]
            product = functools.reduce(lambda x, y: x * y, digits, 1)
            if product % t == 0:
                return n
            n += 1
 
C++:
class Solution {
    public:
        int smallestNumber(const int &n, const int &t) {
            int a = (int) n / 10;
            int b = n % 10;

            if (a == 0) {
                while (b % t) {
                    b++;
                    if (b > 9) {
                        b %= 10;
                        a++;
                    }
                }
            }
            else {
                while ((a * b) % t) {
                    b++;
                    if (b > 9) {
                        b %= 10;
                        a++;
                    }
                }
            }
            return a * 10 + b;
        }
};
 
đi interview mà nó hỏi câu này chắc muốn đuổi khéo
bài này trong contest chỉ có 30 người giải được trong số gần 28k thí sinh. 0.1% AC. Câu này mà hỏi thì đúng là ko khác gì đang nói thôi cook về nhà đi m
XGxqc0v.png
O0w112T.png
 
Python:
class Solution:
    def stoneGameII(self, piles: List[int]) -> int:
        n = len(piles)
        if n < 3:
            return sum(piles)
        @cache
        def maxDiff(i: int, m: int) -> int:
            if i == len(piles) - 1:
                return piles[i]
            choices = []
            for x in range(1, min(len(piles) - i + 1, 2 * m + 1)):
                choices.append(sum(piles[i:i + x]) - maxDiff(i + x, max(x, m)))
            return max(choices) if choices else 0
        return (maxDiff(0, 1) + sum(piles)) // 2
 
Python:
class Solution:
    def stoneGameII(self, piles: List[int]) -> int:
        n = len(piles)
        if n < 3:
            return sum(piles)
        @cache
        def maxDiff(i: int, m: int) -> int:
            if i == len(piles) - 1:
                return piles[i]
            choices = []
            for x in range(1, min(len(piles) - i + 1, 2 * m + 1)):
                choices.append(sum(piles[i:i + x]) - maxDiff(i + x, max(x, m)))
            return max(choices) if choices else 0
        return (maxDiff(0, 1) + sum(piles)) // 2
ae lấy đề bài ở đâu đấy
 
bắt đầu với leetcode thì học gì, ở đâu để lấy nền nhanh gọn nhất vậy các bác?
 

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
456.136
Quay lại
Lên đầu trang