image_2021-12-07_00-48-42.png
27.5 KB
#N1598. Crawler Log Folder
problem link
#solution
problem link
#solution
class Solution {
public int minOperations(String[] logs) {
int res = 0;
for (String s : logs) {
if (s.equals("../")) res = Math.max(0, --res);
else if (s.equals("./")) continue;
else res++;
}
return res;
}
}runtime.png
12.4 KB
#N706. Design HashMap
problem link
#solution
problem link
#solution
class MyHashMap {
public ListNode[] nodes = new ListNode[10000];
public MyHashMap() {
}
public void put(int key, int value) {
int index = key%nodes.length;
if(nodes[index]==null){
nodes[index] = new ListNode(-1, -1);
}
ListNode prev=find(nodes[index], key);
if(prev.next==null)
prev.next=new ListNode(key, value);
else
prev.next.val = value;
}
public int get(int key) {
int index = key%nodes.length;
if(nodes[index] == null) return -1;
ListNode prev = find(nodes[index], key);
if(prev.next == null) return -1;
return prev.next.val;
}
public void remove(int key) {
int index = key%nodes.length;
if(nodes[index] == null) return;
ListNode prev = find(nodes[index], key);
if(prev.next == null) return;
prev.next=prev.next.next;
}CarbonNowShBot.png
327.8 KB
ListNode find(ListNode node, int key){
ListNode head = node, prev=null;
while(head!=null && head.key !=key){
prev=head;
head=head.next;
}
return prev;
}
class ListNode{
int key, val;
ListNode next;
public ListNode(int key, int val){
this.key = key;
this.val = val;
}
}
}image_2021-12-07_01-45-54.png
61.9 KB
#N1394. Find Lucky Integer in an Array
problem link
#solution
problem link
#solution
class Solution {
public int findLucky(int[] arr) {
int[] count = new int[501];
for(int i: arr)
count[i]++;
for(int i=count.length-1; i>=0; i--){
if(i==0) return -1;
if(count[i]==i) return i;
}
return -1;
}
}
Leetcode in Java && Oracle
image_2021-12-07_01-45-54.png
P.S.: another approach with HashMap‼️
int res=-1;
Map<Integer, Integer> map = new HashMap<>();
for(int n: arr)
map.put(n, map.getOrDefault(n, 0) +1);
for(Map.Entry<Integer, Integer> entry: map.entrySet())
if(entry.getKey() == entry.getValue()) res = Math.max(res, entry.getValue());
return res;
image_2021-12-07_02-23-04.png
63.8 KB
#N1260. Shift 2D Grid
problem link
#solution
problem link
#solution
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> list = new ArrayList<>();
int m=grid.length, n=grid[0].length;
while(k-- > 0){
int temp=grid[m-1][n-1];
for(int i=m-1; i>=0; i--){
for(int j=n-1; j>0; j--){
if(i==0 && j==0) break;
grid[i][j]=grid[i][j-1];
}
if(i>0) grid[i][0]=grid[i-1][n-1];
}
grid[0][0]=temp;
}
for(int i=0; i<m; i++){
List<Integer> sublist = new ArrayList<>();
for(int j=0; j<n; j++){
sublist.add(grid[i][j]);
}
list.add(sublist);
}
return list;
}
}image_2021-12-07_11-31-49.png
26.9 KB
#N566. Reshape the Matrix
problem link
#solution
problem link
#solution
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int[][] res = new int[r][c];
int m = mat.length, n = mat[0].length;
if(r*c != m*n) return mat;
for(int i=0; i<r*c; i++)
res[i/c][i%c] = mat[i/n][i%n];
return res;
}
}image_2021-12-07_11-40-43.png
21.3 KB
#N169. Majority Element
problem link
#solution
problem link
#solution
class Solution {
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}
}image_2021-12-07_13-28-33.png
36.4 KB
#medium
#N2079. Watering Plants
problem link
#solution
#N2079. Watering Plants
problem link
#solution
class Solution {
public int wateringPlants(int[] plants, int capacity) {
int steps=0, temp=capacity;
for(int i=0; i<plants.length; i++){
if(plants[i]<=temp){
steps++;
temp-=plants[i];
}else{
temp=capacity-plants[i];
steps+=2*i+1;
}
}
return steps;
}
}image_2021-12-07_13-47-04.png
28.8 KB
#N2022. Convert 1D Array Into 2D Array
problem link
#solution
problem link
#solution
class Solution {
public int[][] construct2DArray(int[] original, int m, int n) {
int[][] res = new int[m][n];
if(m*n != original.length) return new int[0][0];
for(int i=0; i<m*n; i++)
res[i/n][i%n] = original[i];
return res;
}
}image_2021-12-07_13-57-01.png
25.5 KB
#N867. Transpose Matrix
problem link
#solution
problem link
#solution
class Solution {
public int[][] transpose(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
int[][] res = new int[n][m];
for(int i=0; i<m*n; i++)
res[i%n][i/n] = matrix[i/n][i%n];
return res;
}
}image_2021-12-07_17-12-39.png
33.8 KB
#N1437. Check If All 1's Are at Least Length K Places Away
problem link
#solution
problem link
#solution
class Solution {
public boolean kLengthApart(int[] nums, int k) {
int temp=k;
for(int i=0; i<nums.length; i++){
if(nums[i]==1){
if(temp<k) return false;
temp=0;
}else
temp++;
}
return true;
}
}image_2021-12-09_02-41-10.png
41.7 KB
#N888. Fair Candy Swap
problem link
#solution
problem link
#solution
class Solution {
public int[] fairCandySwap(int[] a, int[] b) {
int sumA = 0, sumB = 0;
for(int n: a) sumA+=n;
for(int n: b) sumB+=n;
int delta = (sumB - sumA) / 2;
Set<Integer> set = new HashSet<>();
for(int n: b) set.add(n);
for(int n: a)
if(set.contains(n+delta)) return new int[]{n, n+delta};
return new int[0];
}
}image_2021-12-09_02-58-54.png
28.4 KB
#N283. Move Zeroes
problem link
#solution
problem link
#solution
class Solution {
public void moveZeroes(int[] nums) {
int index = 0;
for (int n: nums)
if(n != 0)
nums[index++] = n;
while(index<nums.length)
nums[index++] = 0;
}
}image_2021-12-09_03-06-43.png
28.7 KB
#N1848. Minimum Distance to the Target Element
problem link
#solution
problem link
#solution
class Solution {
public int getMinDistance(int[] nums, int target, int start) {
int ans = Integer.MAX_VALUE;
for(int i=0; i<nums.length; i++){
if(nums[i] == target)
ans = Math.min(ans, Math.abs(i - start));
}
return ans;
}
}image_2021-12-09_03-19-58.png
27.5 KB
#N1287. Element Appearing More Than 25% In Sorted Array
problem link
#solution
problem link
#solution
class Solution {
public int findSpecialInteger(int[] arr) {
int length = arr.length, quart = length/4;
for(int i=0; i<length - quart; i++)
if(arr[i] == arr[i+quart]) return arr[i];
return -1;
}
}image_2021-12-10_00-32-27.png
75.3 KB
#N748. Shortest Completing Word
problem link
#solution
problem link
#solution
Map<Character, Integer> map=new HashMap<>();
int minLength=Integer.MAX_VALUE;
String result = null;
for(char ch: licensePlate.toLowerCase().toCharArray())
if(Character.isLetter(ch))map.put(ch, map.getOrDefault(ch, 0) +1);
for(String word:words){
System.out.println(word);
Map<Character, Integer> temp = new HashMap<>(map);
int count = temp.size();
for(char ch:word.toCharArray())
if(temp.containsKey(ch))temp.put(ch, temp.get(ch) -1);
for(Map.Entry<Character, Integer> entry: temp.entrySet()){
if(entry.getValue()>0) break;
count--;
}
if(count<=0&&minLength>word.length()){
result=word;
minLength=word.length();
}
}
return result;
image_2021-12-10_00-33-02.png
47.5 KB
#N1629. Slowest Key
problem link
#solution
problem link
#solution
class Solution {
public char slowestKey(int[] releaseTimes, String str) {
int max=releaseTimes[0], ix=0;
for(int i=1; i<releaseTimes.length; i++){
if(max<=releaseTimes[i]-releaseTimes[i-1]){
if(max==releaseTimes[i]-releaseTimes[i-1]&&str.charAt(i)>str.charAt(ix)){
ix=i;
//break;
}else if(max<releaseTimes[i]-releaseTimes[i-1]){
max=releaseTimes[i]-releaseTimes[i-1];
ix=i;}
}
}
return str.charAt(ix);
}
}image_2021-12-10_00-33-29.png
38.1 KB
#N217. Contains Duplicate
problem link
#solution
problem link
#solution
class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums == null){
return false;
}
int len = nums.length;
HashSet<Integer> set = new HashSet<Integer>();
for(int i = 0; i < len; i++){
if(!set.add(nums[i])){
return true;
}
}
return false;
}
}image_2021-12-10_00-33-55.png
37.7 KB
#N896. Monotonic Array
problem link
#solution
problem link
#solution
class Solution {
public boolean isMonotonic(int[] nums) {
int countI=0, countD=0, l=nums.length;
for(int i=0; i<l-1; i++){
if(nums[i] == nums[i+1]){
countI++;
countD++;
}
else if(nums[i] < nums[i+1]) countI++;
else countD++;
}
return countI == l-1 || countD == l-1;
}
}