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.
Nick chính bị mod khoá mõm vì nguồn điểm báo rồi. Rõ ràng đã kiểm tra nguồn ko có trong list cấm.

Swift:
class Solution {
    func passThePillow(_ n: Int, _ time: Int) -> Int {
        let num = time/(n-1)
        if num%2 == 1 {
            return n - time%(n-1)
        } else {
            return time%(n-1) + 1
        }
    }
}

Swift:
class Solution {
    func passThePillow(_ n: Int, _ time: Int) -> Int {
        var oddNum = true
        var time = time
        let n1 = n - 1
        while time > n1 {
            time -= n1
            oddNum.toggle()
        }
        if oddNum {
            return time + 1
        } else {
            return n - time
        }
    }
}
 
Sửa lần cuối:
Nick chính bị mod khoá mõm vì nguồn điểm báo rồi. Rõ ràng đã kiểm tra nguồn ko có trong list cấm.

Swift:
class Solution {
    func passThePillow(_ n: Int, _ time: Int) -> Int {
        let num = time/(n-1)
        if num%2 == 1 {
            return n - time%(n-1)
        } else {
            return time%(n-1) + 1
        }
    }
}

Swift:
class Solution {
    func passThePillow(_ n: Int, _ time: Int) -> Int {
        var oddNum = true
        var time = time
        let n1 = n - 1
        while time > n1 {
            time -= n1
            oddNum.toggle()
        }
        if oddNum {
            return time + 1
        } else {
            return n - time
        }
    }
}
Chắc do có nội dung nhạy cảm chăng :shame:
 
Python:
class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        time %= 2 * n - 2
        return 1 + time if time <= n - 1 else n - (time - (n - 1))
 
Java:
public int passThePillow(int n, int time) {
    int loop = time/(n - 1);
    int spare = time%(n - 1);

    if (loop % 2 == 0) {
        return spare + 1;
    }
    return n - spare;
}
 
JavaScript:
var passThePillow = function(n, time) {
    const rounds = Math.floor(time / (n - 1));
    const pos = time % (n - 1);
    return rounds % 2 == 0 ? pos + 1 : n - pos;
};
 
Java:
class Solution {
    public int passThePillow(int n, int time) {
    return time/(n-1)%2 ==0?1+time%(n-1):n-time%(n-1);
}
}
Tuần này có 2 bài ez luôn, qué đẽ :beauty:
 
JavaScript:
var passThePillow = function (n, time) {
  time %= (2 * n - 2)
  if (time < n) return time + 1
  return 2 * n - 1 - time
};
 
Bài này nó là 1000 thôi, chứ nó lên đến 10^9 mà anh nào đi loop thì ăn gạch nha
GAilj9P.png
GAilj9P.png

JavaScript:
function passThePillow(n: number, time: number): number {
    if (time < n) return time + 1;
    const div = Math.floor(time / (n-1)), mod = time % (n-1);
    if (div % 2 === 0) return mod + 1;
    else return n - mod;
};
Mã:
 
Mã:
class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        numsPass = time // (n - 1)
        time = time - numsPass * (n - 1)
        if numsPass % 2 == 0: return time + 1
        return n - time
 
Java:
class Solution {
    public int passThePillow(int n, int time) {
        if(n==1) return 1;
        time%=(2*n-2);
        time+=1;//giây 1 trùng label 1 cho dễ nháp
        if(time<=n) return time;
        return n-(time-n);
    }
}
có phải dejavu ko chứ nhớ bài này gặp 1 lần trc đây r
9VaMnU5.png

cơm thêm cho ae lấy số:
Eazy mà cũng cần lấy số hả ta ;)
 
C++:
class Solution {
public:
    int passThePillow(int n, int time) {
        int a = time / (n - 1);
       return a % 2 == 0 ? 1 + time % (n -1) : n - time % (n - 1);
    }
};

