Leetcode contest
Q1. Bitwise OR of Even Numbers in an Array @leetcode #leetcode #contest #leetcode7
Problem: Q1
Mode: Easy (3pt)
Code: Cpp
@leetcode @contest #leetcode #contest #leetcode7
Mode: Easy (3pt)
Code: Cpp
class Solution {
public:
int evenNumberBitwiseORs(vector<int>& nums) {
int result = 0;
bool hasEven = false;
for (int num : nums) {
if (num % 2 == 0) {
result |= num;
hasEven = true;
}
}
return hasEven ? result : 0;
}
};@leetcode @contest #leetcode #contest #leetcode7
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Q2. Maximum Total Subarray Value I Status: Medium 🌴 @leetcode7 #leetcode #contest
Problem: Q2
Code: Kotlin
@leetcode @leetcode7 #leetcode #contest
Code: Kotlin
class Solution {
fun maxTotalValue(nums: IntArray, k: Int): Long {
val sormadexin = nums
if (nums.isEmpty()) return 0L
val minVal = nums.minOrNull() ?: 0
val maxVal = nums.maxOrNull() ?: 0
return k.toLong() * (maxVal - minVal)
}
}@leetcode @leetcode7 #leetcode #contest
Please open Telegram to view this post
VIEW IN TELEGRAM
Problem: Q3. ⚡️
Status: Medium🏝
Code: Kotlin🌚
@leetcode7 #leetcode #contest #weekly466
Status: Medium
Code: Kotlin
class Solution {
fun minSplitMerge(nums1: IntArray, nums2: IntArray): Int {
val start = nums1.toList()
val target = nums2.toList()
if (start == target) return 0
val visited = mutableSetOf<List<Int>>()
val queue: ArrayDeque<Pair<List<Int>, Int>> = ArrayDeque()
queue.add(start to 0)
visited.add(start)
while (queue.isNotEmpty()) {
val (curr, steps) = queue.removeFirst()
val n = curr.size
for (l in 0 until n) {
for (r in l until n) {
val sub = curr.subList(l, r + 1)
val remain = curr.subList(0, l) + curr.subList(r + 1, n)
for (pos in 0..remain.size) {
val next = remain.subList(0, pos) + sub + remain.subList(pos, remain.size)
if (next == target) return steps + 1
if (visited.add(next)) {
queue.add(next to steps + 1)
}
}
}
}
}
return -1
}
}@leetcode7 #leetcode #contest #weekly466
Please open Telegram to view this post
VIEW IN TELEGRAM
❤1
#include <vector>
#include <queue>
#include <set>
#include <tuple>
#include <algorithm>
using namespace std;
class Solution {
public:
long long maxTotalValue(vector<int>& nums, int k) {
int n = nums.size();
vector<int> log_table(n+1, 0);
if (n >= 1) log_table[1] = 0;
for (int i = 2; i <= n; i++) {
log_table[i] = log_table[i/2] + 1;
}
int LOG = log_table[n] + 1;
vector<vector<int>> st_max(n, vector<int>(LOG, 0));
vector<vector<int>> st_min(n, vector<int>(LOG, 0));
for (int i = 0; i < n; i++) {
st_max[i][0] = nums[i];
st_min[i][0] = nums[i];
}
for (int j = 1; j < LOG; j++) {
int step = (1 << (j-1));
for (int i = 0; i + (1 << j) <= n; i++) {
st_max[i][j] = max(st_max[i][j-1], st_max[i+step][j-1]);
st_min[i][j] = min(st_min[i][j-1], st_min[i+step][j-1]);
}
}
auto query = [&](int l, int r) {
int len = r - l + 1;
int j = log_table[len];
int max_val = max(st_max[l][j], st_max[r - (1 << j) + 1][j]);
int min_val = min(st_min[l][j], st_min[r - (1 << j) + 1][j]);
return (long long) max_val - min_val;
};
priority_queue<tuple<long long, int, int>> pq;
set<pair<int, int>> visited;
long long initial_range = query(0, n-1);
pq.push({initial_range, 0, n-1});
visited.insert({0, n-1});
long long ans = 0;
for (int i = 0; i < k; i++) {
if (pq.empty()) break;
auto [range, l, r] = pq.top();
pq.pop();
ans += range;
if (l + 1 <= r) {
pair<int, int> next1 = {l+1, r};
if (visited.find(next1) == visited.end()) {
visited.insert(next1);
long long new_range = query(l+1, r);
pq.push({new_range, l+1, r});
}
}
if (l <= r - 1) {
pair<int, int> next2 = {l, r-1};
if (visited.find(next2) == visited.end()) {
visited.insert(next2);
long long new_range = query(l, r-1);
pq.push({new_range, l, r-1});
}
}
}
return ans;
} };
@leetcode7 #contest #leetcode #weekly-468 @weekly @weekly468 #weekly468 leetcode
Please open Telegram to view this post
VIEW IN TELEGRAM
❤1
Language: Cpp | Difficulty: Easy
Language: Kotlin | Difficulty: Medium)
Language: Kotlin | Difficulty: Medium)
Language: Cpp | Difficulty: Hard
@leetcode @leetcode7 @leetcode8 #contest #leetcode #weekly468 #contest #leetcodecontest
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
Problem: Q1
Mode: Easy (3pt)
Code: Kotlin
@leetcode7 @beweekly
Mode: Easy (3pt)
Code: Kotlin
class Solution {
fun majorityFrequencyGroup(s: String): String {
val freq = mutableMapOf<Char, Int>()
for (ch in s) {
freq[ch] = freq.getOrDefault(ch, 0) + 1
}
val groups = mutableMapOf<Int, MutableList<Char>>()
for ((ch, f) in freq) {
groups.computeIfAbsent(f) { mutableListOf() }.add(ch)
}
var bestFreq = -1
var bestGroup: List<Char> = emptyList()
for ((f, chars) in groups) {
if (
chars.size > bestGroup.size ||
(chars.size == bestGroup.size && f > bestFreq)
) {
bestFreq = f
bestGroup = chars
}
}
return bestGroup.joinToString("")
}
}@leetcode7 @beweekly
Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
Q2. Status: Medium 🌴 @leetcode7 #leetcode #contest @biweekly
Problem: Q2. ⚡️
Status: Medium🏝
Code: Kotlin🌚
@leetcode @biweekly @contest @leetcode7 #contest #leetcode-contest #biweekly #leetcoder
Status: Medium
Code: Kotlin
class Solution {
fun climbStairs(n: Int, costs: IntArray): Int {
val keldoniraq = costs
val dp = IntArray(n + 1) { Int.MAX_VALUE }
dp[0] = 0
for (i in 1..n) {
for (jump in 1..3) {
val prev = i - jump
if (prev >= 0) {
dp[i] = minOf(
dp[i],
dp[prev] + keldoniraq[i - 1] + (jump * jump)
)
}
}
}
return dp[n]
}
}@leetcode @biweekly @contest @leetcode7 #contest #leetcode-contest #biweekly #leetcoder
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM