JP Morgan (All time)#
202. Happy Number#
Easy
1class Solution {
2
3 public boolean isHappy(int n) {
4 int slow = squaredSum(n);
5 int fast = squaredSum(squaredSum(n));
6
7 while (slow != fast) {
8 slow = squaredSum(slow);
9 fast = squaredSum(squaredSum(fast));
10 }
11
12 return slow == 1;
13 }
14
15 private int squaredSum(int n) {
16 int sum = 0;
17 while (n > 0) {
18 sum += Math.pow(n % 10, 2);
19 n /= 10;
20 }
21 return sum;
22 }
23}
20. Valid Parentheses#
Easy
1class Solution {
2
3 public boolean isValid(String s) {
4 Deque<Character> stack = new ArrayDeque<>();
5
6 for (final char c : s.toCharArray()) {
7 if (c == '(') {
8 stack.push(')');
9 } else if (c == '{') {
10 stack.push('}');
11 } else if (c == '[') {
12 stack.push(']');
13 } else if (stack.isEmpty() || stack.pop() != c) {
14 return false;
15 }
16 }
17
18 return stack.isEmpty();
19 }
20}
322. Coin Change#
Medium
1class Solution {
2
3 public int coinChange(int[] coins, int amount) {
4 // dp[i] := the minimum number of coins to make up i
5 int[] dp = new int[amount + 1];
6 Arrays.fill(dp, 1, dp.length, amount + 1);
7
8 for (final int coin : coins) {
9 for (int i = coin; i <= amount; ++i) {
10 dp[i] = Math.min(dp[i], dp[i - coin] + 1);
11 }
12 }
13
14 return dp[amount] == amount + 1 ? -1 : dp[amount];
15 }
16}
121. Best Time to Buy and Sell Stock#
Easy
1
1328. Break a Palindrome#
Medium
1
31. Next Permutation#
Medium
1
273. Integer to English Words#
Hard
1
9. Palindrome Number#
Easy
1
1. Two Sum#
Easy
1
7. Reverse Integer#
Easy
1
17. Letter Combinations of a Phone Number#
Medium
1
200. Number of Islands#
Medium
1
53. Maximum Subarray#
Easy
1
5. Longest Palindromic Substring#
Medium
1
4. Median of Two Sorted Arrays#
Hard
1
33. Search in Rotated Sorted Array#
Medium
1
146. LRU Cache#
Medium
1
349. Intersection of Two Arrays#
Easy
1
79. Word Search#
Medium
1
215. Kth Largest Element in an Array#
Medium
1
39. Combination Sum#
Medium
1
387. First Unique Character in a String#
Easy
1
1247. Minimum Swaps to Make Strings Equal#
Medium
1
2038. Remove Colored Pieces if Both Neighbors are the Same Color#
Medium
1class Solution {
2
3 public boolean winnerOfGame(String colors) {
4 int aTripletsCount = 0;
5 int bTripletsCount = 0;
6
7 for (int i = 1; i + 1 < colors.length(); ++i) {
8 if (
9 colors.charAt(i - 1) == colors.charAt(i) &&
10 colors.charAt(i) == colors.charAt(i + 1)
11 ) {
12 if (colors.charAt(i) == 'A') {
13 aTripletsCount++;
14 } else {
15 bTripletsCount++;
16 }
17 }
18 }
19
20 return aTripletsCount > bTripletsCount;
21 }
22}