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.
dùng 3 biến thôi anh tài :p
tức là cả bài dùng độc có 3 biến global thôi ấy hả
V092S5K.gif
 
Ban đầu đọc nhầm đề là tìm min/max difference giữa 2 critical points bất kì, thay vì min/max distance, đi pv chắc rớt :beat_brick:
Python:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
        
        def is_critical_point(p1, p2, p3):
            return p1.val < p2.val > p3.val or p1.val > p2.val < p3.val

        def find_first_critial_point_index(head):
            if not head or not head.next:
                return -1, None
            
            p1, p2, p3 = head, head.next, head.next.next
            counter = 0
            while p3:
                if is_critical_point(p1, p2, p3):
                    return counter + 1, p2
                counter += 1
                p1, p2, p3 = p2, p3, p3.next
            return -1, None
        
        i_1, head = find_first_critial_point_index(head)
        off = i_1
        min_d = max_d = -1
        while head:
            j, head = find_first_critial_point_index(head)
            if head:
                off += j
                max_d = off - i_1
                min_d = off - i_1 if min_d == -1 else min(min_d, j)
        return [min_d, max_d]
 
C++:
class Solution {
public:
    vector<int> nodesBetweenCriticalPoints(ListNode* head) {
        int f[100001];
        int ansMin = INT_MAX;
        int i = 0, j = 0;
        int prev = head->val;
        head = head->next;

        while(head->next != nullptr){
            if ((head->val > prev && head->val > head->next->val) ||
            (head->val < prev && head->val < head->next->val)){
                f[i] = j;
                i++;
            }
            prev = head->val;
            j++;
            head=head->next;
        }

        for(int j = 1; j < i; j++){
            ansMin = min(ansMin, f[j] - f[j -1]);
        }
        
        if(i > 1){
            return {ansMin, f[i-1] - f[0]};
        }else return {-1, -1};
    }
};
 
được thêm 1 chỗ offer nữa rồi mấy bác ơi, mừng quá trời mừng :too_sad:
Bác làm mảng gì, techstack là gì mà thất nghiệp lâu thế
mới 2 tháng mà thím :D, tôi đợt lâu nhất tới 1 năm đây

Thím chắc làm C# backend hỉ
em fresher C# backend :smile:
C#:
public class Solution {
    public int[] NodesBetweenCriticalPoints(ListNode head) {
        ListNode temp = head.next;
        int prev_val = head.val;
        int min = Int32.MaxValue;
        int prev_crit_point = 0;
        int pos = 1;
        int first_crit_point = 0;
        int last_crit_point = 0;
        while(temp.next != null)
        {
            if((temp.val > prev_val && temp.val > temp.next.val) || (temp.val < prev_val && temp.val < temp.next.val))
            {
                if(first_crit_point == 0)
                {
                    first_crit_point = pos;
                    last_crit_point = pos;
                    prev_crit_point = pos;
                }
                else
                {
                    prev_crit_point = last_crit_point;
                    last_crit_point = pos;
                }
            }
            if(prev_crit_point != last_crit_point)
            {
                if(last_crit_point - prev_crit_point < min)
                    min = last_crit_point - prev_crit_point;
            }
            pos++;
            prev_val = temp.val;
            temp = temp.next;
        }
        if(prev_crit_point != last_crit_point)
            return new int[] {min, last_crit_point - first_crit_point};
        else
            return new int[] {-1, -1};
    }
}
 
được thêm 1 chỗ offer nữa rồi mấy bác ơi, mừng quá trời mừng :too_sad:


em fresher C# backend :smile:
C#:
public class Solution {
    public int[] NodesBetweenCriticalPoints(ListNode head) {
        ListNode temp = head.next;
        int prev_val = head.val;
        int min = Int32.MaxValue;
        int prev_crit_point = 0;
        int pos = 1;
        int first_crit_point = 0;
        int last_crit_point = 0;
        while(temp.next != null)
        {
            if((temp.val > prev_val && temp.val > temp.next.val) || (temp.val < prev_val && temp.val < temp.next.val))
            {
                if(first_crit_point == 0)
                {
                    first_crit_point = pos;
                    last_crit_point = pos;
                    prev_crit_point = pos;
                }
                else
                {
                    prev_crit_point = last_crit_point;
                    last_crit_point = pos;
                }
            }
            if(prev_crit_point != last_crit_point)
            {
                if(last_crit_point - prev_crit_point < min)
                    min = last_crit_point - prev_crit_point;
            }
            pos++;
            prev_val = temp.val;
            temp = temp.next;
        }
        if(prev_crit_point != last_crit_point)
            return new int[] {min, last_crit_point - first_crit_point};
        else
            return new int[] {-1, -1};
    }
}
Bác này giỏi thật, thời điểm này kiếm được job không phải là dễ
 
