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.
Java:
class Solution {
    public long maxKelements(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);
        for(int num:nums){
            pq.add(num);
        }
        long res =0;
        while(k-->0){
            int max = pq.poll();
            res += max;
            pq.add((max+2)/3);
        }
        return res;
    }
}
 
Java:
class Solution {
    public long maxKelements(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a,b)->b-a);
        for(int num:nums){
            pq.add(num);
        }
        long res =0;
        while(k-->0){
            int max = pq.poll();
            res += max;
            pq.add((max+2)/3);
        }
        return res;
    }
}
Cái max + 2 / 3 hay thế, không cần phải convert qua double rồi convert lại int.
 
mỗi contest giải 1 câu tắt máy đi ngủ cũng ko xuống dưới 1k5 dc mà tiểu đơn xiao lin hả
mzyidY1.png
Nó nói một câu mà nó chửi xéo nhiều người thế
320PQq9.png
 
Swift có sẵn cái heap rồi, lấy ra xài. Thấy mn tự build cái heap riêng.

Swift:
import Collections
class Solution {
    func maxKelements(_ nums: [Int], _ k: Int) -> Int {
        var heap: Heap<Int> = Heap(nums)
        var result = 0
        for _ in 0..<k {
            let max = heap.popMax() ?? 0
            result += max
            heap.insert((max+2)/3)
        }
        return result
    }
}
 
C-like:
use std::collections::BinaryHeap;

impl Solution {
    pub fn max_kelements(nums: Vec<i32>, k: i32) -> i64 {
        let mut max_heap: BinaryHeap<i64> =
            nums.into_iter().
                map(|num| num as i64).
                collect();

        let mut score = 0;
        for _ in 0..k {
            let top = max_heap.pop().unwrap();
            score += top;
            max_heap.push(top / 3 + (top % 3 != 0) as i64);
        }

        score
    }
}
 
Java:
class Solution {
    public long maxKelements(int[] nums, int k) {
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));       

        for (int num : nums) {
            pq.offer(num);
        }

        long res = 0l;
        
        for (int i = 1; i <= k; i++) {
            int num = pq.poll();
            res += (long) num;
            pq.offer((num + 2) / 3);
        }

        return res;
    }
}
 
Python:
class Solution:
    def maxKelements(self, nums: List[int], k: int) -> int:
        maxHeap = []

        for num in nums:
            heappush(maxHeap, num * -1)

        score = 0
        for _ in range(k):
            point = heappop(maxHeap) * -1
            score += point
            heappush(maxHeap, ceil(point / 3) * -1)

        return score
 
Mã:
class Solution:
    def maxKelements(self, nums: List[int], k: int) -> int:
        q = []
        for i in nums:
            heapq.heappush(q, -i)
        score = 0
        for _ in range(k):
            v = -heapq.heappop(q)
            score += v
            heapq.heappush(q, -math.ceil(v / 3))
        return score
 
Swift có sẵn cái heap rồi, lấy ra xài. Thấy mn tự build cái heap riêng.

Swift:
import Collections
class Solution {
    func maxKelements(_ nums: [Int], _ k: Int) -> Int {
        var heap: Heap<Int> = Heap(nums)
        var result = 0
        for _ in 0..<k {
            let max = heap.popMax() ?? 0
            result += max
            heap.insert((max+2)/3)
        }
        return result
    }
}
swift có built in Heap luôn hả, thế mà mấy nhóc JS vs TS éo có, pùn o_O
 
Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        result, count_one = 0, 0
        for c in s:
            if c == '0':
                result += count_one
            else:
                count_one += 1
        return result
 
mấy ngày medium fake liên tục rồi :ops:
JavaScript:
function minimumSteps(s: string): number {
    let count = 0, res = 0;
    for (const c of s) {
        if (c === '0') res+= count;
        else count++
    }
    return res;
};
 
Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        p = len(s) - 1
        ans = 0
        for i, c in enumerate(s):
            if c == '1':
                while p >= i and s[p] == '1':
                    p -= 1
                if p >= i:
                    ans += (p - i)
                    p -= 1
        return ans
 
C++:
class Solution {
public:
    long long minimumSteps(string s) {
        long st = 0; //step
        long fa = 0; //first anchor
        for(long i = 0 ; i < s.length(); i++) {
            if(s[i] == '0') {
                st += i-fa;
                fa++;
            }
        }
        return st;
    }
};
 
C++:
class Solution {
public:
    long long minimumSteps(string s) {
        long long ans = 0;
        int cnt = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s[i] == '0') ans += cnt;
            else if (s[i] == '1') cnt++;
        }

        return ans;
    }
};
 
Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        count_one = 0
        edit_count = 0
        for idx, c in enumerate(s):
            if c == '1':
                count_one += 1
            else:
                edit_count += count_one
        return edit_count
 
Python:
class Solution:
    def minimumSteps(self, s: str) -> int:
        swaps = 0
        lastWhite = 0
        for i, char in enumerate(s):
            if char == '0':
                swaps += i - lastWhite
                lastWhite += 1
                
        return swaps
 
Swift:
class Solution {
    func minimumSteps(_ s: String) -> Int {
        var result = 0
        var countOne = 0
        for char in s {
            if char == "0" {
                result += countOne
            } else {
                countOne += 1
            }
        }
        return result
    }
}
 
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.213.602
Quay lại
Lên đầu trang