Java Programs

Global and Local Inversions

We have some permutation A of [0, 1, …, N – 1], where N is the length of A. The number of (global) inversions is the number of i < j with 0 <= i < j < N and A[i] > A[j]. The number of local inversions is the number of i with 0 <= i < N and A[i] > A[i+1]. Return true if and only if the number of global …

Global and Local Inversions Read More »

LRU Cache – Clean Code

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys …

LRU Cache – Clean Code Read More »

Lexicographical Numbers : Beauty of DFS

Given an integer n, return 1 – n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. See the beauty of DFS Solution

Reverse an Integer Digits

In this article, we going to see about How to Reverse an Integer?  Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only store integers within the …

Reverse an Integer Digits Read More »

Valid Number

This article explains about Leetcode’s Valid Number Explanation and Solution. Validate if a given string can be interpreted as a decimal number. Some examples:”0″ => true” 0.1 ” => true”abc” => false”1 a” => false”2e10″ => true” -90e3   ” => true” 1e” => false”e3″ => false” 6e-1″ => true” 99e2.5 ” => false”53.5e93″ => true” –6 ” => false”-+3″ => false”95a54e53″ => false Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here …

Valid Number Read More »