From 78371b3d20375852bca888533bbd903c641a3d46 Mon Sep 17 00:00:00 2001 From: sumanas27 Date: Fri, 16 May 2025 22:59:18 +0200 Subject: [PATCH 1/4] Longest Substring solution with bit older class refactoring --- .gitignore | 4 +- src/main/kotlin/Main.kt | 12 ++++-- src/main/kotlin/ds-algo-leetcode/TwoSum.kt | 31 -------------- .../kotlin/dsalgoleetcode/LongestSubstring.kt | 42 +++++++++++++++++++ src/main/kotlin/dsalgoleetcode/TwoSum.kt | 33 +++++++++++++++ 5 files changed, 87 insertions(+), 35 deletions(-) delete mode 100644 src/main/kotlin/ds-algo-leetcode/TwoSum.kt create mode 100644 src/main/kotlin/dsalgoleetcode/LongestSubstring.kt create mode 100644 src/main/kotlin/dsalgoleetcode/TwoSum.kt diff --git a/.gitignore b/.gitignore index 566e06b..d84a02b 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,6 @@ hs_err_pid* replay_pid* # Kotlin Gradle plugin data, see https://kotlinlang.org/docs/whatsnew20.html#new-directory-for-kotlin-data-in-gradle-projects -.kotlin/ \ No newline at end of file +.kotlin/ +.idea/ +out/ \ No newline at end of file diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index f702081..af0944f 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,6 +1,7 @@ package main.kotlin -import TwoSum +import main.kotlin.dsalgoleetcode.LongestSubstring +import main.kotlin.dsalgoleetcode.TwoSum fun main() { println("What's your name?") @@ -8,8 +9,13 @@ fun main() { println("Hello, $name!") // Calling Two Sum class - val obj = TwoSum() - val result = obj.twoSum(nums = intArrayOf(1,2,3,4,5), target = 9) + val twoSum = TwoSum() + val result = twoSum.twoSum(nums = intArrayOf(1, 2, 3, 4, 5), target = 9) + + // Calling Longest Substring + val longestSubstring = LongestSubstring() + val lsResult = longestSubstring.lengthOfLongestSubstring(s = "abcabcbbb") println("Indices: ${result.joinToString(",")}") + println("Longest Substring : $lsResult") } \ No newline at end of file diff --git a/src/main/kotlin/ds-algo-leetcode/TwoSum.kt b/src/main/kotlin/ds-algo-leetcode/TwoSum.kt deleted file mode 100644 index cc46efe..0000000 --- a/src/main/kotlin/ds-algo-leetcode/TwoSum.kt +++ /dev/null @@ -1,31 +0,0 @@ -class TwoSum { - -/* -* Time Complexity O(n) -* Space Complexity O(n) -* -* Step 1 : Declare a Map of Integers which will be used for comparison -* Step 2 : Run through the array with index -* Step 3 : Substract the current element from target sum and store it -* Step 4 : Check if the complement present in the map -* Step 5 : If present then create and return an array of Integers with the index -* of the current element and fetched value from map with the complement key -* Step 6 : Otherwise set the map with current element as key and current index -* as value -* Step 7 : Return an empty array by default -* */ - - fun twoSum(nums: IntArray, target: Int): IntArray { - val seen = mutableMapOf() - - // using array with index as index and value both are required for computation - for((index, element) in nums.withIndex()){ - val complement = target - element - if( complement in seen ) - return intArrayOf(seen.get(complement)!!, index) - // setting the map with the current index - seen[element] = index - } - return intArrayOf() - } -} \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/LongestSubstring.kt b/src/main/kotlin/dsalgoleetcode/LongestSubstring.kt new file mode 100644 index 0000000..d94920e --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/LongestSubstring.kt @@ -0,0 +1,42 @@ +package main.kotlin.dsalgoleetcode + +/** + * Time Complexity O(n) + * Longest Substring Without Repeating Characters + * Given a string s, find the length of the longest substring without duplicate characters. + * + * Example 1: + * + * Input: s = "abcabcbb" + * Output: 3 + * Explanation: The answer is "abc", with the length of 3. + * Example 2: + * + * Input: s = "bbbbb" + * Output: 1 + * Explanation: The answer is "b", with the length of 1. + * Example 3: + * + * Input: s = "pwwkew" + * Output: 3 + * Explanation: The answer is "wke", with the length of 3. + * Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. + */ + +class LongestSubstring { + + fun lengthOfLongestSubstring(s: String): Int { + val seen = mutableMapOf() + var start = 0 + var maxLength = 0 + + for((index, element) in s.withIndex()){ + if( element in seen && seen[element]!! >= start){ + start = seen[element]!! + 1 + } + seen[element] = index + maxLength = maxOf(maxLength, index - start + 1) + } + return maxLength + } +} \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/TwoSum.kt b/src/main/kotlin/dsalgoleetcode/TwoSum.kt new file mode 100644 index 0000000..7ca6ff2 --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/TwoSum.kt @@ -0,0 +1,33 @@ +package main.kotlin.dsalgoleetcode + +/** + * Time Complexity O(n) + * Space Complexity O(n) + * + * Step 1 : Declare a Map of Integers which will be used for comparison + * Step 2 : Run through the array with index + * Step 3 : Subtract the current element from target sum and store it + * Step 4 : Check if the complement present in the map + * Step 5 : If present then create and return an array of Integers with the index + * of the current element and fetched value from map with the complement key + * Step 6 : Otherwise set the map with current element as key and current index + * as value + * Step 7 : Return an empty array by default + */ + +class TwoSum { + + fun twoSum(nums: IntArray, target: Int): IntArray { + val seen = mutableMapOf() + + // using array with index as index and value both are required for computation + for((index, element) in nums.withIndex()){ + val complement = target - element + if( complement in seen ) + return intArrayOf(seen[complement]!!, index) + // setting the map with the current index + seen[element] = index + } + return intArrayOf() + } +} \ No newline at end of file From c25c3d65cbd7c7b8545baaf7c27bd42cab566a69 Mon Sep 17 00:00:00 2001 From: sumanas27 Date: Sun, 18 May 2025 23:53:31 +0200 Subject: [PATCH 2/4] LinkedList Cycle Detection --- src/main/kotlin/Main.kt | 16 ++++++- .../LinkedListCycleDetection.kt | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/dsalgoleetcode/LinkedListCycleDetection.kt diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index af0944f..1e277c1 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,7 @@ package main.kotlin +import main.kotlin.dsalgoleetcode.LinkedListCycleDetection +import main.kotlin.dsalgoleetcode.ListNode import main.kotlin.dsalgoleetcode.LongestSubstring import main.kotlin.dsalgoleetcode.TwoSum @@ -11,11 +13,21 @@ fun main() { // Calling Two Sum class val twoSum = TwoSum() val result = twoSum.twoSum(nums = intArrayOf(1, 2, 3, 4, 5), target = 9) + println("Indices: ${result.joinToString(",")}") // Calling Longest Substring val longestSubstring = LongestSubstring() val lsResult = longestSubstring.lengthOfLongestSubstring(s = "abcabcbbb") - - println("Indices: ${result.joinToString(",")}") println("Longest Substring : $lsResult") + + // Calling LinkedList Cycle Detection + val linkedListCycleDetection = LinkedListCycleDetection() + val node1 = ListNode(1) + val node2 = ListNode(2) + val node3 = ListNode(3) + node1.next = node2 + node2.next = node3 + node3.next = node1 // cycle back to x + val llcdResult = linkedListCycleDetection.hasCycle(node1) + println("Has cycle $llcdResult") } \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/LinkedListCycleDetection.kt b/src/main/kotlin/dsalgoleetcode/LinkedListCycleDetection.kt new file mode 100644 index 0000000..aa49aa6 --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/LinkedListCycleDetection.kt @@ -0,0 +1,48 @@ +package main.kotlin.dsalgoleetcode + +/** + * Linked List Cycle + * + * Given head, the head of a linked list, determine if the linked list has a cycle in it. + * There is a cycle in a linked list if there is some node in the list that can be reached + * again by continuously following the next pointer. Internally, pos is used to denote the + * index of the node that tail's next pointer is connected to. Note that pos is not passed + * as a parameter. + * + * Return true if there is a cycle in the linked list. Otherwise, return false. + * + * Input: head = [3,2,0,-4], pos = 1 + * Output: true + * Explanation: There is a cycle in the linked list, where the tail connects to + * the 1st node (0-indexed). + * */ + +class LinkedListCycleDetection { + + /** + * Example: + * var li = ListNode(5) + * var v = li.`val` + * Definition for singly-linked list. + * class ListNode(var `val`: Int) { + * var next: ListNode? = null + * } + */ + + fun hasCycle(head: ListNode?): Boolean { + var slow = head + var fast = head + while (fast != null && fast.next != null) { + slow = slow?.next + fast = fast.next?.next + if (slow == fast) + return true + } + return false + } +} + + +class ListNode(var `val`: Int) { + var next: ListNode? = null +} \ No newline at end of file From 93499b260db39a58dbc97641103e12fcd68e76ab Mon Sep 17 00:00:00 2001 From: sumanas27 Date: Mon, 19 May 2025 22:32:55 +0200 Subject: [PATCH 3/4] Find duplicate elements in an array --- src/main/kotlin/Main.kt | 7 +++++ .../dsalgoleetcode/ContainsDuplicate.kt.kt | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 1e277c1..6f1119f 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -30,4 +30,11 @@ fun main() { node3.next = node1 // cycle back to x val llcdResult = linkedListCycleDetection.hasCycle(node1) println("Has cycle $llcdResult") + + // Calling Contains Duplicate + val `containsDuplicate.kt` = `ContainsDuplicate.kt`() + val containsDuplicateResult = `containsDuplicate.kt` + .containsDuplicate(intArrayOf(1,2,3,1,0,1)) + println("Contains Duplicate Integers $containsDuplicateResult") + } \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt new file mode 100644 index 0000000..b20745c --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt @@ -0,0 +1,27 @@ +package main.kotlin.dsalgoleetcode + +/** + * Contains Duplicate + * Given an integer array nums, return true if any value appears at least twice in the + * array, and return false if every element is distinct. + * + * Example 1: + * Input: nums = [1,2,3,1] + * Output: true + * Explanation: + * The element 1 occurs at the indices 0 and 3. + * */ + +class `ContainsDuplicate.kt` { + + fun containsDuplicate(nums: IntArray): Boolean { + + val seen = mutableSetOf() + for(i in nums){ + if( i in seen) + return true + seen.add(i) + } + return false + } +} \ No newline at end of file From 8f4aaae79280a3a78a6d962805d222b84459e566 Mon Sep 17 00:00:00 2001 From: sumanas27 Date: Mon, 19 May 2025 22:40:41 +0200 Subject: [PATCH 4/4] Max profit on Buy and Sell stock --- src/main/kotlin/Main.kt | 11 +++++-- .../kotlin/dsalgoleetcode/BuyAndSellStock.kt | 33 +++++++++++++++++++ .../dsalgoleetcode/ContainsDuplicate.kt.kt | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 6f1119f..3d46f90 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,7 @@ package main.kotlin +import main.kotlin.dsalgoleetcode.BuyAndSellStock +import main.kotlin.dsalgoleetcode.ContainsDuplicate import main.kotlin.dsalgoleetcode.LinkedListCycleDetection import main.kotlin.dsalgoleetcode.ListNode import main.kotlin.dsalgoleetcode.LongestSubstring @@ -32,9 +34,14 @@ fun main() { println("Has cycle $llcdResult") // Calling Contains Duplicate - val `containsDuplicate.kt` = `ContainsDuplicate.kt`() - val containsDuplicateResult = `containsDuplicate.kt` + val containsDuplicate = ContainsDuplicate() + val containsDuplicateResult = containsDuplicate .containsDuplicate(intArrayOf(1,2,3,1,0,1)) println("Contains Duplicate Integers $containsDuplicateResult") + // Calling Buy and Sell Stock + val buyAndSellStock = BuyAndSellStock() + val bnsResult = buyAndSellStock.maxProfit(intArrayOf(1,4,8,0,2)) + println("Max profit : $bnsResult") + } \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt b/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt new file mode 100644 index 0000000..385576b --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt @@ -0,0 +1,33 @@ +package main.kotlin.dsalgoleetcode + +/** + * You are given an array prices where prices[i] is the price of a given stock on the ith 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. + * */ + +class BuyAndSellStock { + + fun maxProfit(prices: IntArray): Int { + + var minPrice = Int.MAX_VALUE + var maxProfit = 0 + + for (price in prices){ + if( price < minPrice){ + minPrice = price + } else{ + maxProfit = maxOf(maxProfit, price - minPrice) + } + } + return maxProfit + } +} \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt index b20745c..4e35c07 100644 --- a/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt +++ b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt @@ -12,7 +12,7 @@ package main.kotlin.dsalgoleetcode * The element 1 occurs at the indices 0 and 3. * */ -class `ContainsDuplicate.kt` { +class ContainsDuplicate { fun containsDuplicate(nums: IntArray): Boolean {