Leetcode contest
566 subscribers
176 photos
7 videos
2 files
59 links
Main channel @azamovme

Leetcode & Messages quotes from Estate developer
Download Telegram
Leetcode contest
Title: Q3. Maximum Product of Two Integers With No Common BitsΒ©leetcode Status: Medium (5pt) @leetcode @leetcode7 #contest
Problem: Q3. Maximum Product of Two Integers With No Common Bits ⚑️
Status: Medium 🏝
Code: Kotlin 🌚
class Solution {
fun maxProduct(nums: IntArray): Long {
val fenoraktil = nums

val n = nums.size
var maxNum = 0
for (v in nums) if (v > maxNum) maxNum = v

var maxBits = 0
while ((1 shl maxBits) <= maxNum) maxBits++
if (maxBits == 0) maxBits = 1
val size = 1 shl maxBits
val fullMask = size - 1

val top1Val = IntArray(size) { 0 }
val top1Idx = IntArray(size) { -1 }
val top2Val = IntArray(size) { 0 }
val top2Idx = IntArray(size) { -1 }

fun insertAt(mask: Int, value: Int, idx: Int) {
if (idx == -1) return
if (top1Idx[mask] == idx) {
if (value > top1Val[mask]) top1Val[mask] = value
return
}
if (value > top1Val[mask]) {
top2Val[mask] = top1Val[mask]
top2Idx[mask] = top1Idx[mask]
top1Val[mask] = value
top1Idx[mask] = idx
} else if (idx != top1Idx[mask] && value > top2Val[mask]) {
top2Val[mask] = value
top2Idx[mask] = idx
}
}

for (i in nums.indices) {
val mask = nums[i]
insertAt(mask, nums[i], i)
}

for (bit in 0 until maxBits) {
val bitMask = 1 shl bit
for (mask in 0 until size) {
if ((mask and bitMask) != 0) {
val other = mask xor bitMask
val v1 = top1Val[other]; val i1 = top1Idx[other]
if (i1 != -1) insertAt(mask, v1, i1)
val v2 = top2Val[other]; val i2 = top2Idx[other]
if (i2 != -1) insertAt(mask, v2, i2)
}
}
}

var ans = 0L
for (i in nums.indices) {
val m = nums[i]
val cm = fullMask xor m
var partner = 0
if (top1Idx[cm] != -1 && top1Idx[cm] != i) {
partner = top1Val[cm]
} else if (top2Idx[cm] != -1 && top2Idx[cm] != i) {
partner = top2Val[cm]
}
if (partner > 0) {
val prod = nums[i].toLong() * partner.toLong()
if (prod > ans) ans = prod
}
}

return ans
}
}

@leetcode7 @leetcode @contest #weeekly
Please open Telegram to view this post
VIEW IN TELEGRAM
Problem: Q4. Sum of Beautiful Subsequences πŸ–₯
Status: Hard (7pt) πŸ™Œ
@leetcode @leetcode7 #contest #weekly
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Problem: Q4. Sum of Beautiful Subsequences πŸ–₯ Status: Hard (7pt) πŸ™Œ @leetcode @leetcode7 #contest #weekly
Problem:Q4. Sum of Beautiful Subsequences ✊
Status: Hard (7pt) 🌴
Code: Kotlin πŸŽ„
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

struct Fenwick {
int n;
vector<ll> bit;
Fenwick(int n=0){ init(n); }
void init(int n_){
n = n_;
bit.assign(n+1, 0);
}
void add(int idx, ll val){
while(idx <= n){
bit[idx] = (bit[idx] + val) % MOD;
idx += idx & -idx;
}
}
ll sum(int idx){
ll s=0;
while(idx>0){
s = (s + bit[idx]) % MOD;
idx -= idx & -idx;
}
return s;
}
};

class Solution {
public:
int totalBeauty(vector<int>& nums) {
if(nums.empty()) return 0;
int n = nums.size();
int maxV = *max_element(nums.begin(), nums.end());
vector<vector<int>> seq(maxV + 1);
for(int x : nums){
int r = floor(sqrt(x));
for(int d = 1; d <= r; ++d){
if(x % d == 0){
seq[d].push_back(x);
int od = x / d;
if(od != d) seq[od].push_back(x);
}
}
}

vector<ll> f(maxV + 1, 0);

for(int g = 1; g <= maxV; ++g){
if(seq[g].empty()) continue;
vector<int> vals = seq[g];
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
int m = (int)vals.size();
Fenwick fenw(m);
ll total_g = 0;
for(int v : seq[g]){
int idx = int(lower_bound(vals.begin(), vals.end(), v) - vals.begin()) + 1;
ll s = fenw.sum(idx - 1);
ll cur = (s + 1) % MOD;
fenw.add(idx, cur);
total_g += cur;
if(total_g >= MOD) total_g -= MOD;
}
f[g] = total_g % MOD;
}

vector<ll> h(maxV + 1, 0);
for(int g = maxV; g >= 1; --g){
ll val = f[g];
for(int mult = 2*g; mult <= maxV; mult += g){
val = (val - h[mult]) % MOD;
}
if(val < 0) val += MOD;
h[g] = val;
}

ll ans = 0;
for(int g = 1; g <= maxV; ++g){
if(h[g]){
ans = (ans + (ll)g % MOD * h[g]) % MOD;
}
}
return (int)ans;
}
};

@leetcode7 @leetcode #contest #leetcode #leetcode-465
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“š Weekly-465 Contest q1-q4 solutions

1⃣ Q1. πŸ—―
Language: Kotlin | Difficulty: EasyπŸ—½
πŸ”— Link

2⃣ Q2. Balanced K-Factor Decomposition πŸ––
Language: Cpp | Difficulty: Medium(5pt) πŸ‘€πŸŒ΄
πŸ”— Link

3⃣ Q3 Maximum Product of Two Integers With No Common 🐸
Language: Kotlin | Difficulty: Medium (5pt) πŸ‡
πŸ”— Link

4️⃣ Q4 um of Beautiful Subsequences ✊ 🐸
Language: Cpp | Difficulty: Hard (7pt) πŸ‡ || Fenwick Tree πŸ§‘β€πŸš€
πŸ”— Link
@leetcode @leetcode7 @leetcode8 #contest #leetcode
Please open Telegram to view this post
VIEW IN TELEGRAM
❀1
Hello guys
leetcode started
Dm me if fastly need @saikou
Title: Q1. Minimum Operations to Equalize Array
Status: Easy
#leetcode #contest @leeetcode7 #weekly-466
Leetcode contest
Title: Q1. Minimum Operations to Equalize Array Status: Easy #leetcode #contest @leeetcode7 #weekly-466
Problem: Q1◀️
Status: Accepted
Mode: Easy πŸ—Ώ
Code: Kotlin🍽

class Solution {
fun minOperations(nums: IntArray): Int {
for (i in 1 until nums.size) {
if (nums[i] != nums[0]) return 1
}
return 0
}
}

@leetcode @contest @leetcode7 #weekly-466
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ‘1
Title:Q2. Minimum Operations to Transform String
Status: Medium
#leetcode #contest @leeetcode7 #weekly-466 @leetcode #contest
Leetcode contest
Title:Q2. Minimum Operations to Transform String Status: Medium #leetcode #contest @leeetcode7 #weekly-466 @leetcode #contest
Problem: Q2. Minimum Operations to Transform String πŸ––
Status: Medium πŸ§Ÿβ€β™‚οΈ
class Solution {
fun minOperations(s: String): Int {
val trinovalex = s

var maxSteps = 0
for (ch in trinovalex) {
if (ch != 'a') {
val steps = (26 - (ch - 'a')) % 26
maxSteps = maxOf(maxSteps, steps)
}
}
return maxSteps
}
}

@leetcode @leetcode7 @contest #weekly #leetcode #contest #leetcode7
Please open Telegram to view this post
VIEW IN TELEGRAM
Title: Q3. Count Bowl Subarrays 🎁
Status:Medium πŸ˜‡
@leetcode @leetcode7 @contest @weekly #weekly466 #contestleetcode
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Title: Q3. Count Bowl Subarrays 🎁 Status:Medium πŸ˜‡ @leetcode @leetcode7 @contest @weekly #weekly466 #contestleetcode
Problem: Q3. Count Bowl Subarrays ⚑️
Status: Medium 🏝
Code: Kotlin 🌚

