Skip to content

Instantly share code, notes, and snippets.

View razimantv's full-sized avatar

Raziman T V razimantv

View GitHub Profile
@razimantv
razimantv / paths.py
Created December 3, 2024 13:02
Super-path that takes all grid cells to a sink cell
neigh = {
'R': (0, 1, 'L'), 'L': (0, -1, 'R'), 'D': (1, 0, 'U'), 'U': (-1, 0, 'D')
}
# Type of a cell
# -1: out of bounds. Otherwise the value of the cell
def cell_type(grid: list[list[int]], i: int, j: int) -> int:
if 0 <= i < len(grid) and 0 <= j < len(grid[0]):
return grid[i][j]
"""
Given a string s, count the number of triplets i <= j < k such that
the substrings s[i:j] and s[j+1:k] have the same set of unique characters.
Algorithm:
Scan the array, remembering the last time each character appeared
Use this information to count the number of times each set of characters
appear in substrings ending at each position (max 26 such sets)
Repeat for reversed string to count for substrings starting at each position
For each j, scan through the sets for substrings ending at j and starting at j+1
If the sets are the same, the product of their counts is the number of
@razimantv
razimantv / dynamic.cpp
Created November 3, 2024 17:16
Dynamic connectivity for the Codeforces problem 100551A (Connect and disconnect)
// Dynamic connectivity on a graph
// Solution method: Offline processing + segment tree + undo on union-find
// Details:
// First, scan through the input to find ranges in which an edge is active
// Then insert these ranges into the nodes of a segment tree
// If we merge all the edges in the nodes on a path from root to a leaf,
// we get the number of connected components at that point
// We find the component count for all leaves efficiently by adding undo
// operations to the union-find data structure
@razimantv
razimantv / snakesandladders.py
Created July 8, 2024 18:20
Expected rolls to finish the Snakes and Ladders game
# Find the expected number of steps to solve snakes and ladders
# Author: Raziman T V (github.com/razimantv)
import numpy as np
N = 100
snakes_ladders = {
2: 23, 6: 45, 20: 59, 43: 17, 50: 5, 52: 72, 56: 8, 57: 96, 71: 92,
73: 15, 84: 63, 87: 49, 98: 40
}
@razimantv
razimantv / strikerates.ipynb
Created November 16, 2023 00:04
Strike rate statistics
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@razimantv
razimantv / superman.cpp
Last active November 12, 2023 14:35
Superman Diwali solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
@razimantv
razimantv / splitgame.cpp
Created September 25, 2023 09:50
Split game solver
/**
* Two-Player Split Game Solver
*
* This C++ program solves a two-player game played with two hands
*
* - Two players take turns, starting with two fingers out on each hand
* - Each turn, a player can add the number of fingers on one of their hands to
* that of their opponent (addition wraps around after 5)
* - If the numbers on a hand become 5, it resets to 0
* - If one hand is empty and the other has an even number, it can be split
@razimantv
razimantv / primes.cpp
Last active October 12, 2020 02:50
Comparing prime generation the old way and the C++20 way
#include <chrono>
#include <iostream>
#include <numeric>
#include <ranges>
#include <vector>
const int N = 10000000;
int main() {
auto t1 = std::chrono::steady_clock::now();
@razimantv
razimantv / towers.cpp
Created April 9, 2020 20:04
Solution to the towers puzzle
/******************************************************************************
* File: towers.cpp
*
* Author: Raziman T V
* Created: 04/09/20
* Description: Solving the Towers puzzle
*****************************************************************************/
#include <algorithm>
#include <cassert>
@razimantv
razimantv / Randomstrip.ipynb
Created February 18, 2019 19:05
Generate random points in an angle range around a given latitude and longitude
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.