Random Wits

Life is too short for a diary

$ random wits
├── tags

├── bookshelf

├── resources

├── quotes

├── habits

└── about




Posts Tagged “java”

Thu 04 Nov 2021

Ternary String solution Codeforces - 1354B

Ternary string is an interesting problem that could be solved using two pointers techniques. It's a convenient way to keep track of multiple indices. This helps in making decisions based on two values.

You are given a string s such that each its character is either 1, 2, or 3. You have to choose the shortest contiguous substring of s such that it contains each of these three characters at least once. A contiguous substring of string s is a string that can be obtained from s by removing some (possibly zero) characters from the beginning of s and some (possibly zero) characters from the end of s...


Continue reading → codeforces two pointers java python



Tue 02 Nov 2021

Coin Change solution leetcode

It's one of the most popular questions on leetcode that seems very easy at first. Coin change is a classic dynamic programming problem. I will proceed with an obvious (albeit wrong) solution and subsequently proceed to an efficient correct solution.

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. You may assume that you have an infinite number of each kind of coin...


Continue reading → programming leetcode code dp java python



Sun 10 Nov 2019

Getting started with Graph Part 1

Graph are data structures made of vertices & edges. There are lots of interesting problems that can be solved using graphs. Part 1 covers the basic for creating a template for graph data structure.

First we need to create a vertex. A vertex is a fundamental unit of graph. A graph is a set of points (vertices) connected by lines (edges)..


Continue reading → graph java code



Sat 07 Sep 2019

Recursively sort a array

I recently came across a simple telephonic interview problem. The problem was to sort an array recursively. But even numbers need to be placed before odd numbers. At first glance it was obvious that it was a simple case of implementation of merge sort.

Problem Statement

Given an input array, produce the following output array…..


Continue reading → programming interview merge sort sort java code