This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class T { | |
public long endTime; | |
public int roomId; | |
public T(long endTime, int roomId) { | |
this.endTime = endTime; | |
this.roomId = roomId; | |
} | |
} | |
class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
class Solution { | |
public int furthestBuilding(int[] heights, int bricks, int ladders) { | |
PriorityQueue<Integer> ladderUsed = new PriorityQueue<>(); | |
for (int i = 0; i < heights.length - 1; i++) { | |
int diff = heights[i + 1] - heights[i]; | |
if (diff > 0) { | |
if (ladderUsed.size() < ladders) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Solution { | |
public int findLeastNumOfUniqueInts(int[] arr, int k) { | |
Map<Integer, Integer> freqMap = new HashMap<>(); | |
for (int num : arr) { | |
freqMap.put(num, freqMap.getOrDefault(num, 0) + 1); | |
} | |
List<Integer> frequencies = new ArrayList<>(freqMap.values()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public long largestPerimeter(int[] nums) { | |
Arrays.sort(nums); | |
long[] prefixSum = new long[nums.length]; | |
prefixSum[0] = nums[0]; | |
for (int i = 1; i < prefixSum.length; i++) { | |
prefixSum[i] = nums[i] + prefixSum[i - 1]; | |
} | |
long result = Integer.MIN_VALUE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int[] rearrangeArray(int[] nums) { | |
int n = nums.length, pi = 0, ni = 1; | |
int[] ans = new int[n]; | |
for (int ele : nums) { | |
if (ele >= 0) { | |
ans[pi] = ele; | |
pi += 2; | |
} else { | |
ans[ni] = ele; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String firstPalindrome(String[] strings) { | |
for (String s : strings) { | |
if (isPalindrome(s)) { | |
return s; | |
} | |
} | |
return ""; // If no palindromic string found | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int cherryPickup(int[][] grid) { | |
int m = grid.length; | |
int n = grid[0].length; | |
int[][][] dp = new int[m][n][n]; // dp[i][j1][j2] stores the maximum cherries two robots can pick starting from (i, j1) and (i, j2) | |
for (int i = m - 1; i >= 0; i--) { | |
for (int j1 = 0; j1 < n; j1++) { | |
for (int j2 = 0; j2 < n; j2++) { | |
int cherries = grid[i][j1] + (j1 != j2 ? grid[i][j2] : 0); // Cherries picked by both robots |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int countSubstrings(String s) { | |
int count = 0; | |
for (int i = 0; i < s.length(); i++) { | |
count += countPalindromes(s, i, i); // Odd length palindromes | |
count += countPalindromes(s, i, i + 1); // Even length palindromes | |
} | |
return count; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
class Solution { | |
public int numSquares(int n) { | |
if (n <= 0) return 0; | |
int[] dp = new int[n + 1]; | |
Arrays.fill(dp, Integer.MAX_VALUE); | |
dp[0] = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class Solution { | |
public String frequencySort(String s) { | |
if (s == null || s.length() == 0) | |
return ""; | |
// Count frequency of each character | |
Map<Character, Integer> freqMap = new HashMap<>(); | |
for (char c : s.toCharArray()) { |
NewerOlder