Reactions:
LmaoSuVuong
thảo luận Leetcode mỗi ngày
Trang 6/347
JavaScript:
function isArraySpecial(nums: number[], queries: number[][]): boolean[] {
const numsDiv = []
let curStart = 0
for (let i = 0; i < nums.length; i++) {
if ((nums[i] + nums[i + 1]) % 2 === 0 || i === nums.length - 1) {
numsDiv.push([curStart, i])
curStart = i + 1
}
}
return queries.map(q => {
const l = findSub(q[0], numsDiv)
const r = findSub(q[1], numsDiv)
return l === r
})
};
function findSub(find: number, arr: number[][]) {
let l = 0
let r = arr.length - 1
while (l <= r) {
const mid = Math.floor((l + r) / 2)
if (arr[mid][0] <= find && find <= arr[mid][1]) {
return mid
}
if (find < arr[mid][0]) {
r = mid - 1
} else {
l = mid + 1
}
}
return -1
}
Python:
class Solution:
def isArraySpecial(self, nums: List[int], queries: List[List[int]]) -> List[bool]:
pairs = [a%2 == b%2 for a,b in pairwise(nums)]
pref = list(accumulate(pairs,initial = 0))
return list(map(lambda x: pref[x[0]]==pref[x[1]], queries))
Java:
class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
boolean[] ans = new boolean[queries.length];
List<Integer> violate = new ArrayList<>();
for (int i = 1; i < nums.length; i++) {
if (nums[i] % 2 == nums[i - 1] % 2) violate.add(i);
}
for (int i = 0; i < queries.length; i++) {
int from = queries[i][0];
int to = queries[i][1];
if (binSearch(from + 1, to, violate)) ans[i] = false;
else ans[i] = true;
}
return ans;
}
private boolean binSearch(int l, int r, List<Integer> arr) {
int i = 0, j = arr.size() - 1;
while (i <= j) {
int m = (j + i) / 2;
int sub = arr.get(m);
if (sub < l) i = m + 1;
else if (sub > r) j = m - 1;
else return true;
}
return false;
}
}
Swift:
class Solution {
func isArraySpecial(_ nums: [Int], _ queries: [[Int]]) -> [Bool] {
var (lastP, andPre) = (0, 0)
var properties:[Int] = []
for num in nums {
let andNum = num & 1
if andNum != andPre {
lastP += 1
}
properties.append(lastP)
andPre = andNum
}
return queries.map {
(properties[$0[1]] - properties[$0[0]]) == ($0[1] - $0[0])
}
}
}
Java:
class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
boolean[] result = new boolean[queries.length];
if (nums.length == 1)
{
Arrays.fill(result, true);
return result;
}
int start = 0;
int end = start + 1;
while (end <= nums.length)
{
if (start == nums.length - 1)
{
nums[start] = start;
break;
}
int temp = end - 1;
if ((nums[temp] + nums[end]) % 2 == 0)
{
for (int i = start; i < end; i++)
nums[i] = end - 1;
start = end;
end++;
} else
{
if (end == nums.length - 1)
{
for(int i =start; i <= end; i++)
nums[i] = end;
break;
} else
end++;
}
}
for (int i = 0; i < queries.length; i++)
{
if (nums[queries[i][0]] < queries[i][1])
result[i] = false;
else
result[i] = true;
}
return result;
}
}
xin chào cả nhà,mình mem mới
Đăng kí khoá học udemy bạn nhé
Tìm thầy lmaosuvuong xong nhập code LMAO02 để được học miễn phí
via theNEXTvoz for iPhone
Reactions:
LmaoSuVuong
C++:
class Solution {
public:
vector<bool> isArraySpecial(vector<int>& a, vector<vector<int>>& q) {
vector<int> checker(a.size(), 0);
for (int i=1; i<a.size(); i++) {
checker[i]=checker[i-1];
if ((a[i]+a[i-1])%2==0) {
checker[i]++;
}
}
vector<bool> res(q.size(), 1);
for (int i=0; i<q.size(); i++) {
if (checker[q[i][1]]>checker[q[i][0]]) res[i]=0;
}
return res;
}
};
Reactions:
lilleboybk
Java:
class Solution {
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
boolean[] result = new boolean[queries.length];
List<Integer> vio = new ArrayList<>();
for(int i = 1; i<nums.length;i++){
if(nums[i] % 2 == nums[i - 1] % 2) vio.add(i);
}
for(int i = 0;i<queries.length;i++){
result[i] = binarySearch(vio,queries[i][0] + 1, queries[i][1]);
}
return result;
}
public boolean binarySearch(List<Integer> vio, int from, int to){
int left = 0;
int right = vio.size() - 1;
while(left <= right){
int mid = (left + right) / 2;
int index = vio.get(mid);
if(index < from){
left = mid + 1;
}else if (index > to){
right = mid - 1;
}else{
return false;
}
}
return true;
}
}
Swift:
func isArraySpecial(_ nums: [Int], _ queries: [[Int]]) -> [Bool] {
guard nums.count > 1 else {
return Array(repeating: true, count: queries.count)
}
var checkArr = Array(repeating: 0, count: nums.count)
for i in 1..<nums.count {
if nums[i] % 2 == nums[i - 1] % 2 {
checkArr[i] = i
} else {
checkArr[i] = checkArr[i - 1]
}
}
return queries.map { query in
checkArr[query[0]] == checkArr[query[1]]
}
}
Thêm dòng check count vào thế là từ 50% lên 100%

