bkhoang
Senior Member
cọp lên tag @MasonMaoSuVuong với @Cố Trường Ca nhé fenCái leetcode này e ko làm đc. Copy sol lên đây hỏi nhờ giải thích dc ko các bác
via theNEXTvoz for iPhone
cọp lên tag @MasonMaoSuVuong với @Cố Trường Ca nhé fenCái leetcode này e ko làm đc. Copy sol lên đây hỏi nhờ giải thích dc ko các bác
via theNEXTvoz for iPhone
trong này cũng toàn thợ đụng thôi fen.Em là dân trái ngành đang muốn luyện leetcode 1 cách có hệ thống và bài bản nhất ạ. Em trước thì chỉ học chay như :stack, heap, bfs, dfs, dynamic rồi cứ đụng bài nào làm bài đó nên kiểu đôi khi có câu làm được câu không và thấy cảm giác kiến thức của mình không có cứng. Mấy bác luyện nhiều có lộ trình cụ thể nào chỉ bảo em với ạ .Xem tệp đính kèm 2682502
chạy đi fen ơi trong lày cop sol là trọng tội đấyCái leetcode này e ko làm đc. Copy sol lên đây hỏi nhờ giải thích dc ko các bác
via theNEXTvoz for iPhone
Duyệt chay cày trâu là ok mai fence, đầu tiên gen ra possible candidates, xong rồi khử dấu * xong rồi khử dấu +-cái toán phụ đó hình như cũng tầm medium cứng đóCũng trầy da tróc vẩy chứ k đơn giản hơn trư làm bao nhiêu![]()
Với cả trong trí nhớ của trư bài đó cũng cơ bắp lắm![]()
mấy thằng khôn nó code ngắn giữ thêm cái prev num nữa thì ít bugclass Solution:
def addOperators(self, num: str, target: int) -> List[str]:
candidates = set()
n = len(num)
def calculate(candidate):
n = len(candidate)
left = 0
stack = []
while left < n:
if candidate[left] in "+-*":
stack.append(candidate[left])
else:
current = int(candidate[left])
while left + 1 < n and candidate[left + 1] not in "+-*":
current = current*10 + int(candidate[left + 1])
left += 1
stack.append(current)
left += 1
n = len(stack)
holder = []
for i in range(n):
if holder and holder[-1] == "*":
holder.pop()
holder[-1]*= stack[i]
else:
holder.append(stack[i])
n = len(holder)
ans = []
for i in range(n):
if ans and ans[-1] == "+":
ans.pop()
ans[-1]+= holder[i]
elif ans and ans[-1] == "-":
ans.pop()
ans[-1] -= holder[i]
else:
ans.append(holder[i])
if ans[0] == target:
return candidate
return None
def possibleCandidate(i, current):
if i == n:
candidates.add(current)
return
a = ""
for j in range(i, n):
a += num[j]
if a != "0" and a[0] == "0":
break
if i == 0:
possibleCandidate(j + 1, a)
else:
possibleCandidate(j + 1, current + "+" + a)
possibleCandidate(j + 1, current + "-" + a)
possibleCandidate(j + 1, current + "*" + a)
possibleCandidate(0, -1)
ans = []
for candidate in candidates:
processedCandidate = calculate(candidate)
if processedCandidate != None:
ans.append(processedCandidate)
return ans

class Solution:
def distributeCookies(self, cookies: List[int], k: int) -> int:
n = len(cookies)
def backtrack(i, distribution):
if i == n:
return max(distribution)
ans = float("inf")
for j in range(k):
distribution[j] += cookies[i]
ans = min(ans, backtrack(i + 1, distribution))
distribution[j] -= cookies[i]
if distribution[j] == 0:
break
return ans
return backtrack(0, [0] * k)
public class Solution {
public int LongestSubarray(int[] nums) {
var count = 0;
var result = 0;
var maxValue = nums.Max();
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] == maxValue)
{
count++;
result = count > result ? count : result;
}
else
count = 0;
}
return result;
}
}
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
maxSofar = -inf
count = 0
ans = 0
for num in nums:
if num > maxSofar:
ans = 1
maxSofar = num
count = 1
elif num == maxSofar:
count += 1
ans = max(ans, count)
else:
count = 0
return ans

