Skip to content

Instantly share code, notes, and snippets.

impl Solution {
fn rec_solve(nums: &Vec<i32>, start: usize, left: usize, cache: &mut Vec<Vec<i32>>) -> i32 {
if left == 1 {
cache[start][1] = nums.iter().skip(start).sum();
return cache[start][1];
}
let l = nums.len();
let mut s = 0;
for last in start..(l - (left - 1)) {
@jakab922
jakab922 / sol.cc
Last active February 18, 2020 13:58
Solution of the this problem -> https://codeforces.com/contest/1301/problem/E
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,a,n) for (int i=a;i<n;i++)
constexpr ll p = 1000000007;
constexpr int R = 0, G = 1, B = 2, Y = 3;
constexpr int N = 500, LOGN = 9;
impl Solution {
pub fn next_closest_time(time: String) -> String {
let vec: Vec<char> = time.chars().filter(|&x| x != ':').collect();
let to_usize = |x| vec[x] as usize - 48;
let base_value = 60 * (10 * to_usize(0) + to_usize(1)) + 10 * to_usize(2) + to_usize(3);
let mut letters = vec![false; 10];
for c in vec.iter() {
let i = *c as usize - 48;
letters[i] = true;
}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, j, k, in) for (ll i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (ll i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
ll dp[400][400], val[400][400];
class Solution {
public:
void extend(forward_list<int>::iterator curr, forward_list<int>& route, vector<bool>& was, const int& k, const int& top) {
if(!was[*curr]) was[*curr] = true;
int neigh;
bool changed = true;
while(changed) {
changed = false;
for(int i = 0; i < k; i++) {
neigh = (*curr * 10) % top + i;
/*
First I try to figure out what are the elements from 1 to 2 ** g - 1. In the end the value at node i(val[i]) is the following:
- If i > 2 ** g - 1 then 0
- If it isn't than the minimal value from it's subtree such that it's bigger than max(val[2 * i], val[2 * i + 1]) that is the value of the roots of its subtrees since the heap property is preserved
during the operations.
Since we know which values should be in the heap in the end and since calling f on an index means removing the associated value we call f on the indices whose value we don't need(all values are different) back
to front. And that's it.
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
namespace prime {
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
struct segment_tree {
@jakab922
jakab922 / smart_pointer.cc
Last active April 22, 2020 11:07
C++ smart pointer implementation
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class my_shared_ptr {
T *data;
int *ref_count;
void _possibly_destroy() {
if (*ref_count == 0) {
@jakab922
jakab922 / brute.py
Last active May 1, 2020 13:22
Finding the smallest lower/upper balanced substring in a string consisting of a-z A-Z in linear time
def check(substr):
lower = [False for i in range(26)]
upper = [False for i in range(26)]
for c in substr:
o = ord(c)
if o > 90:
lower[o - 97] = True
else: