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
Python:
class Solution:
    def minimumPushes(self, word: str) -> int:
        res, i, mul = 0, len(word), 1
        while i > 0:
            res += min(8, i) * mul
            mul += 1
            i -= 8
       
        return res
 
C-like:
func minimumPushes(word string) int {
    cnt :=make([]int,26)
    for _,c:= range word {
        cnt[c-'a']++
    }
    sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
    ans:=0
    push:=0;
    for i,v:=range cnt{
        if i%8==0{
            push++
        }
        if v==0{
            break;
        }
        ans+=v*push
    }
    return ans
}
 
ngoi lên húp bài dễ :sexy_girl:
C-like:
func minimumPushes(word string) int {
    count := make([]int, 26)
    for _, c := range word {
        count[c - 'a']++
    }
    slices.SortFunc(count, func(a,b int) int {
        return b - a
    })
    res := 0
    for i, num := range count {
        res += num * (i / 8 + 1)
    }
    return res
}

C-like:
func minimumPushes(word string) int {
    cnt :=make([]int,26)
    for _,c:= range word {
        cnt[c-'a']++
    }
    sort.Sort(sort.Reverse(sort.IntSlice(cnt)))
    ans:=0
    push:=0;
    for i,v:=range cnt{
        if i%8==0{
            push++
        }
        if v==0{
            break;
        }
        ans+=v*push
    }
    return ans
}
sao Lmao huynh code C++ luôn thế này :surrender:
 
ngoi lên húp bài dễ :sexy_girl:
C-like:
func minimumPushes(word string) int {
    count := make([]int, 26)
    for _, c := range word {
        count[c - 'a']++
    }
    slices.SortFunc(count, func(a,b int) int {
        return b - a
    })
    res := 0
    for i, num := range count {
        res += num * (i / 8 + 1)
    }
    return res
}


sao Lmao huynh code C++ luôn thế này :surrender:
click bait thôi, ghi c++ để ko ai mở ra coi
QDXQbEv.jpeg
 
JavaScript:
function minimumPushes(word: string): number {
    const arr = new Array(26).fill(0);
    for (const c of word) {
        arr[c.charCodeAt(0) - 97]++;
    }
    arr.sort((a,b) => b - a);
    let i = 0, k = 1, res = 0;
    while (i < 26 && arr[i]) {
        if (i && i % 8 === 0) k++;
        res+= arr[i] * k;
        i++
    }
    return res;
};
 
Python:
class Solution:
    def minimumPushes(self, word: str) -> int:
        word_map = [0] * 26
        for c in word:
            word_map[ord(c) - ord('a')] += 1
        word_map.sort(reverse = True)
        tap_c = 1
        track_num = 2
        i = 0
        res = 0
        while i < 26 and word_map[i] > 0:
            if track_num > 9:
                track_num = 2
                tap_c += 1
            res += word_map[i] * tap_c
            i += 1
            track_num += 1
        
        return res
 
copy lại bài hôm qua :ops:
C-like:
func minimumPushes(word string) int {
    count := make([]int, 26)
    for _, c := range word {
        count[c - 'a']++
    }
    slices.SortFunc(count, func(a,b int) int {
        return b - a
    })
    res := 0
    for i, num := range count {
        res += num * (i / 8 + 1)
    }
    return res
}
 
Python:
class Solution:
    def stoneGameIII(self, stoneValue: List[int]) -> str:
        n = len(stoneValue)
        dp = [[(0,0)]*2 for _ in range(n+1)]
        for i in range(n-1,-1,-1):
            #player 1
            val = 0
            cur = (-1000000000,-1000000000)
            for j in range(3):
                if i+j>=n:
                    break
                val+=stoneValue[i+j]
                if dp[i+j+1][1][0]+val>cur[0] or (dp[i+j+1][1][0]+val==cur[0] and dp[i+j+1][1][1]<cur[1]):
                    cur = (dp[i+j+1][1][0]+val,dp[i+j+1][1][1])
            dp[i][0]=cur
            #player 2
            val = 0
            cur = (-1000000000,-1000000000)
            for j in range(3):
                if i+j>=n:
                    break
                val+=stoneValue[i+j]
                if dp[i+j+1][0][1]+val>cur[1] or (dp[i+j+1][0][1]+val==cur[1] and dp[i+j+1][0][0]<cur[0]):
                    cur = (dp[i+j+1][0][0],dp[i+j+1][0][1]+val)
            dp[i][1]=cur
        res = dp[0][0]
        if res[0]>res[1]:
            return "Alice"
        elif res[0]<res[1]:
            return "Bob"
        else:
            return "Tie"
 