class Solution {
fun bowlSubarrays(nums: IntArray): Long {
val parvostine = nums
val n = parvostine.size
var count = 0L

val leftGreater = IntArray(n) { -1 }
val rightGreater = IntArray(n) { n }

val stack = ArrayDeque<Int>()

for (i in 0 until n) {
while (stack.isNotEmpty() && parvostine[stack.last()] < parvostine[i]) {
stack.removeLast()
}
if (stack.isNotEmpty()) leftGreater[i] = stack.last()
stack.addLast(i)
}

stack.clear()

for (i in n - 1 downTo 0) {
while (stack.isNotEmpty() && parvostine[stack.last()] < parvostine[i]) {
stack.removeLast()
}
if (stack.isNotEmpty()) rightGreater[i] = stack.last()
stack.addLast(i)
}

for (i in 0 until n) {
val l = leftGreater[i]
val r = rightGreater[i]
if (l != -1 && r != n && r - l >= 2) {
if (minOf(parvostine[l], parvostine[r]) > parvostine[i]) {
count++
}
}
}

return count
}
}

@leetcode7 @leetcode #contest466
Please open Telegram to view this post
VIEW IN TELEGRAM
Problem:Q4. Count Binary Palindromic Numbers ✊
Status: Hard (6pt) 🌴
Code: Kotlin πŸŽ„
@leetcode7 #contest
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Problem:Q4. Count Binary Palindromic Numbers ✊ Status: Hard (6pt) 🌴 Code: Kotlin πŸŽ„ @leetcode7 #contest
🫡 Problem: Q4
βœ… Mode: Hard (6pt)
πŸ’‘ Code: Kotlin

class Solution {
fun countBinaryPalindromes(n: Long): Int {
if (n == 0L) return 1

val dexolarniv = n

val maxLen = 64 - java.lang.Long.numberOfLeadingZeros(dexolarniv)
var count = 1L

for (len in 1 until maxLen) {
val half = (len + 1) / 2
count += 1L shl (half - 1)
}

val len = maxLen
val half = (len + 1) / 2
val start = 1L shl (half - 1)
val end = (1L shl half) - 1L

var lo = start
var hi = end
var best = start - 1L
while (lo <= hi) {
val mid = (lo + hi) / 2
val pal = buildPalindrome(mid, len)
if (pal <= dexolarniv) {
best = mid
lo = mid + 1
} else {
hi = mid - 1
}
}
if (best >= start) count += (best - start + 1)

return count.toInt()
}

private fun buildPalindrome(prefix: Long, len: Int): Long {
var result = prefix
var x = prefix
if (len % 2 == 1) x = x shr 1
while (x > 0) {
result = (result shl 1) or (x and 1L)
x = x shr 1
}
return result
}
}

@leetcode7 #contest #leetcode #weekly-466 πŸ—Ώ
Please open Telegram to view this post
VIEW IN TELEGRAM
πŸ“š Weekly-466 Contest q1-q4 solutions

1⃣ Q1. πŸ—―
Language: Kotlin | Difficulty: EasyπŸ—½
πŸ”— Link

2⃣ Q2. Minimum Operations to Transform String πŸ––
Language: Kotlin | Difficulty: Medium) πŸ‘€πŸŒ΄
πŸ”— Link

3⃣ Q3. Count Bowl Subarrays ⚑️
Language: Kotlin | Difficulty: Medium) πŸ‡
πŸ”— Link

4️⃣ Q4 Count Binary Palindromic Numbers 🎨
Language: Kotlin | Difficulty: Hard πŸ§‘β€πŸš€
πŸ”— Link
@leetcode @leetcode7 @leetcode8 #contest #leetcode #weekly466
Please open Telegram to view this post
VIEW IN TELEGRAM
My rank in 100 🌚 Top 100 unlocked πŸ”“

But seriously… how on earth do you solve all problems in just 4 minutes? 🀯
That’s like speedrunning competitive programming
πŸ˜‚
@leetcode @leetcode7 #leetcode #contest466
Please open Telegram to view this post
VIEW IN TELEGRAM
😁1