Reactions:
bkhoang
C++:
class Solution {
public:
vector<bool> isArraySpecial(vector<int> const &nums, vector<vector<int>> const &queries) {
vector<int> prefix(nums.size(), 0);
for (int i = 0; i < nums.size() - 1; ++i)
prefix[i + 1] = prefix[i] + 1 - ((nums[i] ^ nums[i + 1]) & 1);
vector<bool> res; res.reserve(queries.size());
for (auto const& q : queries)
res.push_back(prefix[q[0]] == prefix[q[1]]);
return res;
}
};
Java:
class Solution {
fun isArraySpecial(nums: IntArray, queries: Array<IntArray>): BooleanArray {
val prefixCnt = IntArray(nums.size)
val suffixCnt = IntArray(nums.size)
for (i in nums.indices) {
prefixCnt[i] = if (i == 0 || nums[i].isEven != nums[i - 1].isEven) {
prefixCnt.getOrElse(i - 1) { 0 } + 1
} else {
prefixCnt[i - 1]
}
val j = nums.lastIndex - i
suffixCnt[j] = if (j == nums.lastIndex || nums[j].isEven != nums[j + 1].isEven) {
suffixCnt.getOrElse(j + 1) { 0 } + 1
} else {
suffixCnt[j + 1]
}
}
return BooleanArray(queries.size) { idx ->
val (l, r) = queries[idx]
val cnt = r - l + 1
prefixCnt[r] - prefixCnt.getOrElse(l - 1) { 0 } == cnt
|| prefixCnt[r] - prefixCnt[l] + 1 == cnt
|| suffixCnt[l] - suffixCnt.getOrElse(r + 1) { 0 } == cnt
|| suffixCnt[l] - suffixCnt[r] + 1 == cnt
}
}
private val Int.isEven get() = this % 2 == 0
}
C++:
class Solution {
public:
vector<bool> isArraySpecial(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
vector<int> prefixSum(n, 0);
prefixSum[0] = 0;
for (int i = 1; i < n; i++) {
if ((nums[i] + nums[i - 1]) % 2 == 0) {
prefixSum[i] = prefixSum[i - 1] + 1;
} else {
prefixSum[i] = prefixSum[i - 1];
}
}
int m = queries.size();
vector<bool> ans(m, 0);
for (int i = 0; i < m; i++) {
int from = queries[i][0];
int to = queries[i][1];
if (prefixSum[to] - prefixSum[from] == 0) {
ans[i] = true;
} else ans[i] = false;
}
return ans;
}
};
JavaScript:
var isArraySpecial = function(nums, queries) {
const n = nums.length;
const validRange = [];
let start = 0;
for(let i = 1; i < n; i++){
if((nums[i] - nums[i-1]) % 2 === 0){
validRange.push([start, i - 1]);
start = i;
}
}
validRange.push([start, n - 1]);
const bs = (num) => {
let left = 0;
let right = validRange.length - 1;
while(left <= right){
const mid = left + Math.floor((right - left)/2);
if(num >= validRange[mid][0] && num <= validRange[mid][1]){
return mid;
}
else if(num < validRange[mid][0]){
right = mid - 1;
}
else left = mid + 1;
}
return -1;
}
return queries.map(([from, to])=>{
return bs(from) === bs(to);
})
};
Chấm cho có động lực 

