image_2023-06-05_11-03-29.png
44.9 KB
Runtime8 msBeats60.13%
Memory42.3 MBBeats87.73%
#N151. Reverse Words in a String
problem link
#solution
Memory42.3 MBBeats87.73%
#N151. Reverse Words in a String
problem link
#solution
class Solution {
public String reverseWords(String s) {
String[] arr = s.split("\\s+");
for(int i = 0, j = arr.length-1; i<j; ){
if(arr[i].equals("")){
i++; continue;
}
if(arr[j].equals("")){
j--; continue;
}
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; j--;
}
StringBuilder sb = new StringBuilder();
for(String ss: arr) {
if(!ss.equals("")) sb.append(ss).append(" ");
}
return sb.toString().trim();
}
}π2π₯1
image_2023-06-05_11-48-24.png
30.2 KB
Runtime2 msBeats50.58%
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
Memory51.1 MBBeats53.48%
#N238. Product of Array Except Self
problem link
#solution
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int arr[] = new int[n];
int left[] = new int[n];
arr[0] = left[n-1] = 1;
for(int i=1; i<n; i++){
arr[i] = arr[i-1]*nums[i-1];
}
for(int i=n-2; i>-1; i--){
left[i] = left[i+1]*nums[i+1];
}
for(int i=0; i<n; i++){
arr[i]*=left[i];
}
return arr;
}
}π₯2β€1
image_2023-06-05_12-50-01.png
23.6 KB
Runtime3 msBeats49.59%
Memory131.8 MBBeats25.49%
#N334. Increasing Triplet Subsequence
problem link
#solution
Memory131.8 MBBeats25.49%
#N334. Increasing Triplet Subsequence
problem link
#solution
class Solution {
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE, mid = Integer.MAX_VALUE;
for(int n: nums){
if(n<=small) small = n;
else if(n<=mid) mid = n;
else return true;
}
return false;
}
}β‘2π2
image_2023-06-08_13-07-40.png
46.5 KB
Runtime1 msBeats96.95%
Memory43 MBBeats49.21%
#N443. String Compression
problem link
#solution
Memory43 MBBeats49.21%
#N443. String Compression
problem link
#solution
class Solution {
public int compress(char[] chars) {
String s;
StringBuilder sb = new StringBuilder();
int count=1;
for(int i=1; i<chars.length; i++){
if(chars[i]==chars[i-1]){
count++;
}else{
sb.append(chars[i-1]);
if(count!=1) sb.append(count);
count=1;
}
}
sb.append(chars[chars.length-1]);
if(count!=1) sb.append(count);
s=sb.toString();
for(int i=0; i<s.length(); i++){
chars[i]=s.charAt(i);
}
return s.length();
}
}π4
image_2023-06-12_10-40-06.png
39.5 KB
Runtime5 msBeats83.94%
Memory41.1 MBBeats40.44%
#N228. Summary Ranges
problem link
#solution
Memory41.1 MBBeats40.44%
#N228. Summary Ranges
problem link
#solution
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<>();
if(nums.length==0) return list;
int a=nums[0], b=nums[0];
for(int i=1; i<nums.length; i++){
if(nums[i]-nums[i-1]==1){
b=nums[i];
}else{
if(a!=b) list.add(a+"->"+b);
else list.add(""+a);
a=nums[i];
b=nums[i];
}
}
if(a!=b) list.add(a+"->"+b);
else list.add(""+a);
return list;
}
}π8β‘1
#note
Use Sieve of Eratosthenes algorithm to find all prime numbers till n.
Use Sieve of Eratosthenes algorithm to find all prime numbers till n.
algorithm Sieve of Eratosthenes is
input: an integer n > 1.
output: all prime numbers from 2 through n.
let A be an array of Boolean values, indexed by integers 2 to n,
initially all set to true.
for i = 2, 3, 4, ..., not exceeding βn do
if A[i] is true
for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
set A[j] := false
return all i such that A[i] is true.
Time - O(nloglogn)
Example problem: https://leetcode.com/problems/closest-prime-numbers-in-range/description
Hey everyone! π
Its been a long break, but I'm finally back here. From now on, I'll start sharing posts based on the book βCracking the Coding Interview.β π
I'm not a pro at all this, but I'll do my best to share what I learn along the way. Hopefully it helps you too β and we can grow together step by step. π
#crackingthecodinginterview
Its been a long break, but I'm finally back here. From now on, I'll start sharing posts based on the book βCracking the Coding Interview.β π
I'm not a pro at all this, but I'll do my best to share what I learn along the way. Hopefully it helps you too β and we can grow together step by step. π
#crackingthecodinginterview
β€1π₯1
1.1. Is Unique: Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
The easy way is to use a hashtable, but without it, one quick idea is using an array to store the count of characters:
I also learned another way using bit vectors.
Itβs similar to the first idea, but instead of an array, it only uses one integer:
Both have the same space complexity, but the second one is more efficient because it only takes 4 bytes.
#crackingthecodinginterview
The easy way is to use a hashtable, but without it, one quick idea is using an array to store the count of characters:
public static boolean isUnique(String s) {
int[] arr = new int[128];
for (int i=0; i<s.length(); i++) {
int idx = s.charAt(i) - 'a';
if (arr[idx] == 1) return false;
arr[idx]++;
}
return true;
}
TC - O(c)
SC - O(1)I also learned another way using bit vectors.
Itβs similar to the first idea, but instead of an array, it only uses one integer:
public static boolean isUnique2(String s) {
int bits = 0;
for (int i=0; i<s.length(); i++) {
int idx = s.charAt(i) - 'a';
if ((bits & (1 << idx)) > 0) {
return false;
}
bits |= (1 << idx);
}
return true;
}
TC - O(c)
SC - O(1)Both have the same space complexity, but the second one is more efficient because it only takes 4 bytes.
#crackingthecodinginterview
π₯2
1.4. Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards.
The first solution is with hashmap:
Let's revise bit vectors and here is the next solution:
For a string to be a palindrome permutation, all letters must appear even number of times, but at most one letter can appear odd number of times (the middle one). With XOR operator, each time a character appears we flip its bit:
Even occurrences β‘οΈ bit ends up as 0
Odd occurrences β‘οΈ bit ends up as 1
At the end:
If all bits are 0 β‘οΈ every letter appears even times.
If exactly one bit is 1 β‘οΈ only one letter appears odd times.
To check the latter case, we use bitmask. if there is one 1 bit in integer, like 00010000, we subtract 1 then it becomes 00001111. after applying AND operator it will be 0, that confirms exactly one bit was set.
#crackingthecodinginterview
The first solution is with hashmap:
public static boolean f1(String s) {
Map<Character, Integer> map = new HashMap<>();
int oddFrequencies = 0;
for (char ch : s.toCharArray()) {
if (!Character.isLetter(ch)) continue;
ch = Character.toLowerCase(ch);
map.put(ch, map.getOrDefault(ch, 0) + 1);
if (map.get(ch) % 2 != 0) oddFrequencies++;
else oddFrequencies--;
}
return oddFrequencies <= 1;
}
// TC - O(n)
// SC - O(n)Let's revise bit vectors and here is the next solution:
public static boolean f2(String s) {
int bits = 0;
for (char ch : s.toCharArray()) {
if (!Character.isLetter(ch)) continue;
int k = Character.toLowerCase(ch) - 'a';
bits ^= (1 << k);
}
return bits == 0 || (bits & (bits - 1)) == 0;
}
// TC - O(n)
// SC - O(1)For a string to be a palindrome permutation, all letters must appear even number of times, but at most one letter can appear odd number of times (the middle one). With XOR operator, each time a character appears we flip its bit:
Even occurrences β‘οΈ bit ends up as 0
Odd occurrences β‘οΈ bit ends up as 1
At the end:
If all bits are 0 β‘οΈ every letter appears even times.
If exactly one bit is 1 β‘οΈ only one letter appears odd times.
To check the latter case, we use bitmask. if there is one 1 bit in integer, like 00010000, we subtract 1 then it becomes 00001111. after applying AND operator it will be 0, that confirms exactly one bit was set.
#crackingthecodinginterview
β‘4
image_2025-12-16_00-53-41.png
21.3 KB
#N1679. Max Number of K-Sum Pairs
problem link
#solution
TC - O(n)
SC - O(n)
problem link
#solution
TC - O(n)
SC - O(n)
class Solution {
public int maxOperations(int[] nums, int k) {
int count = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int n: nums){
if(map.containsKey(n)){
count++;
map.put(n, map.get(n) -1);
if(map.get(n)==0) map.remove(n);
}else if(n<k){
map.put(k-n, map.getOrDefault(k-n, 0) +1);
}
}
return count;
}
}