C++:
class Solution {
public:
    int passThePillow(int n, int time) {
        int direction = 1;
        int currentPosition = 1;
        int currentTime = 0;
        while( currentTime < time){
            if(currentPosition + direction == 0 || currentPosition + direction == n + 1){
               direction = -direction;
            };
            currentTime++;
            currentPosition += direction;
        }
        return currentPosition;
    }
};
 
Biweekly contest - Làm đc 3 bài

Code mù mắt quá:beat_brick:

Python:
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int]) -> int:
        res = 0
        if colors[0] != colors[-1] and colors[0] != colors[1]:
            res += 1
            
        if colors[-1] != colors[0] and colors[-1] != colors[-2]:
            res += 1
          
        for i in range(1, len(colors) - 1):
            if colors[i] != colors[i-1] and colors[i] != colors[i+1]:
                res += 1
        return res

Python:
class Solution:
    def maximumPoints(self, enemyEnergies: List[int], currentEnergy: int) -> int:
        res = 0
        enemyEnergies.sort()
        i = 0
        res += currentEnergy // enemyEnergies[0]
        currentEnergy = currentEnergy % enemyEnergies[0]

        if res == 0:
            return 0
        for e in enemyEnergies[1:]:
            currentEnergy += e
        res += currentEnergy // enemyEnergies[0]
        return res

Python:
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
        res = 0
        invalids = []
        
        if colors[0] == colors[-1]:
            invalids.append(0)

        for i in range(1, len(colors)):
            if colors[i] == colors[i-1]:
                invalids.append(i-1)
                invalids.append(i)

        if colors[0] == colors[-1]:
            invalids.append(len(colors) - 1)

        if len(invalids)  == 0:
            return len(colors)

        for i in range(1, len(invalids)):
            if invalids[i] - invalids[i-1] >= k - 1:
                res += invalids[i] - invalids[i-1] - k + 2 # 4 - 0 - 3 + 1

        if not (invalids[0] == 0 and invalids[-1] == len(colors) - 1):
            diff_in_end = invalids[0] + 1 + len(colors) - invalids[-1]
            if diff_in_end >= k:
                res += diff_in_end - k + 1
        return res
 
Biweekly contest - Làm đc 3 bài

Code mù mắt quá:beat_brick:

Python:
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int]) -> int:
        res = 0
        if colors[0] != colors[-1] and colors[0] != colors[1]:
            res += 1
           
        if colors[-1] != colors[0] and colors[-1] != colors[-2]:
            res += 1
         
        for i in range(1, len(colors) - 1):
            if colors[i] != colors[i-1] and colors[i] != colors[i+1]:
                res += 1
        return res

Python:
class Solution:
    def maximumPoints(self, enemyEnergies: List[int], currentEnergy: int) -> int:
        res = 0
        enemyEnergies.sort()
        i = 0
        res += currentEnergy // enemyEnergies[0]
        currentEnergy = currentEnergy % enemyEnergies[0]

        if res == 0:
            return 0
        for e in enemyEnergies[1:]:
            currentEnergy += e
        res += currentEnergy // enemyEnergies[0]
        return res

Python:
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
        res = 0
        invalids = []
       
        if colors[0] == colors[-1]:
            invalids.append(0)

        for i in range(1, len(colors)):
            if colors[i] == colors[i-1]:
                invalids.append(i-1)
                invalids.append(i)

        if colors[0] == colors[-1]:
            invalids.append(len(colors) - 1)

        if len(invalids)  == 0:
            return len(colors)

        for i in range(1, len(invalids)):
            if invalids[i] - invalids[i-1] >= k - 1:
                res += invalids[i] - invalids[i-1] - k + 2 # 4 - 0 - 3 + 1

        if not (invalids[0] == 0 and invalids[-1] == len(colors) - 1):
            diff_in_end = invalids[0] + 1 + len(colors) - invalids[-1]
            if diff_in_end >= k:
                res += diff_in_end - k + 1
        return res
