Lily's Homework

Sort by

recency

|

276 Discussions

|

  • + 0 comments

    Emergency locksmith Bradford shares a short story inspired by Lily’s Homework, where problem solving and responsibility take center stage. As Lily works through her assignment, she learns the value of planning, focus, and asking for help when needed. The theme reflects everyday challenges faced at home or school, showing how small tasks build confidence. It’s a reminder that learning happens through effort, patience, and support from trusted people around us.

  • + 1 comment

    python3

    def removeOverlapping(arr, sortedArr):
        numToIdx = {n: idx for n, idx in zip(arr, range(len(arr)))}
        swaps = 0
        
        for sIdx in range(len(sortedArr)):
            if sortedArr[sIdx] == arr[sIdx]:
                continue
            else:
                swappedOutItem = arr[sIdx]
                arr[sIdx] = sortedArr[sIdx]
                arr[numToIdx[sortedArr[sIdx]]] = swappedOutItem
                numToIdx[swappedOutItem] = numToIdx[sortedArr[sIdx]]
                numToIdx[sortedArr[sIdx]] = sIdx
                swaps += 1
        return swaps
            
    def lilysHomework(arr):
        sortedArr = sorted(arr, reverse=True)
        return min(removeOverlapping(list(arr), sortedArr) , removeOverlapping(arr, list(reversed(sortedArr))))
    
  • + 0 comments

    Locksmith Leeds provides expert services for lock repairs, replacements, and emergency callouts, keeping homes, businesses, and vehicles safe. With skilled technicians and fast response times, they deliver reliable and professional solutions tailored to customer needs. Known for efficiency and trust, their team ensures peace of mind in every situation. Similarly, Lily's Homework reflects the challenges and learning experiences faced by students, emphasizing dedication, time management, and support to achieve academic success effectively.

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    public static int lilysHomework(List<Integer> arr) {
        return Math.min(minSwaps(new ArrayList<>(arr)), minSwaps(reverseList(arr)));
    }
    
    private static int minSwaps(List<Integer> arr) {
        int n = arr.size();
        int swaps = 0;
    
        int[] sorted = arr.stream().mapToInt(i -> i).toArray();
        Arrays.sort(sorted);
    
        Map<Integer, Integer> indexMap = new HashMap<>();
        for (int i = 0; i < n; i++) {
            indexMap.put(arr.get(i), i);
        }
    
        for (int i = 0; i < n; i++) {
            if (arr.get(i) != sorted[i]) {
                swaps++;
    
                int correctValue = sorted[i];
                int toSwapIdx = indexMap.get(correctValue);
    
                // Update map before swapping
                indexMap.put(arr.get(i), toSwapIdx);
                indexMap.put(correctValue, i);
    
                // Swap in the list
                Collections.swap(arr, i, toSwapIdx);
            }
        }
    
        return swaps;
    }
    
    private static List<Integer> reverseList(List<Integer> list) {
        List<Integer> reversed = new ArrayList<>(list);
        Collections.reverse(reversed);
        return reversed;
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
    
        List<Integer> arr = Arrays.stream(bufferedReader.readLine().trim().split(" "))
            .map(Integer::parseInt)
            .collect(Collectors.toList());
    
        int result = Result.lilysHomework(arr);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments
    /*
     * Complete the 'lilysHomework' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    void _print(const vector<int>& v) {
        for (auto e: v) { cout << e << " "; }
        cout << endl;
    }
    
    int _count(vector<int>& rx) {
        int n = rx.size();
        int i = 0;
        int j;
        int count = 0;
        while (i < n) {
            while ((j = rx[i]) != i) {
                swap(rx[i], rx[j]);
                count++;
            }
            i++;
        }
        return count;
    }
    
    int lilysHomework(vector<int> arr) {
        int n = arr.size();
        // _print(arr);
    
        vector<int> ix(n);
        iota(ix.begin(), ix.end(), 0);
        // _print(ix);    
        sort(ix.begin(), ix.end(), [&arr](int i, int j) { return arr[i] < arr[j]; });
        // _print(ix);
    
        vector<int> rx(n);  // rank
        for (size_t i = 0; i < n; ++i) { rx[ix[i]] = i; }
        // _print(rx);
    
        vector<int> rrx(n); // reversed rank
        for (size_t i = 0; i < n; ++i) { rrx[i] = n - 1 - rx[i]; }
        // _print(rrx);
        
        int c1 = _count(rx);
        int c2 = _count(rrx);
        
        return min(c1, c2);
    }