- Kernel -- software that manages the system by providing access to hardware and other resources (memory, network stack, scheduling cpu, etc). Runs in priveleged CPU mode -- kernel mode. It uses kernel-space stack.
- Process -- an OS abstraction for running programs. Runs in user mode with access to kernel mode via system calls. It consists of: memory address space, file descriptors, thread stacks, registers. Process uses user-space stacks. For example, syscalls use a s kernel exception stack associated with each thread.
Solution for https://leetcode.com/problems/shortest-palindrome/submissions/
everyone is writing KMP and Manacher, I would just write a simple hashing algorithm:
const int p = 31;
const int M = 1e9 + 7; // I prefer always use mod prime even if we don't need to
// find a reverse element. Just for consistancy, it is faster to write one standard way
// every time instead of creating something ad hoc
string shortestPalindrome(string s) {
const int n = s.size();
In order to speed up your linking stage, you can use a linker that takes advantage of multiple CPU cores.
Such linkers are lld and mold.
mold is considered more cutting edge, but it works for me.
Just add
[build]
rustflags = ["-C", "link-arg=-fuse-ld=mold"]
use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId}; | |
use itertools::Itertools; | |
use rand::Rng; | |
type T = u64; | |
fn sum_1(data: &Vec<T>) -> (T, T, T) | |
{ | |
let mut sum: (T, T, T) = (T::default(), T::default(), T::default()); | |
for (x,y,z) in data.iter().tuples() { | |
sum.0 += x; |
# remap prefix to Control + q | |
set -g prefix C-q | |
unbind C-b | |
bind C-q send-prefix | |
# force a reload of the config file | |
unbind r | |
bind r source-file ~/.tmux.conf | |
# quick pane cycling |
It works for rust and for C++. The difference is that for rust you need to specify rustfmt
and for C++ clang-format -i
.
- Add file
pre-commit
to the folder.githooks
in your repo with the following text:
#!/bin/bash
exe=$(which rustfmt)
if [ -n "$exe" ]
Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.
The basic idea is to use map : start -> end
where end
is not inclusive.
Invariant is that this map is always disjoint.
class SummaryRanges {
map<int, int> m; // start to end, [x,y)
Quite interesing problem. We need to minize function of sum_l sum_r f(l,r)
, where f(l,r) = sum_i ai * bi, i<-[l,r]
.
So first of all forget about the fact we have two vectors, try to rewrite sum of all the sum of subarrys in one sum.
Should have a form sum_i ai * coef(i, n)
.
How many subarrays are in the array of length n? The same is how many ordered pairs <i,j>
?
For each i
there (n - i)
subarrays starting at this i
.
The total number is S(n) = sum_i (n - i) = n^2 - n(n-1)/2 = n(n+1)/2
.
// further readings: http://forskning.diku.dk/PATH05/GoldbergSlides.pdf | |
// problem itself https://leetcode.com/problems/minimum-moves-to-move-a-box-to-their-target-location/ | |
class Solution { | |
public: | |
using State = tuple<int,int,int,int>; // xbox, ybox, xme, yme | |
static constexpr int x[] = {1,-1,0,0}; | |
static constexpr int y[] = {0,0, 1, -1}; | |
bool impossible(vector<vector<char>>& grid, const pair<int,int>& pos) { | |
if (pos.first < 0 || pos.first >= grid.size() || |