Skip to content

Instantly share code, notes, and snippets.

import java.util.*;
public class OptimalAccountBalancing{
public int minTransfers(int[][] trans){
HashMap<Integer, Integer> map = new HashMap<>();
for(int[] t:trans){
map.putIfAbsent(t[0],0);
map.get(t[0])+=t[2];
map.putIfAbsent(t[1],0);
map.get(t[1])-=t[2];
import java.util.*;
public class SortTransformedArray{
/**
Sol1:
-for each term x of the the input array, transform it into ax^2 + bx + c
-store the transformed value to a min heap
-pop the items from the min heap and populate the output array
O(nlogn) time for heap sort
import java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class LinkedListSwapAdjacentNodes{
public class WiggleSort2Sorting{
public static void wiggleSort2(int[] nums) {
int n = nums.length;
if (n < 2) return;
int median = findKthLargestElement(nums, n/2);
int i = 0, left = 0, right = n-1;
while (i <= right) {
if (i > left && nums[newIndex(i, n)] > median) {
//bring the larger than median values to first odd slots
swap(nums, newIndex(i, n), newIndex(left++, n));
import java.util.Arrays;
public class WiggleSortII{
/**
Solution 1:
An O(nlogn) time solution, out-of-place:
We can first sort the array, then partition the array into two halves. So all elements in the first half is less than the second half. Then we can pick up one element from each half from the end, and form the solution . e.g.
nums = [1, 3, 2, 2, 3, 1].
public class WiggleSort{
public static void wiggleSort2(int[] nums) {
//todo:handle base case
for(int i=0;i<nums.length; i+=2){
//check if previous odd is valid and is smaller than it
if((i-1)>=0 && nums[i] > nums[i-1]){
//swap them
int temp = nums[i-1];
nums[i-1] = nums[i];
nums[i] = temp;
import java.util.*;
public class WiggleSort {
public static void wiggleSort(int[] nums) {
for(int i = 1; i < nums.length; i++) {
int current = nums[i - 1];
if((i % 2 == 1) == (current > nums[i])) {
nums[i - 1] = nums[i];
nums[i] = current;
public class WordSearchII{
private class TrieNode{
TrieNode[] next = new TrieNode[26];
String word;
}
public List<String> findWords(char[][] mat, String[] words) {
List<String> res = new ArrayList<>();
TrieNode root = buildTrie(words);
for(int i=0;i<mat.length; i++){
public void displayExcelReport(JasperPrint jasperPrint){
Font font = new Font(12, "SansSerif", true);
Style titleStyle = new StyleBuilder(false).setFont(font).build();
JRXlsExporter exporter = new JRXlsExporter();
try {
byte[] data = JasperExportManager.exportReportToPdf(jasperPrint);
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) FacesContext
.getCurrentInstance().getExternalContext().getResponse();
public String generatePDFReport(){
this.setFilter((ReviewFilterDTO)this.initialize(this.getFilter()));
this.service.generatePdfReportFromView(this.getFilter().getSelectedHeaders(),reviewReportListForDisplay);
return "";
}
Corresponding method on Service:
@Override
public boolean generatePdfReportFromView(List headers, List reportElements) {
JasperPrint jasperPrint=this.buildReport(headers, reportElements);
this.reportGenerator.displayPdfReport(jasperPrint);