xưa chuỗi Stone Games đúng 1 tuần luôn :angry:
JavaScript:
function stoneGameIII(stoneValue: number[]): string {
    const n = stoneValue.length;
    const dp = Array(n + 1).fill(0);
    for (let i = n - 1; i >= 0; i--) {
        dp[i] = stoneValue[i] - dp[i + 1];
        if (i + 2 <= n) {
            dp[i] = Math.max(dp[i], stoneValue[i] + stoneValue[i + 1] - dp[i + 2]);
        }
        if (i + 3 <= n) {
            dp[i] = Math.max(dp[i], stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] - dp[i + 3]);
        }
    }
    if (dp[0] > 0) {
        return "Alice";
    }
    if (dp[0] < 0) {
        return "Bob";
    }
    return 'Tie'
};
 
Python:
class Solution:
    def stoneGameIII(self, stoneValue: List[int]) -> str:
        n = len(stoneValue)

        @cache
        def maxDiff(i: int, n: int) -> int:
            if i == n - 1:
                return stoneValue[i]
            val1, val2, val3 = float(-inf), float(-inf), float(-inf)
            val1 = stoneValue[i] - maxDiff(i + 1, n)
            if n - i >= 2:
                val2 = stoneValue[i] + stoneValue[i + 1] - maxDiff(i + 2, n) if n - i > 2 else stoneValue[i] + stoneValue[i + 1]
            if n - i >= 3:
                val3 = stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] - maxDiff(i + 3, n) if n - i > 3 else stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2]
            return max(val1, val2, val3)
        

        diff = maxDiff(0, n)
        if diff > 0:
            return "Alice"
        elif diff < 0:
            return "Bob"
        else:
            return "Tie"
 
JavaScript:
function findMissingElements(nums: number[]): number[] {
    const set = new Set();
    let max = 0, min = Infinity;
    for (const num of nums) {
        set.add(num);
        max = Math.max(max, num);
        min = Math.min(min, num);
    }
    const res: number[] = [];
    for (let i = min + 1; i < max; i++) {
        if (!set.has(i)) res.push(i)
    }
    return res;
};
 
Python:
class Solution:
    def findMissingElements(self, nums: List[int]) -> List[int]:
        nums.sort()
        missing = []
        track, i = 0, 0
        while i < len(nums):
            if (nums[i] - nums[0]) != track:
                missing.append(track + nums[0])
            else:
                i += 1
            track += 1
        
        return missing
 
bfs cơ bản
JavaScript:
function remainingMethods(n: number, k: number, invocations: number[][]): number[] {
    const g: number[][] = Array.from({length: n}, () => []);
    for (const [u, v] of invocations) {
        g[u].push(v);
    }

    const sus = new Array(n).fill(false);
    const q = new Queue<number>();
    q.enqueue(k);
    sus[k] = true;
    while (!q.isEmpty()) {
        const cur = q.dequeue();
        for (const nxt of g[cur]) {
            if (!sus[nxt]) {
                sus[nxt] = true;
                q.enqueue(nxt);
            }
        }
    }

    for (const [u, v] of invocations) {
        if (!sus[u] && sus[v]) return Array.from({length: n}, (_, idx) => idx);
    }

    const res: number[] = [];
    for (let i = 0; i < n; i++) {
        if (!sus[i]) res.push(i)
    }
    return res;
};
 

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