Please open Telegram to view this post
VIEW IN TELEGRAM
Leetcode contest
We have something for tomarrow :⌨️
@azamovme if this channel reaches 250 subscribers i send all of contest answers )
import java.util.*;
class Solution {
public int[][] sortMatrix(int[][] grid) {
int n = grid.length;
// Sort the bottom-left triangle diagonals (non-increasing order)
for (int d = 0; d < n; d++) {
List<Integer> diagonal = new ArrayList<>();
for (int i = d, j = 0; i < n && j < n; i++, j++) {
diagonal.add(grid[i][j]);
}
Collections.sort(diagonal, Collections.reverseOrder());
int index = 0;
for (int i = d, j = 0; i < n && j < n; i++, j++) {
grid[i][j] = diagonal.get(index++);
}
}
// Sort the top-right triangle diagonals (non-decreasing order)
for (int d = 1; d < n; d++) {
List<Integer> diagonal = new ArrayList<>();
for (int i = 0, j = d; i < n && j < n; i++, j++) {
diagonal.add(grid[i][j]);
}
Collections.sort(diagonal);
int index = 0;
for (int i = 0, j = d; i < n && j < n; i++, j++) {
grid[i][j] = diagonal.get(index++);
}
}
return grid;
}
}👍3
import java.util.*;
class Solution {
public int[] assignElements(int[] groups, int[] elements) {
int n = groups.length;
int m = elements.length;
int[] assigned = new int[n];
// Preprocess elements: store the smallest index for each unique element value
Map<Integer, Integer> elementMap = new HashMap<>();
for (int j = 0; j < m; j++) {
if (!elementMap.containsKey(elements[j])) {
elementMap.put(elements[j], j);
}
}
// Iterate over each group
for (int i = 0; i < n; i++) {
int groupSize = groups[i];
int minIndex = Integer.MAX_VALUE;
// Find all divisors of groupSize and check if they exist in the elementMap
for (int d = 1; d * d <= groupSize; d++) {
if (groupSize % d == 0) {
// Check divisor d
if (elementMap.containsKey(d)) {
minIndex = Math.min(minIndex, elementMap.get(d));
}
// Check divisor groupSize / d
if (d != groupSize / d && elementMap.containsKey(groupSize / d)) {
minIndex = Math.min(minIndex, elementMap.get(groupSize / d));
}
}
}
// Assign the smallest index or -1 if no divisor found
assigned[i] = (minIndex != Integer.MAX_VALUE) ? minIndex : -1;
}
return assigned;
}
}©leetcode
class Solution {
public int[] assignElements(int[] groups, int[] elements) {
int n = groups.length;
int m = elements.length;
int[] assigned = new int[n];
// Preprocess elements: store the smallest index for each unique element value
Map<Integer, Integer> elementMap = new HashMap<>();
for (int j = 0; j < m; j++) {
if (!elementMap.containsKey(elements[j])) {
elementMap.put(elements[j], j);
}
}
// Iterate over each group
for (int i = 0; i < n; i++) {
int groupSize = groups[i];
int minIndex = Integer.MAX_VALUE;
// Find all divisors of groupSize and check if they exist in the elementMap
for (int d = 1; d * d <= groupSize; d++) {
if (groupSize % d == 0) {
// Check divisor d
if (elementMap.containsKey(d)) {
minIndex = Math.min(minIndex, elementMap.get(d));
}
// Check divisor groupSize / d
if (d != groupSize / d && elementMap.containsKey(groupSize / d)) {
minIndex = Math.min(minIndex, elementMap.get(groupSize / d));
}
}
}
// Assign the smallest index or -1 if no divisor found
assigned[i] = (minIndex != Integer.MAX_VALUE) ? minIndex : -1;
}
return assigned;
}
}©leetcode
👍4
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
long long modExp(long long base, long long exp, long long modVal) {
long long result = 1;
base %= modVal;
while(exp > 0){
if(exp & 1)
result = (result * base) % modVal;
base = (base * base) % modVal;
exp >>= 1;
}
return result;
}
long long countSubstrings(string s) {
int n = s.size();
vector<int> pre3(n+1, 0), pre9(n+1, 0);
pre3[0] = 0;
pre9[0] = 0;
for (int i = 0; i < n; i++) {
int digit = s[i]-'0';
pre3[i+1] = (pre3[i] + digit) % 3;
pre9[i+1] = (pre9[i] + digit) % 9;
}
vector<int> P7(n+1, 0);
P7[0] = 0;
for (int i = 0; i < n; i++) {
int digit = s[i]-'0';
P7[i+1] = (P7[i]*10 + digit) % 7;
}
vector<int> pow7(n+1, 0);
pow7[0] = 1;
for (int i = 1; i <= n; i++) {
pow7[i] = (pow7[i-1] * 10) % 7;
}
vector<int> invPow7(n+1, 0);
for (int i = 0; i <= n; i++) {
invPow7[i] = (int) modExp(pow7[i], 5, 7);
}
vector<int> Q(n+1, 0);
for (int i = 0; i <= n; i++) {
Q[i] = (P7[i] * invPow7[i]) % 7;
}
vector<int> f3(3, 0);
vector<int> f9(9, 0);
vector<int> f7(7, 0);
long long ans = 0;
for (int j = 0; j < n; j++) {
int d = s[j]-'0';
long long contr = 0;
if(d == 0) {
contr = 0;
}
else if(d == 1 d == 2 d == 5) { // TO‘G‘RI YOZILISH
contr = 1LL + j;
}
else if(d == 3 || d == 6) {
contr = 1LL + f3[ pre3[j] ];
}
else if(d == 9) {
contr = 1LL + f9[ pre9[j] ];
}
else if(d == 4) {
if(j >= 1 && ((s[j-1]-'0') % 2 == 0))
contr = 1LL + j;
else
contr = 1LL;
}
else if(d == 7) {
contr = 1LL + f7[ Q[j] ];
}
else if(d == 8) {
long long multi = 0;
if(j == 0) {
multi = 0;
}
else if(j == 1) {
multi = (((s[0]-'0') % 4) == 0 ? 1 : 0);
}
else {
if (((s[j-1]-'0') % 4) == 0)
multi += 1;
if ((((s[j-2]-'0')*10 + (s[j-1]-'0')) % 4) == 0)
multi += (j - 1);
}
contr = 1LL + multi;
}
ans += contr;
f3[ pre3[j] ]++;
f9[ pre9[j] ]++;
f7[ Q[j] ]++;
}
return ans;
}
};
©leetcode
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
class Solution {
public:
long long modExp(long long base, long long exp, long long modVal) {
long long result = 1;
base %= modVal;
while(exp > 0){
if(exp & 1)
result = (result * base) % modVal;
base = (base * base) % modVal;
exp >>= 1;
}
return result;
}
long long countSubstrings(string s) {
int n = s.size();
vector<int> pre3(n+1, 0), pre9(n+1, 0);
pre3[0] = 0;
pre9[0] = 0;
for (int i = 0; i < n; i++) {
int digit = s[i]-'0';
pre3[i+1] = (pre3[i] + digit) % 3;
pre9[i+1] = (pre9[i] + digit) % 9;
}
vector<int> P7(n+1, 0);
P7[0] = 0;
for (int i = 0; i < n; i++) {
int digit = s[i]-'0';
P7[i+1] = (P7[i]*10 + digit) % 7;
}
vector<int> pow7(n+1, 0);
pow7[0] = 1;
for (int i = 1; i <= n; i++) {
pow7[i] = (pow7[i-1] * 10) % 7;
}
vector<int> invPow7(n+1, 0);
for (int i = 0; i <= n; i++) {
invPow7[i] = (int) modExp(pow7[i], 5, 7);
}
vector<int> Q(n+1, 0);
for (int i = 0; i <= n; i++) {
Q[i] = (P7[i] * invPow7[i]) % 7;
}
vector<int> f3(3, 0);
vector<int> f9(9, 0);
vector<int> f7(7, 0);
long long ans = 0;
for (int j = 0; j < n; j++) {
int d = s[j]-'0';
long long contr = 0;
if(d == 0) {
contr = 0;
}
else if(d == 1
contr = 1LL + j;
}
else if(d == 3 || d == 6) {
contr = 1LL + f3[ pre3[j] ];
}
else if(d == 9) {
contr = 1LL + f9[ pre9[j] ];
}
else if(d == 4) {
if(j >= 1 && ((s[j-1]-'0') % 2 == 0))
contr = 1LL + j;
else
contr = 1LL;
}
else if(d == 7) {
contr = 1LL + f7[ Q[j] ];
}
else if(d == 8) {
long long multi = 0;
if(j == 0) {
multi = 0;
}
else if(j == 1) {
multi = (((s[0]-'0') % 4) == 0 ? 1 : 0);
}
else {
if (((s[j-1]-'0') % 4) == 0)
multi += 1;
if ((((s[j-2]-'0')*10 + (s[j-1]-'0')) % 4) == 0)
multi += (j - 1);
}
contr = 1LL + multi;
}
ans += contr;
f3[ pre3[j] ]++;
f9[ pre9[j] ]++;
f7[ Q[j] ]++;
}
return ans;
}
};
©leetcode
👍1
Leetcode contest pinned «@azamovme if this channel reaches 250 subscribers i send all of contest answers )»