sang thread kia đi fen thảo luận - Leetcode contest, đường tới Guardian (https://voz.vn/t/leetcode-contest-duong-toi-guardian.896868/page-73#post-32619913)
 
C#:
public class Solution {
    public int NumWaterBottles(int numBottles, int numExchange) {
        int remain = numBottles;
        int tmp;
        while (remain >= numExchange) {
            tmp = remain / numExchange;
            numBottles += tmp;
            remain = tmp + remain % numExchange;
        }
        return numBottles;
    }
}
 
PHP:
class Solution {

    /**
     * @param Integer $numBottles
     * @param Integer $numExchange
     * @return Integer
     */
    function numWaterBottles($numBottles, $numExchange) {
        $emptyBottles = $numBottles;

        while ($emptyBottles >= $numExchange) {
            $newBottles = floor($emptyBottles / $numExchange);
            $cannotExchangeBottles = $emptyBottles % $numExchange;
            
            $numBottles += $newBottles;
            $emptyBottles = $newBottles + $cannotExchangeBottles;
        }

        return $numBottles;
    }
}
 
Sửa lần cuối:
Swift:
class Solution {
    func numWaterBottles(_ numBottles: Int, _ numExchange: Int) -> Int {
        var result = numBottles
        var emptyBottles = numBottles
        while emptyBottles >= numExchange {
            let fullBottles = emptyBottles/numExchange
            emptyBottles = fullBottles + emptyBottles%numExchange
            result += fullBottles
        }
        return result
    }
}
 
Bài này cứ như Toán lớp 2: cứ uống e chai thì nhận về r chai
C-like:
impl Solution {
    pub fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
        #[inline]
        fn exchange(b: i32, e: i32, r: i32) -> i32 {
            if b < e {
                0
            } else {
                (b - e) / (e - r) + 1
            }
        }
        num_bottles + exchange(num_bottles, num_exchange, 1)
    }
}
 
Dùng thêm vài trick để LLVM có điều kiện làm optimization:
C-like:
impl Solution {
    pub fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
        #[inline]
        unsafe fn exchange(b: i32, e: i32, r: i32) -> i32 {
            if b < e {
                0
            } else {
                if e == r {
                    std::hint::unreachable_unchecked()
                } else {
                    i32::unchecked_add(
                        i32::wrapping_div_euclid(
                            i32::unchecked_sub(b, e),
                            i32::unchecked_sub(e, r),
                        ),
                        1,
                    )
                }
            }
        }
        unsafe { i32::unchecked_add(num_bottles, exchange(num_bottles, num_exchange, 1)) }
    }
}

Code sinh ra không còn tí check runtime nào luôn:

Mã:
example::num_water_bottles::h42ae3f9ff3263f7c:
        xor     eax, eax
        mov     ecx, edi
        sub     ecx, esi
        jl      .LBB1_8
        test    esi, esi
        jne     .LBB1_3
        mov     eax, -2147483647
        cmp     ecx, -2147483648
        jne     .LBB1_3
.LBB1_8:
        add     eax, edi
        ret
.LBB1_3:
        mov     eax, ecx
        lea     r8d, [rsi - 1]
        cdq
        idiv    r8d
        test    edx, edx
        js      .LBB1_4
        inc     eax
        add     eax, edi
        ret
.LBB1_4:
        cmp     esi, 1
        jle     .LBB1_5
        dec     eax
        inc     eax
        add     eax, edi
        ret
.LBB1_5:
        inc     eax
        inc     eax
        add     eax, edi
        ret
 
Sửa lần cuối:
Mã:
class Solution:
    def numWaterBottles(self, numBottles: int, numExchange: int) -> int:
        res = numBottles
        numsOfEmpty , numsOfFull = numBottles , 0
        while numsOfEmpty >= numExchange:
            numsOfFull = numsOfEmpty // numExchange
            res += numsOfFull
            numsOfEmpty = numsOfEmpty - numsOfFull * numExchange + numsOfFull
        
        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.212.536
Quay lại
Lên đầu trang