Mã:
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
        current = head.next
        ans = [100001,-1]
        idx = 1
        temp = [0,0]
        previous_val = head.val
        while current and current.next:
            idx+=1
            if (current.val > previous_val and current.val > current.next.val) or (current.val < previous_val and current.val < current.next.val):
                if temp[0] == 0:
                    temp[0] = idx
                else:
                    ans[1] = idx - temp[0]
                    ans[0] = min(ans[0], idx-temp[1])
                temp[1] = idx


            previous_val = current.val
            current = current.next
        if ans[0] == 100001:
            ans[0] = -1
        return ans
 
Python:
class Solution:
    def passThePillow(self, n: int, time: int) -> int:
        if (time // (n - 1)) % 2 == 0:
            return time % (n - 1) + 1
        else:
            return n - (time % (n - 1))
Ngẫm mãi mới ra được cái công thức:beat_brick:
 
C#:
public class Solution {
    public int PassThePillow(int n, int time) {
        if ((time / (n - 1) % 2 == 0)) return time % (n - 1) + 1;
        return n - time % (n - 1);
    }
}
 
Bài này ứng dụng được nhiều chỗ nè :D hồi tôi xài để chỉ định điểm patrol tiếp theo cho mấy thằng lính gác trong game
C#:
public class Solution
{
    public int PassThePillow(int n, int time)
    {
        int twoTimeIndex = n * 2 - 2;
        int remainder = time % twoTimeIndex;

        return remainder < n ? remainder + 1 : twoTimeIndex - remainder + 1;
    }
}
 
C-like:
impl Solution {
    pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
        if (time / (n - 1)) & 1 == 0 {
            1 + time % (n - 1)
        } else {
            n - time % (n - 1)
        }
    }
}
 
C-like:
impl Solution {
    pub fn pass_the_pillow(n: i32, time: i32) -> i32 {
        let (q, r) = (time / (n - 1), time % (n - 1));

        if q % 2 == 0 {
            1 + r
        } else {
            n - r
        }
    }
}
 
Sửa lần cuối:
PHP:
class Solution {

    /**
     * @param Integer $n
     * @param Integer $time
     * @return Integer
     */
    function passThePillow($n, $time) {
        $holder = 1;
        $rev = false; // check direction changes
        while ($time) {
            $holder += (!$rev) ? 1 : -1;
            $rev = ($holder == $n || $holder == 1) ? !$rev : $rev;
            $time--;
        }

        return $holder;
    }
}
 
Sửa lần cuối:
Loop :beat_brick:
C#:
public class Solution {
    public int PassThePillow(int n, int time) {
        bool check = true;
        int result = 1;
        while(time > 0)
        {
            time--;
            if(check)
                result++;
            else
                result--;
            if(result == n)
                check = false;
            if(result == 1)
                check = true;
        }
        return result;
    }
}
same
C:
class Solution {
public:
    int passThePillow(int n, int time) {
        while(time) {
            for(int i = 1; i < n; i++) {
                if(time == 0)
                    return i;
                --time;
            }
            for(int i = n; i> 1; i--) {
                if(time == 0)
                    return i;
                --time;
            }
        }
        return 1;
    }       
};
 
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ố:
bài này thì trư giải dễ dàng
9tSTebu.png


C:
class Solution {
public:
    int numberOfChild(int n, int k) {
        k %= (2 * n - 2);
        if(k < n)
            return k;
        return (n - 1) - (k % (n - 1));
    }
};
 
Sao code O(1) 3ms runtime mà code O(n) lại 0ms runtime nhỉ @@
C++:
class Solution {
public:
    int passThePillow(int n, int time) {
        return (time/(n-1)%2)?(n - time % (n -1)) : (time % (n - 1) + 1);
    }
};
C++:
class Solution {
public:
    int passThePillow(int n, int time) {
        int currentPillowPosition = 1;
        int currentTime = 0;
        int direction = 1;
        while (currentTime < time) {
            if (0 < currentPillowPosition + direction &&
                currentPillowPosition + direction <= n) {
                currentPillowPosition += direction;
                currentTime++;
            } else {
                // Reverse the direction if the next position is out of bounds
                direction *= -1;
            }
        }
        return currentPillowPosition;
    }
};
 
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.693
Quay lại
Lên đầu trang