class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length, max = 0;
for (int num : nums) {
max = Math.max(max, num);
}
int res = 1, left = 0;
while (left < n) {
if (nums[left] != max) {
left++;
continue;
}
int right = left + 1;
while (right < n && nums[right] == max) {
right++;
}
res = Math.max(res, right - left);
left = right;
}
return res;
}
}
Làm vậy dễ bị memory limit error.Bài hard thực ra làm 1 pass thế này hơi khó, mình có cách tiếp cận là generate ra hết các possible string. Time complexity sẽ là 2^10*10*3, rồi dùng 1 pass tiếp theo để khử phép *, 1 pass nữa để khử phép +- bằng stack.
Time complexity sẽ là 2^10*10*10*6 chắc chắn vẫn sẽ pass
via theNEXTvoz for iPhone
Ko fence, 2^10 thì ko bị gì về memory đâu, mình làm bên trên vẫn pass đóLàm vậy dễ bị memory limit error.
class Solution {
public int longestSubarray(int[] nums) {
int max = 0;
int maxNum = -1;
int len = 0;
for (int i : nums) {
if (i > maxNum) {
maxNum = i;
len = 1;
max = 1;
}
else if (i == maxNum) {
len++;
max = Math.max(len , max);
}
else len = 0;
}
return max;
}
}
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
max_val = ans = curr = 0
for i in nums:
if max_val < i:
max_val = i
ans = curr = 0
if max_val == i:
curr += 1
else:
curr = 0
ans = max(ans, curr)
return ans
public class Solution
{
public int LongestSubarray(int[] nums)
{
int max = int.MinValue;
for (int i = 0; i < nums.Length; i++)
{
max = Math.Max(max, nums[i]);
}
int result = 0;
int subLength = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] == max)
{
subLength++;
result = Math.Max(result, subLength);
continue;
}
subLength = 0;
}
return result;
}
}
T k để ý constraint của bài này nhưng ý t là làm cách này thì nên để ý đến cái memory limit, bị ăn hành bởi cái đó trong contest nhiều rồi.
Mấy bài này từng ra daily rồi, mà lúc đó trư chưa làm được, may là trư để dành để phục thù chứ ko copMấy bài nay dễ quá, hôm nay tôi sẽ giới thiệu cho anh em 1 thuật toán là sweep line.Python:class Solution: def longestSubarray(self, nums: List[int]) -> int: maxSofar = -inf count = 0 ans = 0 for num in nums: if num > maxSofar: ans = 1 maxSofar = num count = 1 elif num == maxSofar: count += 1 ans = max(ans, count) else: count = 0 return ans
Bài toán là, có 1 cái nhà hàng và list của people, với mỗi ith people thì [start, end] là thời điểm mà người này vào nhà hàng hoặc rời nhà hàng, tính xem có tối đa bao nhiêu người ở trong 1 nhà hàng ở 1 đơn vị thời gian.
Làm xong 2 bài cơm thêm này sẽ học được thuật toán sweep line, thêm kiến thức mới vào đầu phá đảo interview
class Solution:
def longestSubarray(self, nums: List[int]) -> int:
n = len(nums)
max_value = max(nums)
cnt = 0
res = 0
for i in range(n):
if max_value == nums[i]:
cnt += 1
else:
res = max(res , cnt)
cnt = 0
res = max(res , cnt)
return res
bài này có xài tí gì bitmask đâu, biết AND vào thì chỉ có nhỏ đi là dc màk thich bitmaskMã:class Solution: def longestSubarray(self, nums: List[int]) -> int: n = len(nums) max_value = max(nums) cnt = 0 res = 0 for i in range(n): if max_value == nums[i]: cnt += 1 else: res = max(res , cnt) cnt = 0 res = max(res , cnt) return res![]()
class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
int max= -1;
int res=1;
int cnt=0;
for(int num:nums){
max= Math.max(max, num);
}
for(int i =0 ; i <n;i++){
if(nums[i]<max) {
cnt=0;
continue;
}
if(nums[i]==max)
{
cnt++;
}
res=Math.max(res, cnt);
}
return res;
}
}