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.
em còn định gắn chuỗi nào gặp số thì nhân chuỗi trước đó theo số lần
xong nhìn chuỗi dài quá, tắt điện :))
 
em còn định gắn chuỗi nào gặp số thì nhân chuỗi trước đó theo số lần
xong nhìn chuỗi dài quá, tắt điện :))
y như cái solution của mình, mình gặp digit sẽ tính số item trước nó, thằng sau gặp digit thì lấy số items trước đó * với digit value.
Hí hửng implement, dính 2 thằng digit liên tục tắt điện luôn ko biết làm sao lol
Pass hết test case nhưng mà dính Memory Limit Exceeded, concat string là cách sai lầm :after_boom:
Test cases tới 2^63 mà concat gì mai fence :( mình đọc constrains là biết phải có 1 cách O(n) mới giải quyết được vấn đề, medium mà khó vl =(( hèn gì gần đây ko có thằng nào hỏi lúc phỏng vấn
1695784171786.png
 
Chắc đ ai thèm làm bằng TS luôn, beat 100% TC, SC :haha:
JavaScript:
function decodeAtIndex(s: string, k: number): string {
    let curSize = 0;
    let i = 0;

    while (curSize < k) {
        if (!isNaN(Number(s[i]))) {
            curSize *= Number(s[i]);
        } else curSize++;
        i++;
    }

    for (let j = i - 1; j >= 0; j--) {
        if (!isNaN(Number(s[j]))) {
            curSize /= Number(s[j]);
            k %= curSize;
        } else {
            if (k === 0 || k === curSize) {
                return s[j];
            }
            curSize--;
        }
    }

    return "bai nhu cc";
};
 
Học python code alogrithm thôi. Code C# như cái cc cay quá, trash language. Để ý bao nhiêu lần rồi, chắc lần này bỏ hẳn C# cmnl.
Code O(n) mà nhìn run time + memory đúng chán. Lại còn dính edge cases về data type tùm lum :ah:
1695784514058.png
 
Học python code alogrithm thôi. Code C# như cái cc cay quá, trash language. Để ý bao nhiêu lần rồi, chắc lần này bỏ hẳn C# cmnl.
Code O(n) mà nhìn run time + memory đúng chán. Lại còn dính edge cases về data type tùm lum :ah:
Xem tệp đính kèm 2094812
data type mới là điểm thêm khó cho cái bài ấy chứ thím. Chứ cứ như Python với JS thì bao giờ mới thấy khó :big_smile:
 
Chắc đ ai thèm làm bằng TS luôn, beat 100% TC, SC :haha:
JavaScript:
function decodeAtIndex(s: string, k: number): string {
    let curSize = 0;
    let i = 0;

    while (curSize < k) {
        if (!isNaN(Number(s[i]))) {
            curSize *= Number(s[i]);
        } else curSize++;
        i++;
    }

    for (let j = i - 1; j >= 0; j--) {
        if (!isNaN(Number(s[j]))) {
            curSize /= Number(s[j]);
            k %= curSize;
        } else {
            if (k === 0 || k === curSize) {
                return s[j];
            }
            curSize--;
        }
    }

    return "bai nhu cc";
};
return thế kia auto pass tier 1 ở VN rồi, tại hạ bái phục
 
Ban đầu nghĩ theo hướng đệ quy, tìm các mốc rồi mod thằng k, thu hẹp lại input, mà dính edge case quá trời. :mad:
Python:
class Solution:
    def decodeAtIndex(self, S: str, K: int) -> str:
        size = 0

        for char in S:
            if char.isdigit():
                size *= int(char)
            else:
                size += 1

        for char in reversed(S):
            K %= size
            if K == 0  and char.isalpha():
                return char

            if char.isdigit():
                size //= int(char)
            else:
                size -= 1
 
JavaScript:
class S {
    inner = null;
    times = 0;
    suffix = '';
    _length = null;
    at(idx) {
        const innerLength = this.inner?.length() ?? 0;
        const prefixLength = innerLength ? innerLength * this.times : 0;
        if (idx >= prefixLength) {
            return this.suffix[idx - prefixLength];
        } else {
            return this.inner.at(idx % innerLength);
        }
    }
    length() {
        return this._length ??= (() =>
            (this.inner ? this.inner.length() * this.times : 0) + this.suffix.length
        )();
    }
}

/**
* @param {string} s
* @param {number} k
* @return {string}
*/
var decodeAtIndex = function (s, k) {
    let t = new S();
    for (const ch of s) {
        if (ch.match(/[a-z]/)) {
            t.suffix += ch;
        } else {
            t = Object.assign(new S(), {
                inner: t,
                times: Number(ch),
            });
        }
    }
    return t.at(k - 1);
};
 
Lâu lắm rồi mới gặp bài daily chưa làm :sweat:
Python:
class Solution:
    def decodeAtIndex(self, s: str, k: int) -> str:
        current_length = 1
        current_index = 1

        while current_index < len(s):
            if s[current_index].isalpha():
                current_length += 1
            else:
                current_length *= int(s[current_index])
            if current_length >= k:
                break
            current_index += 1
       
        while current_index >= 0:
            if s[current_index].isdigit():
                current_length //= int(s[current_index])            
                k = current_length if k % current_length == 0 else k % current_length
            elif k == current_length:
                return s[current_index]
            else:
                current_length -= 1
           
            current_index -= 1

        return ""
 
mém tí quên làm, bài này code bừa cũng ăn mà
JiZo9zf.png

C++:
class Solution {
public:
    string decodeAtIndex(string_view s, int k) {
        for (char lastChar{};;) {
            for (uint64_t len = 0; char c : s) {
                uint64_t newLen = len;
                if (isalpha(c)) {
                    ++newLen;
                    lastChar = c;
                } else {
                    newLen *= c - '0';
                }
                if (newLen == k) return string(1, lastChar);
                if (newLen > k) {
                    k %= len;
                    if (k == 0) return string(1, lastChar);
                    break;
                }
                len = newLen;
            }
        }
        return {};
    }
};
 
2 pointers beats gần hết :ah: bài nay dễ quá

C#:
public class Solution {
    public int[] SortArrayByParity(int[] nums) {
      var left = 0;
      var right = nums.Length -1;
      while(left < right)
      {
          while(nums[left]%2 == 0 && left < right)
          {
              left++;
          }

          while(nums[right]%2 == 1  && left < right)
          {
              right--;
          }
          var temp = nums[left];
          nums[left] = nums[right];
          nums[right] = temp;
          left++;
          right--;
      }

      return nums;
    }
}
 
mém tí quên làm, bài này code bừa cũng ăn mà
JiZo9zf.png

C++:
class Solution {
public:
    string decodeAtIndex(string_view s, int k) {
        for (char lastChar{};;) {
            for (uint64_t len = 0; char c : s) {
                uint64_t newLen = len;
                if (isalpha(c)) {
                    ++newLen;
                    lastChar = c;
                } else {
                    newLen *= c - '0';
                }
                if (newLen == k) return string(1, lastChar);
                if (newLen > k) {
                    k %= len;
                    if (k == 0) return string(1, lastChar);
                    break;
                }
                len = newLen;
            }
        }
        return {};
    }
};
Kêu code bừa vẫn ăn thành ra sỉ nhục anh em tôi quá :devilish:
 
ko quan tâm space thì cứ tạo mảng mới bỏ vô loop 2 lần
Python:
class Solution:
    def sortArrayByParity(self, nums: List[int]) -> List[int]:
        return [x for x in nums if x % 2 == 0] + [x for x in nums if x % 2 == 1]
 
mấy hôm không làm được giờ mới có bài dễ
C++:
class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& nums) {
        sort(nums.begin(), nums.end(), [](int a, int b){
            if(a % 2 == 0 && b % 2 == 1)
                return true;
            return false;
        });
        return nums;
    }
};
 
Swap in-place qua don gian :sure:
JavaScript:
function sortArrayByParity(nums: number[]): number[] {
    for (let i = 0, j = 0; j < nums.length; j++) {
        if (nums[j] % 2 === 0) {
            let temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
        }
    }
    return nums;
}
 
bài hôm này là kiểu làm thuật toán sắp xếp theo dk " num % 2 " nhỉ chẵn bên trái, lẻ bên phải.
lúc đầu em cứ chia thành 2 mảng xong gộp lại xD
 
std::ranges::sort
cgE9MkI.gif

C++:
struct Solution {
    vector<int> sortArrayByParity(vector<int>& nums) {
        ranges::sort(nums, less{}, [](int n){ return n % 2; });
        return move(nums);
    }
};
 
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.216
Quay lại
Lên đầu trang