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.
C#:
public class Solution
{
    public int NumRescueBoats(int[] people, int limit)
    {
        Array.Sort(people);
        int left = 0;
        int right = people.Length - 1;

        int result = 0;
        while (left <= right)
        {
            int remain =  limit - people[right];
            if (people[left] <= remain)
            {
                left++;
            }
            right--;
            result++;
        }

        return result;
    }
}

C# trash language mà :shame:

via theNEXTvoz for iPhone
á à :what:
 
C#:
public class Solution
{
    public int NumRescueBoats(int[] people, int limit)
    {
        Array.Sort(people);
        int left = 0;
        int right = people.Length - 1;

        int result = 0;
        while (left <= right)
        {
            int remain =  limit - people[right];
            if (people[left] <= remain)
            {
                left++;
            }
            right--;
            result++;
        }

        return result;
    }
}


á à :what:
Muốn lên level thì đổi qua Python ngay :shame: toy đổi từ python rating tăng từ 1k5 lên 1k8 rồi :shame:

via theNEXTvoz for iPhone
 
một solution khác ngu muội hơn:

Mã:
use std::cmp;

impl Solution {
    pub fn num_rescue_boats(mut people: Vec<i32>, limit: i32) -> i32 {
        let mut freq = vec![0i32; 3 * 10usize.pow(4) + 1];
        let npeople = people.len();

        for i in 0..npeople {
            freq[people[i] as usize] += 1;
        }

        let mut l = 0;
        let mut r =  3 * 10usize.pow(4);
        let mut count = 0;
        let ulimit = limit as usize;

        while (l < r) {
            if (l + r <= ulimit) {
                let min = cmp::min(freq[l], freq[r]);
                count += min;

                freq[l] -= min;
                freq[r] -= min;
            } else {
                count += freq[r];
                freq[r] = 0;
            }

            if (freq[l] == 0) {
                l += 1;
            }

            if (freq[r] == 0) {
                r -= 1;
            }
        }

        if (l == r) {
            if (l * 2 <= ulimit) {
                count += freq[l] / 2 + (freq[l] % 2);
            } else {
                count += freq[l];
            }
        }

        count
    }
}
 
Python:
class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort()
        i, j, b = 0, len(people) - 1, 0
        while i <= j:
            if people[i] + people[j] <= limit:
                i += 1
                
            j -= 1
            b += 1
        
        return b
 
JavaScript:
var numRescueBoats = function(people, limit) {
    people.sort((u, v) => u - v);
    let ans = 0;
    for (let i = 0, j = people.length - 1; i <= j;) {
        if (i === j || people[i] + people[j] <= limit) {
            i++; j--;
        } else {
            j--;
        }
        ans++;
    }
    return ans;
};
 
để chử 2 pointer bên ngoài cái code nó hiện ra trong đầu luôn, ko cho thời gian suy nghỉ nữa
yp40V27.png
jdzp8kF.gif
lần sau bỏ hint vào bên trong spoiler lun đi bác
tận hưởng đi chống cự làm gì :)
 
một solution khác ngu muội hơn:

Mã:
use std::cmp;

impl Solution {
    pub fn num_rescue_boats(mut people: Vec<i32>, limit: i32) -> i32 {
        let mut freq = vec![0i32; 3 * 10usize.pow(4) + 1];
        let npeople = people.len();

        for i in 0..npeople {
            freq[people[i] as usize] += 1;
        }

        let mut l = 0;
        let mut r =  3 * 10usize.pow(4);
        let mut count = 0;
        let ulimit = limit as usize;

        while (l < r) {
            if (l + r <= ulimit) {
                let min = cmp::min(freq[l], freq[r]);
                count += min;

                freq[l] -= min;
                freq[r] -= min;
            } else {
                count += freq[r];
                freq[r] = 0;
            }

            if (freq[l] == 0) {
                l += 1;
            }

            if (freq[r] == 0) {
                r -= 1;
            }
        }

        if (l == r) {
            if (l * 2 <= ulimit) {
                count += freq[l] / 2 + (freq[l] % 2);
            } else {
                count += freq[l];
            }
        }

        count
    }
}
Cách này tối ưu hay hơn đấy chứ
NlogN thì nhìn ra greedy xong là cứ thế code chả cần nghĩ gì
Cách này tốn time tốn sức tốn chất xám để cài đặt hơn
 
