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.
đầu tháng đến giờ chưa có bài khó nhỉ
G0MaAQB.gif

C#:
public class Solution {
    public int NumWaterBottles(int numBottles, int numExchange) {
        int result = numBottles;
        while(numBottles / numExchange > 0)
        {
            result += numBottles / numExchange;
            numBottles = numBottles / numExchange + numBottles % numExchange;
        }
        return result;
    }
}
 
JavaScript:
/**
 * @param {number} numBottles
 * @param {number} numExchange
 * @return {number}
 */
var numWaterBottles = function(numBottles, numExchange) {
    let fullWater = numBottles;
    let emptyBottles = numBottles;

    while (emptyBottles >= numExchange) {
        let curFull = Math.floor(emptyBottles / numExchange);
        fullWater += curFull;
        let carry = emptyBottles % numExchange;
        emptyBottles = carry + curFull;
    }

    return fullWater;
};
 
không có profiler, không có phân tích assembly thì không nên xl về LLVM các bạn à :(

C-like:
impl Solution {
    pub fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
        fn recurse(mut bottles: i32, exchanges: i32, mut count: i32) -> i32 {
            if bottles < exchanges {
                return count + bottles;
            }

            let mut rem = 0;

            while bottles != 0 {
                count += exchanges * (bottles / exchanges);
                rem += bottles % exchanges;
                bottles = bottles / exchanges;
            }

            recurse(rem, exchanges, count)
        }

        recurse(num_bottles, num_exchange, 0)
    }
}
 
Sửa lần cuối:
C++:
class Solution {
public:
    int numWaterBottles(int numBottles, int numExchange) {
        int drunk = numBottles;
        int empty;
        while(numBottles / numExchange > 0){
            empty = numBottles % numExchange; //3 , 2
            //start exchange
            numBottles /= numExchange; //3 , 1
            drunk += numBottles; // 15 + 3 + 1
            //get all empty bottles
            numBottles += empty; //3 + 3 = 6, 1 + 2 = 3,
        }
        return drunk;
    }
};
 
JavaScript:
var numWaterBottles = function(numBottles, numExchange) {
    if (numBottles < numExchange) return numBottles;
    
    let ans = numBottles;
    while (numBottles >= numExchange) {
        let exchangeBottles = Math.floor(numBottles / numExchange);
        numBottles = exchangeBottles + numBottles % numExchange;
        ans += exchangeBottles;
    }

    return ans;
};
 
Java:
class Solution {
    public int numWaterBottles(int numBottles, int numExchange) {
        int total = numBottles;
        while(numBottles>=numExchange){
            total += numBottles/numExchange;
            numBottles = numBottles/numExchange+numBottles%numExchange;           
        }
        return total;
}
 
the chad "oh hey look, the convoluted code I wrote really in fact did eliminate runtime checks against division-by-zero (very big deal btw), made a significant impact on the performance of the program (which I haven't proven through benchmarks btw), the function I transformed into this convoluted mess was in fact a critical part of an important programme (and just not some throwaway LC problem) and therefore worth the effort, and I totally did all that not because I wanted to demonstrate my superior code crafting skill, but there was legitimate justification btw"

vs.

the virgin "I did it for the heck of it, your problem?"
 
the chad "oh hey look, the convoluted code I wrote really in fact did eliminate runtime checks against division-by-zero (very big deal btw), made a significant impact on the performance of the program (which I haven't proven through benchmarks btw), the function I transformed into this convoluted mess was in fact a critical part of an important programme (and just not some throwaway LC problem) and therefore worth the effort, and I totally did all that not because I want to demonstrate my superior code crafting skill, but there was legitimate justification btw"

vs.

the virgin "I did it for the heck of it, your problem?"

FB_IMG_1687525858415.jpg

Uhmm this dad joke took so long it became a granddad bro.
 
Điểm danh 7/7

C++:
class Solution {
public:
    int numWaterBottles(int numBottles, int numExchange) {
        int result = numBottles;
        while (numBottles / numExchange >= 1){
            result += numBottles / numExchange;
            int mod = numBottles % numExchange;
            numBottles /= numExchange;
            numBottles += mod;
        }
        return result;
    }
};
 
Mấy hôm nay toàn div với mod vậy
4RJD3gO.png

JavaScript:
function numWaterBottles(n: number, k: number): number {
    let m = n, res = n;
    while (m >= k) {
        const div = Math.floor(m / k), mod = m % k;
        res+= div,
        m = div + mod;
    }
    return res;
};
 
7/7 - 1518. Water Bottles
C#:
public class Solution {
    public int NumWaterBottles(int numBottles, int numExchange) {
        var result = numBottles;
    
        while (numBottles >= numExchange) {
            result += numBottles / numExchange;
            numBottles = numBottles / numExchange + numBottles % numExchange;
        }

        return result;
    }
}

Mã:
impl Solution {
    pub fn num_water_bottles(num_bottles: i32, num_exchange: i32) -> i32 {
        let mut current_bottles = num_bottles;
        let mut result = current_bottles;

        while current_bottles >= num_exchange {
            result += current_bottles / num_exchange;
            current_bottles = current_bottles / num_exchange + current_bottles % num_exchange;
        }

        result
    }
}
1720370102314.png


Rust bá quá.
1720370305263.png
 
Sửa lần cuối:
JavaScript:
var findTheWinner = function(n, k) {
    // Create a queue of element from 1 to n
    // Do the loop until there is only one element left
    // Step 1. Move first k elements to the end of queue
    // Step 2. Remove the kth element
    // Repeat step 1

    let queue = [];
    for (let i = 1; i <= n; i++) {
        queue[i-1] = i;
    }

    while (queue.length > 1) {
        for (let i = 0; i < k - 1; i++) {
            queue.push(queue.shift());
        }

        queue.shift(); // remove the kth element
    }

    return queue[0];
};
 
08/07
Java:
class Solution {
    public int findTheWinner(int n, int k) {
        return (n == 1) ? 1 : (findTheWinner(n - 1, k) + k - 1) % n + 1;
    }
}
 
Java:
class Solution {
    public int findTheWinner(int n, int k) {
        ArrayList<Integer> list =new ArrayList<>();
        for(int i =1;i<=n;i++){
            list.add(i);
        }
        int index =0;
        while(list.size()>1){
            index = (index+k-1)%list.size();
            list.remove(index);
        }
        return list.get(0);
    }
}
 
PHP:
// class ListNode {
//     public $val = 0;
//     public $next = null;
//     function __construct($val = 0, $next = null) {
//         $this->val = $val;
//         $this->next = $next;
//     }
// }

class Solution {

    /**
     * @param Integer $n
     * @param Integer $k
     * @return Integer
     */
    function findTheWinner($n, $k) {
        if ($k == 1) return $n;

        // create circular linkedlist
        $firstPlayer = new ListNode(1);
        $cur = $firstPlayer;
        for ($i=2; $i<=$n; $i++) {
            $player = new ListNode($i);
            $cur->next = $player;
            $cur = $player;
        }
        $cur->next = $firstPlayer;

        // let's play game
        $count = 1;
        $winner = $firstPlayer;
        while ($winner->val != $winner->next->val) {
            if ($count == $k-1) {
                $winner->next = $winner->next->next;
                $count = 0;
            }

            $winner = $winner->next;
            $count++;
        }

        return $winner->val;
    }
}
 
int findTheWinner(int n, int k) {
bool check[n+1];
for(int i = 1 ; i <= n; i++) check = false;
int count = 0;
int tmp = k;
while(count != n-1) {
check[tmp] = true;
// cout <<"index=" << tmp << endl;
count++;
tmp++;
int c = k;
while(c){
if(tmp == n+1) tmp =1;
if(!check[tmp]) c--;
tmp++;
}
tmp--;
}
for(int i = 1 ; i <=n ; i++) {
if(!check) return i;
}
return 0;
}
 
Java:
class Solution {
    public int findTheWinner(int n, int k) {
        LinkedList<Integer> circle = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            circle.add(i);
        }

        int begin = 0;
        while (circle.size() != 1) {
            begin = (circle.size() + begin + (k % circle.size()) - 1) % circle.size();
            circle.remove(begin);
            if (begin >= circle.size()) {
                begin = 0;
            }
        }

        return circle.get(0);
    }
 
Sửa lần cuối:
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.566
Quay lại
Lên đầu trang