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.
submit từ 2 năm trước, h đọc lại ko hiểu :ROFLMAO::ROFLMAO::ROFLMAO:

var myPow = function(x, n) {
let result = Math.exp(n*Math.log(Math.abs(x)));

if (x < 0 && n % 2 ===1) result = result*-1;

return result;

};
e^(nln(abs(x))) = (e^(ln(abs(x)))^n = abs(x)^n đấy, ct toán mà. bài này ko cho dùng hàm mũ, mà xài ct này cũng dc :))
 
Python:
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left = 1
        right = len(arr) - 1

        while left < right:
            mid = (left + right) // 2
            
            if arr[mid] > arr[mid - 1]:
                left = mid + 1
            else:
                right = mid
        
        return right - 1
 
C++:
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int lo = 1, hi = arr.size() - 2;
        while (lo <= hi){
            int mi = (lo + hi) / 2;
            if (arr[mi] > arr[mi - 1] && arr[mi] > arr[mi + 1])
                return mi;
            if (arr[mi] < arr[mi + 1]) lo = mi + 1;
            else hi = mi - 1;
        }
        return lo;
    }
};
 
Python:
class Solution:
    def peakIndexInMountainArray(self, A: List[int]) -> int:
        return A.index(max(A))
 
Bài hôm nay thì ez rồi, ko biết sao cho thành medium nữa :D
JavaScript:
function peakIndexInMountainArray(arr: number[]): number {
    let l = 0, r = arr.length - 1;
    while(l < r) {
        const m = (l + r) >> 1;
        if (arr[m] < arr[m+1]) {
            l = m + 1;
        } else {
            r = m;
        }
    }
    return l;
};
 
JavaScript:
var peakIndexInMountainArray = function(arr) {
    let l = 0, r = arr.length - 1;
    while (l < r) {
        const m = (l + r) >> 1;
        if (arr[m] < arr[m+1]) {
            l = m + 1;
        } else {
            r = m;
        }
    }
    return l;
};
 
C++:
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int l=2,r=arr.size()-2;
        while(l<=r)
        {
            int m=(l+r)>>1;
            if(arr[m]>arr[m-1])
            {
                l=m+1;
            }
            else
            {
                r=m-1;
            }
        }
        return l-1;
    }
};
 
C++:
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int low = 1, hi = arr.size() - 2, index = -1;
        while (low <= hi) {
            int mid = low + (hi - low)/2;
            if (arr[mid] > arr[mid-1]) {
                index = mid;
                low = mid + 1;
            }
            else {
                hi = mid - 1;
            }
        }
        return index;
    }
};
Dạo này thấy code yếu quá, phải leetcode tiếp:amazed:
 
Java:
class Solution {
    public int peakIndexInMountainArray(int[] arr) {
        int l=0, r= arr.length-1, m;
        while(l<r){
            m = l+(r-l)/2;
            if(arr[m]>arr[m-1] && arr[m]>arr[m+1]) return m;
            if(arr[m-1]<arr[m] && arr[m]<arr[m+1]) l=m;
            else r=m;
        }
        return 0;
    }
}
Đang học lại C nhưng luyện code bằng Java trên leetcode còn nhiều hơn luyện web bài tập của trường :feel_good:
 
C++:
class Solution {
public:
    bool check(vector<int>& dist, int v, double hour){
        double consume = 0;
        for (int i = 0; i < dist.size(); i++){
            if (i == dist.size() - 1){
                consume += 1.0 * dist[i] / v;
            } else{
                consume += ceil(1.0 * dist[i] / v);
            }
        }
        return consume <= hour;
    }
    int minSpeedOnTime(vector<int>& dist, double hour) {
        int lo = 1, hi = (int)1e9+1;
        while (lo < hi){
            int mid = (lo + hi) / 2;
            if (check(dist,mid,hour)) hi = mid;
            else lo = mid + 1;
        }
        if (!check(dist,lo,hour)) return -1;
        return lo;
    }
};
 
JavaScript:
var minSpeedOnTime = function(dist, hour) {
    const go = v => {
        let total = 0;
        for (const d of dist) {
            total = Math.ceil(total) + d / v;
            if (total > hour) {
                return false;
            }
        }
        return true;
    };
    let l = 1, r = 1e7 + 1;
    while (l < r) {
        const m = Math.trunc((l + r) / 2);
        if (go(m)) {
            r = m;
        } else {
            l = m + 1;
        }
    }
    return (l === 1e7 + 1) ? -1 : l;
};
 
Tuần của Binary Seach
JavaScript:
function minSpeedOnTime(dist: number[], hour: number): number {
    const n = dist.length;
    let l = 1, r = 1e7, ans = -1;
    while (l <= r) {
        const mid = (r + l) >> 1;
        let sum = 0;
        for (let i = 0; i < n - 1; i++) {
            sum += Math.ceil((dist[i]) / mid);
        }
        sum = sum + ((dist[n - 1]) / mid);
        if (sum > hour) {
            l = mid + 1;
        } else {
            ans = mid;
            r = mid - 1;
        }
    }
    return ans;

};
 
Python:
def minSpeedOnTime(dist: List[int], hour: float) -> int:
    def check(speed):
        time = 0
        for d in dist[:-1]:
            time += math.ceil(d / speed)
        return time + dist[-1] / speed <= hour

    left, right = 1, 10 ** 7
    while left <= right:
        mid = (left + right) >> 1
        if check(mid):
            right = mid - 1
        else:
            left = mid + 1

    if left <= 10 ** 7:
        return left

    return -1
 
TrMT7Oq.png
Đếch thể ngờ bài hôm nay nó lại là tìm kiếm nhị phân kiểu này

VKL
 
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.745
Quay lại
Lên đầu trang