Python:
class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        sortPeople, size = sorted(people), len(people)
        start, result, end = 0, 0, size - 1
        while start <= end:
            if sortPeople[start] + sortPeople[end] <= limit:
                start += 1
            end -= 1
            result += 1
        return result
 
Cách này tối ưu hay hơn đấy chứ
NlogN thì nhìn ra greedy xong là cứ thế code chả cần nghĩ gì
Cách này tốn time tốn sức tốn chất xám để cài đặt hơn
bài này giải đơn giản là được rồi, tốn sức optimize làm gì, nên mới gọi là ngu muội :D
 
Mã:
class Solution(object):
    def numRescueBoats(self, people, limit):
        """
        :type people: List[int]
        :type limit: int
        :rtype: int
        """

        if (len(people) == 1): return 1

        boatNum = 0
        people.sort()
        sortedPeople = people

        left, right = 0, len(sortedPeople) -1

        while left < right :
            if sortedPeople[left] + sortedPeople[right] <= limit:
                print(left, right)
                left += 1
                right -= 1
                boatNum += 1
            else:
                print(left, right)
                right -= 1
                boatNum += 1
        
        if left == right:
            boatNum += 1

        return boatNum
 
JavaScript:
function numRescueBoats(people: number[], limit: number): number {
    people.sort((a, b) => b - a)

    let count = 0;
    let left = 0;
    let right = people.length - 1;
    while (left <= right) {
        if (people[left] + people[right] <= limit) {
            left++;
            right--;
        } else {
            left++
        }

        count++
    }

    return count
};
 
JavaScript:
var numRescueBoats = function(people, limit) {
    people.sort((a, b) => a - b);
    
    let res = 0;   
    let l = 0;
    let r = people.length - 1;
    while (l <= r) {
        if (people[l] + people[r] <= limit) {
            l++;
        }

        r--;
        res++;
    }
    
    return res;
};
 
Python:
class Solution:
    def numRescueBoats(self, people: List[int], limit: int) -> int:
        people.sort(reverse=True)
        i = 0
        res = 0
        while people[i] == limit:
            res += 1
            i += 1
        j = len(people) - 1
        while i <= j:
            if i != j:
                if people[i] + people[j] <= limit:
                    res += 1
                    i += 1
                    j -= 1
                else:
                    res += 1
                    i += 1
            else:
                res += 1
                i += 1
        return res
 
:canny:

Java:
class Solution {
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int count=0;
        int r = people.length-1;
        int l =0;
        while(l<=r){
            if(people[r]==limit){
                count++;
            }
            else{
                if(people[r]+people[l]<=limit){
                    l++;
                }
                count++;
            }
            r--;
        }
        return count;
    }
}
sáng dậy vào thớt tự dưng thấy hint nên ko muốn đăng nữa
osCpCsi.png
 
:canny:

Java:
class Solution {
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int count=0;
        int r = people.length-1;
        int l =0;
        while(l<=r){
            if(people[r]==limit){
                count++;
            }
            else{
                if(people[r]+people[l]<=limit){
                    l++;
                }
                count++;
            }
            r--;
        }
        return count;
    }
}
sáng dậy vào thớt tự dưng thấy hint nên ko muốn đăng nữa
osCpCsi.png
nhìn phát làm được chứng tỏ sớm muộn gì fen cũng làm được thôi, tận hưởng đi chứ còn gì nữa :byebye:
 
C#:
public class Solution {
    public int NumRescueBoats(int[] people, int limit) {
        Array.Sort(people);
        int pointer1 = 0;
        int pointer2 = people.Length - 1;
        int result = 0;
        while(pointer1 <= pointer2)
        {
            result++;
            if(people[pointer1] + people[pointer2] <= limit)
            {
                pointer1++;
                pointer2--;
            }
            else
                pointer2--;
        }
        return result;
    }
}
 
Ko ngờ là nó dễ vậy, này mà medium gì ta
JavaScript:
var deleteNode = function(node) {
    const next = node.next;
    node.val = next.val;
    node.next = next.next;
};
 
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.215.669
Quay lại
Lên đầu trang