Calculate the Total Amount of Points using Java

The challenge Our football team finished the championship. The result of each match look like “x:y”. Results of all matches are recorded in the collection. For example: ["3:1", "2:2", "0:1", ...] Write a function that takes such collection and counts the points of our team in the championship. Rules for counting points for each match: if x>y – 3 points if x<y – 0 point if x=y – 1 point Notes:...

October 1, 2020 · 2 min · 220 words · Andrew

FizzBuzz in Java

The challenge Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" ] The solution in Java class Solution { public List<String> fizzBuzz(int n) { List<String> l = new ArrayList<>(); for (int i=1; i<=n; i++) { if (i%3==0 && i%5==0) { l....

July 11, 2020 · 1 min · 122 words · Andrew

Get the Next Small Integer in Python

The challenge Write a function: def solution(A) that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. Given A = [1, 2, 3], the function should return 4. Given A = [−1, −3], the function should return 1. Write an efficient algorithm for the following assumptions:...

July 8, 2020 · 1 min · 155 words · Andrew

Find Numbers with Even Number of Digits using Java

The challenge Given an array nums of integers, return how many of them contain an even number of digits. Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 digits (even number of digits).  345 contains 3 digits (odd number of digits).  2 contains 1 digit (odd number of digits).  6 contains 1 digit (odd number of digits).  7896 contains 4 digits (even number of digits).  Therefore only 12 and 7896 contain an even number of digits....

June 18, 2020 · 1 min · 178 words · Andrew

How to use a Java HashSet by example

What is a HashSet A HashSet is an unordered collection containing unique elements. It has the standard collection operations Add, Remove, Contains, but since it uses a hash-based implementation, these operations are O(1) Learn with a Programming Question Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister....

June 16, 2020 · 2 min · 271 words · Andrew

Get The Shortest Path in Binary Matrix using Python

The challenge In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are different and share an edge or corner) C_1 is at location (0, 0) (ie. has value grid[0][0]) C_k is at location (N-1, N-1) (ie....

June 15, 2020 · 3 min · 434 words · Andrew

Palindrome Partitioning in Python

The problem Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"] ] The solution def partition(self, s: str) -> List[List[str]]: ret = [] def isPal(s): return s == s[::-1] def fn(s, l): if not s: ret.append(l) return for i in range(1, len(s)+1): if isPal(s[:i]): fn(s[i:], l+[s[:i]]) fn(s, []) return ret

June 14, 2020 · 1 min · 70 words · Andrew

Best Time to Buy and Sell Stock with Python

The problem Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again)....

June 12, 2020 · 2 min · 292 words · Andrew

Find the Minimum Absolute Difference in BST using Java

The question Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 \ 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3). Note: There are at least two nodes in this BST. The solution We will start with a stub to build out:...

June 2, 2020 · 2 min · 341 words · Andrew

Find Maximum Subarrays using Java

The problem Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. The code // This method takes an array of `ints` and returns an `int` public int maxSubArray(int[] nums) { // Set some `max` variables, // ..firstly the minimum `Integer` value // ..secondly `0` int max = Integer....

May 31, 2020 · 1 min · 144 words · Andrew