Python:
class Solution:
def maximumLength(self, s: str) -> int:
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start == 0:
return count
count+=1
result, temp = -1, s[0]
for i in range(1, len(s)):
if len(temp) > result and occurrences(s, temp) >= 3:
result = len(temp)
if temp[-1] != s[i]:
temp = s[i]
else:
temp += s[i]
return result
Python:
class Solution:
def maximumLength(self, s: str) -> int:
n = len(s)
def findTarget(target):
count = 0
maxLength = 0
freq = defaultdict(int)
for current in s:
if current == target:
count += 1
maxLength = max(maxLength, count)
else:
freq[count] += 1
count = 0
freq[count] += 1
if freq[maxLength] >= 3:
return maxLength
elif freq[maxLength] == 2 or freq[maxLength - 1] > 0:
return maxLength - 1
return maxLength - 2
ans = -1
for i in range(26):
ans = max(ans, findTarget(chr(i + ord('a'))))
return -1 if ans == 0 else ans
Reactions:
LmaoSuVuong, bkhoang and nchhnchh
C++:
class Solution {
public:
bool checkSpecial(string s) {
for (int i=0; i<s.size()-1; i++) {
if (s[i]!=s[i+1]) return false;
}
return true;
}
int maximumLength(string s) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
map<string, int> count;
for (int len=1; len<s.size(); len++) {
for (int i=0; i<=s.size()-len; i++) {
if (checkSpecial(s.substr(i, len))) count[s.substr(i, len)]++;
}
}
int res=-1;
for (auto ele: count) {
if (ele.second>=3) {
res=max(res, (int)(ele.first.size()));
}
}
return res;
}
};
Reactions:
lilleboybk
O(n) mà chạy chậm vl
JavaScript:
function maximumLength(s: string): number {
let prev = s[0], n = s.length, l = 1, res = -1;
const arr = new Array(26).fill(null).map(e => Array(n + 1).fill(0));
const D = (a, b) => a.charCodeAt(0) - b.charCodeAt(0);
arr[D(prev, 'a')][1] = 1;
for (let i = 1; i < n; i++) {
if (s[i] === prev) l++, arr[D(s[i], 'a')][l]++
else prev = s[i], l = 1, arr[D(s[i], 'a')][1]++;
}
for (let i = 0; i < 26; i++) {
for (let j = n - 1; j >= 1; j--) {
arr[i][j]+= arr[i][j + 1];
if (arr[i][j] >= 3) {
res = Math.max(res, j);
break;
}
}
}
return res;
};
JavaScript:
function maximumLength(s: string): number {
const hashs = {}
let curSub = s[0]
for (let i = 0; i < s.length; i++) {
if (i === s.length - 1 || s[i] !== s[i + 1]) {
for (let j = 0; j < curSub.length; j++) {
hashs[s[i].repeat(j + 1)] = (hashs[s[i].repeat(j + 1)] || 0) + curSub.length - j
}
curSub = s[i + 1] || ''
} else {
curSub += s[i + 1]
}
}
let result = -1
const keys = Object.keys(hashs)
for (let i = 0; i < keys.length; i++) {
if (hashs[keys[i]] >= 3) {
result = Math.max(result, keys[i].length)
}
}
return result
};
.
. Cứ gặp mấy bài này tự dưng não nó teo lại đình công đòi nghỉ hưu