thảo luận Leetcode mỗi ngày

Code:
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
 
@LmaoSuVuong code đâu?
fd80e89f23663dc95baacfdb1f9c6bb9.gif
 
: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;
};
 
medium fake à
Java:
  public void deleteNode(ListNode node) {
    while (true) {
      node.val = node.next.val;
      if (node.next != null && node.next.next == null) {
        node.next = null;
        break;
      }
      node = node.next;
    }
  }
 
Code:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void DeleteNode(ListNode node) {
        node.val = node.next.val;  
        node.next = node.next.next;
    }
}
}
 
Last edited:
bài hôm nay bựa thế nhỉ :angry:
C#:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void DeleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}
 
Java:
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}
 
JavaScript:
function deleteNode(node: ListNode | null): void {
    node.val = node.next.val;
    node.next = node.next.next;
};
 
Sao bài hôm nay dễ bất ngờ vậy, submit cứ run run liệu thiếu edge case nào ko
:sweat:
C++:
void deleteNode(ListNode* node) {
    ListNode *cur = node, *next = node->next;
    while(next->next) {
        swap(cur->val, next->val);
        cur = cur->next;
        next = next->next;
    }
    swap(cur->val, next->val);
    cur->next = nullptr;
}
Ps: Cách gọn hơn
C++:
void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
 
Back
Top