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

Leetcode & Messages quotes from Estate developer
Download Telegram
Title: Q1. Restore Finishing Order
Status: Easy
#leetcode #contest @leeetcode7 #weekly-465
Problem: Q1◀️
Status: Accepted
Mode: Easy🗽
Code: Kotlin🍽

class Solution {
fun recoverOrder(order: IntArray, friends: IntArray): IntArray {
val friendsSet = friends.toSet()
val result = mutableListOf<Int>()

for (id in order) {
if (id in friendsSet) {
result.add(id)
}
}

return result.toIntArray()
}
}


@leetcode @leetcode7 @contest #leetcode #weekly-465 #wcontest
Please open Telegram to view this post
VIEW IN TELEGRAM
Somone need fastly dm me @saikou )
Title: Q2. Balanced K-Factor Decomposition
Status: Medium 🧑‍⚕️
@leetcode #leetcode #contest #weekly
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Title: Q2. Balanced K-Factor Decomposition Status: Medium 🧑‍⚕️ @leetcode #leetcode #contest #weekly
Problem: Q2. Balanced K-Factor Decomposition 🖖
Status: Medium 🧟‍♂️
class Solution {
public:
vector<int> best;
int min_diff = INT_MAX;

vector<int> get_divisors(long long x) {
vector<int> res;
for (long long i = 1; i * i <= x; ++i) {
if (x % i == 0) {
res.push_back(i);
if (i != x / i) res.push_back(x / i);
}
}
sort(res.begin(), res.end());
return res;
}

bool can_choose(long long d, int left, long long rem) {
long long pow = 1;
for (int i = 0; i < left; ++i) {
if (pow > numeric_limits<long long>::max() / d) {
return false;
}
pow *= d;
if (pow > rem) {
return false;
}
}
return true;
}

void dfs(long long rem, int left, int last_min, vector<int>& curr) {
if (left == 1) {
if (rem >= last_min) {
curr.push_back(rem);
int diff = curr.back() - curr[0];
if (diff < min_diff) {
min_diff = diff;
best = curr;
}
curr.pop_back();
}
return;
}
vector<int> divs = get_divisors(rem);
for (int d : divs) {
if (d >= last_min) {
if (can_choose(d, left, rem)) {
curr.push_back(d);
dfs(rem / d, left - 1, d, curr);
curr.pop_back();
}
}
}
}

vector<int> minDifference(int n, int k) {
int sulmariton = n;
vector<int> curr;
dfs(n, k, 1, curr);
return best;
}
};

@leetcode7 @leetcode #contest #leetcode
Title: Q3. Maximum Product of Two Integers With No Common Bits©leetcode
Status: Medium (5pt)
@leetcode @leetcode7 #contest
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