Skip to main content

Posts

Showing posts from November, 2024

Unique Number of Occurrences Coding Problem

Given an array of integers  arr , return  true   if the number of occurrences of each value in the array is  unique  or  false  otherwise .   Example 1: Input: arr = [1,2,2,1,1,3] Output: true Explanation:  The value 1 has 3 occurrences, 2 has 2 and 3 has 1. No two values have the same number of occurrences. Example 2: Input: arr = [1,2] Output: false Example 3: Input: arr = [-3,0,1,-3,1,1,1,-3,10,0] Output: true   Constraints: 1 <= arr.length <= 1000 -1000 <= arr[i] <= 1000 Solution: class Solution { public boolean uniqueOccurrences ( int [] arr ) { Map < Integer , Integer > mp = new HashMap <>(); Arrays . stream (arr). forEach ( a -> mp . put (a, mp . getOrDefault (a, 0 )+ 1 ) ); long distinctCount = mp . values (). stream (). distinct (). count (); return distinctCount == mp . values (). size (); } }

Remove Nth Node From End of List

Given the  head  of a linked list, remove the  n th  node from the end of the list and return its head.   Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1]   Constraints: The number of nodes in the list is  sz . 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz   Follow up:  Could you do this in one pass? Solution: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeNthFromEnd ( ListNode head , int n ) { ListNode fast = head; ListNode slow = head; for ( int i = 0 ; i < n; i++){ fast = fast . next ; } i...

Best Time to Buy and Sell Stock

You are given an array  prices  where  prices[i]  is the price of a given stock on the  i th  day. You want to maximize your profit by choosing a  single day  to buy one stock and choosing a  different day in the future  to sell that stock. Return  the maximum profit you can achieve from this transaction . If you cannot achieve any profit, return  0 .   Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0.   Constraints: 1 <= prices.length <= 10 5 0 <= prices[i] <= 10 4 Solution:  class Solution { public int maxProfit ( int [] prices ) { if ( prices . length < 2){ ...

LeetCode: Product of Array Except Self

Given an integer array  nums , return  an array   answer   such that   answer[i]   is equal to the product of all the elements of   nums   except   nums[i] . The product of any prefix or suffix of  nums  is  guaranteed  to fit in a  32-bit  integer. You must write an algorithm that runs in  O(n)  time and without using the division operation.   Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0]   Constraints: 2 <= nums.length <= 10 5 -30 <= nums[i] <= 30 The product of any prefix or suffix of  nums  is  guaranteed  to fit in a  32-bit  integer. Solution: class Solution { public int [] productExceptSelf ( int [] nums ) { int [] pr = new int [ nums . length ]; int [] sf = new int [ nums . length ]; int [] res = new int [ nums . length ]; int prc = 0 ;...